Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python数据帧中的滚动回归估计_Python_Pandas_Dataframe - Fatal编程技术网

Python数据帧中的滚动回归估计

Python数据帧中的滚动回归估计,python,pandas,dataframe,Python,Pandas,Dataframe,我有这样一个数据帧: Date Y X1 X2 X3 22 2004-05-12 9.348158e-09 0.000081 0.000028 0.000036 23 2004-05-13 9.285989e-09 0.000073 0.000081 0.000097 24 2004-05-14 9.732308e-09 0.000085 0.0000

我有这样一个数据帧:

           Date         Y         X1         X2          X3
22   2004-05-12  9.348158e-09  0.000081  0.000028     0.000036   
23   2004-05-13  9.285989e-09  0.000073  0.000081     0.000097   
24   2004-05-14  9.732308e-09  0.000085  0.000073     0.000096   
25   2004-05-17  2.235977e-08  0.000089  0.000085     0.000099   
26   2004-05-18  2.792661e-09  0.000034  0.000089     0.000150   
27   2004-05-19  9.745323e-09  0.000048  0.000034     0.000053 

......

1000   2004-05-20  1.835462e-09  0.000034  0.000048     0.000099   
1001   2004-05-21  3.529089e-09  0.000037  0.000034     0.000043   
1002   2004-05-24  3.453047e-09  0.000043  0.000037     0.000059   
1003   2004-05-25  2.963131e-09  0.000038  0.000043     0.000059   
1004   2004-05-26  1.390032e-09  0.000029  0.000038     0.000054   
我想运行一个滚动100天窗口OLS回归估计,即:

首先,对于第101行,我使用第1行到第100行对Y-X1、X2、X3进行回归,并估计第101行的Y

然后对于第102行,我使用第2行到第101行对Y-X1、X2、X3进行回归,并估计第102行的Y

然后,对于第103行,我使用第2行到第101行对Y-X1、X2、X3进行回归,并估计第103行的Y

直到最后一排


如何做到这一点?

我还需要做一些滚动回归,并在pandas.ols中遇到了pandas折旧函数的问题。
model = pd.stats.ols.MovingOLS(y=df.Y, x=df[['X1', 'X2', 'X3']], 
                               window_type='rolling', window=100, intercept=True)
df['Y_hat'] = model.y_predict
下面是我的作品

基本上,我首先使用创建一个空的numpy数组,然后使用numpy polyfit在for循环中生成回归值。然后我将numpy数组添加到panda数据帧中。希望对社会有所帮助

data = pd.DataFrame(x_data, y_data)

regression = np.zeros((len(data.index),2)) #set the regression numpy array empty first
for row in range(0, len(data.index), 1):
    y = data.y_data[row: row + 300]
    x = data.x_data[row: row + 300]
    regression[row] = np.polyfit(x, y, 1)

data['beta'] = regression[:,0]
data['alpha'] = regression[:,1]

statsmodels 0.11.0添加了滚轴(2020年1月)

或者使用R型回归公式

model = RollingOLS.from_formula('Y ~ X1 + X2 + X3' , data = df, window=20)
rres = model.fit()
rres.params.tail()

熊猫的哪个版本?版本是:0.18.0
print(model.summary)
df.plot.scatter(x='x',y='y',s=0.1)
此功能似乎已被弃用,其他软件包中是否有替代品?@Lost1
x.rolling(window=60).apply(sm.ols)
有没有办法重叠窗口?@guy我认为它们重叠了。方法不错,但我认为存在问题。该行将超出范围,因为您要求返回[行:行+300]内的数据。我认为在for循环中,您需要迭代到
len(data.index)-300
model = RollingOLS.from_formula('Y ~ X1 + X2 + X3' , data = df, window=20)
rres = model.fit()
rres.params.tail()