【问题标题】:rolling polynomial regression in pandas熊猫中的滚动多项式回归
【发布时间】:2015-01-10 21:22:21
【问题描述】:

我在这里对 pandas 很陌生,搜索过但如果是或不可行,我找不到答案。

到目前为止,我能够像这样计算简单回归 (Y= coef1 * A + coef2 * B) 的滚动系数:

model = pd.ols(y=df['Y'],x=df[['A','B']],window_type='rolling',window=100)

model.beta 返回 coef1 和 coef2 的 DataFrame ..

在 statsmodels 中我可以进行多项式回归,但没有滚动窗口选项:

poly_2 = smf.ols(formula='Y ~ 1 + A+ I(B** 2.0)', data=df).fit()

我如何“混合”这 2 并获得此多项式回归的滚动系数? 我没有在 pandas 中看到编写 patsy 式公式的方法,但也许我搜索得不好。

感谢您的帮助

【问题讨论】:

    标签: pandas regression


    【解决方案1】:

    如图所示的多项式回归只是一个普通的普通最小二乘回归,其中一个变量具有指数。您从使用 smf.ols 中获得的唯一好处(我认为)是使用 R-esque 公式的能力。就像那个I(B**2.0) 公式。但幸运的是,没有 smf 也可以构建相同的逻辑,然后可以使用 Pandas ols。

    让我们设置一些示例数据(在提问时总是一个很好的第一步,顺便说一句):

    n=500
    df = pd.DataFrame(randn(n), index=pd.date_range('1/1/2000', periods=n))
    df.columns = ['A']
    df['B'] = randn(n)
    df['Y'] = 5 + 3 * df.A + 6 * df.B **2 + randn(n)
    

    所以在这个例子中,数据 Y 是 A 和 B^2 的函数。所以我们可以做一个没有多项式的窗口OLS:

    pd.ols(y=df['Y'],x=df[['A','B']],window_type='rolling',window=100)
    -------------------------Summary of Regression Analysis-------------------------
    
    Formula: Y ~ <A> + <B> + <intercept>
    
    Number of Observations:         100
    Number of Degrees of Freedom:   3
    
    R-squared:         0.1184
    Adj R-squared:     0.1003
    
    Rmse:              9.6488
    
    F-stat (2, 97):     6.5159, p-value:     0.0022
    
    Degrees of Freedom: model 2, resid 97
    
    -----------------------Summary of Estimated Coefficients------------------------
          Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
    --------------------------------------------------------------------------------
                 A     3.8514     1.0675       3.61     0.0005     1.7592     5.9436
                 B     0.0693     0.9073       0.08     0.9393    -1.7091     1.8476
         intercept    11.8889     0.9655      12.31     0.0000     9.9965    13.7813
    ---------------------------------End of Summary---------------------------------
    

    但是如果你想要多项式,你只需创建一个变量就是多项式:

    df['B2'] = df.B **2
    

    然后您可以使用 B2 运行 OLS:

    pd.ols(y=df['Y'],x=df[['A','B2']],window_type='rolling',window=100)
    
    -------------------------Summary of Regression Analysis-------------------------
    
    Formula: Y ~ <A> + <B2> + <intercept>
    
    Number of Observations:         100
    Number of Degrees of Freedom:   3
    
    R-squared:         0.9869
    Adj R-squared:     0.9867
    
    Rmse:              1.1748
    
    F-stat (2, 97):  3662.8849, p-value:     0.0000
    
    Degrees of Freedom: model 2, resid 97
    
    -----------------------Summary of Estimated Coefficients------------------------
          Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
    --------------------------------------------------------------------------------
                 A     2.8258     0.1304      21.67     0.0000     2.5702     3.0814
                B2     6.0091     0.0748      80.29     0.0000     5.8624     6.1558
         intercept     5.1074     0.1448      35.28     0.0000     4.8237     5.3911
    ---------------------------------End of Summary---------------------------------
    

    【讨论】:

    • 正交多项式呢?
    • @tmthyjames 他们存在。要获得更丰富的答案,您需要提出一个具体问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 2018-07-30
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    相关资源
    最近更新 更多