I am trying to fit a zero-inflated Poisson with regression parameters for lambda as well as p. I am following the framework of:
"Zero-Inflated Poisson Regression with an Application to Defects in Manufacturing, Diane Lambert, Technometrics, Vol 34, No 1 Feb 1992 pp 1-14"
However I cannot seem to get my code to fit right. I thought I had it earlier in the day, but now it is not working. Note that I used this post for some help as well. Link
Here is the code that I am using to simulate ZIP data in r
n<-50
covL<-seq(0,1,length.out=n)
covp<-seq(0,1,length.out=n)
trueMeans<-exp(1.5-2*covL)
probability<-exp(-1.5+covp*2)/(1+exp(-1.5+covp*2))
U<-runif(n,0,1)
y <- rpois(n,trueMeans)
y[U<probability] <- 0
y is my simulated data. I was working on making sure my coding was correct before putting the code into a function. The following is the code I created:
#initialize values
initial.l<-glm(y[y>0] ~ covL[y>0], family = "poisson")$coefficients
Beta0.l<-as.numeric(initial.l[1])
Beta1.l<-as.numeric(initial.l[2])
Beta0.p<-(sum(y==0)-sum(exp(Beta0.l+Beta1.l*covL)))/length(y)
Beta1.p<-0
#going to use the EM algorithm, so zhat is going to be my latent variable, initialize it here.
zhat <- rep(0,length(y))
for (i in 1:100) {
#rep through the EM algorithm 100 times, I know this is excessive
#E step
zhat[y==0]<-((1+exp(-(Beta0.p+Beta1.p*covp)-exp(Beta0.l+Beta1.l*covL)))^(-1))[y==0]
#M step for the coefficients that estimate the mean (lambda)
lValues<-glm(y ~ covL, family = "poisson",weights=1-zhat)$coefficients
Beta0.l<-as.numeric(lValues[1])
Beta1.l<-as.numeric(lValues[2])
#M step for the coefficients that estimate the p value
y_star<-as.numeric(y!=0)
y_star<-c(y_star,y[y==0])
weightsV<-c(1-zhat,zhat[y==0])
covariates<-c(covp,covp[y==0])
pValues<- glm(y_star~covariates,family=binomial ,weights=weightsV)$coefficients
Beta0.p<-as.numeric(pValues[1])
Beta1.p<-as.numeric(pValues[2])
cat("Iteration: ",i, " Beta0.l: ",Beta0.l, " Beta1.l: ",Beta1.l," Beta0.p: ", Beta0.p," Beta1.p: ", Beta1.p,"\n")
}
The values I should be getting are (values that I am getting are in parentheses):
Beta0.l: 1.5 (1.73)
Beta1.l: -2 (-2.81)
Beta0.p: -1.5 (1.13)
Beta1.p: 2 (-2.62)
What concerns me are the Beta.p values, the signs should be reversed. Any help would be appreciated. Sorry if this is in the wrong place, this is my first time posting. I will try and answer anything that I left out.