如何从R零膨胀计数数据回归中获得标准误差?[关闭]


9

以下代码

PredictNew <- predict (glm.fit, newdata = Predict, X1 =X1, Y1= Y1, 
                       type = "response", se.fit = TRUE)

产生3列data.frame--PredictNew,拟合值,标准误差和残差标度项。

完美...但是使用的模型配备zeroinfl {pscl}

PredictNew <- predict (zeroinfl.fit, newdata = Predict, X1 =X1, Y1= Y1, 
                       type = "response", se.fit = TRUE)

要么

PredictNew <- predict (zeroinfl.fit, newdata = Predict, X1 =X1, Y1= Y1, 
                       type = "response", se.fit = TRUE, MC = 2500, conf = .95))

仅产生拟合值的单列向量。但是,我非常希望出现标准错误。我读过的所有内容都说应该制作它们。

(代码已进行了一些简化,实际上我有四个变量和一个偏移量- predict.glmse.fit = TRUE产生SE的概率无关。)


5
看看R-Help上的该线程:stat.ethz.ch/pipermail/r-help/2008-December/thread.html#182806(特别是来自Achim Zeileis的消息,他提供了执行我认为您所要做的事情的代码)尝试做)。目前看来predict()函数中尚未实现标准错误zeroinfl()
smillig

谢谢,该代码似乎产生了相当合理的结果。其他人应注意,为了提取预测间隔和se
KalahariKev 2012年

Answers:


4

据我所知,产生predict结果的方法zeroinfl不包括标准误差。如果您的目标是构建置信区间,则一种有吸引力的替代方法是使用自举。我之所以说具有吸引力,是因为自举有潜力变得更强大(如果满足SE的所有假设,效率就会降低)。

这是一些粗略的代码,可以执行您想要的操作。它不能完全正常工作,但是希望您可以进行必要的更正。

## load boot package
require(boot)
## output coefficients from your original model
## these can be used as starting values for your bootstrap model
## to help speed up convergence and the bootstrap
dput(round(coef(zeroinfl.fit, "count"), 3))
dput(round(coef(zeroinfl.fit, "zero"), 3))

## function to pass to the boot function to fit your model
## needs to take data, an index (as the second argument!) and your new data
f <- function(data, i, newdata) {
  require(pscl)
  m <- zeroinfl(count ~ child + camper | persons, data = data[i, ], start = list(count = c(1.598, -1.0428, 0.834), zero = c(1.297, -0.564)))
  mparams <- as.vector(t(do.call(rbind, coef(summary(m)))[, 1:2]))
  yhat <- predict(m, newdata, type = "response")
  return(c(mparams, yhat))    
}

## set the seed and do the bootstrap, make sure to set your number of cpus
## note this requires a fairly recent version of R
set.seed(10)
res <- boot(dat, f, R = 1200, newdata = Predict, parallel = "snow", ncpus = 4)

## get the bootstrapped percentile CIs
## the 10 here is because in my initial example, there were 10 parameters before predicted values
yhat <- t(sapply(10 + (1:nrow(Predict)), function(i) {
  out <- boot.ci(res, index = i, type = c("perc"))
  with(out, c(Est = t0, pLL = percent[4], pUL = percent[5]))
}))

## merge CIs with predicted values
Predict<- cbind(Predict, yhat)

我从两页我写的,一个引导参数吸引了这段代码从零膨胀泊松回归zeroinfl 零膨胀泊松和一个演示如何获得自举置信区间预测值从零截断负二项式模型零截断负二项分布。结合起来,希望能为您提供足够的示例,使其与零膨胀泊松的预测值一起使用。您可能还会得到一些图形提示:)


我尝试将您的代码改编为VGAM软件包中的零截断负二项式模型,但收到错误。我应该在此处在简历上创建一个新问题并在此处链接吗?非常感谢您的帮助。具体来说,这是我得到的错误: Error in X.vlm.save %*% coefstart : non-conformable arguments
拉斐尔K
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.