This tutorial shows how you can calculate Type 2 ANOVA in R with an example. Anova() in the package of Companion to Applied Regression (CAR) can be used to calculate Type 2 ANOVA in R.
Note that, Anova() in CAR only provides options of Type 2 and Type 3 ANOVA. It is different from another function anova() in R, which can be used to calculate Type 1 ANOVA.
Step 1: Prepare the Data
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)
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
Step 2: Type 2 ANOVA in R
In the following, we use ANOVA() in R to do Type 2 ANOVA. Note that, we specify type=2
in the statement.
#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
Step 3 (Optional): Calculate SST
We can use the following R code to calculate the SST for Type 2 ANOVA.
#calculate the all the SS from different components
print(sum(result4['Sum Sq']))
Output:
[1] 1960.15
We can see that 984.15+93.75+183.75+698.50 = 1960.15.
To compare, we can also calculate SST. SST can be calculated via a model only with intercept. We can see that the SST is also 1878.5.
# Calculate SST in R
sales_intercept <- lm(sales ~ 1, data = df)
anova(sales_intercept)
Output:
Response: sales Df Sum Sq Mean Sq F value Pr(>F) Residuals 9 1878.5 208.72
Thus, SSA | B + SSB | A +SSAB | A, B + SSE ≠ SST for Type 2 ANOVA. This conclusion is consistent with what I discussed in another post.