# R Example of Residual Analysis to # check assumptions in the ANOVA model # We will analyze the Kenton Foods data from the example in class # Save the data file into a directory and # use the full path name: kenton.data <- read.table(file = "z:/My Documents/teaching/stat_705/kentondata.txt", header=FALSE, col.names = c('sales', 'design', 'store')) # attaching the data frame: attach(kenton.data) # Making "design" a factor: design <- factor(design) # Fitting the ANOVA model and producing the ANOVA table: kenton.fit <- lm(sales ~ design); anova(kenton.fit) # Residual plots for checking ANOVA model assumptions: # plotting residuals versus fitted values: plot(fitted.values(kenton.fit), residuals(kenton.fit), xlab="Fitted Values", ylab="Residuals"); abline(h=0) # A normal Q-Q plot of the residuals: qqnorm(residuals(kenton.fit)) ## A short function to perform the Brown-Forsythe test ## for equal variances across the populations: BF.test <- function(y, group) { group <- as.factor(group) # precautionary meds <- tapply(y, group, median) resp <- abs(y - meds[group]) anova(lm(resp ~ group))[1, 4:5] } # Implementing the function for our data: BF.test(sales, design) # The Shapiro-Wilk test for normality is produced by: shapiro.test(resid(kenton.fit)) # We see a test statistic of 0.972 and a P-value of 0.8216. # So the hypothesis of normal errors is reasonable. ##### Outlier Detection ##### # This prints the internally studentized residuals: rstandard(kenton.fit) # No observations seem to be outliers.