How to perform ANOVA using the StatsModels library in python?

In this recipe, you will learn how to perform ANOVA using Python's StatsModels library

Recipe Objective - How to perform ANOVA using the StatsModels library in python?

Analysis of variance model includes anova_lm for ANOVA analysis using the linear OLS model and AnovaRM for ANOVA using iterative measurements within the ANOVA of the ANOVA.

For more related projects -

https://www.dezyre.com/projects/data-science-projects/deep-learning-projects
https://www.dezyre.com/projects/data-science-projects/neural-network-projects

Example:

# Importing libraries
import statsmodels.api as sm
from statsmodels.formula.api import ols

# Importing moore dataset from carData package
X = sm.datasets.get_rdataset("Moore", "carData").data

# Renaming column
X = X.rename(columns={"partner.status":"partner_status"})

# Fit and summarize ols model
model = ols('fscore ~ C(partner_status, Sum)*C(fcategory, Sum)',data=X).fit()

# type-1 anova summary
table_type_1 = sm.stats.anova_lm(model, typ=1)

# type-2 anova summary
table_type_2 = sm.stats.anova_lm(model, typ=2)
table_type_1

Output - 

			df	sum_sq	mean_sq	F	PR(>F)
C(partner_status, Sum)	1.0	7.932587	7.932587	0.231555	6.330623e-01
C(fcategory, Sum)	2.0	7561.365635	3780.682818	110.359336	8.768957e-17
C(partner_status, Sum):C(fcategory, Sum)	2.0	29.086807	14.543403	0.424527	6.570656e-01
Residual	39.0	1336.059416	34.257934	NaN	NaN

table_type_2

Output - 

			sum_sq	df	F	PR(>F)
C(partner_status, Sum)	67.253778	1.0	1.963159	1.690834e-01
C(fcategory, Sum)	7561.365635	2.0	110.359336	8.768957e-17
C(partner_status, Sum):C(fcategory, Sum)	29.086807	2.0	0.424527	6.570656e-01
Residual	1336.059416	39.0	NaN	NaN

In this way, we can perform ANOVA using the StatsModels library in python.

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python