zeroinfl模型-警告消息:在sqrt(diag(对象$vcov))中:生成了NAN
我试图运行零膨胀负二项式,但在检查模型时,我遇到了一个“NaNs生成的”警告,它阻止我看到结果。以下是一些模拟数据,它是我实际数据的简化版本-我的实际数据中每个物种有更多的观测值+更多的物种:zeroinfl模型-警告消息:在sqrt(diag(对象$vcov))中:生成了NAN,r,R,我试图运行零膨胀负二项式,但在检查模型时,我遇到了一个“NaNs生成的”警告,它阻止我看到结果。以下是一些模拟数据,它是我实际数据的简化版本-我的实际数据中每个物种有更多的观测值+更多的物种: df1 <- data.frame(species = c("Rufl","Rufl","Rufl","Rufl","Assp","Assp","Assp",&
df1 <- data.frame(species = c("Rufl","Rufl","Rufl","Rufl","Assp","Assp","Assp","Assp","Elre", "Elre","Elre", "Elre","Soca","Soca","Soca","Soca"),
state = c("warmed","ambient","warmed","ambient","warmed","ambient","warmed","ambient","warmed","ambient","warmed","ambient","warmed","ambient","warmed","ambient"),
p_eaten = c(0, 0, 3, 0, 0, 1, 15, 0, 20, 0, 0, 2, 0, 3, 87, 0))
df1对于这样一个复杂的模型来说,这是非常有限的数据,但是如果对数据帧的版本执行xtabs
,您会发现其中一个引用类别的计数为零。如果交换state
变量的级别,NA将消失,尽管仍存在一些较大的标准错误
xtabs(p_eaten~ state + species, data=df1)
species
state Assp Elre Rufl Soca
ambient 1 2 0 3
warmed 15 20 3 87
未清理的控制台输出如下所示:
df1 <- data.frame(species = c("Rufl","Rufl","Rufl","Rufl","Assp","Assp","Assp","Assp","Elre", "Elre","Elre", "Elre","Soca","Soca","Soca","Soca"),
+ state = factor(c("warmed","ambient","warmed","ambient","warmed","ambient","warmed","ambient","warmed","ambient","warmed","ambient","warmed","ambient","warmed","ambient"), levels=c("warmed","ambient")),
+ p_eaten = c(0, 0, 3, 0, 0, 1, 15, 0, 20, 0, 0, 2, 0, 3, 87, 0))
> xtabs(p_eaten~ state + species, data=df1)
species
state Assp Elre Rufl Soca
warmed 15 20 3 87
ambient 1 2 0 3
df1xtabs(p_~state+species,data=df1)
种
国家法律援助委员会
温暖的1520387
环境1 2 0 3
尝试:
> library(pscl)
> mod1 <- zeroinfl(p_eaten ~ state * species,
+ dist = 'negbin',
+ data = df1)
> summary(mod1)
Call:
zeroinfl(formula = p_eaten ~ state * species, data = df1, dist = "negbin")
Pearson residuals:
Min 1Q Median 3Q Max
-0.98868 -0.80384 -0.00342 0.80387 0.98872
Count model coefficients (negbin with log link):
Estimate Std. Error z value Pr(>|z|)
(Intercept) 2.708e+00 2.582e-01 10.488 < 2e-16 ***
stateambient -3.401e+00 1.033e+00 -3.292 0.000994 ***
speciesElre 2.877e-01 3.416e-01 0.842 0.399623
speciesRufl -1.671e+00 6.874e-01 -2.431 0.015068 *
speciesSoca 1.758e+00 2.796e-01 6.288 3.22e-10 ***
stateambient:speciesElre 8.714e-01 1.400e+00 0.622 0.533627
stateambient:speciesRufl -3.972e-04 NA NA NA
stateambient:speciesSoca -2.763e-02 1.218e+00 -0.023 0.981906
Log(theta) 1.501e+01 1.848e+02 0.081 0.935234
Zero-inflation model coefficients (binomial with logit link):
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.327e-04 1.414e+00 0.000 1.000
stateambient -8.538e+00 1.206e+02 -0.071 0.944
speciesElre 1.379e-04 2.000e+00 0.000 1.000
speciesRufl -1.267e-01 2.083e+00 -0.061 0.952
speciesSoca 1.757e-04 2.000e+00 0.000 1.000
stateambient:speciesElre 8.016e+00 1.206e+02 0.066 0.947
stateambient:speciesRufl 1.757e+01 NA NA NA
stateambient:speciesSoca 8.411e+00 1.206e+02 0.070 0.944
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Theta = 3316847.5216
Number of iterations in BFGS optimization: 41
Log-likelihood: -21.87 on 17 Df
Warning message:
In sqrt(diag(object$vcov)) : NaNs produced
>库(pscl)
>mod1概要(mod1)
电话:
zeroinfl(公式=p_~state*物种,数据=df1,dist=“negbin”)
皮尔逊残差:
最小1季度中值3季度最大值
-0.98868 -0.80384 -0.00342 0.80387 0.98872
计算模型系数(带有日志链接的negbin):
估计标准误差z值Pr(>z)
(截距)2.708e+00 2.582e-01 10.488<2e-16***
州环境温度-3.401e+00 1.033e+00-3.2920.000994***
规格ELRE 2.877e-01 3.416e-01 0.842 0.399623
规格RUFL-1.671e+00 6.874e-01-2.431 0.015068*
规格SOCA 1.758e+00 2.796e-01 6.288 3.22e-10***
州环境:规范ELRE 8.714e-01 1.400e+00 0.622 0.533627
州环境:物种RUFL-3.972e-04 NA
州环境:规格SOCA-2.763e-02 1.218e+00-0.023 0.981906
对数(θ)1.501e+01 1.848e+02 0.081 0.935234
零通胀模型系数(带有logit链接的二项式):
估计标准误差z值Pr(>z)
(截距)-1.327e-04 1.414e+00 0.000 1.000
州环境温度-8.538e+00 1.206e+02-0.071 0.944
规格ELRE 1.379e-04 2.000e+00 0.000 1.000
规格RUFL-1.267e-01 2.083e+00-0.061 0.952
规格SOCA 1.757e-04 2.000e+00 0.000 1.000
州环境:规格ELRE 8.016e+00 1.206e+02 0.066 0.947
州环境:规格RUFL 1.757e+01 NA
州环境:规范SOCA 8.411e+00 1.206e+02 0.070 0.944
---
签名。代码:0'***'0.001'***'0.01'*'0.05'.'0.1''1
θ=3316847.5216
BFGS优化中的迭代次数:41
对数可能性:-17 Df时为21.87
警告信息:
在sqrt中(diag(对象$vcov)):生成的NAN
方差-协方差矩阵应为正定矩阵,负值可能会妨碍反演工作。当CrossValidated.com上提出类似问题时,Ben Bolker建议使用brglm2版本的glm:
> library(brglm2)
> summary(m1 <- glm(p_eaten~ state * species, data=df1,
+ family=poisson,
+ method="brglmFit"))
Call:
glm(formula = p_eaten ~ state * species, family = poisson, data = df1,
method = "brglmFit")
Deviance Residuals:
Min 1Q Median 3Q Max
-9.3541 -1.8708 -0.7071 0.8567 5.7542
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 2.0477 0.2540 8.062 7.52e-16 ***
stateambient -2.3354 0.8551 -2.731 0.00631 **
speciesElre 0.2796 0.3366 0.831 0.40619
speciesRufl -1.4881 0.5918 -2.514 0.01192 *
speciesSoca 1.7308 0.2756 6.281 3.37e-10 ***
stateambient:speciesElre 0.2312 1.0863 0.213 0.83142
stateambient:speciesRufl 0.3895 1.7369 0.224 0.82258
stateambient:speciesSoca -0.8835 1.0141 -0.871 0.38362
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 443.21 on 15 degrees of freedom
Residual deviance: 183.08 on 8 degrees of freedom
AIC: 225.38
Number of Fisher Scoring iterations: 1
>库(brglm2)
>总结(m1 | z |)
(截距)2.0477 0.2540 8.0627.52e-16***
州环境-2.3354 0.8551-2.731 0.00631**
规格0.2796 0.3366 0.831 0.40619
规格RUFL-1.4881 0.5918-2.514 0.01192*
规格SOCA 1.7308 0.2756.281 3.37e-10***
州环境:speciesElre 0.2312 1.0863 0.213 0.83142
州环境:speciesRufl 0.3895 1.7369 0.224 0.82258
州环境:规格SOCA-0.8835 1.0141-0.871 0.38362
---
签名。代码:0'***'0.001'***'0.01'*'0.05'.'0.1''1
(泊松族的色散参数取1)
零偏差:在15个自由度上为443.21
剩余偏差:8自由度时为183.08
AIC:225.38
Fisher评分迭代次数:1
事实上,互动的必要性似乎值得怀疑。随着相互作用项的去除,偏差的变化很小。很难知道完整的数据集是否也会出现than mich。这很可能是一种情况,但如果没有完整的数据集,则无法确定
当类别全部为零或全部为非零时,可能会发生这种情况。在您上面给出的示例中:
with(df1,表(物种,状态,p_=0))
表明在物种==“Rufl”、状态==“ambient”和p
==0为FALSE
的情况下,无观察结果;换句话说,对于这些因素的组合,所有的观察结果都是零。因此,涉及与该状态进行比较的任何系数将具有较大的参数值(即abs(β)>>1);理论上它应该是无限的,但通常在10到30之间(取决于数值方法放弃的地方)。这些系数要么具有大得离谱的标准误差和(Wald)置信区间,要么(如您的情况)具有NaN
值
本说明适用于speciesRufl
和statewarmed:speciesRufl
的零膨胀系数。计数模型系数不大,但仍有NaN
标准误差,我认为,因为它们的不确定性与零通胀系数的不确定性有关
计算模型系数(带有日志链接的negbin):
估计标准误差z值Pr(>z)
(截距)-0.69312 1.00016-0.693 0.488307
...
种类RUFL-1.69427楠楠
零通胀模型系数(带有logit链接的二项式):
估计标准误差z值Pr(>z)
...
品种17.560楠楠楠楠楠
对此你能做些什么
- 忽略这个问题。对于受影响的系数,您将无法获得可靠的标准误差(以及p值、置信区间等)
> library(brglm2)
> summary(m1 <- glm(p_eaten~ state * species, data=df1,
+ family=poisson,
+ method="brglmFit"))
Call:
glm(formula = p_eaten ~ state * species, family = poisson, data = df1,
method = "brglmFit")
Deviance Residuals:
Min 1Q Median 3Q Max
-9.3541 -1.8708 -0.7071 0.8567 5.7542
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 2.0477 0.2540 8.062 7.52e-16 ***
stateambient -2.3354 0.8551 -2.731 0.00631 **
speciesElre 0.2796 0.3366 0.831 0.40619
speciesRufl -1.4881 0.5918 -2.514 0.01192 *
speciesSoca 1.7308 0.2756 6.281 3.37e-10 ***
stateambient:speciesElre 0.2312 1.0863 0.213 0.83142
stateambient:speciesRufl 0.3895 1.7369 0.224 0.82258
stateambient:speciesSoca -0.8835 1.0141 -0.871 0.38362
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 443.21 on 15 degrees of freedom
Residual deviance: 183.08 on 8 degrees of freedom
AIC: 225.38
Number of Fisher Scoring iterations: 1