Type 3 ANOVA in R can be calculated using Anova()
function from CAR. The following shows the steps of doing it in R.
Step 1: Prepare data in R
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: Use Anova() for Type 3 ANOVA in R
The following is the R code using ANOVA() to calculate Type 3 ANOVA in R. Note that, it states type=3, which suggests it is a Type 3 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
Step 3: Interpretation the output
The p-value for the interaction is 0.256, which is greater than 0.05. Thus, the interactin effect of cities*stores is not significant.
Further, we can see that 158.70+270.00+183.75+698.50 = 1310.95. Thus, it is not equal to SST, which is 1878.5 (see here regarding how to calculate SST). Thus, it is consistent with what I discussed in another post, namely SSA | B, AB + SSB | A, AB + SSAB | A, B + SSE ≠ SST for Type 3 ANOVA.
Step 4 (optional): Type 2 vs. Type 3 ANOVA in R
If you test the same data for Type 2 ANOVA, you will see the following 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
If you use You might have question why Type 2 ANOVA dose not have the output for the intercept here, where there is intercept in Type 3 ANOVA in R.
This is due to the fact that Type III ANOVA is equal to the linear regression model below. However, this is not the case for Type I or Type II ANOVA.
sales = b0 + b1 cities + b2 stores +b3 cities*stores
To verify that, we can test the same data using linear regression. That is, the following is the linear model we are going to test.
# Linear regression for the same data
estimated_coefficients <- lm(sales ~ cities*stores, data = df)
summary(estimated_coefficients)
Output:
> summary(estimated_coefficients) Call: lm(formula = sales ~ cities * stores, data = df) Residuals: Min 1Q Median 3Q Max -15.000 -3.125 -1.000 3.875 15.000 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 20.000 6.229 3.211 0.0184 * citiesCity2 -11.500 9.850 -1.168 0.2873 storesstore2 15.000 9.850 1.523 0.1786 citiesCity2:storesstore2 -17.500 13.929 -1.256 0.2557 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 10.79 on 6 degrees of freedom Multiple R-squared: 0.6282, Adjusted R-squared: 0.4422 F-statistic: 3.379 on 3 and 6 DF, p-value: 0.09539
We can see that all the p-values in linear regression are consistent with Type 3 ANOVA, but not Type 2 ANOVA.