【发布时间】:2021-02-05 23:56:25
【问题描述】:
我正在使用answer here 在 python 中运行面板回归,因为我无权访问statsmodels
我的数据框如下所示:
Sum Amt_1 ... Sum Amt_2
Date Range_1 Range_2 info_1 info_2 info_3 info_4 ...
01/01/19 >720 >30.0 >5.0 <=70.0 lessthan_12m <= 0 0.00 ... 631427.36
1-10 0.00 ... 30420.78
21-30 0.00 ... 20276.26
31-40 0.00 ... 76939.48
morethan_12m > 50 0.00 ... 10288.87
应答器数据框如下所示:
Intercept beta r12to2 r36to13
caldt
1963-07-01 -1.497012 -0.765721 4.379128 -1.918083
1963-08-01 11.144169 -6.506291 5.961584 -2.598048
1963-09-01 -2.330966 -0.741550 10.508617 -4.377293
1963-10-01 0.441941 1.127567 5.478114 -2.057173
1963-11-01 3.380485 -4.792643 3.660940 -1.210426
我尝试使用下面的代码运行相同的回归,我实际上想要做与答案中相同的事情,但通过对除 Sum Amt_1 和 Sum Amt_2 之外的所有列进行分组,因为这些都是分类的变量。
def ols_coef(x,formula):
return ols(formula,data=x).fit().params
gamma = (df.groupby(['Date', 'Range_1', 'Range_2', 'info_1', 'info_2']))
.apply(ols_coef,'Sum_Amt_1 ~ C(Range_1) + C(Range_2) + C(info_1) + C(info_2)'))
但是,当我运行 print(gamma) 时,我得到:
Intercept
Date Range_1 Range_2 info_1 info_2
01/01/19 > 30.0 > 5.0 DQ_lessthan_12m > 50 1994.545600
<= 0 0.000000
1-10 0.000000
11-20 0.000000
21-30 5740.748889
31-40 0.000000
41-50 0.000000
我知道回归只对非索引元素运行,但我如何对这些索引元素运行回归,即'Range_1', 'Range_2', 'info_1', 'info_2' on Sum_Amt_1?
【问题讨论】:
标签: python pandas regression