# Compute main effects and interaction effects with the data in Section 6-2 A <- c(-1, 1, -1,1) B <- c(-1, -1, 1, 1) I <- c(28, 36, 18, 31) II <- c(25, 32, 19, 30) III <- c(27, 32, 23, 29) data <- data.frame(A, B, I, II, III) data # compute sums for each combination sums <- apply(data[,3:5], 1, sum) names(sums) <- c("(1)", "(a)", "(b)", "(ab)") sums ybar <- sums/3 # Interaction plots par(mfrow=c(1,2)) interaction.plot(A, B, ybar) interaction.plot(B, A, ybar) # Build ANOVA table y <- c(I, II, III) y [1] 28 36 18 31 25 32 19 30 27 32 23 29 factorA <- as.factor(rep(A,3)) factorB <- as.factor(rep(B,3)) g <- lm(y ~ factorA + factorB + factorA*factorB) anova(g) windows() # residual analysis par(mfrow=c(1,2)) # normal plot qqnorm(g$residuals) qqline(g$residuals) # residual plot plot(g$fitted.values,g$residuals) abline(h=0)