mar*_*llt 4 r logistic-regression
我创建了一个零膨胀负二项式模型,想研究将多少个零分配给采样或结构零。我如何在R中实现这一点。zeroinfl页面上的示例代码对我来说还不清楚。
data("bioChemists", package = "pscl")
fm_zinb2 <- zeroinfl(art ~ . | ., data = bioChemists, dist = "negbin")
table(round(predict(fm_zinb2, type="zero")))
> 0 1
> 891 24
table(round(bioChemists$art))
> 0 1 2 3 4 5 6 7 8 9 10 11 12 16 19
> 275 246 178 84 67 27 17 12 1 2 1 1 2 1 1
Run Code Online (Sandbox Code Playgroud)
这告诉我什么?
当我对数据执行相同操作时,我得到的读数只是样本量在1下列出?谢谢
Zeileis(2008)在论文中提供了详细信息,网址为https://www.jstatsoft.org/article/view/v027i08/v27i08.pdf
收集predict有关pscl库中每个模型的功能作用的所有解释的工作量很大(几年,您的问题仍未得到答案),并且将其埋藏在(第19,23页)似然函数(等式7、8)。我已经将您的问题解释为意味着您希望/需要知道如何使用不同type的预测:
type="response")type="zero")type="prob")要读取pscl软件包随附的数据:
data("bioChemists", package = "pscl")
Run Code Online (Sandbox Code Playgroud)
然后拟合零膨胀负二项式模型:
fm_zinb2 <- zeroinfl(art ~ . | ., data = bioChemists, dist = "negbin")
Run Code Online (Sandbox Code Playgroud)
如果您希望预测期望值,则可以使用
predict(fm_zinb2, type="response")[29:31]
29 30 31
0.5213736 1.7774268 0.5136430
Run Code Online (Sandbox Code Playgroud)
因此,在此模型下,博士学位的最近三年中预期发表的文章数量是生化学家29和31的一半,而生化学家30则接近2。
但是我相信您追求的是过零的可能性(点质量为零)。此命令将执行此操作,并打印出第29到31行中项目的值(是的,我钓鱼了!):
predict(fm_zinb2, type="zero")[29:31]
Run Code Online (Sandbox Code Playgroud)
它产生以下输出:
29 30 31
0.58120120 0.01182628 0.58761308
Run Code Online (Sandbox Code Playgroud)
因此,第29个项目为多余零(您称为抽样零,即非结构性零,因此未由协变量解释)的概率为58%,第30个为1.1%,第31个为是59%。因此,这两名生物化学家预计将发表的论文为零,而且超过了可以由各个协变量的负二项式回归解释的那些生物化学家。
您已将整个数据集中的这些预测概率制成表格
table(round(predict(fm_zinb2, type="zero")))
0 1
891 24
Run Code Online (Sandbox Code Playgroud)
因此,您的输出告诉您,只有24位生物化学家可能是一个多余的零,即,一个多余的零的预测概率超过一半(由于四舍五入)。
如果以百分比表制表成10分制的表格,可能会更容易解释
table(cut(predict(fm_zinb2, type="zero"), breaks=seq(from=0,to=1,by=0.1)))
Run Code Online (Sandbox Code Playgroud)
给
(0,0.1] (0.1,0.2] (0.2,0.3] (0.3,0.4] (0.4,0.5] (0.5,0.6]
751 73 34 23 10 22
(0.6,0.7] (0.7,0.8] (0.8,0.9] (0.9,1]
2 0 0 0
Run Code Online (Sandbox Code Playgroud)
因此,您可以看到751名生化学家不太可能是多余的零,但是22名生化学家有超过50%的可能性成为过零,而只有2名生化学家的可能性更高(60-70%)。没有人极有可能成为多余的零。以图形方式可以在直方图中显示
hist(predict(fm_zinb2, type="zero"), col="slateblue", breaks=seq(0,0.7,by=.02))
Run Code Online (Sandbox Code Playgroud)
您将每个生化学家的实际计数列表了表格(无需四舍五入,因为这些是计数):
table(bioChemists$art)
0 1 2 3 4 5 6 7 8 9 10 11 12 16 19
275 246 178 84 67 27 17 12 1 2 1 1 2 1 1
Run Code Online (Sandbox Code Playgroud)
谁是特别的生物化学家,拥有19种出版物?
most_pubs <- max(bioChemists$art)
most_pubs
extreme_biochemist <- bioChemists$art==most_pubs
which(extreme_biochemist)
Run Code Online (Sandbox Code Playgroud)
您可以获得每个生物化学家拥有任意数目的酒吧的准确估计概率,恰好是0,最大是19,这真是令人难以置信!
preds <- predict(fm_zinb2, type="prob")
preds[extreme_biochemist,]
Run Code Online (Sandbox Code Playgroud)
您可以为我们的一位特殊生物化学家查看,他有19种出版物(使用此处的R基作图,但ggplot更漂亮)
expected <- predict(fm_zinb2, type="response")[extreme_biochemist]
# barplot returns the midpoints for counts 0 up to 19
midpoints<-barplot(preds[extreme_biochemist,],
xlab="Predicted #pubs", ylab="Relative chance among biochemists")
# add 1 because the first count is 0
abline(v=midpoints[19+1],col="red",lwd=3)
abline(v=midpoints[round(expected)+1],col="yellow",lwd=3)
Run Code Online (Sandbox Code Playgroud)
这表明尽管我们期望生化学家915出版4.73本书,但在这种模式下,2-3家酒吧的可能性更大,远不及实际的19家酒吧(红线)。
回到问题,对于生物化学家29,过量零的概率为
pzero <- predict(fm_zinb2, type="zero")
pzero[29]
29
0.5812012
Run Code Online (Sandbox Code Playgroud)
总体(略)为零的概率为
preds[29,1]
[1] 0.7320871
Run Code Online (Sandbox Code Playgroud)
因此,相对于结构(即通过回归解释)而言,过量的零的预测概率的比例为:
pzero[29]/preds[29,1]
29
0.7938962
Run Code Online (Sandbox Code Playgroud)
或超过零的机会之外的零的额外概率为:
preds[29,1] - pzero[29]
29
0.1508859
Run Code Online (Sandbox Code Playgroud)
生物化学家29的实际出版物数是
bioChemists$art[29]
[1] 0
Run Code Online (Sandbox Code Playgroud)
因此,预计生物化学家发表论文的数量为零的原因很少通过回归分析来解释(20%),而大多数则没有(即超过80%)。
总体而言,我们发现对于大多数生物化学家而言,情况并非如此。我们的生物化学家29是不寻常的,因为他们零客栈的机会大部分是过剩的,即通过回归无法解释。我们可以通过以下方式看到它:
hist(pzero/preds[,1], col="blue", xlab="Proportion of predicted probability of zero that is excess")
Run Code Online (Sandbox Code Playgroud)
这给你: