*! regeq ver 1.00 19feb2010
*! 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.8f"
}

macro drop  eq feq b*
matrix zzzmat=e(b)
local regvar :colnames e(b)
global eq  ""
global feq  ""
local zzzn 0
local zzzp 0
foreach zzz in `regvar' {
    local zzzn=`zzzn'+1
    local coef=zzzmat[1,`zzzn']
    if abs(`coef')>0.00000001 {
	local zzzp=`zzzp'+1
    global b`zzzp'=`coef'
    global feq="$feq" + " + b" + "`zzzp'"+" * `zzz'"
    global eq="$eq"+" + "+string(`coef',"`format'")+" * `zzz'"
    }
  }
 global feq=substr("$feq",2,.)
 global  eq=substr("$eq" ,2,.)
display "$eq"
display "$feq"
end