How to Perform a Three-Way ANOVA in Python

Spread the love


Analysis of variance (ANOVA) is a powerful statistical method used to analyze the differences among group means. It is often used in research and experimentation to determine whether the means of several groups are equal or whether there are statistically significant differences among them. A three-way ANOVA is a type of ANOVA that is used when there are three independent variables, often referred to as factors. It allows you to examine how three factors impact a dependent variable. This article will guide you through the process of performing a three-way ANOVA in Python.

Structure of Data

For a three-way ANOVA, you will need data that has one dependent variable and three independent variables (factors). The data should be structured in such a way that the dependent variable is numerical, and the independent variables are categorical.

Step 1: Importing Libraries

First, we need to import the required libraries. We will be using pandas for data manipulation, numpy for numerical computations, and statsmodels for the ANOVA.

import pandas as pd
import numpy as np
import statsmodels.api as sm
from statsmodels.formula.api import ols
from scipy import stats

Step 2: Loading the Data

Next, load the data into a pandas DataFrame. You might have the data in various formats like CSV, Excel, or SQL databases. Here’s an example of loading data from a CSV file:

data = pd.read_csv('path_to_your_file.csv')

Step 3: Data Exploration

Before performing the ANOVA, explore the dataset. This helps to understand the data, identify any missing values or outliers, and check assumptions.


Step 4: Checking Assumptions

ANOVA relies on several assumptions:

  1. Normality: The dependent variable should be approximately normally distributed within each group.
  2. Homogeneity of variances: The variances within each group should be equal.
  3. Independence: The observations should be independent of each other.

We can use Shapiro-Wilk test for normality and Levene’s test for homogeneity of variances.

# Checking normality
_, p_normality = stats.shapiro(data['dependent_variable'])
print(f'p-value for normality: {p_normality}')

# Checking homogeneity of variances
_, p_homogeneity = stats.levene(data['independent_variable_1'], data['independent_variable_2'], data['independent_variable_3'])
print(f'p-value for homogeneity of variances: {p_homogeneity}')

Step 5: Performing the Three-Way ANOVA

We will use the ols (Ordinary Least Squares) method from the statsmodels library to perform the three-way ANOVA. We need to specify the formula for the model, which includes the dependent variable and the interaction of the three independent variables.

model = ols('dependent_variable ~ C(independent_variable_1) * C(independent_variable_2) * C(independent_variable_3)', data=data).fit()
anova_table = sm.stats.anova_lm(model, typ=2)

Here, C() indicates that the variable should be treated as categorical.

Step 6: Interpreting the Results

The resulting table contains several columns, including the sum of squares, degrees of freedom, mean square, F-statistic, and the p-value.

  • Sum of squares (SS): The sum of squared deviations from the mean.
  • Degrees of freedom (df): The number of values that are free to vary.
  • Mean square (MS): The sum of squares divided by the degrees of freedom.
  • F-statistic (F): A ratio of two mean squares (MS_between / MS_within).
  • p-value (PR(>F)): The probability of observing a more extreme test statistic assuming the null hypothesis is true.

To determine whether the interaction of the independent variables has a significant effect on the dependent variable, look at the p-value for the interaction term. If the p-value is below the significance level (commonly set at 0.05), you can reject the null hypothesis and conclude that there is a significant interaction effect.


In this article, we discussed how to perform a three-way ANOVA in Python. This technique is useful when you need to examine the effects of three independent variables on a dependent variable. Remember that checking the assumptions of ANOVA is critical for the validity of the results.

Leave a Reply