Guest User

Untitled

a guest
Jun 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. n<-50
  2. covL<-seq(0,1,length.out=n)
  3. covp<-seq(0,1,length.out=n)
  4. trueMeans<-exp(1.5-2*covL)
  5. probability<-exp(-1.5+covp*2)/(1+exp(-1.5+covp*2))
  6. U<-runif(n,0,1)
  7. y <- rpois(n,trueMeans)
  8. y[U<probability] <- 0
  9.  
  10. #initialize values
  11. initial.l<-glm(y[y>0] ~ covL[y>0], family = "poisson")$coefficients
  12. Beta0.l<-as.numeric(initial.l[1])
  13. Beta1.l<-as.numeric(initial.l[2])
  14.  
  15. Beta0.p<-(sum(y==0)-sum(exp(Beta0.l+Beta1.l*covL)))/length(y)
  16. Beta1.p<-0
  17.  
  18. #going to use the EM algorithm, so zhat is going to be my latent variable, initialize it here.
  19.  
  20. zhat <- rep(0,length(y))
  21. for (i in 1:100) {
  22. #rep through the EM algorithm 100 times, I know this is excessive
  23.  
  24.  
  25. #E step
  26. zhat[y==0]<-((1+exp(-(Beta0.p+Beta1.p*covp)-exp(Beta0.l+Beta1.l*covL)))^(-1))[y==0]
  27.  
  28.  
  29. #M step for the coefficients that estimate the mean (lambda)
  30. lValues<-glm(y ~ covL, family = "poisson",weights=1-zhat)$coefficients
  31. Beta0.l<-as.numeric(lValues[1])
  32. Beta1.l<-as.numeric(lValues[2])
  33.  
  34.  
  35. #M step for the coefficients that estimate the p value
  36. y_star<-as.numeric(y!=0)
  37. y_star<-c(y_star,y[y==0])
  38. weightsV<-c(1-zhat,zhat[y==0])
  39. covariates<-c(covp,covp[y==0])
  40.  
  41. pValues<- glm(y_star~covariates,family=binomial ,weights=weightsV)$coefficients
  42.  
  43. Beta0.p<-as.numeric(pValues[1])
  44. Beta1.p<-as.numeric(pValues[2])
  45.  
  46. cat("Iteration: ",i, " Beta0.l: ",Beta0.l, " Beta1.l: ",Beta1.l," Beta0.p: ", Beta0.p," Beta1.p: ", Beta1.p,"n")
  47. }
  48.  
  49. set.seed(1071)
  50. n<-50
  51. covL<-seq(0,1,length.out=n)
  52. covp<-seq(0,1,length.out=n)
  53. trueMeans<-exp(1.5-2*covL)
  54. probability<-exp(-1.5+covp*2)/(1+exp(-1.5+covp*2))
  55. U<-runif(n,0,1)
  56. y <- rpois(n,trueMeans)
  57. y[U<probability] <- 0
  58.  
  59. nll_zip <- function(par) {
  60. lambda <- exp(par[1] + par[2] * covL)
  61. p <- exp(par[3] + par[4] * covp) / (1 + exp(par[3] + par[4] * covp))
  62. lik <- p * (y == 0) + (1 - p) * dpois(y, lambda)
  63. -sum(log(lik))
  64. }
  65.  
  66. opt <- optim(rep(0, 4), nll_zip, hessian = TRUE)
  67.  
  68. library("pscl")
  69. m <- zeroinfl(y ~ covL | covp)
  70.  
  71. cbind("True" = c(1.5, -2, -1.5, 2), "zeroinfl" = coef(m), "optim" = opt$par)
  72. ## True zeroinfl optim
  73. ## count_(Intercept) 1.5 1.350510 1.350757
  74. ## count_covL -2.0 -1.877199 -1.877871
  75. ## zero_(Intercept) -1.5 -1.452235 -1.453011
  76. ## zero_covp 2.0 1.739965 1.739817
  77.  
  78. cbind("zeroinfl" = sqrt(diag(vcov(m))), "optim" = sqrt(diag(solve(opt$hessian))))
  79. ## zeroinfl optim
  80. ## count_(Intercept) 0.2566790 0.2566963
  81. ## count_covL 0.7814104 0.7816395
  82. ## zero_(Intercept) 0.8410982 0.8414687
  83. ## zero_covp 1.8843705 1.8856983
Add Comment
Please, Sign In to add comment