--- title: "STAT 516 Lec 11" format: html --- ```{r} vo2max <- data.frame(chg = c(17.05,4.96,10.40,11.05,0.26,2.51, -0.87,-10.74,-3.27,-1.97,7.50,-7.25), grp = as.factor(c(rep("aerobic",6),rep("running",6))), age = c(31,23,27,28,22,24,23,22,22,25,27,20)) vo2max ``` ```{r} plot(chg ~ age, data = vo2max) ``` Estimate the parameters: ```{r} lm_out <- lm(chg ~ grp + age, data = vo2max) summary(lm_out) ``` Construct full-reduced model F test statistics. For testing no treatment effect: ```{r} lm_full <- lm(chg ~ grp + age, data = vo2max) lm_red <- lm(chg ~ age, vo2max) # drop grp SSE_full <- sum(lm_full$residuals^2) SSE_red <- sum(lm_red$residuals^2) a <- 2 N <- nrow(vo2max) alpha <- 0.05 num <- (SSE_red - SSE_full) / (a-1) den <- SSE_full / (N - a - 1) Ftest <- num / den Fcrit <- qf(1 - alpha,a-1, N - a- 1) pval <- 1 - pf(Ftest,a -1, N - a - 1) Fcrit Ftest pval ``` Full-reduced model F test to see if age is significant: ```{r} lm_full <- lm(chg ~ grp + age, data = vo2max) # same lm_red <- lm(chg ~ grp, vo2max) SSE_full <- sum(lm_full$residuals^2) SSE_red <- sum(lm_red$residuals^2) a <- 2 N <- nrow(vo2max) alpha <- 0.05 num <- (SSE_red - SSE_full) / 1 # only removed 1 predictor den <- SSE_full / (N - a - 1) Ftest <- num / den Fcrit <- qf(1 - alpha,1, N - a- 1) pval <- 1 - pf(Ftest,1, N - a - 1) Fcrit Ftest pval ``` Don't use `anova()` function here because it gives sequential sums of squares. ```{r} anova(lm(chg ~ grp + age, data = vo2max)) anova(lm(chg ~ age + grp, data = vo2max)) ``` Instead, use `Anova()` function from `car` package: ```{r} library(car) # install.packages("car") Anova(lm(chg ~ grp + age, data = vo2max)) Anova(lm(chg ~ age + grp, data = vo2max)) ``` ```{r} x.. <- mean(vo2max$age) x1. <- mean(vo2max$age[vo2max$grp=="aerobic"]) x2. <- mean(vo2max$age[vo2max$grp=="running"]) ``` Check assumptions: ```{r} plot(lm_out, which = 1, add.smooth = F) plot(lm_out, which = 2) ```