Statsmodels: Statistical Analysis in Python

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Statsmodels

Statsmodels is a powerful Python library designed for statistical data analysis. It provides tools for building regression models, estimating parameters, testing statistical hypotheses, and analyzing time series data. Widely used in econometrics, biostatistics, and social sciences, Statsmodels offers deep statistical interpretation of results.

Key Features of the Statsmodels Library

Regression Analysis

Statsmodels offers extensive regression analysis capabilities, including Ordinary Least Squares (OLS), Generalized Linear Models (GLM), robust regression, and panel data models. The library supports both parametric and non-parametric estimation methods.

Time Series Analysis

The time series module includes ARIMA, SARIMA, exponential smoothing, Vector Autoregression (VAR) models, and many more. Functions for stationarity testing, series decomposition, and forecasting are readily available.

Hypothesis Testing

The library contains a wide range of statistical tests: t-tests, F-tests, normality tests, tests for equality of variances, autocorrelation tests, and many others.

Analysis of Variance (ANOVA)

Supports one-way and multi-factor ANOVA models, ANCOVA, repeated measures, and mixed-effects models.

Non-parametric Methods

Includes kernel density estimation, non-parametric regression, and rank-based tests.

Installing and Importing Statsmodels

Installing the Library

Install the library with the standard command:

pip install statsmodels

For additional features, install with full dependencies:

pip install statsmodels[complete]

Importing Required Components

import statsmodels.api as sm
import statsmodels.formula.api as smf
import statsmodels.tsa.api as tsa
import statsmodels.stats.api as sms

Library Structure and Core Modules

Main API Interfaces

statsmodels.api — classic interface with direct data matrix specification

statsmodels.formula.api — formula-based interface, similar to R

statsmodels.tsa — module for time series analysis

statsmodels.stats — statistical tests and diagnostics

statsmodels.graphics — result visualization

Data Preparation and Handling

Statsmodels works best with pandas DataFrames:

import pandas as pd
import numpy as np

# Load data
data = pd.read_csv("dataset.csv")

# Check for missing values
print(data.isnull().sum())

# Basic statistics
print(data.describe())

Linear Regression (OLS)

Classic Interface

import statsmodels.api as sm

# Prepare data
X = data[["x1", "x2", "x3"]]
X = sm.add_constant(X)  # add intercept
y = data["y"]

# Build model
model = sm.OLS(y, X).fit()
print(model.summary())

# Extract coefficients
print("Coefficients:", model.params)
print("P-values:", model.pvalues)
print("R-squared:", model.rsquared)

Formula Interface

import statsmodels.formula.api as smf

# Using formulas
model = smf.ols("y ~ x1 + x2 + x3", data=data).fit()
print(model.summary())

# Variable interactions
model_interaction = smf.ols("y ~ x1 * x2 + x3", data=data).fit()

# Categorical variables
model_cat = smf.ols("y ~ C(category) + x1", data=data).fit()

Generalized Li

Recommendations