## An R Companion for the Handbook of Biological Statistics

Salvatore S. Mangiafico

# Type I, II, and III Sums of Squares

An in-depth discussion of Type I, II, and III sum of squares is beyond the scope of this book, but readers should at least be aware of them.  They come into play in analysis of variance (anova) tables, when calculating sum of squares, F-values, and p-values.

Perhaps the most salient point for beginners is that SAS tends to use Type III by default whereas R will use Type I with the anova function.  In R, Type II and Type III tests are accessed through Anova in the car package, as well as through some other functions for other types of analyses.  However, for Type III tests to be correct, the way R codes factors has to be changed from its default with the options(contrasts =… ) function.  Changing this will not affect Type I or Type II tests.

Type I sum of squares are “sequential.”  In essence the factors are tested in the order they are listed in the model.

Type III sum of squares are “partial.”  In essence, every term in the model is tested in light of every other term in the model.  That means that main effects are tested in light of interaction terms as well as in light of other main effects.

Type II sum of squares are similar to Type III, except that they preserve the principle of marginality.  This means that main factors are tested in light of one another, but not in light of the interaction term.

When data are balanced and the design is simple, types I, II, and III will give the same results.  But readers should be aware that results may differ for unbalanced data or more complex designs.  The code below gives an example of this.

There are disagreements as to which type should be used routinely in analysis of variance.  In reality, the user should understand what hypothesis she wants to test, and then choose the appropriate tests.  As general advice, I would recommend not using Type I except in cases where you intend to have the effects assessed sequentially.  Beyond that, probably a majority of those in the R community recommend Type II tests, while SAS users are more likely to consider Type III tests.

Some experimental designs will call for using a specified type of sum of squares, for example when you see “/ SS1” or “HTYPE=1” in SAS code.

##### Note

As a final note, readers should not confuse these sums of squares with “Type I error”, which refers to rejecting a null hypothesis when it is actually true (a false positive), and “Type II error”, which is failing to reject null hypothesis when it actually false (a false negative).

### Packages used in this chapter

The following commands will install these packages if they are not already installed:

if(!require(car)){install.packages("car")}

### Setting contrasts for different sums of squares

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

### needed for type III tests

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

### returns the default contrasts in R

#### Contrasts can also be set within a model.

model = lm(Y ~ A + B + A:B,
contrasts=list(A="contr.sum", B="contr.sum"))

### Example for Type I, II, II sums of squares

A = factor(c("a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "b", "b")
B = factor(c("x", "y", "x", "y", "x", "y", "x", "y", "x", "x", "x", "x")
Y =        c( 14,  30,  15,  35,  50,  51,  30,  32,  51,  55,  53,  55)

#### Type I tests

model.1 = lm(Y ~ A + B + A:B)

anova(model)

Analysis of Variance Table

Df  Sum Sq Mean Sq F value   Pr(>F)

A          1 1488.37 1488.37 18.3892 0.002658 **

B          1   18.22   18.22  0.2252 0.647809

A:B        1  390.15  390.15  4.8204 0.059406 .

Residuals  8  647.50   80.94

#### Type II tests

model.2 = lm(Y ~ A + B + A:B)

library(car)

Anova(model, type="II")

Anova Table (Type II tests)

Response: response

Sum Sq Df F value   Pr(>F)

A         1476.22  1 18.2391 0.002722 **

B           18.22  1  0.2252 0.647809

A:B        390.15  1  4.8204 0.059406 .

Residuals  647.50  8

#### Type III tests

model.3 = lm(Y ~ A + B + A:B,
contrasts=list(A="contr.sum", B="contr.sum"))

library(car)

Anova(model.3, type="III")

Anova Table (Type III tests)

Sum Sq Df  F value    Pr(>F)

(Intercept) 11343.8  1 140.1544 2.377e-06 ***

A            1135.3  1  14.0275  0.005662 **

B              66.1  1   0.8173  0.392381

A:B           390.1  1   4.8204  0.059406 .

Residuals     647.5  8

Residuals     647.5  8

### References

Georg-August-Universität Göttingen. Anova – Type I/II/III SS explained.

RExRepos. Sum of squares type I, II, and III.