####Chapter 11: Categorical Predictors

###Section 11.1: Coding Schemes
grp <-c(1,1,1,1,2,2,2,2,3,3,3,3)
y <-c(2,3,4,3,6,2,7,9,7,6,6,7)

#ANOVA
anova.mod <-aov(y~factor(grp))
summary(anova.mod)

#Create Chart
grpmeans  <- tapply(y, grp, mean)
barplot(grpmeans, main="Fitness by Condition",
col=c("black","gray","black"),density=c(20,15,10), angle=c(30,20,0),
xlab= "Conditions", names = c("Control","Weights","Ripomatic 450"), 
ylim = c(0, 7), ylab = "Fitness")

#Create coding scheme function for a balanced 3-grp design
codes.3 <-function(y,a,b,c,d,e,f){
 n=length(y)/3
 c1 <-c(rep(a,4),rep(b,4),rep(c,4))
 c2 <-c(rep(d,4),rep(e,4),rep(f,4))
 mod <-lm(y~c1+c2)
}

#Enter criterion and coding scheme
summary(ortho1 <-codes.3(y, -.5, 0, .5, -1/3, 2/3, -1/3))
summary(dummy <-codes.3(y, 0, 1, 0, 0, 0, 1))
summary(effect <-codes.3(y, 1, 0, -1, 0, 1, -1))


###Section 11.2:  Orthogonal Contrast Codes
#Gram-Schmidt Orthogonalization
v1 <-rbind(-.25,-.25,.25,.25)
v2 <-rbind(0,0,-.5,.5)
c3 <-rbind(1,2,3,4)     #Seed vector
v3<-c3-(v1%*%t(c3)%*%v1)/as.vector(t(v1)%*%v1)-(v2%*%t(c3)%*%v2)/as.vector(t(v2)%*%v2)
v3 <-v3-mean(v3)
codes <-cbind(v1,v2,v3);codes

#Regression Analysis Using Orthogonal Codes
y=c(6,6,2,1,2,2,7,7,8,8,4,6)
orth1 <-c(rep(v1[1],3),rep(v1[2],3),rep(v1[3],3),rep(v1[4],3))
orth2 <-c(rep(v2[1],3),rep(v2[2],3),rep(v2[3],3),rep(v2[4],3))
orth3 <-c(rep(v3[1],3),rep(v3[2],3),rep(v3[3],3),rep(v3[4],3))
gram.reg <-lm(y~orth1+orth2+orth3)
summary(gram.reg)

#Create Orthogonal Polynomial Coefficients
seed1 <-rbind(1,2,5,7)     
seed2 <-seed1^2
seed3 <-seed1^3
line <-seed1-mean(seed1)
quad.reg <-lm(seed2~seed1+line)
quad <-resid(quad.reg)
cubic.reg <-lm(seed3~line+quad)
cube <-resid(cubic.reg)

#Regression Analysis Using Orthogonal Polynomial Coefficients
y=c(7,8,6,6,1,2,4,1,2,7,5,6)
poly1 <-c(rep(line[1],3),rep(line[2],3),rep(line[3],3),rep(line[4],3))
poly2 <-c(rep(quad[1],3),rep(quad[2],3),rep(quad[3],3),rep(quad[4],3))
poly3 <-c(rep(cube[1],3),rep(cube[2],3),rep(cube[3],3),rep(cube[4],3))
poly.reg <-lm(y~poly1+poly2+poly3)
summary(poly.reg)

###Section 11.3:  Unbalanced Designs
#Unbalanced Designs with Orthogonal Contrast Codes
g <-c(1,1,2,2,2,2,2,3,3,3,4,4)
y <-c(2,1,8,9,8,6,5,3,6,3,6,7)
c1 <-c(3,3,rep(-1,10))
c2 <-c(0,0,rep(2,5),rep(-1,5))
c3 <-c(rep(0,7),rep(-1,3),rep(1,2))

#Unweighted Regression
unweighted.reg <-lm(y~c1+c2+c3)
summary(unweighted.reg)
vcov(unweighted.reg)
anova(unweighted.reg)

#Weighted Regression
linear <-c1-mean(c1)
quad.reg <-lm(c2~c1+linear)
quadratic <-resid(quad.reg)
cubic.reg <-lm(c3~linear+quadratic)
cubic <-resid(cubic.reg)
wgts <-cbind(linear,quadratic,cubic)
wgts
weighted.reg <-lm(y~wgts)
summary(weighted.reg)
vcov(weighted.reg)
anova(weighted.reg)

#Weighted Effect Codes With Last Group as Base Group
u1 <-c(1,1,0,0,0,0,0,0,0,0,-1,-1)
u2 <-c(0,0,1,1,1,1,1,0,0,0,-1,-1)
u3 <-c(0,0,0,0,0,0,0,1,1,1,-1,-1)
gsize <-c(rep(2,2),rep(5,5),rep(3,3),rep(2,2))
e1 <-u1/gsize
e2 <-u2/gsize
e3 <-u3/gsize
wgt.eff <-lm(y~e1+e2+e3)
summary(wgt.eff)
