Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 28 ยท First Steps

What Is Statsmodels?

Meet Statsmodels, the Python library for statistical models, tests and time series. See what it does, how it differs from scikit-learn, and run your first model.

Statsmodels is a Python library for statistical modeling. You give it data and a model, and it estimates the model's parameters and tells you how certain those estimates are: standard errors, confidence intervals, p-values and test statistics. It covers linear regression, generalized linear models, time series, hypothesis tests, mixed-effects models and more.

If you have used scikit-learn, you may wonder why you need another library for regression. The answer is the question you ask. Scikit-learn is built for prediction: how well can this model guess new data? Statsmodels is built for inference: what does the data say about the relationship between variables, and how sure can I be? Both are useful, and many analysts use them together.

Prediction versus inference

Imagine you run an online shop and want to know whether free shipping raises order size. A prediction model answers "what will this customer spend?". An inference model answers "how much does free shipping add on average, and could that effect be zero?". Statsmodels gives you the second kind of answer, with a full report.

What Statsmodels gives you

Regression OLS, WLS, GLS and robust fits.
GLMs Logistic, Poisson and more.
Time series ARIMA, SARIMAX, smoothing.
Tests t-tests, ANOVA, power.
1 / 2

The two import styles

Most code starts with one of two imports:

Python
import statsmodels.api as sm            # array-based API
import statsmodels.formula.api as smf   # formula-based API (R-style)

The array API takes arrays or DataFrames and needs you to build the design matrix yourself. The formula API lets you write "price ~ area + rooms" and builds the matrix for you. You will learn both; the formula API is usually more convenient for tables, and it is covered in Formulas and Patsy.

The fit pattern

Every model follows the same three steps:

  1. Create the model object with data: model = sm.OLS(y, X).
  2. Fit it: results = model.fit().
  3. Inspect the results object: results.params, results.summary(), results.predict(...).

The model holds the data and the equation. The results object holds everything estimated from it. This separation is the key idea of the whole library.

Run it yourself

Check that Statsmodels loads and see its version. Then fit a line to noisy data. The data are simulated with a fixed seed so the numbers repeat.

Python Check the install and fit a first line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The fitted numbers land close to the truth (3 and 2), not exactly on it. That gap is the sampling error, and Statsmodels quantifies it for you in the next example.

Python Estimates come with uncertainty

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The 95% intervals say which true values are consistent with the data. The slope interval should contain 2, because the data were generated with that slope.

What is in the library

Statsmodels is organized in modules you will meet one by one:

Module Purpose Lesson
statsmodels.regression OLS, WLS, GLS, mixed effects First OLS Model
statsmodels.genmod Generalized linear models Logistic and Probit
statsmodels.tsa Time series Time Series Basics
statsmodels.stats Tests, power, multiple testing Classical Tests
statsmodels.nonparametric KDE, LOWESS LOWESS and KDE

Statsmodels works with NumPy arrays, pandas DataFrames and Series, and it needs NumPy and SciPy underneath. When you pass pandas objects, results keep your column names, which makes reports far easier to read.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main goal of Statsmodels compared with scikit-learn?

    Statsmodels focuses on statistical inference and reports uncertainty; scikit-learn focuses on prediction pipelines.
  2. 2. Which line correctly shows the fit pattern?

    You create the model with the data, call fit(), and keep the results object.
  3. 3. What does sm.add_constant(x) do?

    The array API does not add an intercept on its own; add_constant appends the column of ones.
  4. 4. Which import gives you R-style formulas such as price ~ area?

    The formula API (smf) builds design matrices from formula strings.

Summary

  • Statsmodels estimates statistical models and reports uncertainty, not just predictions.
  • Use sm for arrays and smf for formulas.
  • Every model follows create, fit, inspect.
  • The results object holds parameters, standard errors, intervals and p-values.
  • It builds on NumPy, SciPy and pandas.

Next: Your First OLS Model