--- title: "STAT 516 Lec 08 in class" format: html --- ```{r} y <- c(128,127,129,126,128,121,120,123,122,125, 126,125,127,125,124,125,126,129,128,127) machine <- as.factor(c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)) ``` ```{r} boxplot(y~ machine) ``` ```{r} lm_out <- lm(y ~ machine) anova(lm_out) ``` Get the method of moments estimators for the two variance components: ```{r} a <- 4 n <- 5 SStot <- sum((y - mean(y))^2) SSerror <- sum(lm_out$residuals^2) SStrt <- SStot - SSerror MStrt <- SStrt / (a - 1) MSerror <- SSerror / ( a*(n-1)) sigma_Asq <- (MStrt - MSerror)/n sigma_esq <- MSerror sigma_Asq sigma_esq ``` Get REML estimates of the variance components: ```{r} library(lmerTest) # first time run install.packages("lmerTest") lmer_out <- lmer(y ~ 1 + (1|machine)) summary(lmer_out) ``` Confidence interval for the overall mean: ```{r} y.. <- mean(y) # overall mean alpha <- 0.05 tval <- qt(1- alpha/2,a-1) me <- tval * sqrt(MStrt / (a*n)) lo <- y.. - me up <- y.. + me c(lo,up) ``` Obtain the predictions of the realized values of the random effects $A_1,\dots,A_a$. ```{r} predict(lmer_out,random.only=T) ``` Check for equal variances across treatment groups: ```{r} plot(lmer_out) ``` Build a normal QQ plot of the residuals ```{r} yhat <- predict(lmer_out) ehat <- y - yhat # residuals qqnorm(ehat) ```