17
$\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$
1

2 Answers 2

31
$\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$
6
  • 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
    Commented Feb 20, 2012 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
    Commented Feb 21, 2012 at 0:20
  • 4
    $\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
    Commented Feb 21, 2012 at 1:51
  • 1
    $\begingroup$ Will the following reset the contrasts to the R standard? options(contrasts=c("contr.treatment", "contr.poly")) $\endgroup$ Commented Oct 17, 2017 at 11:17
  • 1
    $\begingroup$ yes ........... $\endgroup$
    – Ben Bolker
    Commented Oct 17, 2017 at 11:47
2
$\begingroup$

aov3 in the sasLM package in R will give the same results as SAS Type III. (Continued after output.)

library(sasLM)
aov3(Y ~ T*B, Data) # Data is defined in question

giving:

Response : Y
                Df Sum Sq Mean Sq F value  Pr(>F)  
MODEL            5 77.900  15.580  8.9029 0.02733 *
 T               1 23.077  23.077 13.1868 0.02213 *
 B               2 31.053  15.527  8.8724 0.03384 *
 T:B             2 47.853  23.927 13.6724 0.01629 *
RESIDUALS        4  7.000   1.750                  
CORRECTED TOTAL  9 84.900                          
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

drop1 does not necessarily give the same result because the way it works is that it partitions the columns into two

  1. those corresponding to the term of interest and
  2. the other columns

and then compares the model without (1) to the full model; however, what SAS Type III does is that it conceptually partitions the columns into 3 groups, rather than 2 groups:

  1. those that do not contain the terms of interest
  2. the columns that correspond to the term of interest
  3. the columns representing interactions that contain the term of interest.

It then modifies the third set of columns in a way described in the first link below and its references to compare the model containing the first group and the modified third group to the full model.

See https://stackoverflow.com/questions/75594861/differences-in-sum-of-squares-between-jmp-and-r-when-nested-effects-in-the-model/75595079 and https://pubmed.ncbi.nlm.nih.gov/32656159/ for more information.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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