我正try 用Python语言实现滚动回归,但使用统计模型‘MovingOLS’失败了.在我的数据框中,‘Year’栏指定了各自观测的年份.
现在,我想用一个滚动的两年窗口来回归‘F1_yield ’和‘WC’,这样,1998年对1999年的预测是基于之前的两年,1997年和1998年,但我没有得到有意义的结果,可能是因为我不知道如何正确设置window
参数.那么,如何将window
参数与‘Year’变量相关联呢?
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pandas_datareader as pdr
import seaborn
import statsmodels.api as sm
from statsmodels.regression.rolling import RollingOLS
d1 = {'ID': [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6], 'Earnings': [100, 200,
400, 250, 300, 350, 400, 550, 700, 259, 300, 350, 270, 450, 340, 570, 340, 340], 'WC':
[20, 40, 35, 55, 60, 65, 30, 28, 32, 45, 60, 52, 23, 54, 45, 87, 54, 65], 'Year': [1995,
1996, 1997, 1996, 1997, 1998, 1995, 1997, 1998, 1996, 1997, 1998, 1995, 1997, 1998, 1996,
1997, 1998], 'F1_Earnings': [120, 220, 420, 280, 530, 670, 780, 210, 950, 100, 120, 430,
780, 210, 950, 100, 120, 430]}
df1 = pd.DataFrame(data=d1)
y = df1['F1_Earnings']
features = ["Earnings", "WC"]
x = df1[features]
rols = RollingOLS(y, x, window=2)
rres = rols.fit()
params = rres.params.copy()
params.index = np.arange(1, params.shape[0] + 1)
params.head()