1

a

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.

c

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")

d

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.

e

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.

2

a

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.

b

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.

c

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
  • Hypothesis: $H_0 : ^2_{}>0, i =1,2,3,j=1,2 $
  • Decision rule: Reject \(H_0\) if \(F* >F_{0.95,2,30}\)
  • Conclusion: Since \(F∗ = \frac{MSAB}{MSE} = 1.0581\) , we do not reject \(H_0\) and conclude that there are no interaction effects with p = 0.3597133

d

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

i. Factor A (age) main effect

  • Hypothesis: \(H_0 : \alpha_i = 0 vs H_a\) : at least one \(\alpha_i\) is not 0, i = 1, 2, 3
  • Decision rule: Reject \(H_0\) if \(F∗ = MSA/MSE > F_{0.95,2,2} = 19\)
  • Conclusion: Since F∗ = 158.361/2.528 =62.6428 , we reject \(H_0\) and conclude that there is Factor A main effect for the number of coats with p =0.01486089

ii. Factor B (gender) main effect

  • Hypothesis: \(H_0 : \beta_i = 0 vs H_a\) : at least one \(\beta_i\) is not 0, i = 1, 2
  • Decision rule: Reject \(H_0\) if \(F∗ = MSB/MSE > F_{0.95,1,2} = 18.51282\)
  • Conclusion: Since F∗ = 2.2791 , we do not reject \(H_0\) and conclude that there is no Factor B main effect for the number of coats with p =0.2701973

f

Yes, age has large effect and gender has little effect, and there are no apparent interactions

g

  • single factor: age 316.7, Residuals 82.2
  • two factor: age 316.7, gender 5.44, age:gender 5.06, Residuals 71.67 factor A age has same sum of squares, and residuals’ sum of squares in single factor ANOVA equals to all other sum of squares in two factor ANOVA except age. Yes, the degree holds the same relation.