This tutorial explains what Type I, Type II, and Type III ANOVA are. Further, it provides examples showing how you can do Type I, Type II, and Type III ANOVA in R.
1. Introduction
Type I, Type II, and Type III ANOVA are 3 different ways of calculating sum of squares in ANOVA.
Type I ANOVA:
- SS(A) for factor A
- SS(B | A) for factor B
- SS(AB | A, B) for interaction AB
Type II ANOVA:
- SS(A | B) for factor A
- SS(B | A) for factor B
- SS(AB | A, B) for interaction AB
Type III ANOVA:
- SS(A | B, AB) for factor A
- SS(B | A, AB) for factor B
- SS(AB | A, B) for interaction AB
You can calculate them using R and the following shows the examples. First, we will generate the data being use later. The data has two categorical IVs (cities and stores) and one DV (sales).
x_1 = rep(c('City1','City2'),each=5) x_2 = rep(c('store1','store2'), 5) sales=c(10,20,20,50,30,10,5,4,12,4) df <- data.frame (cities = x_1, stores = x_2, sales=sales) print(df)
The following is the output:
cities stores sales 1 City1 store1 10 2 City1 store2 20 3 City1 store1 20 4 City1 store2 50 5 City1 store1 30 6 City2 store2 10 7 City2 store1 5 8 City2 store2 4 9 City2 store1 12 10 City2 store2 4
2. Type I ANOVA in R
You can use either anova() or aov() to do Type I ANOVA in R.
#Use anova() to Calculate Type I ANOVA in R
sales_ANOVA <- lm(sales ~ cities*stores, data = df)
ANOVA_result<-anova(sales_ANOVA)
print(ANOVA_result)
Output:
Analysis of Variance Table Response: sales Df Sum Sq Mean Sq F value Pr(>F) cities 1 902.50 902.50 7.7523 0.03182 * stores 1 93.75 93.75 0.8053 0.40408 cities:stores 1 183.75 183.75 1.5784 0.25569 Residuals 6 698.50 116.42
#Use aov() to Calculate Type I ANOVA in R
aov_result<-aov(sales ~ cities*stores, data = df)
summary(aov_result)
Output:
Df Sum Sq Mean Sq F value Pr(>F) cities 1 902.5 902.5 7.752 0.0318 * stores 1 93.8 93.8 0.805 0.4041 cities:stores 1 183.7 183.7 1.578 0.2557 Residuals 6 698.5 116.4
As we can see that, the p-values from anova() and aov() are exactly the same. You can read an independent tutorial about Type 1 ANOVA in R here.
3. Type II ANOVA in R
We can use the Anova() function within the package of Companion to Applied Regression (CAR) to calculate Type II ANOVA in R.
Note that, the Anova() function only provides options of Type II and Type III ANOVA. Thus, you can not use it to calculate Type I ANOVA.
#Using Anova() for Type II ANOVA
result4<-car::Anova(lm(sales ~ cities*stores, data = df),type=2)
print(result4)
Output:
Anova Table (Type II tests) Response: sales Sum Sq Df F value Pr(>F) cities 984.15 1 8.4537 0.02707 * stores 93.75 1 0.8053 0.40408 cities:stores 183.75 1 1.5784 0.25569 Residuals 698.50 6 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
We can see that the SS (cities) is 984.15, which is different from SS (cities) in Type I ANOVA. You can read an independent tutorial about Type 2 ANOVA in R here.
4. Type III ANOVA in R
We use Anova() function from CAR to calculate Type III ANOVA. The following is the R code.
# Using Anova() for Type III ANOVA result5<-car::Anova(lm(sales ~ cities*stores, data = df),type=3) print(result5)
Output:
Anova Table (Type III tests) Response: sales Sum Sq Df F value Pr(>F) (Intercept) 1200.00 1 10.3078 0.01835 * cities 158.70 1 1.3632 0.28728 stores 270.00 1 2.3193 0.17861 cities:stores 183.75 1 1.5784 0.25569 Residuals 698.50 6 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
As we can see, the results are different from what are in Type I and Type II ANOVA. For more detailed discussions about Type 3 ANOVA in R, please refer to this tutorial.