dat<-read.table("CH19PR10.txt")
names(dat)<-c('y','age','gender')
dat$age<-factor(dat$age)
dat$gender<-factor(dat$gender)
fit<-aov(data=dat,y~age*gender)
summary(fit)
## Df Sum Sq Mean Sq F value Pr(>F)
## age 2 316.7 158.36 66.291 9.79e-12 ***
## gender 1 5.4 5.44 2.279 0.142
## age:gender 2 5.1 2.53 1.058 0.360
## Residuals 30 71.7 2.39
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library("ggpubr")
## Warning: package 'ggpubr' was built under R version 3.4.2
## Loading required package: ggplot2
## Loading required package: magrittr
ggboxplot(dat, x = "age", y = "y", color = "gender",
palette = c("#00AFBB", "#E7B800"))
## b
for (i in c(1,2,3,4,5,6)){
print (sum(fit$residuals[(6*(i-1)+1):(6*i)]))
}
## [1] -2.553513e-15
## [1] 3.996803e-15
## [1] 2.914335e-15
## [1] -2.88658e-15
## [1] -4.440892e-16
## [1] -7.771561e-16
Yes, they sum to zero for each treatment.
par(mfrow=c(1,2))
stripchart(split(resid(fit), dat$gender), method = "stack", pch = 19)
abline(h = seq(2, 4)-0.1)
title("Aligned Residual Dot Plot gender")
stripchart(split(resid(fit), dat$age), method = "stack", pch = 19)
abline(h = seq(2, 4)-0.1)
title("Aligned Residual Dot Plot age")
rq<-c()
for (i in c(1:36)) {
qq<-qnorm((i-3/8)/(36+1/4))
rq<-c(rq,qq)
}
plot(rq,sort(fit$residuals))
abline(0,1)
cor(rq,sort(fit$residuals))**2
## [1] 0.9720399
when n = 36 and significance value = 0.05, the Critical Values for Coefficient of Correlation between Ordered Residuals and Expected Values under Normality is 0.97. And the correlation calculated is 0.9720399. So it appears reasonable.
par(mfrow=c(1,2))
arrofres = matrix(nrow = 6,ncol = 6)
for (i in c(1,2,3,4,5,6)){
arrofres[i,]<-fit$residuals[(6*(i-1)+1):(6*i)]
}
matplot(arrofres)
plot(fit$residuals,type = 'b')
residuals in each treatment’s sum is equal to zero, and it seems that the residuals has no relation with treatments.
interaction.plot(dat$age,dat$gender,dat$y,type="b",col=c("red","blue"),pch=c(16,18))
age has larger effect and gender has small effect, since they are nearly parrallel, they have little interaction.
fit<-aov(data=dat,y~age*gender)
anova(fit)
## Analysis of Variance Table
##
## Response: y
## Df Sum Sq Mean Sq F value Pr(>F)
## age 2 316.72 158.361 66.2907 9.789e-12 ***
## gender 1 5.44 5.444 2.2791 0.1416
## age:gender 2 5.06 2.528 1.0581 0.3597
## Residuals 30 71.67 2.389
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
age, it has the largest SSR.
fit<-aov(data=dat,y~age*gender)
anova(fit)
## Analysis of Variance Table
##
## Response: y
## Df Sum Sq Mean Sq F value Pr(>F)
## age 2 316.72 158.361 66.2907 9.789e-12 ***
## gender 1 5.44 5.444 2.2791 0.1416
## age:gender 2 5.06 2.528 1.0581 0.3597
## Residuals 30 71.67 2.389
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
qf(0.95,2,30)
## [1] 3.31583
1-pf(1.0581,2,30)
## [1] 0.3597133
qf(0.95,2,2)
## [1] 19
qf(0.95,1,2)
## [1] 18.51282
1-pf(66.2907,2,2)
## [1] 0.01486089
1-pf(2.2791,1,2)
## [1] 0.2701973
Yes, age has large effect and gender has little effect, and there are no apparent interactions