The following program is 3*3 Crossover design analysis in R. I'd like to know how to calculate Residuals's Sum Sq "8.5089". But trying it, it was “7.29”. The image is the table in Excel, and the following long program in R. I don’t know what point is wrong. Please give me some advice.
group<-factor(c(c(rep(1,9)),c(rep(2,9)),c(rep(3,9))))
id<-factor(rep(1:9, each=3))
period<-factor(rep(c(1,2,3),9))
treat<-factor(c(rep(c(1,2,3),3),rep(c(3,1,2),3),rep(c(2,3,1),3)))
y<-c(7.2,6.6,7.9,8.4,8,7.1,9.2,10.7,8.8,7.9,8.2,9.2,7.8,9.7,10.1,6,9.4,10.2,5.6,6.8,9.2,6.4,6.2,8.6,7.3,7.3,9.5)
(anova<-anova(lm(y~group+treat+id*group+period)))
# Df Sum Sq Mean Sq F value Pr(>F)
#group 2 7.5822 3.7911 6.2377 0.011561 *
#treat 2 10.4422 5.2211 8.5905 0.003678 **
#id 6 11.4978 1.9163 3.1530 0.035919 *
#period 2 12.1756 6.0878 10.0165 0.001993 **
#Residuals 14 8.5089 0.6078
#I'd like to know how to calculate Residuals's Sum Sq "8.5089"
And the following long program for WRONG Residuals's Sum Sq...
#group1
x<-c(7.2,8.4,9.2,6.6,8,10.7,7.9,7.1,8.8)
(n<-length(x))#number of group1
(gm<-mean(x))#mean of group1
x1<-c(7.2,8.4,9.2)#period1@group1
x2<-c(6.6,8,10.7)#period2@group1
x3<-c(7.9,7.1,8.8)#period3@group1
a<-c(1,2,3,1,2,3,1,2,3)#id
b<-c(1,1,1,2,2,2,3,3,3)#period
(tbl<-table(a,b))#number of id, period
(na<-nrow(tbl))#number of id
(ma<-tapply(x,a,mean))#mean of ids
(nb<-ncol(tbl))#number of period
(mb<-tapply(x,b,mean))#mean of period
(x1p<-ma+mb[1]-gm)#mi1=mi.+m.1-mT
(x2p<-ma+mb[2]-gm)#mi2=mi.+m.2-mT
(x3p<-ma+mb[3]-gm)#mi3=mi.+m.3-mT
(rss1<-sum((x1-x1p)^2+(x2-x2p)^2+(x3-x3p)^2))#3.351111
#group2
x<-c(7.9,8.2,9.2,7.8,9.7,10.1,6,9.4,10.2)
(n<-length(x))#number of group2
(gm<-mean(x))#mean of group2
x1<-c(7.9,8.2,9.2)#period1@group2
x2<-c(7.8,9.7,10.1)#period2@group2
x3<-c(6,9.4,10.2)#period3@group2
a<-c(1,2,3,1,2,3,1,2,3)#id
b<-c(1,1,1,2,2,2,3,3,3)#period
(tbl<-table(a,b))#number of id, period
(na<-nrow(tbl))#number of id
(ma<-tapply(x,a,mean))#mean of ids
(nb<-ncol(tbl))#number of period
(mb<-tapply(x,b,mean))#mean of period
(x1p<-ma+mb[1]-gm)#mi1=mi.+m.1-mT
(x2p<-ma+mb[2]-gm)#mi2=mi.+m.2-mT
(x3p<-ma+mb[3]-gm)#mi3=mi.+m.3-mT
(rss2<-sum((x1-x1p)^2+(x2-x2p)^2+(x3-x3p)^2))#3.111111
#group3
x<-c(5.6,6.8,9.2,6.4,6.2,8.6,7.3,7.3,9.5)
(n<-length(x))#number of group3
(gm<-mean(x))#mean of group3
x1<-c(5.6,6.8,9.2)#period1@group3
x2<-c(6.4,6.2,8.6)#period2@group3
x3<-c(7.3,7.3,9.5)#period3@group3
a<-c(1,2,3,1,2,3,1,2,3)#id
b<-c(1,1,1,2,2,2,3,3,3)#period
(tbl<-table(a,b))#number of id, period
(na<-nrow(tbl))#number of id
(ma<-tapply(x,a,mean))#mean of ids
(nb<-ncol(tbl))#number of period
(mb<-tapply(x,b,mean))#mean of period
(x1p<-ma+mb[1]-gm)#mi1=mi.+m.1-mT
(x2p<-ma+mb[2]-gm)#mi2=mi.+m.2-mT
(x3p<-ma+mb[3]-gm)#mi3=mi.+m.3-mT
(rss3<-sum((x1-x1p)^2+(x2-x2p)^2+(x3-x3p)^2))#0.8266667
rss1+rss2+rss3#7.288889
table(group,id)
and you can see that for example id 1-3 only exist in group 1 $\endgroup$