2017-08-11 94 views
0

我使用statsmodel,这是我使用生成一个多元线性回归代码:Python:如何根据statsmodel HuberT线性回归对时间数据进行加权?

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() 

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

回答

0

在这个页面上有一个缩放指数衰减的例子,但我不确定相同的技术是否适用于您(也许它只适用于绘图环境,但您可以尝试缩放自己的变量) 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

0

目前无法使用这种权重。

查看statsmodels -- weights in robust linear regression的相关答案。

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