UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

SAS Code Fragments
Zero-inflated Poisson and negative binomial using proc nlmixed

The two examples here use data set fish.sas7bdat.

*zero inflated poisson, producing the same result as "zip count child camper persons, inflate(child)" in Stata;
proc nlmixed data=fish;
   parameters b0=0 b1=0 b2=0 b3 = 0
              a0=0 a1 = 0 ;
   /* linear predictor for the inflation probability      */
   linpinfl = a0 + a1*child;
   /* infprob = inflation probability for zeros           */
   /*         = logistic transform of the linear predictor*/
   infprob  = 1/(1+exp(-linpinfl));
   /* Poisson mean */
   lambda   = exp(b0 + b1*child + b2*camper + b3*persons );
   /* Build the ZIP log likelihood */
   if count=0 then 
        ll = log(infprob + (1-infprob)*exp(-lambda));
   else ll = log((1-infprob)) - lambda  + count*log(lambda) - lgamma(count + 1);
   model count ~ general(ll);
run;
                                          Fit Statistics
                             -2 Log Likelihood                 1532.1
                             AIC (smaller is better)           1544.1
                             AICC (smaller is better)          1544.4
                             BIC (smaller is better)           1565.2

                                       Parameter Estimates

                        Standard
   Parameter  Estimate     Error    DF  t Value  Pr > |t|   Alpha     Lower     Upper  Gradient

   b0          -1.0572    0.1812   250    -5.83    <.0001    0.05   -1.4141   -0.7003   0.00011
   b1          -1.1675   0.09471   250   -12.33    <.0001    0.05   -1.3541   -0.9810  -0.00016
   b2           0.7709   0.09384   250     8.21    <.0001    0.05    0.5861    0.9557   0.00005
   b3           0.8886   0.04663   250    19.06    <.0001    0.05    0.7967    0.9804  0.000476
   a0          -0.9150    0.2503   250    -3.66    0.0003    0.05   -1.4080   -0.4220  0.000018
   a1           1.1857    0.2654   250     4.47    <.0001    0.05    0.6631    1.7083  2.408E-6

*zero inflated negative binomial, producing the same result as "zinb count child camper persons, inflate(child)" in Stata;;
proc nlmixed data=fish;
   parameters b0=0 b1=0 b2=0 b3 = 0
              a0=0 a1 = 0 alpha = 1;
   /* linear predictor for the inflation probability      */
   linpinfl = a0 + a1*child;
   /* infprob = inflation probability for zeros           */
   /*         = logistic transform of the linear predictor*/
   infprob  = 1/(1+exp(-linpinfl));
   /* negative binomial with mean-dispersion */
   lambda   = exp(b0 + b1*child + b2*camper + b3*persons );
   /* Build the ZIP log likelihood */
   m = 1/alpha;
   p = 1/(1+alpha*lambda);
   if count=0 then 
        ll = log(infprob + (1-infprob)*(p**m));
   else ll = log(1-infprob) + log(gamma(m + count)) - log(gamma(count + 1))
   			 - log(gamma(m)) + m*log(p) + count*log(1-p);
   model count ~ general(ll);
run;
                             Fit Statistics

                             -2 Log Likelihood                  799.8
                             AIC (smaller is better)            813.8
                             AICC (smaller is better)           814.3
                             BIC (smaller is better)            838.5


                                       Parameter Estimates

                        Standard
   Parameter  Estimate     Error    DF  t Value  Pr > |t|   Alpha     Lower     Upper  Gradient

   b0          -1.6599    0.3197   250    -5.19    <.0001    0.05   -2.2896   -1.0303  0.000035
   b1          -1.2056    0.2715   250    -4.44    <.0001    0.05   -1.7402   -0.6709  -0.00029
   b2           0.5834    0.2379   250     2.45    0.0149    0.05    0.1149    1.0520  -0.00006
   b3           1.0516    0.1110   250     9.48    <.0001    0.05    0.8331    1.2702   -0.0016
   a0          -4.4306    1.5163   250    -2.92    0.0038    0.05   -7.4169   -1.4442  -0.00024
   a1           2.9265    0.8479   250     3.45    0.0007    0.05    1.2564    4.5965  -0.00011
   alpha        1.7903    0.3264   250     5.49    <.0001    0.05    1.1475    2.4331  -0.00016

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.