我正在尝试在以下数据框(此处仅前五行)上进行逻辑回归:
index player_id room_number treatment belief_elicited msg_sent msg_received choice_made subjective_belief
2 oBR4gpHO oBR4gpHO 14 sk_prosocial_1 no_message 0 0 0 95
3 dkKsCGaY dkKsCGaY 14 sk_prosocial_1 no_message 0 0 0 95
8 imAekwQL imAekwQL 17 standard_2 no_message 1 1 0 66
9 O5ziuYET O5ziuYET 17 standard_2 no_message 1 1 0 85
10 PbZPJWlD PbZPJWlD 18 prosocial_1 no_message 0 0 0 55
我使用Python的statsmodels的方程式API来运行以下回归。因为choice_made
是分类变量,所以我使用C(choice_made)
。
fit = glm(formula='C(choice_made) ~ subjective_belief + treatment - 1',
data=all_treatments).fit()
print(fit.summary())
不幸的是,这样做会给我以下错误:
Traceback (most recent call last):
File "10_code/analyse_data.py", line 477, in <module>
data=all_treatments).fit()
File "/home/lieu/anaconda3/envs/human-compactness/lib/python3.7/site-packages/statsmodels/genmod/generalized_linear_model.py", line 1028, in fit
cov_kwds=cov_kwds, use_t=use_t, **kwargs)
File "/home/lieu/anaconda3/envs/human-compactness/lib/python3.7/site-packages/statsmodels/genmod/generalized_linear_model.py", line 1139, in _fit_irls
self.scale = self.estimate_scale(mu)
File "/home/lieu/anaconda3/envs/human-compactness/lib/python3.7/site-packages/statsmodels/genmod/generalized_linear_model.py", line 753, in estimate_scale
return self._estimate_x2_scale(mu)
File "/home/lieu/anaconda3/envs/human-compactness/lib/python3.7/site-packages/statsmodels/genmod/generalized_linear_model.py", line 773, in _estimate_x2_scale
resid = np.power(self.endog - mu, 2) * self.iweights
ValueError: operands could not be broadcast together with shapes (79,2) (79,)
如果我删除分类方法,则回归运行良好,但是我怀疑这不是我想要的。