*! version 1.6.0 3/29/01

capture program drop _pemarg
program define _pemarg, rclass
    version 6.0
    tempname b x xb sxb prob marg tmp bxb bxbm1 difpdf tmp2
    tempname b x o xo xb sxb prob marg tmp bxb bxbm1 difpdf tmp2 bi alpha
    tempname sumxb sp PredVal PredPr sump
    tempname pdiag sumpb o

    if "`e(cmd)'"=="cloglog"  { local hasmarg = "not yet" }
    if "`e(cmd)'"=="cnreg"    { local hasmarg = "not applicable" }
    if "`e(cmd)'"=="fit"      { local hasmarg = "not applicable" }
    if "`e(cmd)'"=="gologit"  { local hasmarg = "not yet" }
    if "`e(cmd)'"=="intreg"   { local hasmarg = "not applicable" }
    if "`e(cmd)'"=="logistic" { local hasmarg = "yes" }
    if "`e(cmd)'"=="logit"    { local hasmarg = "yes" }
    if "`e(cmd)'"=="mlogit"   { local hasmarg = "yes" }
    if "`e(cmd)'"=="nbreg"    { local hasmarg = "yes" }
    if "`e(cmd)'"=="ologit"   { local hasmarg = "yes" }
    if "`e(cmd)'"=="oprobit"  { local hasmarg = "yes" }
    if "`e(cmd)'"=="poisson"  { local hasmarg = "yes" }
    if "`e(cmd)'"=="probit"   { local hasmarg = "yes" }
    if "`e(cmd)'"=="regress"  { local hasmarg = "not applicable" }
    if "`e(cmd)'"=="tobit"    { local hasmarg = "not applicable" }
    if "`e(cmd)'"=="zinb"     { local hasmarg = "not yet" }
    if "`e(cmd)'"=="zip"      { local hasmarg = "not yet" }
    return local hasmarg `hasmarg'
    if "`hasmarg'"!="yes" { exit }

    matrix `b' = e(b)
    if "`e(cmd)'"=="mlogit" {
        version 5.0
        matrix `b' = get(_b)
        version 6.0
    }
    if "`e(cmd)'"=="nbreg" {
        local nb = colsof(`b') - 1 /* -1 for alpha */
        matrix `b' = `b'[1,1..`nb']
    }
    matrix `x' = PE_base

    if "`e(cmd)'"=="logit"  | "`e(cmd)'"=="probit" | /*
    */ "`e(cmd)'"=="logistic" | /*
    */"`e(cmd)'"=="poisson" | "`e(cmd)'"=="nbreg"  {
        matrix `x' = `x',J(1,1,1)
        matrix `xb' = `x' * `b''
        local nb = colsof(`b') - 1
        matrix `b' = `b'[1,1..`nb'] /* get rid of _con */
        scalar `sxb' = `xb'[1,1]
        if "`e(cmd)'"=="logit" | "`e(cmd)'"=="logistic" {
            scalar `prob' = exp(`sxb')/(1+exp(`sxb'))
            scalar `tmp' = `prob'*(1-`prob')
            matrix `marg' = `tmp'*`b'
        }
        else if "`e(cmd)'"=="probit" {
            scalar `prob' = normprob(`sxb')
            scalar `tmp'  = exp(-`sxb'*`sxb'/2)/sqrt(2*_pi)
            matrix `marg' = `tmp'*`b'
        }
        else if "`e(cmd)'"=="poisson" | "`e(cmd)'"=="nbreg" {
            scalar `prob' = exp(`sxb')
            matrix `marg' = `prob'*`b'
            scalar `prob' = -exp(`sxb') /* to undo change below */
        }
        matrix `prob' = -1 * `marg'
        matrix `marg' = `prob' \ `marg'
    }

    if "`e(cmd)'"=="oprobit" | "`e(cmd)'"=="ologit" {
        local ncats = e(k_cat)
        * xb without intercept
        local nb = colsof(`b') - `ncats' + 1
        matrix `b' = `b'[1,1..`nb'] /* get rid of _con's */
        matrix `xb' = `x' * `b''
        scalar `sxb' = `xb'[1,1]
        matrix `difpdf' = J(1,`ncats',1)
        matrix `marg' = J(`nb',`ncats',1)
        * compute probabilities
        if "`e(cmd)'"=="oprobit" {
            matrix `prob' = J(1,`ncats',1)
            scalar `bxb' = _b[_cut1]-`sxb'
            matrix `prob'[1,1] = normprob(`bxb') /* prob for cat 1 */
            matrix `difpdf'[1,1] = exp(-`bxb'*`bxb'/2)/sqrt(2*_pi)
            local i 2
            while `i'<`ncats' {
                local im1 = `i' - 1
                scalar `bxb' = _b[_cut`i'] - `sxb'
                scalar `bxbm1' = _b[_cut`im1'] - `sxb'
                matrix `prob'[1,`i'] = normprob(`bxb') - normprob(`bxbm1')
                matrix `difpdf'[1,`i'] = exp(-`bxb'*`bxb'/2)/sqrt(2*_pi) /*
                */ - exp(-`bxbm1'*`bxbm1'/2)/sqrt(2*_pi)
                local i = `i' + 1
            }
            local im1 = `i' - 1
            scalar `bxb' = `sxb'-_b[_cut`im1']
            matrix `prob'[1,`ncats'] = normprob(`bxb')
            * 12/6/00
            matrix `difpdf'[1,`ncats'] = -1*exp(-`bxb'*`bxb'/2)/sqrt(2*_pi)
        }
        if "`e(cmd)'"=="ologit" {
            matrix `prob' = J(1,`ncats',1)
            scalar `tmp' = 1/(1+exp(`sxb'-_b[_cut1]))
            matrix `prob'[1,1] = `tmp'
            matrix `difpdf'[1,1] = `tmp'*(1-`tmp')
            local i 2
            while `i'<`ncats' {
                local im1=`i'-1
                scalar `tmp' = 1/(1+exp(`sxb'-_b[_cut`i']))
                scalar `tmp2' = 1/(1+exp(`sxb'-_b[_cut`im1']))
                matrix `prob'[1,`i'] = `tmp' - `tmp2'
                matrix `difpdf'[1,`i'] = (`tmp'*(1-`tmp')) - (`tmp2'*(1-`tmp2'))
                local i=`i'+1
            }
            local im1 = `i'-1
            scalar `tmp' = 1/(1+exp(`sxb'-_b[_cut`im1']))
            matrix `prob'[1,`ncats'] = 1-`tmp'
            matrix `difpdf'[1,`i'] = - (`tmp'*(1-`tmp'))
        }
        local i 1
        while `i'<=`nb' {
            local j 1
            while `j'<=`ncats' {
                matrix `marg'[`i',`j'] = -1 * `difpdf'[1,`j'] * `b'[1,`i']
                local j = `j' + 1
            }
            local i = `i' + 1
        }
    }

    if "`e(cmd)'"=="mlogit" {
        matrix `x'  = PE_base
        matrix `xo' = `x',J(1,1,1)
        matrix `PredVal'  = J(1,1,1)
        matrix colnames `PredVal' = xb
        * to get a matrix rather than a vector
        version 5.0
        matrix `b' = get(_b)
        version 6.0
        local ncats = e(k_cat)
        matrix `prob' = J(1,`ncats',-1)
        matrix `xb' = `b'*`xo'' /* one row for each set of b's */
        matrix `PredVal' = `xb'
        scalar `sumxb' = 1
        local i = 1
        while `i' < `ncats' {
            scalar `sxb' = exp(`xb'[`i',1])
            scalar `sumxb' = `sumxb' + `sxb' /* sum of exp(xb) */
            matrix `prob'[1,`i'] = `sxb'
            local i = `i' + 1
        }
        scalar `sumxb' = 1/`sumxb'
        matrix `prob'[1,`ncats'] = 1
        matrix `prob' = `sumxb'*`prob'
        matrix `PredPr' = `prob'
        matrix `pdiag' = `PredPr'
        matrix `pdiag' = diag(`pdiag')
        local ncats = e(k_cat)
        local nb = colsof(`b')
        matrix `b' = `b' \ J(1,`nb',0) /* add 0's for last outcome */
        matrix `marg' = `pdiag' * (`b' - (J(`ncats',`ncats',1)*`pdiag'*`b'))
        local nb = colsof(`b') - 1
        matrix `marg' = `marg'[.,1..`nb']
        matrix `marg' = `marg''
    }
    return matrix marginal `marg'

end