This tutorial shows how to do Type 1 ANOVA in R. There are two functions in R that can be used to calculate Type 1 ANOVA, anova()
or aov()
.
Note that, anova()
here is different from Anova()
in Companion to Applied Regression (CAR). Anova()
in CAR can be used only for Type 2 and Type 3 ANOVA.
Sample 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
Example 1: anova() for Type 1 ANOVA in R
Note that, Type 1 ANOVA is Sequential Sum of Squares (please refer to my another tutorial). When given a sequence of objects, anova tests the models against one another in the order specified.
Below is to use anova()
to do the Type 1 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
Example 2: aov() for Type 1 ANOVA in R
aov()
can be also used for Type 1 ANOVA in R. Below is the R code and output.
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, in both anova()
and aov()
, the p-values are exactly the same.
SST of Type 1 ANOVA
For Type I ANOVA, SSA + SSB | A +SSAB | A, B + SSE = SST. We can test that by adding all SS up, which is 1878.5.
SSA + SSB | A +SSAB | A, B + SSE = 902.5 + 93.8+183.7+698.5 =1878.5 = SST
We can also calculate the SST by using the following R code. That is, to calculate SST, we can use the 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