Learn statsmodels

Statistical models, tests and time series with full inference in Python

16 lessons · 3 hours · 4 levels · covers statsmodels 0.15

statsmodels estimates statistical models such as linear and generalised linear regression, mixed models and ARIMA-family time series, and reports the inference around them: standard errors, confidence intervals, hypothesis tests and diagnostics. It supports R-style formulas on pandas DataFrames. Use it when you need to explain and test relationships in data, not just predict.

Before you start

statsmodels builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics5 lessons · 52 min in total
2. PracticalUse it productively at work, with the usual idioms10 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs13 lessons · 3 hours in total
4. ExpertInternals and the edge cases experts know16 lessons · 3 hours in total

The statsmodels tree

statsmodelsLinear regression with OLSLinear regressionwith OLSModel formulasModel formulasReading a regression summaryReading a regressionsummaryPrediction and confidence intervalsPrediction andconfidence intervalsLogistic regression and odds ratiosLogistic regressionand odds ratiosCategories, contrasts and interactionsCategories, contrastsand interactionsGeneralised linear modelsGeneralised linearmodelsRegression diagnosticsRegressiondiagnosticsRobust and clustered standard errorsRobust and clusteredstandard errorsDecomposition, ACF and stationarityDecomposition, ACFand stationarityDataFrameDataFrameDefining functionsDefining functionsDates and times basicsDates and timesbasics
Essentials and Practical shown (10 of 16 lessons); the full list is below. Click a lesson to read it.

All 16 lessons, in order

Linear regression

  1. Linear regression with OLS · Essentials · 10 min

    Fit an ordinary least squares model with statsmodels.api and get coefficients with their uncertainty.

  2. Model formulas · Essentials · 10 min

    Describe models as R-style formulas like 'price ~ area + C(city)' and let statsmodels build the design matrix.

  3. Reading a regression summary · Essentials · 12 min

    Interpret coefficients, standard errors, t-statistics, p-values, confidence intervals, R² and the F-test.

  4. Prediction and confidence intervals · Essentials · 9 min

    Predict new values and quantify their uncertainty with get_prediction and summary_frame.

  5. Categories, contrasts and interactions · Practical · 12 min

    Choose reference levels, read dummy coefficients and model effects that depend on each other.

Generalised linear models

  1. Logistic regression and odds ratios · Essentials · 11 min

    Model a binary outcome with Logit and interpret coefficients as log-odds and odds ratios.

  2. Generalised linear models · Practical · 12 min

    Model counts, rates, proportions and skewed positive outcomes with GLM families and link functions.

Diagnostics and inference

  1. Regression diagnostics · Practical · 13 min

    Check residuals, heteroscedasticity, influential points and multicollinearity before trusting a model.

  2. Robust and clustered standard errors · Practical · 10 min

    Get valid standard errors under heteroscedasticity or clustered data with cov_type options.

  3. ANOVA, multiple comparisons and power analysis · Advanced · 12 min

    Compare groups with ANOVA tables, adjust for multiple comparisons and size experiments with power analysis.

Time series

  1. Decomposition, ACF and stationarity · Practical · 13 min

    Decompose series into trend and seasonality, read ACF/PACF plots and test for stationarity.

  2. ARIMA and SARIMAX forecasting · Advanced · 14 min

    Fit seasonal ARIMA models with exogenous regressors, check residuals and forecast with intervals.

  3. State space models and the Kalman filter · Expert · 15 min

    Use statsmodels' state space framework: unobserved components, smoothed states, missing data and custom models.

Advanced models and internals

  1. Mixed effects models · Advanced · 13 min

    Model grouped or repeated-measures data with random intercepts and slopes using MixedLM.

  2. Custom maximum likelihood models · Expert · 13 min

    Estimate any model you can write a log-likelihood for with GenericLikelihoodModel and get full inference.

  3. Convergence, separation and identification · Expert · 12 min

    Diagnose failed or misleading fits: non-convergence, perfect separation, collinearity and unidentified parameters.