我正在使用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'
上的Sum_Amt_1
进行回归?
答案 0 :(得分:0)
您不能通过调用熊猫的名称来对它们进行分组。当您运行df.groupby(['Date', 'Range_1', 'Range_2', 'info_1', 'info_2'])
时,它实际上什么也没做。这是我的意思的示例:
### Creating a multi-index dataframe
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
df = pd.DataFrame(np.random.randn(6, 6), index=index[:6], columns=index[:6])
df.columns = [' '.join(col).strip() for col in df.columns]
### grouping by named index
df.groupby(['first', 'second']).sum() ##Nothing happens
df.groupby(level = 0).sum() ## Correct way to groupby index
我建议,如果要对索引变量进行回归,则可以通过简单地df.reset_index
(然后可以调用名称)来重新索引数据框,或者显式调用级别。选择是您的。
更多信息可以在此帖子中找到:Pandas group by index and calculate sum