Two Way ANOVA (Analysis of Variance)


Two way ANOVA (Analysis of Variance) is applied to test how two independent variables affects a dependent variable.
For example, suppose you want to compare the marks scored by urban, rural and semi-urban students, you will have three different means to compare with each other. Here, appropriate test of significance will be one way ANOVA. ("One way" - signifying only one independent variable, residence). Also, we can deduce that there are three levels (or categories or groups) withing this independent variable. Now, suppose that, another independent variable is added i. e. Gender, in the study. Now, we have two independent variables, residence - having three levels and gender having two levels. If our first independent varible - residence - has significant impact on scores, then means scores of students from these three categories of residents will have significantly different scores. Similarly, if gender has significant impact on scores, then means scores in two genders will be significantly different. But, the impact of gender on three different categories of residences can be different, e. g. rural and urban students can experience different impact of gender! So this is interaction effect.

In this situation, we want to test whether both independent variables have significant effect on the continuous variable, separately. Additionally, we may also want to test whether there is an interaction effect. Two way ANOVA is the test applied in this situation.

Assumptions
1. There are two categorical independent variables, with two or more levels / categories in each independent variable.
2. Dependent variable is continuous and normally distributed in all the possible combinations of two independent variable levels.
3. Samples are drawn using random sampling technique.
4. The observations are independent. There are no observations, which are in more than one group.
5. The variance in all the groups is same. (homogeneity of variance / homoscedasticity). It can be tested by Levene's test of homogeneity of variance.
6. There are no outliers.

Steps:

1. Null hypothesis and Alternate Hypothesis

Null Hypothesis

A. The sample means for all categories in first independent variable (A) are equal . (x̄i = x̄j )
B. The sample means for all categories in second independent variable (B) are equal . (x̄i = x̄j )
C. There is no interaction between factors A and B.

 Alternate Hypothesis

A. The sample means for at least two categories in first independent variable (A) are not equal . (x̄i≠ x̄j )
B. The sample means for at least two categories in second independent variable (B) are not equal . (x̄i ≠ x̄j )
C. There is significant interaction between factors A and B.

2. Calculate Sum of Squares for IDV A (SSA Between)

ANOVA SS Treatment

where ni is the sample size of category / group i in IDV A.
x̄ = Grand mean (Mean of all the observations)
i = Mean of level (group) i for IDV A.
ni = Sample size in group i for IDV A.

3. Calculate Sum of Squares for IDV B (SSB Between)

ANOVA SS Treatment

where nj is the sample size of level / category / group j in IDV B
x̄ = Grand mean (Mean of all the observations)
j = Mean of level (group) j for IDV B.
nj = Sample size in group j for IDV B.

4. Calculate Sum of Squares Within (SSE) (Error)

ANOVA SS Within

xijk = kth observation in group ij. (Observation from level i for IDV A and level j for IDV B)
ij = Mean of group ij (Mean of observations from level i for IDV A and level j for IDV B).

5. Calculate Interaction Sum of Squares (SS AB)

ANOVA SSAB


ij = Mean of group ij (Mean of observations from level i for IDV A and level j for IDV B).
i = Mean of group i from IDV A.
j = Mean of group j from IDV B.
x̄ = Grand Mean (Mean of all the observations)
nij = Sample size in group ij.

6. Calculate Total Sum of Squares (SS T)

ANOVA SSAB

xijk = kth observation in group ij. (Observation from level i for IDV A and level j for IDV B).
x̄ = Grand mean

6. Calculate degrees of freedom

df_A = Degrees of freedom for IDV A= a - 1
df_B = Degrees of freedom for IDV B= b - 1
df_AB = Degrees of freedom interaction AB = (a -1) * (b - 1)
df_Error = Degrees of freedom for Error = N - a * b
df_total = Degrees of freedom Total = N -1


N = Grand Sample Size, a = Number of levels / groups / categories in IDV A, b = Number of levels / groups / categories in IDV B, IDV = Independent variable

7. Calculate Mean Sum of Squares for IDV A, IDV B, interaction

ANOVA MSS

8. Calculate F value for IDV A, IDV B and interaction

F ANOVA

6. Calculate p values, using F table for IDV A, IDV B and interaction AB.

7. Interpret all three p values

If, p < = alpha, then reject the respective Null hypothesis
If, p > alpha, then accept respective null hypthesis

8. Post hoc tests

If, p < = alpha, alternate hypothesis is accepted (at least two means are significantly different)
Which pair (or pairs) of means are significantly different is now identified by post hoc tests.
There are multiple post hoc tests available, out of which Benferroni and Tukey HSD are most commonly used.


Steps for applying two way ANOVA using this software

1. To apply two way ANOVA here, you need to arrange the data in three columns. First row of the data can be used for variable names. First Column must be of dependent variable, which must be numerical. Second and third column should be of independent variables 1 and 2. You can copy the data (in three columns in exact sequence as specified above), from any other file such as excel, and paste directly into the text box. Please do not include any other column (such as subject no / sr no etc).

 2. Specify your choosen significance level or alpha (usually 5 or 1).

3. Specify the type of "Sum of Squares (SSQ) you want. There are three options availabale. Most commonly used option is "Type III". You can select that default option of Type III. Other options are "Type I" and "Type II". Please see details about SSQ given at the bottom.

4. Click on "Run Test".

5. Following is example of input and output, with detailed description and explanation.

 

Suppose you want to compare mean systolic blood pressure in athletes participating in four different sports: Marathon, Football, Chess and Swimming. Also you want to add Gender as your second independent variable. Following is the data of athletes, with their respective systolic blood pressures, Sports and Gender. Please note that data is arranged in three columns with specified sequence.

SBP Sports Gender
136 Chess Male
122 Chess Female
118 Swimming Male
110 Football Female
140 Chess Male
136 Chess Male
120 Football Male
128 Chess Male
112 Football Female
118 Swimming Female
126 Swimming Male
128 Marathon Male
122 Football Male
118 Football Female
126 Swimming Male
124 Football Male
126 Chess Female
116 Chess Female
132 Chess Male
114 Marathon Female
118 Football Male
134 Marathon Female
120 Marathon Female
114 Swimming Male
128 Chess Male
136 Chess Male
120 Football Female
120 Swimming Male
128 Marathon Male
128 Swimming Female
126 Football Female
118 Chess Male
128 Chess Male
116 Football Male
120 Football Male
124 Marathon Male
120 Chess Female
136 Chess Male
120 Marathon Male
112 Chess Female
120 Swimming Female
110 Marathon Female
122 Swimming Female
122 Chess Female
118 Marathon Male
130 Chess Female
110 Chess Female
126 Marathon Female
122 Swimming Male
124 Football Male
124 Swimming Female
150 Chess Male
144 Chess Female
116 Marathon Male
118 Marathon Female
114 Marathon Female
120 Swimming Male
120 Chess Male
120 Swimming Female
144 Chess Male

You can copy and paste above data into excel sheet, to have it in the required format. Then from excel you copy and paste the data in the given textbox. Select 5% as alpha and Type III model type for SSQ. Finally click on "Run Test".
Following are the results
Descriptive: Sports
Category Mean SD Sample Size
Chess128.818210.755422
Swimming121.38463.86313
Football119.16674.783212
Marathon120.76926.857513

Above table shows the mean SBP, SD and Number of study participants in four different sports.


Descriptive: Gender
Category Mean SD Sample Size
Male125.93948.681733
Female120.59267.821427

Above table shows Gender wise mean SBP, SD and Number of study participants.

Descriptive: Both Independent Variables
Chess Swimming Football Marathon
Male133.2308 (13)120.8571 (7)120.5714 (7)122.3333 (6)
Female122.4444 (9)122 (6)117.2 (5)119.4286 (7)

Above table shows Gender and Sports wise mean (Number of subjects)

ANOVA Table
Sum of Squares (Type III) df Mean Sum of Squares F p Partial eta squared
Between Groups (Sports) 784.37493261.45834.96030.0042 0.2225
Between Groups (Gender) 220.39041220.39044.18110.046 0.0744
Interaction (Sports * Gender) 321.8683107.28932.03540.12030.1051
Corrected model1685.98447240.85494.56940.00050.3808
Within Groups (Error)2740.9495252.7106

Above table shows the results of Two Way ANOVA. This table is the main output of the test. It shows Sum of Squares: SSQ(Type III) for each of the independent variable and interaction variable, degrees of freedom: df, Mean Sum of Squares (MSQ), F value, p and partial eta squared.
We are interested only in three rows. Two rows corresponding to two independent variables (Sports and Gender) and one row corresponding to interaction. (Rows with highlighted background)

First row, corresponding to IDV- Stay, shows that the F = 4.9603 and p = 0.0042. As p < 0.05. So we can infer that there is significant difference in at least two category means of SBP for different Sports.
Similary, for second row, corresponding to IDV- Gender, F=4.1811 and p=0.046, with similar interpretation of significant difference in at least two category means in Gender (Incidently there are only two means in Gender)
Partial eta square is the measure effect size, quantifying the variance explained by the independent variable, after excluding variance explained by other independent variable. Here partial eta squared for Sports is 0.2225, signifying that about 22.25% of variation in SBP can be explained by Sports.
Finally p value for interaction is 0.1203 (non-significant), indicating that there is no significant interaction of Sports and Gender for SBP.
So, we can infer that at least two means in Sports and at least two  means in Gender are significanly different. For Gender, as there are only two means (for Males and Females), there is no issue in identifying such two menas. However, for IDV Sports, we need to apply post-hoc test to identify which means are significantly different. Most commonly used post hoc tests are Bonferroni test and Tuckey HSD test. Following are the results of the both the tests for IDV Sports.
 
Bonferroni test (Sports)
Comparison between Difference between means Standard error t critical t Bonferroni adjusted p Confidence interval LB Confidence interval UB
Mean:Chess AND Mean:Swimming 7.4336 2.53982.92682.7430.03040.467 14.4001
Mean:Chess AND Mean:Football 9.6515 2.60553.70432.7430.00312.5048 16.7982
Mean:Chess AND Mean:Marathon 8.049 2.53983.16912.7430.01541.0824 15.0155
Mean:Swimming AND Mean:Football 2.2179 2.90640.76312.7431-5.7542 10.1901
Mean:Swimming AND Mean:Marathon 0.6154 2.84770.21612.7431-7.1957 8.4265
Mean:Football AND Mean:Marathon -1.6026 2.90640.55142.7431-9.5747 6.3696

We can see that p vaules for comparision between means of Chess and Swimming, Chess and Football, Chess and Marathon (three pairs of means) are significantly different. Thus, the Bonferroni test has identified three pairs of means which are significantly different than each other.

Following are the results of post hoc Tuckey HSD test
Comparison between Difference between means Standard error p Confidence interval LB Confidence interval UB
Mean: Chess AND Mean: Swimming 7.4336 2.53980.02530.6919 14.1752
Mean: Chess AND Mean: Football 9.6515 2.60550.00282.7355 16.5675
Mean: Chess AND Mean: Marathon 8.049 2.53980.01321.3073 14.7906
Mean: Swimming AND Mean: Football 2.2179 2.90640.8706-5.4969 9.9328
Mean: Swimming AND Mean: Marathon 0.6154 2.84770.9964-6.9435 8.1743
Mean: Football AND Mean: Marathon -1.6026 2.90640.9458-9.3174 6.1122

Results are Tuckey HSD test are also similar to Bonferroni test.

Two Way ANOVA

Above chart shows the Sports-wise means of SBP in two Genders. It can be seen that the lines for two genders are just crossing each other, suggesting a little bit of interaction. However, just after crossing the lines are almost parallel, suggesting non-signficant interaction. If lines cross each other, and diverse apart, it suggests significant interaction.
How to report these findings:
A two-way ANOVA was performed to compare the effects of Sports in which a person indulged and Gender on Systolic Blood Pressure. It revealed that there was significant difference in SBP between at least two group means in Sports (F=4.9603, df=3, p=0.0042). Post hoc Bonferroni test revealed that the means of SBP for Chess players and Swimmers were significantly different (Bonferroni adjusted p = 0.467 ). Similarly means of SBP in Chess players and Marathon runners (Bonferroni adjusted p = 0.0154 ) and those in Chess players and Football players (Bonferroni adjusted p =0.0031) were also significantly different. Tukey’s HSD test also reveled similar findings.
The two-way ANOVA also revealed that there was significant difference between means of SBP in males and females (F=4.1811, p=0.046). There was no significant effect of interaction between Sports and Gender (F=2.0354, p=0.1203).
@ Sachin Mumbare

@Sachin Mumbare