##14 ybars <- c(14.62, 34.12, 33.61, 29.1, 27, 26.97, 26.8) sds <- c(5.039, 11.942, 6.582, 4.593, 3.818, 9.010, 5.969) ns <- c(9, 5, 6, 9, 2, 6, 9) I <- length(ns) SSE <- sum((ns-1)*sds^2) # a) print(sp <- sqrt(SSE/sum(ns-1)) ) # b) this test should use pooled estimate of s print( t <- (14.62 - 34.12)/sp/sqrt(1/9 + 1/5) ) print(pvalue <- 1 - pt(abs(t), df = sum(ns-1)) ) ##15 data <- read.csv("Spock.csv", header = TRUE) Y <- data[,1] var(Y) SST <- var(Y)*(length(Y)-1) c(SST, SSE) n <- length(Y) anova.table <- function(SSE, SST, n, I){ SSB <- SST - SSE MSB <- SSB/(I-1) MSE <- SSE/(n-I) F <- MSB/MSE pval <- 1 - pf(F, df1 = I-1, df2 = n-I) print("Source DF SS MS F P") print(paste("Between", I-1, round(SSB,1), " ", round(MSB,1), " ", round(F,2), " ", round(pval,3))) print(paste("Within ", n-I, round(SSE,1), " ", round(MSE,1))) print(paste("Total ", n-1, round(SST,1))) } anova.table(SSE, SST, n, I) SOURCE DF SS MS F P BETWEEN 6 1927.1 321.2 6.72 0.000 WITHIN(Error) 39 1864.5 47.8 TOTAL 45 3791.5 ##17 anova.table(35088, 70907, 32, 8) Source DF SS MS F P Between 7 35819 5117 3.5 0.01 Within 24 35088 1462 Total 31 70907 ##21 attach(data) lm1 <- lm(PERCENT ~ JUDGE) detach(data) res.sq <- lm1$res^2 lm2 <- lm(res.sq ~ data$JUDGE) anova(lm2) # p-value for Levene's test = 0.078: not a significant difference in variances # ##23 data <- read.csv("trex.csv", header = TRUE) attach(data) boxplot(OXYGEN ~ BONEGRP) lm3 <- lm(OXYGEN ~ BONEGRP) anova(lm3) qqnorm(lm3$resid) # the QQ plot looks ok plot(lm3$fit, lm3$resid) # p-value for the test is about 1e-06 => evidence that the temperatures *are* different throughout the body # however, the boxplots show potential difficulty with equal variances assumption kruskal.test(OXYGEN ~ BONEGRP) # Kruskal-Wallis test arrives at a similar conclusion detach(data)