据我所知,产生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
零膨胀泊松和一个演示如何获得自举置信区间预测值从零截断负二项式模型零截断负二项分布。结合起来,希望能为您提供足够的示例,使其与零膨胀泊松的预测值一起使用。您可能还会得到一些图形提示:)
predict()
函数中尚未实现标准错误zeroinfl()
。