零膨胀回归出现随机NaN值及theta含义咨询
蜜蜂多样性零膨胀回归分析问题
我正在研究不同场景下的蜜蜂多样性:苹果园内部与外部(Placement变量)、不同农场、不同品种之间的差异。针对蜜蜂(Honeybee)的零膨胀回归分析中,三个变量均能正常运行,但针对独居蜂(Wildbee)及部分熊蜂(Bumblebee)的分析结果出现了NaN值。我设置了近500个陷阱,共记录到约500只蜜蜂、150只熊蜂、70只独居蜂,请问:
- 是否因数据量不足导致
NaN? - 该如何解决?
theta并非我的变量,它的含义是什么?
苹果园内外分析代码及结果
# This is what happened for inside vs outside > p1 <- zeroinfl(Honeybee...29 ~ Placement, data = diversity, dist = "negbin") > summary(p1) Call: zeroinfl(formula = Honeybee...29 ~ Placement, data = diversity, dist = "negbin") Pearson residuals: Min 1Q Median 3Q Max -0.5872 -0.5872 -0.4080 0.1483 11.2744 Count model coefficients (negbin with log link): Estimate Std. Error z value Pr(>|z|) (Intercept) 0.20377 0.11021 1.849 0.0645 . PlacementWild 0.09042 0.23433 0.386 0.6996 Log(theta) -0.73459 0.16043 -4.579 4.67e-06 *** Zero-inflation model coefficients (binomial with logit link): Estimate Std. Error z value Pr(>|z|) (Intercept) -9.685 53.494 -0.181 0.856 PlacementWild 9.499 53.489 0.178 0.859 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Theta = 0.4797 Number of iterations in BFGS optimization: 127 Log-likelihood: -607.6 on 5 Df > > p2 <- zeroinfl(Bumblebee ~ Placement, data = diversity, dist = "negbin") > summary(p2) Call: zeroinfl(formula = Bumblebee ~ Placement, data = diversity, dist = "negbin") Pearson residuals: Min 1Q Median 3Q Max -0.3761 -0.3761 -0.3289 -0.3289 5.9230 Count model coefficients (negbin with log link): Estimate Std. Error z value Pr(>|z|) (Intercept) -1.6686 0.1939 -8.607 < 2e-16 *** PlacementWild 1.8530 0.3853 4.809 1.52e-06 *** Log(theta) -0.5619 0.5234 -1.074 0.283 Zero-inflation model coefficients (binomial with logit link): Estimate Std. Error z value Pr(>|z|) (Intercept) -6.747 76.346 -0.088 0.930 PlacementWild 7.366 76.258 0.097 0.923 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Theta = 0.5701 Number of iterations in BFGS optimization: 84 Log-likelihood: -299.5 on 5 Df > > p3 <- zeroinfl(Wildbee ~ Placement, data = diversity, dist = "negbin") > summary(p3) Call: zeroinfl(formula = Wildbee ~ Placement, data = diversity, dist = "negbin") Pearson residuals: Min 1Q Median 3Q Max -0.3369 -0.3369 -0.3132 -0.3132 7.1499 Count model coefficients (negbin with log link): Estimate Std. Error z value Pr(>|z|) (Intercept) -1.7629 0.2065 -8.535 < 2e-16 *** PlacementWild 0.2712 0.2817 0.963 0.336 Log(theta) -1.4738 0.2675 -5.510 3.58e-08 *** Zero-inflation model coefficients (binomial with logit link): Estimate Std. Error z value Pr(>|z|) (Intercept) -12.886 325.692 -0.04 0.968 PlacementWild -7.375 NaN NaN NaN --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Theta = 0.2291 Number of iterations in BFGS optimization: 18 Log-likelihood: -246.7 on 5 Df Warning message: In sqrt(diag(object$vcov)) : NaNs produced
农场间分析代码及结果
> f1 <- zeroinfl(Honeybee...29 ~ Farm, data = diversity, dist = "negbin") > summary(f1) Call: zeroinfl(formula = Honeybee...29 ~ Farm, data = diversity, dist = "negbin") Pearson residuals: Min 1Q Median 3Q Max -0.54910 -0.49323 -0.44511 0.01922 8.19537 Count model coefficients (negbin with log link): Estimate Std. Error z value Pr(>|z|) (Intercept) 0.2768 0.1441 1.920 0.0548 . FarmFruktgården -0.4622 0.3009 -1.536 0.1245 FarmSando -0.1801 0.2744 -0.656 0.5116 Log(theta) -0.9391 0.1857 -5.056 4.27e-07 *** Zero-inflation model coefficients (binomial with logit link): Estimate Std. Error z value Pr(>|z|) (Intercept) -8.688 42.152 -0.206 0.837 FarmFruktgården 7.379 42.133 0.175 0.861 FarmSando 6.753 42.126 0.160 0.873 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Theta = 0.391 Number of iterations in BFGS optimization: 32 Log-likelihood: -612.9 on 7 Df > > f2 <- zeroinfl(Bumblebee ~ Farm|Cultivar, data = diversity, dist = "negbin") > summary(f2) Call: zeroinfl(formula = Bumblebee ~ Farm | Cultivar, data = diversity, dist = "negbin") Pearson residuals: Min 1Q Median 3Q Max -0.4019 -0.3099 -0.3099 -0.2951 12.0230 Count model coefficients (negbin with log link): Estimate Std. Error z value Pr(>|z|) (Intercept) -1.9397 0.2679 -7.241 4.46e-13 *** FarmFruktgården 0.1666 0.3705 0.450 0.653 FarmSando 1.4289 0.3324 4.299 1.71e-05 *** Log(theta) -1.5096 0.2126 -7.102 1.23e-12 *** Zero-inflation model coefficients (binomial with logit link): Estimate Std. Error z value Pr(>|z|) (Intercept) -13.227 362.819 -0.036 0.971 CultivarDiscovery -3.861 NaN NaN NaN CultivarSummerred -5.736 6888.605 -0.001 0.999 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Theta = 0.221 Number of iterations in BFGS optimization: 14 Log-likelihood: -296.3 on 7 Df
问题解答
1. NaN值的成因
确实和数据量不足高度相关:
- 独居蜂仅70只,样本量远小于蜜蜂(500只),零膨胀模型需要同时拟合计数和零膨胀两个部分,当某类群的观测值过于稀疏(比如某分组下几乎没有个体),模型无法稳定估计系数的标准误,就会出现
NaN。 - 看
p3的结果,零膨胀部分PlacementWild的系数出现NaN,大概率是该分组下独居蜂的零值比例极高,或者观测数太少,导致模型计算时方差趋近于无穷大,标准误无法计算。 - 熊蜂的
f2模型中CultivarDiscovery出现NaN,也是同样逻辑:该品种下熊蜂的观测数据太稀疏,模型没法稳定估计参数。
2. 解决方法
- 简化模型结构:
- 先试试去掉零膨胀部分,改用负二项回归(
glm.nb())——看蜜蜂的结果,零膨胀部分的P值都很高,说明零膨胀效应可能不强,没必要强行用零膨胀模型。 - 如果必须保留零膨胀结构,可以让零膨胀部分的公式更简单,比如只保留截距,不加入
Placement或Cultivar:zeroinfl(Wildbee ~ Placement | 1, data = diversity, dist = "negbin")。
- 先试试去掉零膨胀部分,改用负二项回归(
- 合并分组:
- 把样本量太小的农场/品种合并成相似组,减少模型参数数量,提升估计稳定性。
- 补充数据:如果条件允许,增加该类群的采样次数或采样点,这是最根本的解决办法。
- 换用贝叶斯模型:用
brms包拟合贝叶斯零膨胀负二项模型,贝叶斯方法在小样本下的估计稳定性通常更好,通过设置合理先验,能避免NaN出现。
3. Theta的含义
theta是负二项分布的离散参数,用来衡量计数数据的离散程度:
- 当
theta趋近于无穷大时,负二项分布就退化为泊松分布(离散程度等于均值); theta越小,数据的离散程度越高(过度离散越严重)。
在zeroinfl()函数中,指定dist = "negbin"时,模型会自动估计这个参数,它不是你输入的自变量,而是模型拟合得到的、描述计数部分数据分布特征的参数。
内容的提问来源于stack exchange,提问作者Jane
相关产品推荐
相关产品推荐

