在R中更新zeroinfl模型

我有一个示例数据框如下.

data.df = data.frame(Y=c(0,0,0,0,0,0,0,0,0,0,1,2,3),X1=c(3,5,3,2,5,6,3,5,1,3,1,7,8),X2=c(6,2,1,6,7,1,1,4,2,6,7,2,3))

要创建和更新泊松模型,我将执行以下操作

model.poi = glm(Y~X1+X2,data=data.df,family="poisson") 
summary(model.poi)
model.poi.2 = update(model.poi,~. -X2)
summary(model.poi.2)

我可以通过以下方式创建零膨胀泊松模型

require(pscl)
model.zip = zeroinfl(Y~X1+X2|X1+X2,data=data.df,dist="poisson")
summary(model.zip)

我将如何以与Poisson glm相同的方式更新零膨胀模型?

最佳答案 “zeroinfl”类没有更新方法,但我们可以定义一个:

library(Formula)
update.zeroinfl <- function(object, new, ...) {
    call <- object$call
    call$formula <- update(as.Formula(formula(object)), new)
    eval.parent(call)
}

现在我们测试一下:

> update(model.zip, . ~ . - X2 | . - X2)

Call:
zeroinfl(formula = Y ~ X1 | X1, data = data.df, dist = "poisson")

Count model coefficients (poisson with log link):
(Intercept)           X1  
    -4.0538       0.6139  

Zero-inflation model coefficients (binomial with logit link):
(Intercept)           X1  
   -6.50595     -0.06057  
点赞