[R] help with zicounts

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Fri Mar 9 09:32:14 CET 2007


Jaap:

> I have simulated data from a zero-inflated Poisson model, and would like
> to use a package like zicounts to test my code of fitting the model.
> My question is: can I use zicounts directly with the following simulated
> data?

I guess you can use zicounts, but personally I'm more familiar with 
zeroinfl() from package "pscl" (because I have written this function :)). 
With that you can easily do:

> beta.true<-1.0
> gamma.true<-1.0
> n<-1000
> x<-matrix(rnorm(n),n,1)
> pi<-expit(x*beta.true)
> mu<-exp(x*gamma.true)
> y<-numeric(n) # blank vector
> z<-(runif(n)<pi) # logical: T with prob p_i, F otherwise
> y[z]<-rpois(sum(z),mu[z]) # draw y_i ~ Poisson(mu_i) where z_i = T
> y[!z]<-0 # set y_i = 0 where z_i = F

library("pscl")
zeroinfl(y ~ 0 + x | 0 + x)

which by default fits a ZIP (with log link and logit inflation).

hth,
Z



More information about the R-help mailing list