Skip to content

Instantly share code, notes, and snippets.

@jseabold
Created September 17, 2013 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jseabold/6601357 to your computer and use it in GitHub Desktop.
Save jseabold/6601357 to your computer and use it in GitHub Desktop.
Fixed Effects Zero-Inflated Poisson from Majo and van Soest (2011).
set more off
capture program drop ZIP_FE_model
program define ZIP_FE_model
version 9.1
args todo b lnf
tempvar theta1 lambda last nonz w sln0 sln r0 r nb0 nb1 nb00 nb2 L2
local by "$MY_panel"
local byby "by `by'"
sort `by' year
local y "$ML_y1"
mleval `theta1' = `b'
di "`b'"
quietly {
gen double `lambda' = exp(`theta1')
`byby': gen double `last' = (_n == _N)
`byby': egen double `nonz' = min(`y')
`byby': egen double `w' = sum(`y')
`byby': gen double `sln0' = lngamma(`y' + 1)
`byby': egen double `sln' = sum(`sln0')
`byby': gen double `r0' = `y'*ln(`lambda')
`byby': egen double `r' = sum(`r0')
`byby': egen double `nb0' = sum(`lambda')
`byby': gen double `nb1' = `nb0'^`w'
`byby': gen double `nb00' = `lambda'^`w'
`byby': egen double `nb2' = sum(`nb00')
`byby': gen double `L2' = lngamma(`w'+1) - `sln' + `r' - ln(`nb1' - `nb2') if (`last' == 1 & `nonz'>0)
di `L2'
mlsum `lnf' = `L2' if (`last' == 1 & `nonz' > 0)
} /* end quietly */
end
/* Taken from http://cameron.econ.ucdavis.edu/mmabook/mmaprograms.html */
infile CUSIP ARDSSIC SCISECT LOGK SUMPAT LOGR70 LOGR71 LOGR72 LOGR73 LOGR74 LOGR75 LOGR76 LOGR77 LOGR78 LOGR79 PAT70 PAT71 PAT72 PAT73 PAT74 PAT75 PAT76 PAT77 PAT78 PAT79 using http://cameron.econ.ucdavis.edu/mmabook/patr7079.asc, clear
gen id = _n
label variable id "id"
reshape long PAT LOGR, i(id) j(year)
label variable PAT "Patents"
drop if year<75
xtpoisson PAT LOGR, fe i(id)
sort id year
gen nonz = 0
replace nonz = 1 if PAT > 0
/*sort id wave*/
global MY_panel id
ml model d0 ZIP_FE_model (PAT = LOGR, nocons) if nonz > 0
ml check
ml search
ml maximize, difficult
@ACL90
Copy link

ACL90 commented Aug 6, 2019

Dear Skipper,
I found this page from here: https://www.stata.com/statalist/archive/2013-09/msg00686.html

I think "nonz" should be defined by
bysort id: gen nonz = min(PAT[1],PAT[2])
as in the paper the two authors write: *p7: "First, we discard all observations with Yi1=0 or Yi2=0"

Unfortunately, this does not fix the program. Have you found out how to proceed?

Thanks in advance,
Alexandre Cazenave-Lacroutz

PS: I also tried to ask the question on stackoverflow (so far unsuccessfully):
https://stackoverflow.com/questions/57408961/how-to-make-run-this-user-written-fixed-effects-zero-inflated-poisson-in-stata?noredirect=1#comment101302641_57408961

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment