python-3.x - 为什么在使用 statsmodels logit 函数时会得到 p 值的 NAN?

标签 python-3.x statsmodels mlogit

My data

我使用 statsmodels 构建逻辑回归如下:

        X = np.copy(train_data)
        X = sm_.add_constant(X)

        model = sm.Logit(train_y, X)

        result = model.fit(method='bfgs', maxiter=10000)

        p_values[i-1, j-1, :] = result.pvalues
        logistic_Coefficients[i-1, j-1, :] = result.params

但我收到以下错误,我的 p 值都是 NAN:
C:\Users\maryamr\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\statsmodels\base\model.py:488: HessianInversionWarning: Inverting hessian failed, no bse or cov_params available
  'available', HessianInversionWarning)
C:\Users\maryamr\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\scipy\stats\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in greater
  return (self.a < x) & (x < self.b)
C:\Users\maryamr\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\scipy\stats\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in less
  return (self.a < x) & (x < self.b)
C:\Users\maryamr\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\scipy\stats\_distn_infrastructure.py:1821: RuntimeWarning: invalid value encountered in less_equal
  cond2 = cond0 & (x <= self.a)

我也在 r 中尝试过 glm 但我没有得到任何错误,只有其中一个特征具有 NAN 系数和 p 值。

最佳答案

根据您收到的第一个错误(“反转 Hessian 失败”),这是由于 Statsmodels 逻辑模型无法找到对数似然函数的最大值,随后无法找到您的数据以及因变量和自变量集的解正在使用。

查看您的数据,您有很多 0 和相同的值,这对于找到解决方案可能有问题。但由于看起来您在 R 中获得了收敛,您可以尝试更改模型的一些 Statsmodels 参数以查看是否有帮助(或者首先尝试找出 R 的 glm 包使用了哪些参数并使用 Statsmodels 复制它们)。

例如,logit.fit方法允许您选择八种不同的预定义优化方法之一。 'nm' (Nelder-Mead) 被其他人推荐用于这种情况。

您可以在此处找到 Statsmodels Logit 文档:http://www.statsmodels.org/devel/generated/statsmodels.discrete.discrete_model.Logit.fit.html

您也可以在 Cross Validated 上发布此问题网站,因为您可能会在那里得到更多回复。

关于python-3.x - 为什么在使用 statsmodels logit 函数时会得到 p 值的 NAN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55555205/

相关文章:

python - 使用 `statsmodels` 的正常 Q-Q​​ 图中的置信区间

python - 从 Pandas 到 Statsmodels 的 OLS 中弃用的滚动窗口选项

Python - StatsModels,OLS 置信区间

Python - 分割非json请求的响应

python-3.x - 用pyparsing解析数学表达式

R mlogit 模型,计算奇异

r - 使用 mlogit R 函数时出错 : the two indexes don't define unique observations

r - 通过 R 中的组查找 boolean 值是否为真

python - MappingProxyType 和 PEP 416 frozendict 的区别

python 3 : Get Bytes from File