100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Statsmodels Cheat Sheet

Statsmodels Cheat Sheet

Statsmodels reference for OLS and logistic regression, the R-style formula API, and ARIMA time-series modeling with full statistical summaries.

2 PagesAdvancedMar 25, 2026

OLS Regression (Array API)

Fit and summarize a linear model.

python
import statsmodels.api as smX = sm.add_constant(X)               # adds an intercept columnmodel = sm.OLS(y, X).fit()print(model.summary())               # coefficients, R-squared, p-values, F-statprint(model.params, model.pvalues, model.rsquared)

Formula API

R-style formulas for regression models.

python
import statsmodels.formula.api as smfmodel = smf.ols("sales ~ tv + radio + newspaper", data=df).fit()print(model.summary())logit = smf.logit("churn ~ tenure + monthly_charges", data=df).fit()print(logit.params)

Time Series (ARIMA)

Test stationarity and forecast.

python
from statsmodels.tsa.arima.model import ARIMAfrom statsmodels.tsa.stattools import adfullerresult = adfuller(series)             # test for stationarityprint("p-value:", result[1])model = ARIMA(series, order=(1, 1, 1))fitted = model.fit()forecast = fitted.forecast(steps=12)

Key Models & Tools

Commonly used statsmodels components.

  • OLS- ordinary least squares linear regression
  • Logit / Probit- binary classification GLMs
  • GLM- generalized linear models (Poisson, Binomial, etc.)
  • ARIMA / SARIMAX- autoregressive time-series models
  • anova_lm- analysis of variance table for fitted models
  • acf / pacf- autocorrelation and partial autocorrelation functions
Pro Tip

Use the formula API (statsmodels.formula.api) with R-style formulas like 'y ~ x1 + x2' when you want automatic categorical encoding and an intercept — the array-based API (sm.OLS) requires sm.add_constant() and manual dummy encoding.

Was this cheat sheet helpful?

Explore Topics

#Statsmodels#StatsmodelsCheatSheet#DataScience#Advanced#OLS#Regression#Array#API#MachineLearning#APIs#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet