15
$\begingroup$

I'm analyzing data from an unbalanced factorial experiment both with SAS and R. Both SAS and R provide similar Type I sum of squares but their Type III sum of squares are different from each other. Below are SAS and R codes and outputs.

DATA ASD;
INPUT Y T B;
DATALINES;
 20 1 1
 25 1 2
 26 1 2
 22 1 3
 25 1 3
 25 1 3
 26 2 1
 27 2 1
 22 2 2
 31 2 3
;

PROC GLM DATA=ASD;
CLASS T B;
MODEL Y=T|B;
RUN;

Type I SS from SAS

Source  DF       Type I SS     Mean Square    F Value    Pr > F
T       1     17.06666667     17.06666667       9.75    0.0354
B       2     12.98000000      6.49000000       3.71    0.1227
T*B     2     47.85333333     23.92666667      13.67    0.0163

Type III SS from SAS

Source  DF     Type III SS     Mean Square    F Value    Pr > F
T       1     23.07692308     23.07692308      13.19    0.0221
B       2     31.05333333     15.52666667       8.87    0.0338
T*B     2     47.85333333     23.92666667      13.67    0.0163

R Code

Y <- c(20, 25, 26, 22, 25, 25, 26, 27, 22, 31)
T <- factor(x=rep(c(1, 2), times=c(6, 4)))
B <- factor(x=rep(c(1, 2, 3, 1, 2, 3), times=c(1, 2, 3, 2, 1, 1)))
Data <- data.frame(Y, T, B)
Data.lm <- lm(Y~T*B, data = Data)
anova(Data.lm)
drop1(Data.lm,~.,test="F") 

Type I SS from R

Analysis of Variance Table

Response: Y
          Df Sum Sq Mean Sq F value  Pr(>F)  
T          1 17.067  17.067  9.7524 0.03543 *
B          2 12.980   6.490  3.7086 0.12275  
T:B        2 47.853  23.927 13.6724 0.01629 *
Residuals  4  7.000   1.750                  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Type III SS from R

Single term deletions

Model:
Y ~ T * B
       Df Sum of Sq    RSS     AIC F value  Pr(>F)  
<none>               7.000  8.4333                  
T       1    28.167 35.167 22.5751 16.0952 0.01597 *
B       2    20.333 27.333 18.0552  5.8095 0.06559 .
T:B     2    47.853 54.853 25.0208 13.6724 0.01629 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Am I missing something here? If not which one is correct Type III SS?

$\endgroup$

migrated from stackoverflow.com Feb 20 '12 at 22:09

This question came from our site for professional and enthusiast programmers.

29
$\begingroup$

Type III SS depend on the parameterization used. If I set

  options(contrasts=c("contr.sum","contr.poly"))

before running lm() and then drop1() I get exactly the same type III SS as SAS does. For the R-community dogma on this issue, you should read Venables' Exegeses on linear models.

See also: How does one do a Type-III SS ANOVA in R with contrast codes?

$\endgroup$
  • 1
    $\begingroup$ @Peter If you think it can fit in a comment, why not. I don't think so, so why not asking a new question (and link to this one)? $\endgroup$ – chl Feb 20 '12 at 22:40
  • 1
    $\begingroup$ @chl My basic point is that main effects do have meaning in the presence of interactions - they are the effect when the other variable is 0. Often this is meaningful. Not sure it's worth a whole thread. $\endgroup$ – Peter Flom Feb 21 '12 at 0:20
  • 3
    $\begingroup$ I agree that there are situations where the main effects can be interpreted -- Venables takes a very strong line -- but there are lots of situations where they are difficult. I think "don't do this unless you know what you're doing" is a reasonable default setting ... $\endgroup$ – Ben Bolker Feb 21 '12 at 1:51
  • 1
    $\begingroup$ Will the following reset the contrasts to the R standard? options(contrasts=c("contr.treatment", "contr.poly")) $\endgroup$ – Rasmus Larsen Oct 17 '17 at 11:17
  • 1
    $\begingroup$ yes ........... $\endgroup$ – Ben Bolker Oct 17 '17 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.