statsmodels.regression.rolling.RollingOLS

class statsmodels.regression.rolling.RollingOLS(endog, exog, window=None, *, min_nobs=None, missing='drop', expanding=False)[source]

Rolling Ordinary Least Squares

Parameters:
endogarray_like

A 1-d endogenous response variable. The dependent variable.

exogarray_like

A nobs x k array where nobs is the number of observations and k is the number of regressors. An intercept is not included by default and should be added by the user. See statsmodels.tools.add_constant.

windowint

Length of the rolling window. Must be strictly larger than the number of variables in the model.

min_nobs{int, None}

Minimum number of observations required to estimate a model when data are missing. If None, the minimum depends on the number of regressors in the model. Must be smaller than window.

missingstr, default “drop”

Available options are “drop”, “skip” and “raise”. If “drop”, any observations with nans are dropped and the estimates are computed using only the non-missing values in each window. If ‘skip’ blocks containing missing values are skipped and the corresponding results contains NaN. If ‘raise’, an error is raised. Default is ‘drop’.

expandingbool, default False

If True, then the initial observations after min_nobs are filled using an expanding scheme until window observations are available, after which rolling is used.

See also

statsmodels.regression.linear_model.OLS

OLS estimation and parameter testing.

Notes

Tested against OLS for accuracy.

Results may differ from OLS applied to windows of data if this model contains an implicit constant (i.e., includes dummies for all categories) rather than an explicit constant (e.g., a column of 1s).

Examples

>>> from statsmodels.regression.rolling import RollingOLS
>>> from statsmodels.datasets import longley
>>> data = longley.load()
>>> exog = add_constant(data.exog, prepend=False)
>>> mod = RollingOLS(data.endog, exog)
>>> rolling_res = mod.fit(reset=50)

Use params_only to skip all calculations except parameter estimation

>>> rolling_params = mod.fit(params_only=True)

Use expanding and min_nobs to fill the initial results using an expanding scheme until window observation, and the roll.

>>> mod = RollingOLS(data.endog, exog, window=60, min_nobs=12,
... expanding=True)
>>> rolling_res = mod.fit()

Methods

fit([method, cov_type, cov_kwds, reset, ...])

Estimate model parameters.

from_formula(formula, data, window[, ...])

Create a Model from a formula and dataframe.


Last update: Mar 18, 2024