* 2-parameter log-inverse Gaussian regression model
*! version 1.0.0  16 March 1999
program define ivglog, eclass
        version 6
        if replay() {
                if "`e(cmd)'" ~= "ivglog" {
                        error 301
                }
                Display `0'
        }
        else   Estimate `0'
end

program define Estimate, eclass
        syntax varlist [fw pw iw aw] [if] [in] [, /*
                */ Robust CLuster(varname) Level(passthru) /*
                */ IRr EForm noCONstant noLOG SCore(string) *]
        if "`cluster'" ~= "" {
                local clopt "cluster(`cluster')"
        }
        mlopts mlopts, `options'   /* ADD OPTIONS */
        tokenize `varlist'
        local lhs "`1'"
        mac shift
        local rhs "`*'"

                             /* MARK SAMPLE */

        marksample touse
        markout `touse' `cluster', strok

                             /* SCORE */
if `"`score'"'!="" {
		confirm new variable `score'
		local nword : word count `score'
		if `nword' > 2 {
			di in red "score() must contain the name of " /*
			*/ "one or two new variables"
			exit 198
		}
		local scname1 : word 1 of `score'
		local scname2 : word 2 of `score'
		tempvar scvar1 scvar2
		local scopt "score(`scvar1' `scvar2')"
	}
                             /* WEIGHTS */
        if `"`weight'"'!="" {
                if `"`weight'"'=="fweight" {
                     local wt `"[fw `exp']"'
                }
        else  local wt `"[aw `exp']"'
        }

        if `"`weight'"'!= "" {        /* not same as glm */
                local iwt `"[iw `exp']"'
        }

                             /* ESTIMATE CONSTANT-ONLY MODEL */
        if "`log'"=="" {
          display in green _n "Fitting constant-only model:"
        }
        ml model lf ivgln_ll (`lhs':`lhs'=) /ln_phi /*
        */ [`weight'`exp'] if `touse', /*
        */ max missing nopreserve search(off) `mlopts' `log'

                             /* ESTIMATE FULL MODEL */
        if "`log'"=="" {
          display in green _n "Fitting full model:"
        }
        ml model lf ivgln_ll (`lhs':`lhs'=`rhs') /ln_phi /*
        */ [`weight'`exp'] if `touse', /*
        */ max missing nopreserve continue search(off) /*
        */ `robust' `clopt' `scopt' `mlopts' `log' /*
        */ title("Log-inverse Gaussian model")

                             /* CHANGE SCORES TO REAL VALUES */
        if `"`score'"'!="" {
            label var `scvar1' "Score index for x*b from ivglog"
            rename `scvar1' `scname1'
            if `"scname2'"'!="" {
               lab var `scvar2' "Score index for /ln_phi from ivglog"
               rename `scvar2' `scname2'
            }
        }
                             /* FILL IN e() */
est local predict "poisso_p"
est local cmd "ivglog"

                             /* DISPLAY FINAL RESULTS */
        Display, `level' `eform' `irr'
end

program define Display
        syntax [, Level(int $S_level) IRr EForm]
        if "`eform'"!="" | `"`irr'"'!="" {
           local eopt "eform(IRR)"
        }
        ml display, level(`level') `eopt' plus first
         _diparm ln_phi, level(`level')
        di in gr _dup(9) "-" "+" _dup(68) "-"
        _diparm ln_phi, level(`level') exp label("phi")
        di in gr _dup(78) "-"
end

exit