#Program to analyze the data in Table 17.4 for Example 17.1
library(car)
yd=read.table("C:\\Research\\Book\\Data\\table174.txt")
u1 = yd[,1]
y1 = yd[,2]
y2 = yd[,3]
y3 = yd[,4]
y4 = yd[,5]
y5 = yd[,6]

y  = rbind(cbind(y1),cbind(y2),cbind(y3),cbind(y4),cbind(y5))
x1 = rep(u1,5)
x2 = rep(1:5,each=10)
trt=as.factor(x1)
block = as.factor(x2)
ntrt=recode(trt,"c('1a','1b','lc','1d')='1'")
tapply(y, ntrt, mean)

#Analysis of variance with y=response, treatment and blocks as factors
aov1 = aov(y~block + trt)
aov1
anova(aov1)
summary(aov1)

########################################################################

#Program to analyze the data in Table 17.6 for Example 17.2
library(car)
library(nortest)
library(asbio)
yd=read.table("C:\\Research\\Book\\Data\\table176.txt")
u1 = yd[,1]
y1 = yd[,2]
y2 = yd[,3]
y3 = yd[,4]
y  = rbind(cbind(y1),cbind(y2),cbind(y3))
x1 = rep(u1,3)
x2 = rep(1:3,each=10)
litter=as.factor(x1)
reagent = as.factor(x2)

#Analysis of variance with y=response, litter and reagents as factors
aov1 = aov(y~litter + reagent)
aov1
anova(aov1)
tapply(y,reagent,mean)
TukeyHSD(aov1,"reagent")
resid=residuals(aov1)
coef(aov1)

linearHypothesis(aov1,c(0,0,0,0,0,0,0,0,0,0,1,-1)) #contrast one
linearHypothesis(aov1,c(0,0,0,0,0,0,0,0,0,0,1, 1)) #contrast two

#Checking the anova assumptions: requires the nortest package
shapiro.test(resid)
lillie.test(resid)
cvm.test(resid)
ad.test(resid)

#Non-additivity test requires the asbio package
tukey.add.test(y,reagent,litter)