*!  version 0.2.0 2005-02-03 jsl
    * pe2 to pe

*   determine if model has a constant in it

capture program drop _penocon
program define _penocon, rclass
    version 8.0

    tempname beta
    matrix `beta' = e(b)
    local nbeta = colsof(`beta')
    local names : colnames `beta'
    local isnocon = 1 - (index("`names'","_cons")!=0)

// binary

    if "`e(cmd)'"=="cloglog"  {
        local isnocon = `isnocon'
    }
    if "`e(cmd)'"=="logistic" {
        local isnocon = `isnocon'
    }
    if "`e(cmd)'"=="logit"    {
        local isnocon = `isnocon'
    }
    if "`e(cmd)'"=="probit"   {
        local isnocon = `isnocon'
    }

// ordered : nocon not allowed

    if "`e(cmd)'"=="ologit"   {
        local isnocon = 0
    }
    if "`e(cmd)'"=="oprobit"  {
        local isnocon = 0
    }
    if "`e(cmd)'"=="gologit"  {
        local isnocon = `isnocon'
    }

// count

    if "`e(cmd)'"=="poisson"  {
        local isnocon = `isnocon'
    }
    if "`e(cmd)'"=="nbreg"    {
        local nrhs = e(df_m)
        local nbeta = `nbeta' - 1 // subtract alpha
        local isnocon = (`nrhs'==`nbeta')
    }

    if "`e(cmd)'"=="zinb"     {
        local nc = index("`names'","_cons")
        * first _cons could be nb or inflate
        local names = substr("`names'",`nc'+1,.)
        local nc = index("`names'","_cons")
        * 2nd cons could be inf or alpha
        local names = substr("`names'",`nc'+1,.)
        local isnocon = 1 - (index("`names'","_cons")!=0)
    }
    if "`e(cmd)'"=="zip"      {
        local nc1 = index("`names'","_cons")
        local names2 = substr("`names'",`nc1'+1,.)
        local isnocon = 1 - (index("`names2'","_cons")!=0)
    }

// regression models

    if "`e(cmd)'"=="regress"  {
        local isnocon = `isnocon'
    }
    if "`e(cmd)'"=="tobit"    {
        local isnocon = `isnocon'
    }
    if "`e(cmd)'"=="cnreg"    {
        local isnocon = `isnocon'
    }
    if "`e(cmd)'"=="fit"      {
        local isnocon = `isnocon'
    }
    if "`e(cmd)'"=="intreg"   {
        local isnocon = `isnocon'
    }

//  nominal

    if "`e(cmd)'"=="mlogit"   {
        _perhs
        local nrhs = r(nrhs)
        local ncatm1 = e(k_cat) - 1
        local isnocon = (`nrhs'*`ncatm1'==`nbeta')
    }

//  return results

    return local nocon  "`isnocon'"

end