Introduction to Regression with statsmodels in Python
Fit linear and logistic regression models in Python with statsmodels, make predictions, and check model fit with diagnostic plots and confusion matrices.
Regression models describe how a response variable changes with an explanatory one, and they let you predict values you have not observed. You fit straight-line models in Python’s statsmodels with numeric and categorical explanatory variables, turn them into predictions, and test whether the fit can be trusted using residuals, leverage and influence. A second kind of model handles yes-or-no outcomes, such as a bank customer closing an account, and expresses its predictions as probabilities and odds.
What You Will Achieve
- Fit a linear regression in statsmodels and describe how the response changes with its explanatory variable
- Predict new values from a fitted model and judge when extrapolation makes a prediction unreliable
- Diagnose problems with model fit from diagnostic plots and flag the observations that drive the model
- Predict a yes-or-no outcome as a probability and report how often a logistic model classifies correctly
Program Overview
Simple Linear Regression and Predictions
Fits straight-line models with ols() and uses them to predict Taiwanese house prices and Facebook advert clicks.
- Response and explanatory variables, intercept and slope
- Categorical explanatory variables and means by category
- Predictions, their limits and model objects
- Regression to the mean and variable transformation
Assessing Linear Model Fit
Measures how well a linear model fits the data and finds the observations with the most pull on it.
- Coefficient of determination and residual standard error
- Residuals vs. fitted values, Q-Q and scale-location plots
- Outliers, leverage and influence
Simple Logistic Regression Modeling
Predicts whether a bank customer closes an account and measures how well the logistic model performs.
- Fitting logistic models with logit()
- Probabilities, odds, log odds and most likely outcome
- Confusion matrix and mosaic plot
- Accuracy, sensitivity and specificity
Certificate Awarded (Paid)
Job Outlook
Regression with statsmodels is used wherever analysts explain or predict a numeric value or a yes-or-no outcome from data, such as house prices, motor insurance claims, advert clicks and customers closing bank accounts.
Good for: Python users who want to fit, interpret and check simple linear and logistic regression models in statsmodels code.
Keep in mind: Each model has one explanatory variable, since the course keeps to simple linear and logistic regression rather than models with several predictors.
Bottom line: A code-first look at regression output in statsmodels that spends as much time checking fit as fitting.
Frequently Asked Questions
What does statsmodels do?
Statsmodels is a Python library for estimating statistical models and running statistical tests. In these lessons you fit linear models with its ols() function and logistic models with logit(), then pull coefficients and other elements out of the model objects.
Which real-world datasets appear in the exercises?
The exercises draw on motor insurance claims, house prices in Taiwan, fish sizes, Facebook advert clicks and consecutive portfolio returns, and the logistic regression chapter uses data on bank customers who close their accounts.
How does statsmodels differ from scikit-learn?
Both can fit regression models. Statsmodels is a statistical package aimed at understanding a model, while scikit-learn is a machine learning package aimed at prediction. The lessons here work in statsmodels, and the better choice depends on the task at hand.
User Reviews
Write your review
You must be logged in to post a review.
Reviews can only be posted by registered learners. Log in or create a free account, then you will return here to post.


There are no reviews yet.