# R Example of 3-factor ANOVA # We use the stress data example we looked at in class. # Save the data file into a directory and # use the full path name: stress.data <- read.table(file = "z:/My Documents/teaching/stat_705/stressdata.txt", header=FALSE, col.names = c('stresstest', 'gender', 'diet', 'smoking', 'subject')) # attaching the data frame: attach(stress.data) # Making "gender", "diet" and "smoking" factors: gender <- factor(gender) diet <- factor(diet) smoking <- factor(smoking) # The ANOVA model with all interaction terms included: stress.fit <- lm(stresstest ~ gender + diet + smoking + gender:diet + gender:smoking + diet:smoking + gender:diet:smoking) anova(stress.fit) # We see there is no significant 3-way interaction (P-value = 0.66). # The is significant Diet*Smoking Interaction. # We also see a significant effect due to gender. # What is the effect of leaving out the highest-order interaction? stress.fit2 <- lm(stresstest ~ gender + diet + smoking + gender:diet + gender:smoking + diet:smoking) anova(stress.fit2)