Python Linearmodels FamaMacBeth';列表';参数必须没有否定元素
我试图使用linearmodels(版本4.8)中的FamaMacBeth功能运行一些Fama MacBeth回归,但遇到以下错误:Python Linearmodels FamaMacBeth';列表';参数必须没有否定元素,python,numpy,regression,linearmodels,Python,Numpy,Regression,Linearmodels,我试图使用linearmodels(版本4.8)中的FamaMacBeth功能运行一些Fama MacBeth回归,但遇到以下错误: ValueError Traceback (most recent call last) <ipython-input-7-f393d4c071f0> in do_panel(list_dfs, y_var, control_vars, method) 37
ValueError Traceback (most recent call last)
<ipython-input-7-f393d4c071f0> in do_panel(list_dfs, y_var, control_vars,
method)
37 print(fmb_df[y_var].head())
38 print(fmb_df[new_x_vars].head())
---> 39 model = FamaMacBeth(fmb_df[y_var], sm.add_constant(fmb_df[new_x_vars])).fit()
40 return model
41
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\linearmodels\panel\model.py in fit(self, cov_type, debiased, **cov_config)
1940 cov = cov_est(wy, wx, params, all_params, debiased=debiased, **cov_config)
1941 df_resid = wy.shape[0] - params.shape[0]
-> 1942 res = self._postestimation(params, cov, debiased, df_resid, weps, wy, wx, root_w)
1943 index = self.dependent.index
1944 res.update(dict(df_resid=df_resid, df_model=x.shape[1], nobs=y.shape[0],
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\linearmodels\panel\model.py in _postestimation(self, params, cov, debiased, df_resid, weps, y, x, root_w)
441 f_pooled = InapplicableTestStatistic(reason='Model has no effects',
442 name='Pooled F-stat')
--> 443 entity_info, time_info, other_info = self._info()
444 nobs = weps.shape[0]
445 sigma2 = float(weps.T @ weps / nobs)
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\linearmodels\panel\model.py in _info(self)
223
224 entity_info = stats(self.dependent.entity_ids.squeeze(),
--> 225 'Observations per entity')
226 time_info = stats(self.dependent.time_ids.squeeze(),
227 'Observations per time period')
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\linearmodels\panel\model.py in stats(ids, name)
217
218 def stats(ids, name):
--> 219 bc = np.bincount(ids)
220 index = ['mean', 'median', 'max', 'min', 'total']
221 out = [bc.mean(), np.median(bc), bc.max(), bc.min(), bc.shape[0]]
ValueError: 'list' argument must have no negative elements
ValueError回溯(最近一次调用)
在do_面板中(列出dfs、y_变量、控制变量、,
(方法)
37打印(fmb_df[y_var].head())
38打印(fmb_df[new_x_vars].head())
--->39 model=fammacbeth(fmb_-df[y_-var],sm.add_常数(fmb_-df[new_-x_-vars])).fit()
40返回模式
41
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site packages\linearmodels\panel\model.py合适(self,cov\u type,debiaed,**cov\u config)
1940 cov=cov_est(wy,wx,参数,所有参数,debiased=debiased,**cov_配置)
1941 df_resid=wy.形状[0]-参数形状[0]
->1942 res=自我后估计(参数、cov、借方、df_剩余、weps、wy、wx、根w)
1943索引=自相关索引
1944 res.update(dict(df_resid=df_resid,df_model=x.shape[1],nobs=y.shape[0],
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site packages\linearmodels\panel\model.py in\u postestimation(self、params、cov、debiased、df\u resid、weps、y、x、root\u w)
441 f_pooled=不适用的TestStatistic(reason='Model has no effects',
442 name='pool F-stat')
-->443实体信息、时间信息、其他信息=自身信息
444 nobs=weps.shape[0]
445 sigma2=浮动(weps.T@weps/nobs)
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site packages\linearmodels\panel\model.py in\u info(self)
223
224 entity_info=stats(self.dependent.entity_id.squence(),
-->225“每个实体的观察结果”)
226 time_info=stats(self.dependent.time_id.squence(),
227“每个时间段的观测值”)
统计信息中的~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site packages\linearmodels\panel\model.py(ID、名称)
217
218 def统计数据(ID、名称):
-->219 bc=np.bincount(ID)
220指数=[“平均值”、“中值”、“最大值”、“最小值”、“总计”]
221 out=[bc.mean(),np.median(bc),bc.max(),bc.min(),bc.shape[0]]
ValueError:“list”参数必须没有负元素
我试着在谷歌上搜索,我得到的另一个结果是来自numpy来源:
我一辈子都搞不清楚到底出了什么问题。我的熊猫数据框中的值肯定允许为负值,对吗?非常感谢您的帮助,提前表示感谢
编辑:用详细的错误回溯更新。您能提供代码中错误发生位置的其他上下文吗?显示错误发生位置的代码示例将非常棒!我用更详细的错误回溯编辑了原始帖子。我非常确定错误发生在FamaCambeth中,因为当我使用更简化的d时ATAMACBETH(y_var,sm.add_constant(fmb_df[x_vars])).fit(),我得到了相同的错误。这有帮助吗?谢谢!发生的是
np.bincount()
不接受负值,这就是出现错误的原因。linearmodels
包应该有逻辑,以确保此操作正常失败,并显示更详细的错误消息。谢谢!那么您知道传递给np.bincount()的是什么吗所以我可以避免它?你能提供一个你正在使用的输入数据的例子,这样我们就可以重新创建错误吗?看起来像是从数据帧的索引中派生出来的某种东西正在传递给np.bincount()
,但是你的数据结构可能会影响该索引的外观。