setwd("C:/Documents and Settings/William Christensen/My Documents/stat230")

#################################################
### AUDITOR TRAINING (Randomize Complete Block CB[1]) ###
#################################################

## Either of the two lines below will access the data ##
audit <- read.table("auditor.csv", sep=",", header=TRUE)
audit <- read.table(header=TRUE, text="
                    score  block	method
                    73	1	home
                    81	1	local
                    92	1	national
                    76	2	home
                    78	2	local
                    89	2	national
                    75	3	home
                    76	3	local
                    87	3	national
                    74	4	home
                    77	4	local
                    90	4	national
                    76	5	home
                    71	5	local
                    88	5	national
                    73	6	home
                    75	6	local
                    86	6	national
                    68	7	home
                    72	7	local
                    88	7	national
                    64	8	home
                    74	8	local
                    82	8	national
                    65	9	home
                    73	9	local
                    81	9	national
                    62	10	home
                    69	10	local
                    78	10	national
                    ")
audit

audit$block <- factor(audit$block)
audit$method <- factor(audit$method)

audit.lm <- lm(score ~ block + method , data=audit)
tapply(audit$score, audit$method, mean)
anova(audit.lm)


#################################################
### DENTAL PAIN (CB[2])                       ###
#################################################

dental <- read.table(header=TRUE, text="
                     relief block codeine acupunc
                     0.0      1      1      1
                     0.6      1      1      2
                     0.5      1      2      1
                     1.2      1      2      2
                     0.3      2      1      1
                     0.7      2      1      2
                     0.6      2      2      1
                     1.3      2      2      2
                     0.4      3      1      1
                     0.8      3      1      2
                     0.8      3      2      1
                     1.6      3      2      2
                     0.4      4      1      1
                     0.9      4      1      2
                     0.7      4      2      1
                     1.5      4      2      2
                     0.6      5      1      1
                     1.5      5      1      2
                     1.0      5      2      1
                     1.9      5      2      2
                     0.9      6      1      1
                     1.6      6      1      2
                     1.4      6      2      1
                     2.3      6      2      2
                     1.0      7      1      1
                     1.7      7      1      2
                     1.8      7      2      1
                     2.1      7      2      2
                     1.2      8      1      1
                     1.6      8      1      2
                     1.7      8      2      1
                     2.4      8      2      2")
dental$block <- factor(dental$block)
dental$codeine <- factor(dental$codeine)
dental$acupunc <- factor(dental$acupunc)

dental.lm <- lm(relief ~ block + codeine + acupunc + codeine:acupunc, data=dental)
tapply(dental$relief, dental$codeine, mean)
tapply(dental$relief, dental$acupunc, mean)
tapply(dental$relief, dental$codeine:dental$acupunc, mean)
anova(dental.lm)

####  TYPE I AND TYPE III ANOVA TABLES  ######

object <- read.csv("filename", header = TRUE, sep = ",")
head(object)
library(car) #If this isn't working for you, then you need to install the car package

# ANOVA with Type I SS
model <- lm(response ~ factor1 + factor2 + factor1 * factor2, data = object)
anova(model)

# ANOVA with Type III SS
model2 <- lm(response ~ factor1 + factor2 + factor1 * factor2, data = object,
             contrasts = list(factor1 = contr.sum, factor2 = contr.sum)) #Don't worry too much about understnding this "contrast" statement
Anova(model2, type = "III") #Don't need to include the Intercept line of the ANOVA table

