I have a linear regression model with one categorical variable $A$ (male & female) and one continuous variable $B$.
I set up contrasts codes in R with options(contrasts=c("contr.sum","contr.poly"))
.
And now I have Type III sums of squares for $A$, $B$, and their interaction (A:B) using drop1(model, .~., test="F")
.
What I am stuck with is how sums of squares is calculated for $B$. I think it is sum((predicted y of the full model - predicted y of the reduced model)^2)
. The reduced model would look like y~A+A:B
. But when I use predict(y~A+A:B)
, R is returning predicted values that are the same as the full model predicted values. Therefore, the sums of squares would be 0.
(For the sums of squares of $A$, I used a reduced model of y~B+A:B
, which is the same as y~A:B
.)
Here is example code for randomly generated data:
A<-as.factor(rep(c("male","female"), each=5))
set.seed(1)
B<-runif(10)
set.seed(5)
y<-runif(10)
model<-lm(y~A+B+A:B)
options(contrasts = c("contr.sum","contr.poly"))
#type3 sums of squares
drop1(model, .~., test="F")
#or same result:
library(car)
Anova(lm(y~A+B+A:B),type="III")
#full model
predFull<-predict(model)
#Calculate sum of squares
#SS(A|B,AB)
predA<-predict(lm(y~B+A:B))
sum((predFull-predA)^2)
#SS(B|A,AB) (???)
predB<-predict(lm(y~A+A:B))
sum((predFull-predB)^2)
#Sums of squares should be 0.15075 (according to anova table)
#but calculated to be 2.5e-31
#SS(AB|A,B)
predAB<-predict(lm(y~A+B))
sum((predFull-predAB)^2)
#Anova Table (Type III tests)
#Response: y
# Sum Sq Df F value Pr(>F)
#(Intercept) 0.16074 1 1.3598 0.2878
#A 0.00148 1 0.0125 0.9145
#B 0.15075 1 1.2753 0.3019
#A:B 0.01628 1 0.1377 0.7233
#Residuals 0.70926 6