Pandas滚动OLS被弃用

ser*_*ajo 7 python pandas

当我运行旧代码时,我收到以下警告:"pandas.stats.ols模块已弃用,将在以后的版本中删除.我们引用像statsmodels这样的外部软件包".我无法理解statsmodel中是否存在用户友好的滚动OLS模块.pandas.stats.ols模块的优点在于,您可以轻松地声明是否需要拦截,窗口类型(滚动,扩展)和窗口长度.是否有一个完全相同的模块?

例如:

YY = DataFrame(np.log(np.linspace(1,10,10)),columns=['Y'])
XX = DataFrame(np.transpose([np.linspace(1,10,10),np.linspace(??2,10,10)]),columns=[??'XX1','XX2'])
from pandas.stats.ols import MovingOLS
MovingOLS( y=YY['Y'], x=XX, intercept=True, window_type='rolling', window=5).resid
Run Code Online (Sandbox Code Playgroud)

我想要一个如何使用statsmodel或任何其他模块获取最后一行(剩余的移动ols)的结果的示例.

谢谢

Bra*_*mon 3

我创建了一个ols旨在模仿 pandas 已弃用的模块MovingOLS;是这里

它具有三个核心类:

  • OLS:静态(单窗口)普通最小二乘回归。输出是 NumPy 数组
  • RollingOLS:滚动(多窗口)普通最小二乘回归。输出是更高维度的 NumPy 数组。
  • PandasRollingOLS:将结果包装RollingOLS在 pandas Series 和 DataFrames 中。旨在模仿已弃用的 pandas 模块的外观。

请注意,该模块是包的一部分(我目前正在将其上传到 PyPi),并且需要一次包间导入。

上面的前两个类完全在 NumPy 中实现,并且主要使用矩阵代数。 RollingOLS还广泛利用广播。属性很大程度上模仿 statsmodels 的 OLS RegressionResultsWrapper

一个例子:

# Pull some data from fred.stlouisfed.org
from pandas_datareader.data import DataReader

syms = {'TWEXBMTH' : 'usd', 
        'T10Y2YM' : 'term_spread', 
        'PCOPPUSDM' : 'copper'
       }
data = (DataReader(syms.keys(), 'fred', start='2000-01-01')
        .pct_change()
        .dropna())
data = data.rename(columns=syms)
print(data.head())
                # usd  term_spread   copper
# DATE                                     
# 2000-02-01  0.01260     -1.40909 -0.01997
# 2000-03-01 -0.00012      2.00000 -0.03720
# 2000-04-01  0.00564      0.51852 -0.03328
# 2000-05-01  0.02204     -0.09756  0.06135
# 2000-06-01 -0.01012      0.02703 -0.01850

# Rolling regressions

from pyfinance.ols import OLS, RollingOLS, PandasRollingOLS

y = data.usd
x = data.drop('usd', axis=1)

window = 12  # months
model = PandasRollingOLS(y=y, x=x, window=window)

# Here `.resids` will be a stacked, MultiIndex'd DataFrame.  Each outer
#     index is a "period ending" and each inner index block are the
#     subperiods for that rolling window.
print(model.resids)
# end         subperiod 
# 2001-01-01  2000-02-01    0.00834
            # 2000-03-01   -0.00375
            # 2000-04-01    0.00194
            # 2000-05-01    0.01312
            # 2000-06-01   -0.01460
            # 2000-07-01   -0.00462
            # 2000-08-01   -0.00032
            # 2000-09-01    0.00299
            # 2000-10-01    0.01103
            # 2000-11-01    0.00556
            # 2000-12-01   -0.01544
            # 2001-01-01   -0.00425

# 2017-06-01  2016-07-01    0.01098
            # 2016-08-01   -0.00725
            # 2016-09-01    0.00447
            # 2016-10-01    0.00422
            # 2016-11-01   -0.00213
            # 2016-12-01    0.00558
            # 2017-01-01    0.00166
            # 2017-02-01   -0.01554
            # 2017-03-01   -0.00021
            # 2017-04-01    0.00057
            # 2017-05-01    0.00085
            # 2017-06-01   -0.00320
# Name: resids, dtype: float64
Run Code Online (Sandbox Code Playgroud)