Statsmodels Tutorial
Learn Statsmodels step by step: 28 lessons in 10 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
First Steps
- What Is Statsmodels? Meet Statsmodels, the Python library for statistical models, tests and time series. See what it does, how it differs from scikit-learn, and run your first model.
- Your First OLS Model Fit ordinary least squares in Statsmodels with arrays and DataFrames, read the coefficients, make predictions and plot the fitted line, step by step.
- Reading the Summary Table Learn to read every part of the Statsmodels OLS summary: R-squared, F-statistic, AIC, coefficients, p-values, Omnibus, Durbin-Watson and Condition Number.
Linear Regression in Depth
- Formulas and Patsy Write regression models as formulas like y ~ x1 + np.log(x2) in Statsmodels. Learn the Patsy operators, transformations, the no-intercept form and how to inspect design matrices.
- Categorical Variables and Interactions Add categories to regression with C(), choose the reference level, read dummy coefficients and model interactions between a category and a number, with runnable examples.
- Weighted and Robust Regression Handle unequal variance and outliers in Statsmodels with WLS, GLS, robust standard errors (HC3, HAC) and RLM. Learn which tool fixes which assumption failure.
- Regularized Regression Shrink coefficients with ridge, lasso and elastic net in Statsmodels using fit_regularized. See how the penalty changes estimates and why inference needs care.
Diagnostics
- Residual Diagnostics Check linearity, constant variance, normality and independence of OLS residuals in Statsmodels with plots and tests: Breusch-Pagan, Jarque-Bera, Q-Q plots, RESET and Durbin-Watson.
- Influence and Outliers Find outliers and influential points in OLS with Statsmodels: leverage, studentized residuals, Cook's distance, DFFITS and influence plots, and decide what to do with them.
- Multicollinearity and VIF Detect correlated predictors with the variance inflation factor in Statsmodels, see how they inflate standard errors, and fix the problem with dropping, combining or ridge.
Hypothesis Testing
- Classical Tests and Power Run t-tests, z-tests, proportion tests and confidence intervals with statsmodels.stats, then plan sample size with power analysis for t-tests and proportions.
- ANOVA and Multiple Testing Compare several groups with one-way and two-way ANOVA in Statsmodels, run Tukey HSD post-hoc tests, and control false positives with Bonferroni, Holm and FDR corrections.
Generalized Linear Models
- Logistic and Probit Regression Model yes/no outcomes with Logit and Probit in Statsmodels. Read log-odds, odds ratios and marginal effects, predict probabilities and judge classification accuracy.
- Count Models Model counts such as visits, claims or defects with Poisson and negative binomial regression in Statsmodels. Handle overdispersion, exposure offsets and excess zeros.
- GLM Families and Links Understand generalized linear models in Statsmodels: pick a family and link for the outcome, fit Gaussian, Binomial, Poisson and Gamma GLMs, and read deviance and residuals.
Time Series
- Time Series Basics Learn the building blocks of time series in Statsmodels: trend and seasonality decomposition, autocorrelation (ACF, PACF), stationarity and the ADF test, and differencing.
- ARIMA Models Fit AR, MA and ARIMA models with Statsmodels, choose orders from ACF/PACF and AIC, check residuals with Ljung-Box, and produce forecasts with confidence intervals.
- Exponential Smoothing and SARIMAX Forecast seasonal data with Holt-Winters exponential smoothing and SARIMAX in Statsmodels, add exogenous regressors, compare models on a holdout and read the forecasts.
Other Models
- Mixed-Effects Models Model grouped or repeated data with mixed-effects models in Statsmodels. Fit random intercepts and slopes with MixedLM, read variance components and compare with plain OLS.
- Quantile Regression Model the median and other quantiles with QuantReg in Statsmodels. See why OLS misses changing spread, fit several quantile lines and use them as prediction bands.
- LOWESS and KDE Use nonparametric tools in Statsmodels: LOWESS to draw a flexible trend line without a formula, and kernel density estimation to estimate a distribution shape.
Pitfalls and Production
- Pitfalls and Debugging Avoid the usual Statsmodels mistakes: missing intercept, NaN and dtype errors, wrong column order, extrapolation, causal claims, p-hacking and convergence warnings, with fixes.
- Predict, Save and Deploy Move a Statsmodels model into production: predict with intervals, shrink and pickle results, reload them, validate inputs, test on a holdout and wrap everything in a function.
Projects
- Project: House Price Model Build a complete regression workflow in Statsmodels: explore the data, fit and improve an OLS model, run diagnostics, use robust errors and validate predictions on a holdout.
- Project: Customer Churn Analyze why customers leave using logistic regression in Statsmodels: build features, read odds ratios and marginal effects, pick a threshold and evaluate with a confusion matrix.
- Project: Sales Forecast Forecast monthly sales end to end with Statsmodels: decompose the series, build baselines, fit Holt-Winters and SARIMAX, compare with rolling-origin validation and report intervals.
Practice and Review
- Practice Exercises Test your Statsmodels skills with eight hands-on exercises on OLS, formulas, diagnostics, logit, counts, tests and forecasting. Each task has a runnable solution to compare against.
- Statsmodels Cheat Sheet A one-page Statsmodels reference: imports, model classes, formula syntax, results attributes, diagnostics, GLM families, time-series tools and a model-choice guide.