Python
Statsmodels
AttributeError
OLS
Debugging

AttributeError module 'statsmodels.formula.api' has no attribute 'OLS'

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This error usually comes from mixing up two different Statsmodels interfaces. The formula API exposes lowercase helper functions such as ols, while the matrix-based API exposes the uppercase OLS class. If you call statsmodels.formula.api.OLS, Python raises an AttributeError because that exact name is not defined there.

Formula API versus matrix API

Statsmodels offers two common ways to fit an ordinary least squares model:

  • The formula interface, which looks like y ~ x1 + x2
  • The matrix interface, which takes explicit response and predictor arrays

Use the formula interface when you want R-style formulas and automatic design-matrix creation.

python
1import pandas as pd
2import statsmodels.formula.api as smf
3
4df = pd.DataFrame(
5    {
6        "sales": [10, 12, 13, 16, 18],
7        "ads": [1, 2, 2, 3, 4],
8        "price": [9, 9, 8, 8, 7],
9    }
10)
11
12model = smf.ols("sales ~ ads + price", data=df).fit()
13print(model.params)

Notice the lowercase ols. That is the correct entry point from statsmodels.formula.api.

When to use uppercase OLS

The uppercase class belongs to the non-formula API. It works with arrays or pandas objects that you prepare manually.

python
1import pandas as pd
2import statsmodels.api as sm
3
4df = pd.DataFrame(
5    {
6        "sales": [10, 12, 13, 16, 18],
7        "ads": [1, 2, 2, 3, 4],
8        "price": [9, 9, 8, 8, 7],
9    }
10)
11
12X = df[["ads", "price"]]
13X = sm.add_constant(X)
14y = df["sales"]
15
16model = sm.OLS(y, X).fit()
17print(model.params)

Here the uppercase OLS is correct because the import came from statsmodels.api, not from statsmodels.formula.api.

Why the error happens

This failing pattern is the classic cause:

python
import statsmodels.formula.api as smf

model = smf.OLS("sales ~ ads + price", data=df).fit()

The module alias is fine, but the attribute name is wrong. The fix is simply:

python
model = smf.ols("sales ~ ads + price", data=df).fit()

That lowercase function builds the design matrix behind the scenes. By contrast, sm.OLS expects you to pass y and X separately.

How to choose between them

Use smf.ols if your data is already in a DataFrame and a formula expresses the model clearly. It is concise, readable, and convenient for categorical variables, interactions, and quick experiments.

Use sm.OLS if you need explicit control over the predictor matrix, if the data is already split into arrays, or if you want to manage the intercept yourself. This API is more verbose, but it is very explicit.

Both approaches fit the same kind of model. They differ mainly in how you specify the inputs.

Common Pitfalls

The most common mistake is the uppercase-versus-lowercase mismatch. smf.ols exists, smf.OLS does not.

Another issue appears when switching from the formula API to the matrix API and forgetting the intercept. The formula version includes one automatically unless you remove it from the formula, while sm.OLS requires you to add a constant column yourself with sm.add_constant.

A more subtle problem is shadowing the library with a local file. If your project contains a file named statsmodels.py, Python may import that file instead of the installed package, leading to strange attribute errors. Check your working directory if the obvious fix does not work.

Finally, verify the environment you are actually running. Notebook kernels, virtual environments, and IDE interpreters can point at different installations, which makes debugging package behavior harder than it should be.

Summary

  • 'statsmodels.formula.api exposes ols, not OLS.'
  • Use smf.ols("y ~ x", data=df) for the formula interface.
  • Use sm.OLS(y, X) for the matrix interface from statsmodels.api.
  • Remember to add a constant manually when using sm.OLS.
  • If the fix still fails, check for environment or import shadowing problems.

Course illustration
Course illustration

All Rights Reserved.