Import Paths and Structure

We offer two ways of importing functions and classes from statsmodels:

  1. API import for interactive use

    • Allows tab completion

  2. Direct import for programs

    • Avoids importing unnecessary modules and commands

API Import for interactive use

For interactive use the recommended import is:

import statsmodels.api as sm

Importing statsmodels.api will load most of the public parts of statsmodels. This makes most functions and classes conveniently available within one or two levels, without making the “sm” namespace too crowded.

To see what functions and classes available, you can type the following (or use the namespace exploration features of IPython, Spyder, IDLE, etc.):

>>> dir(sm)
['GLM', 'GLS', 'GLSAR', 'Logit', 'MNLogit', 'OLS', 'Poisson', 'Probit', 'RLM',
'WLS', '__builtins__', '__doc__', '__file__', '__name__', '__package__',
'add_constant', 'categorical', 'datasets', 'distributions', 'families',
'graphics', 'iolib', 'nonparametric', 'qqplot', 'regression', 'robust',
'stats', 'test', 'tools', 'tsa', 'version']

>>> dir(
['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'abline_plot', 'beanplot', 'fboxplot', 'interaction_plot', 'qqplot',
'rainbow', 'rainbowplot', 'violinplot']

>>> dir(sm.tsa)
['AR', 'ARMA', 'SVAR', 'VAR', '__builtins__', '__doc__',
'__file__', '__name__', '__package__', 'acf', 'acovf', 'add_lag',
'add_trend', 'adfuller', 'ccf', 'ccovf', 'datetools', 'detrend',
'filters', 'grangercausalitytests', 'interp', 'lagmat', 'lagmat2ds', 'kpss',
'pacf', 'pacf_ols', 'pacf_yw', 'periodogram', 'q_stat', 'range_unit_root_test',
'stattools', 'tsatools', 'var']


The api modules may not include all the public functionality of statsmodels. If you find something that should be added to the api, please file an issue on github or report it to the mailing list.

The subpackages of statsmodels include modules that are mainly intended to collect the imports needed for those subpackages. The subpackage/ files are imported into statsmodels api, for example

from .nonparametric import api as nonparametric

Users do not need to load the subpackage/ modules directly.

Direct import for programs

statsmodels submodules are arranged by topic (e.g. discrete for discrete choice models, or tsa for time series analysis). Our directory tree (stripped down) looks something like this:


The submodules that can be import heavy contain an empty, except for some testing code for running tests for the submodules. The intention is to change all directories to have an and empty in the next release.

Import examples

Functions and classes:

from statsmodels.regression.linear_model import OLS, WLS
from import rank, add_constant


from statsmodels.datasets import macrodata
import statsmodels.stats import diagnostic

Modules with aliases

import statsmodels.regression.linear_model as lm
import statsmodels.stats.diagnostic as smsdia
import statsmodels.stats.outliers_influence as oi

We do not have currently a convention for aliases of submodules.

Last update: Dec 14, 2023