Back to Study Material Python

StatsModel

Statsmodels is a Python library dedicated to statistical modeling, hypothesis testing, and data exploration. Unlike Scikit-learn which focuses on predictio...

StatsModel

Statistical Modeling with Statsmodels

Statsmodels is a Python library dedicated to statistical modeling, hypothesis testing, and data exploration. Unlike Scikit-learn which focuses on prediction, Statsmodels emphasizes understanding relationships through detailed statistical summaries, p-values, confidence intervals, and diagnostic tests.

Install with pip install statsmodels. The two main APIs are the formula API (similar to R) and the array-based API.

Ordinary Least Squares (OLS) regression using the formula API: import statsmodels.formula.api as smf import pandas as pd

df = pd.read_csv('housing.csv') model = smf.ols('Price ~ SquareFeet + Bedrooms + Age', data=df).fit() print(model.summary())

The summary table output is the heart of Statsmodels. It reports R-squared, F-statistic, AIC, BIC, coefficient estimates, standard errors, t-values, p-values, and 95% confidence intervals all in one block.

Logistic regression for classification: import statsmodels.formula.api as smf logit_model = smf.logit('Admitted ~ GPA + GRE', data=df).fit() print(logit_model.summary())

Time series analysis is another strong suit. Statsmodels provides ARIMA, SARIMAX, VAR, and Exponential Smoothing models: from statsmodels.tsa.arima.model import ARIMA model = ARIMA(series, order=(1, 1, 1)).fit() forecast = model.forecast(steps=12)

Hypothesis testing tools include t-tests, chi-square tests, Durbin-Watson (autocorrelation), and Jarque-Bera (normality). These help validate model assumptions.

Diagnostic plots check residuals. A good regression model has residuals that are normally distributed and show no patterns when plotted against fitted values.

Practical tip: Always read the model summary before trusting a regression. A high R-squared with non-significant p-values (p > 0.05) for individual predictors often signals multicollinearity.

Exam tip: Know that AIC and BIC are used for model comparison; lower values indicate a better-fitting model. BIC penalizes complexity more heavily than AIC and is preferred when the goal is identifying the true model.