Linear Regression using the StatsModels library in python

In this recipe, you will learn how to apply Linear Regression using Python's StatsModels library

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

A linear model of errors that are independently and uniformly distributed and errors that are non-uniformly distributed or autocorrelated. This module allows estimation by ordinary least squares (OLS), weighted least squares (WLS), generalized least squares (GLS), and feasible generalized least squares with autocorrelation AR (p) errors.

Learn to Implement Customer Churn Prediction Using Machine Learning in Python 

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

# Importing cancer dataset from statsmodels in the form of pandas dataframe
data = sm.datasets.cancer.load_pandas()

# Storing data
X = data.data

# Fit and summarize OLS model with 'cancer' as dependent variable and 'population' as independent variable
model = sm.OLS(X['cancer'], X['population'])
model = model.fit()

# Model summary
model.summary()

Output - 
OLS Regression Results
Dep. Variable:	cancer	R-squared (uncentered):	0.960
Model:	OLS	Adj. R-squared (uncentered):	0.960
Method:	Least Squares	F-statistic:	7161.
Date:	Sat, 13 Nov 2021	Prob (F-statistic):	2.09e-211
Time:	10:34:51	Log-Likelihood:	-1198.3
No. Observations:	301	AIC:	2399.
Df Residuals:	300	BIC:	2402.
Df Model:	1		
Covariance Type:	nonrobust		
coef	std err	t	P>|t|	[0.025	0.975]
population	0.0036	4.21e-05	84.623	0.000	0.003	0.004
Omnibus:	69.228	Durbin-Watson:	1.820
Prob(Omnibus):	0.000	Jarque-Bera (JB):	1477.032
Skew:	0.153	Prob(JB):	0.00
Kurtosis:	13.848	Cond. No.	1.00

# Prediction
model.predict([45])

Output -
array([0.16014454])

In this way, we can perform linear regression using the StatsModel library in python.

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

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

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

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.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification