*! regeq ver 1.10 29nov2010
*! ver 1.10 modify to woek on models with long equations
*! Postestimation command to -regress-, logit, logistic, st cox
*!
*! Authors: Morten Frydenberg

program regeq
version 9
syntax [, format(string)]

if !inlist(e(cmd),"regress","logit","logistic","cox") {
   display as error "The last estimation should be -regress-,-logit-,-logistic- or cox"
   exit
}

if "`format'" == "" {
   local format = "%12.4f"
}

macro drop b*
tempname zzzmat
matrix `zzzmat'=e(b)
mata: cnames=st_matrixcolstripe("e(b)")
local ncol=colsof(`zzzmat')
local zzzp 0
foreach zzzn of numlist 1/`ncol' {
    mata: st_local("coefname",cnames[`zzzn',2])
    local coef=`zzzmat'[1,`zzzn']
    if abs(`coef')>0.00000001 {
	local zzzp=`zzzp'+1
	local c`zzzp'="`coefname'"
    local b`zzzp'=`coef'
     }
  }
 disp "estimated equation"
 local feq = string(`b`zzzp'',"`format'")+ " "
 local dum=`zzzp'-1

foreach zzz of numlist 1/`dum' {
 if length("`feq'")>50 {
  disp "`feq'" " /// "
  local feq " "
  }
 if `b`zzz''>=0 local feq= "`feq'" + " +"
 local feq= "`feq'" +  string(`b`zzz'',"`format'")+ " * `c`zzz'' "
}
 if length("`feq'")>0   disp "`feq'"

 disp  _newline(1)  "equation"
 local feq = "  b`zzzp' "
foreach zzz of numlist 1/`dum' {
 if length("`feq'")>50 {
  disp "`feq'" " /// "
  local feq " "
  }
 local feq= "`feq'" +  "+ b`zzz' * `c`zzz'' "
}
 if length("`feq'")>0   disp "`feq'"
foreach zzz of numlist 1/`ncol'  {
  global b`zzz'=`b`zzz''
}

end