*! STB-6: smv5 *! version 2.0 /* Modified from Poisson.ado by: D. H. Judson Department of Sociology Washington State University Pullman, WA 99164-4020 (509) 335-4595 Program code enhancements and suggestions are welcome at the above address. This program calls ado files: emdef.ado _crclf.ado STATA version: 2.1, 12/1/91 */ program define poilog version 2.1 mac def _options "Ltol(string) Iter(string) Offset(string) Rescale *" mac def _varlist "req ex" mac def _in "opt" mac def _if "opt" mac def _exp "opt" parse "%_*" parse "%_varlist", parse(" ") mac def _dv "%_1" cap assert %_1>=0 & %_1==int(%_1) %_if %_in if (_rc>0) { di in bl "Note: You are responsible for interpretation of non-count dependent variable" } mac shift qui gen _wt = 1 if ("%_exp"!="") { qui replace _wt %_exp } if ("%_rescale"!="") { di in bl "Note: You are responsible for interpretation of rescaled weights" qui summ _wt %_if %_in replace _wt = _wt / (_result(3)) } if ("%_ltol"=="") { mac def _ltol "1e-7" } if ("%_iter"=="") { mac def _iter 100 } if ("%_offset"!="") { mac def _poffset "+%_offset" mac def _moffset "-%_offset" mac def _offopt "offset(%_offset)" } mac def _dll = 1 mac def _niter 0 capture { gen long _lastobs=_n %_if %_in replace _lastobs=. if %_dv==. | _wt==. replace _lastobs=_lastobs[_n-1] if _lastobs==. mac def _lastobs=_lastobs[_N] drop _lastobs summ %_dv %_exp %_if %_in mac def _emu = _result(3) if ("%_offset"!="") { gen float _predz = exp(%_offset) summ _predz %_exp %_if %_in mac def _emu = %_emu / _result(3) drop _predz } gen float _predz = log(%_emu) %_poffset %_if %_in gen float _epred = exp(_predz) %_if %_in mac def _nobs=_result(1) _crclf _Poisson = %_dv %_if %_in replace _Poisson = sum(_Poisson*_wt) %_if %_in mac def _llconst = _Poisson[%_lastobs] replace _Poisson = cond(%_dv==0,0,%_dv*(log(%_dv)-1)) %_if %_in replace _Poisson = sum(_Poisson*_wt) %_if %_in mac def _llperf = _Poisson[%_lastobs] - %_llconst replace _Poisson= %_dv*_predz - _epred %_if %_in replace _Poisson= sum(_Poisson*_wt) %_if %_in mac def _ll0 = _Poisson[%_lastobs] - %_llconst mac def _myll = %_ll0 di while ((abs(%_dll)>%_ltol*abs(%_myll) & %_niter<%_iter) | (%_niter==0)) { di in gr "Iteration %_niter: Log Likelihood = ", /* */ in ye %_myll, in gr "Change = ", in ye %_dll /* CHANGED ABOVE LINE: ADDED CHANGE =... */ replace _Poisson = ((%_dv - _epred)/_epred) + _predz %_moffset %_if %_in reg _Poisson %_* =(_epred)*_wt, noscale mac def _regF = _result(6) mac def _regMDF = _result(3) mac def _RMSE = _result(9) drop _predz predict _predz %_if %_in replace _predz = _predz %_poffset replace _epred = exp(_predz) replace _Poisson = %_dv*_predz - _epred %_if %_in replace _Poisson = sum(_Poisson*_wt) %_if %_in mac def _mynll = _Poisson[%_lastobs] - %_llconst mac def _dll = %_mynll - %_myll mac def _myll "%_mynll" mac def _niter = %_niter+1 } * In the future: * replace _Poisson = (%_dv - _epred) * cap noisily _huber _Poisson _epred %_exp %_if %_in replace _Poisson = ((%_dv - _epred)/_epred) + _predz %_moffset /* CHANGED ABOVE LINE. REMOVED /%_RMSE */ if ((abs(%_dll)>%_ltol*abs(%_myll)) | %_niter>=%_iter) { di in bl "ERROR: Convergence not achieved. Estimates should be considered" di in bl "highly suspect or in error. Last change = ", in ye %12.11g %_dll } else di in bl "Note: Convergence achieved. Last change = ", in ye %12.11g %_dll /* CHANGED SEVERAL LINES ABOVE. ADDED INFO. ON CONVERGENCE CRITERION. */ di di in gre "Poisson Regression (Log link function)" _col(49) /* */ "Number of cells =" in yel %8.0f %_nobs di in gre "Goodness of fit chi2(" %_nobs-%_regMDF-1 ") =" /* */ in yel %10.3f -2*(%_myll - %_llperf) /* */ _col(49) in gre "Model chi2(%_regMDF) =" /* */ in yel %8.3f 2*(%_myll-%_ll0) mac def S_RSLT2 = 2*(%_myll-%_ll0) mac def S_RSLT3 = %_regMDF di in gre "Prob > chi2 = " /* */ in yel %9.4f chiprob(%_nobs-%_regMDF-1,-2*(%_myll-%_llperf)) /* */ _col(49) in gre "Prob > chi2 =" /* */ in yel %8.4f chiprob(%_regMDF,2*(%_myll-%_ll0)) /* CHANGED ABOVE LINE: REPLACED %regMDF with %_regMDF */ di in gre "Log Likelihood =" in yel %10.3f %_myll qui reg _Poisson %_* =(_epred)*_wt, noscale mac def _RMSE = _result(9) /* CHANGED ABOVE LINE, ADDED MAC DEF */ di di in gre "Variable | Coefficient Std. Error t Prob > |t| Mean" di in gre "---------+" _dup(62) "-" mac def _x = 8-length("%_dv") di _dup(%_x) " " in gre "%_dv |", /* */ in yel _col(61) %10.0g %_emu di in gre "---------+" _dup(62) "-" parse "%_* _cons", parse(" ") while ("%_1"!="") { emdef S_t : t %_1 mac def S_t=%S_t*%_RMSE /* CHANGED ABOVE LINE ADDED *%_RMSE */ if ("%_1"!="_cons") { cap summ %_1 %_exp %_if %_in mac def _mm = _result(3) } else { mac def _mm 1 } mac def _x = 8-length("%_1") di _dup(%_x) " " in gre "%_1 |", /* */ in yel _col(15) %8.0g _b[%_1], /* */ _col(28) %8.0g abs(_b[%_1]/%S_t), /* */ _col(37) %8.3f %S_t, /* */ _col(46) %8.3f tprob(%_nobs-%_regMDD-1,%S_t),/* */ _col(57) %10.0g %_mm mac shift } di in gre "---------+" _dup(62) "-" mac def S_RSLT1 = %_nobs } mac def _rc = _rc cap drop _Poisson cap drop _predz cap drop _epred cap drop _wt exit %_rc end