Statistics with statsmodels
19 steps in 6 sets of Python.
Where scikit-learn predicts, statsmodels explains. It gives you coefficients, standard errors, p-values and a summary table that looks like the ones in papers, which is exactly why people reach for it.
Linear models first, then model design with formulas and categorical variables, diagnostics and robust standard errors, generalized linear models and quantile regression, and finally time series. If you have ever needed to defend a regression rather than just run one, this is the tour.
Linear models
- Ordinary least squaresThe summary table is the point: coefficients with their uncertainty.
- The formula APIA patsy formula reads like the model you would write on paper.
- Reading a summaryEvery block of the table answers a different question.
Model design
- Categorical predictorsC() makes a column categorical, and the formula builds the dummies.
- Interactions and transformsThe formula language covers products, powers and functions inline.
- Predicting with intervalsget_prediction gives the mean and both intervals, which predict alone does not.
Diagnostics & robustness
- Residual diagnosticsThe tests that say whether the OLS assumptions actually held.
- Robust and weighted fitsRLM downweights outliers; WLS uses weights you supply.
Beyond least squares
- Logistic regressionLogit models a probability; the coefficients are log odds.
- Generalised linear modelsOne framework, a family per response type: binomial, Poisson, Gaussian.
- ANOVA tablesanova_lm turns a fitted model into a sum-of-squares table.
- Proportions and powerA z-test for proportions, and the sample size that would detect an effect.
Time series
- AutocorrelationHow much a series remembers, at each lag.
- StationarityMost time-series models assume it, so test and difference first.
- Seasonal decompositionSplitting a series into trend, season and what is left.
- ARIMAOrder (p, d, q), a fit, and a forecast with an interval.
- SARIMAXSeasonal orders and exogenous regressors, in one model.
- Does one series lead another?The Granger test asks whether past x improves a forecast of y.
Encore
- sm_regression_report.pyA regression worked properly: fit, diagnose, correct, compare, forecast.