【问题标题】:Python: How to weight data by time for a statsmodel HuberT linear regression?Python:如何为 statsmodel HuberT 线性回归按时间加权数据?
【发布时间】:2017-08-11 21:08:57
【问题描述】:

我正在使用 statsmodel,这是我用来生成多线性回归的代码:

def regression():
    Data = pd.read_csv("CSV_file")
    DependentVariable = Data[["Variable1"]].values.tolist()
    IndependentVariables = Data[["Variable2","Variable3","Variable4"]].values.tolist()

    huber_t = sm.RLM(DependentVariable, IndependentVariables, M=sm.robust.norms.HuberT())

    hub_results = huber_t.fit()
    return hub_results.summary()

这给出了一个正常的输出。但是,我还想对我的数据进行加权,以便较新的数据比较旧的数据更重要。我正在考虑使用某种指数衰减来计算权重。在计算线性回归时有什么方法可以考虑这个权重?

【问题讨论】:

    标签: python statsmodels


    【解决方案1】:

    此页面上有一个使用指数衰减进行缩放的示例,但我不确定相同的技术是否适合您(也许它仅适用于绘图环境,但您可以尝试自己缩放多变的) http://blog.yhat.com/posts/predicting-the-presidential-election.html

    weight <- function(i) {
      exp(1)*1 / exp(i)
    }
    
    w <- data.frame(poll=1:8, weight=weight(1:8))
    ggplot(w, aes(x=poll, weight=weight)) +
      geom_bar() +
      scale_x_continuous("nth poll", breaks=1:8) +
      scale_y_continuous("weight")
    

    或者您可以使用 numpy 生成一个指数衰减的序列,答案如下:

    Pandas: Exponentially decaying sum with variable weights

    【讨论】:

      【解决方案2】:

      目前不能使用这种权重。

      请参阅statsmodels -- weights in robust linear regression 以获得相关答案。

      由于 HuberT 在小残差处是局部二次的,因此通过该答案中的权重重新缩放数据可以作为近似值。但是,这并不等同于每次观察对目标函数的贡献增加权重。

      【讨论】:

        猜你喜欢
        • 2018-01-08
        • 2015-12-12
        • 2019-06-13
        • 2016-10-23
        • 1970-01-01
        • 2022-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-17
        相关资源
        最近更新 更多