python上的滚动回归

bnlyeluc  于 2023-03-16  发布在  Python
关注(0)|答案(1)|浏览(220)

我正在尝试使用以下代码运行几个x_variable的滚动回归:

# Get data for specified date range
pred = pred[beg_date:end_date]

# Lag and standardize pred
pred[var_list_all] = pred[var_list_all].shift(1)
pred[var_list_all] = preprocessing.StandardScaler().fit_transform(pred[var_list_all])

# Initialize dictionary of lists
d = {}
for i in ['coeff', 't-stat', 'r2']:
    d[i] = []

# Run bi-variate regression for each pred variable
y_var = 'EP'
for x_var in var_list_all:
    formula = y_var + ' ~ ' + x_var
    results = RollingOLS.from_formula(formula, window=60, data=pred)

    d['coeff'].append(results.params[x_var])
    d['t-stat'].append(results.params[x_var] / results.bse[x_var])
    d['r2'].append(results.rsquared * 100)

目的是收集每个变量的系数、t-stat和r2,并在单独的图表上绘制每个变量的r2。
每当我运行回归分析时,我总是收到以下消息:
属性错误:“NoneType”对象没有属性“f_locals”
我还在https://www.statsmodels.org/stable/examples/notebooks/generated/rolling_ls.html上尝试了这个示例,看看我是否理解发生了什么,但是我得到了相同的错误消息。
我不知道我做错了什么。
希望能得到任何帮助来解决这个问题。
先谢了。

uplii1fm

uplii1fm1#

下面是一个使用statmodels的RollingOLS的工作示例,灵感来自answer to this question on Rolling OLS Regressions and Predictions by Group

from statsmodels.regression.rolling import RollingOLS
from statsmodels.tools.tools import add_constant
import statsmodels.api as sm
import pandas as pd
import numpy as np

data = sm.datasets.grunfeld.load()
df_grunfeld = pd.DataFrame(data.data)
df_grunfeld.set_index(['firm'], append=True, inplace=True)

# Simple Model
# $$invest = \beta_0 + \beta_1 value$$

def invest_params(df_gf, intercept=False):

    """
    Function to operate on the data of a single firm.
    Assumes df_gf has the columns 'invest' and 'value' available.
    Returns a dataframe containing model parameters
    """

    # we should have at least k + 1 observations
    min_obs = 3 if intercept else 2
    wndw = 8 

    # if there are less than min_obs rows in df_gf, RollingOLS will throw an error
    # Instead, handle this case separately

    if df_gf.shape[0] < min_obs:
        cols = ['coef_intercept', 'coef_value'] if intercept else ['coef_value']
        return pd.DataFrame(index=df_gf.index, columns=cols)

    y = df_gf['invest']
    x = add_constant(df_gf['value']) if intercept else df_gf['value']

    model = RollingOLS(y, x, expanding=True, min_nobs=min_obs, window=wndw).fit()

    parameters = model.params
    params_shifted = model.params.shift(1)
    mse = model.mse_resid
    parameters['invest_hat'] = (parameters.mul(add_constant(df_gf['value']), axis=0)\
                                .sum(axis=1, min_count=1)).to_frame('invest_hat')

    parameters['invest_hat_shift'] = (params_shifted.mul(add_constant(df_gf['value']), axis=0)\
                                      .sum(axis=1, min_count=1)).to_frame('invest_hat_shift')

    parameters['mse'] = mse
    parameters['rmse'] = np.sqrt(mse)
    parameters['nobs'] = model.nobs
    parameters['ssr'] = model.ssr
    parameters['t_const'] = model.tvalues['const']
    parameters['t_value'] = model.tvalues['value']
    parameters.rename(columns = {'const' : 'b0', 'value' : 'b1'}, inplace = True)
    parameters['r2_adj'] = model.rsquared_adj
    
    return parameters

grouped = df_grunfeld.groupby('firm')
df_params = grouped.apply(lambda x: invest_params(x, True))
df_grunfeld_output = df_grunfeld.join(df_params, rsuffix='_coef')

相关问题