问题描述
为了进行评估,我想运行在此URL中找到的数据集的滚动1000窗口OLS regression estimation
: https://drive.google.com/open?id=0B2Iv8dfU4fTUa3dPYW5tejA0bzg 使用以下Python
脚本.
For my evaluation, I wanted to run a rolling 1000 window OLS regression estimation
of the dataset found in this URL:https://drive.google.com/open?id=0B2Iv8dfU4fTUa3dPYW5tejA0bzgusing the following Python
script.
# /usr/bin/python -tt
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from statsmodels.formula.api import ols
df = pd.read_csv('estimated.csv', names=('x','y'))
model = pd.stats.ols.MovingOLS(y=df.Y, x=df[['y']],
window_type='rolling', window=1000, intercept=True)
df['Y_hat'] = model.y_predict
但是,当我运行Python脚本时,出现此错误:AttributeError: module 'pandas.stats' has no attribute 'ols'
.这个错误可能是我使用的版本造成的吗?我的Linux节点上安装的pandas
的版本为0.20.2
However, when I run my Python script, I am getting this error: AttributeError: module 'pandas.stats' has no attribute 'ols'
. Could this error be from the version that I am using? The pandas
installed on my Linux node has a version of 0.20.2
推荐答案
pd.stats.ols.MovingOLS
在Pandas 0.20.0版中已删除
pd.stats.ols.MovingOLS
was removed in Pandas version 0.20.0
http://pandas- docs.github.io/pandas-docs-travis/whatsnew.html#whatsnew-0200-prior-deprecations
https://github.com/pandas-dev/pandas/pull/11898
对于滚动回归等显而易见的用例,我找不到现成的"解决方案.
I can't find an 'off the shelf' solution for what should be such an obvious use case as rolling regressions.
下面的方法应该可以解决问题,而不必在更优雅的解决方案上花费太多时间.它使用numpy根据回归参数和滚动窗口中的X值来计算回归的预测值.
The following should do the trick without investing too much time in a more elegant solution. It uses numpy to calculate the predicted value of the regression based on the regression parameters and the X values in the rolling window.
window = 1000
a = np.array([np.nan] * len(df))
b = [np.nan] * len(df) # If betas required.
y_ = df.y.values
x_ = df[['x']].assign(constant=1).values
for n in range(window, len(df)):
y = y_[(n - window):n]
X = x_[(n - window):n]
# betas = Inverse(X'.X).X'.y
betas = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
y_hat = betas.dot(x_[n, :])
a[n] = y_hat
b[n] = betas.tolist() # If betas required.
上面的代码与下面的代码等效,并且快大约35%:
The code above is equivalent to the following and about 35% faster:
model = pd.stats.ols.MovingOLS(y=df.y, x=df.x, window_type='rolling', window=1000, intercept=True)
y_pandas = model.y_predict
这篇关于python pandas没有属性ols-错误(滚动OLS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!