*! version 1.1.0  31aug2001
program define streg2, eclass byable(recall)
	version 6
	if replay() {
		if _by() { error 190 }
		syntax [, Dist(string) *]
		if "`dist'"=="" {
			if "`e(cmd2)'" != "streg" { error 301 }
			Display `0'
			exit
		}
	}
	st_is 2 analysis
	syntax [varlist(default=empty)] [if] [in] /*
		*/ [, CLuster(string) CMD Level(integer $S_level) /*
		*/ Dist(string) Robust  FRailty(string) SHared(passthru)/*
		*/ TIme TR noHR noSHow SCore(passthru) *]
	if _by() {
		_byoptnotallowed score() `"`score'"'
	}
	GetCmd `"`dist'"' `"`frailty'"'
	local ecmd `s(cmd)'
	local frailty `s(frailty)'

	GetClass `ecmd'
	local class `s(class)'

	Opt_`class', `time' `tr' `hr'
	local etime `s(etime)'
	local otime `s(otime)'
	local rotime `s(rotime)'

	if `level' != $S_level {
		local otime `"level(`level') `otime'"'
		local rottime `"level(`level') `rotime'"'
	}


					/* obtain info from -st- 	*/
	local t0 "t0(_t0)"
	local d "dead(_d)"
	local id : char _dta[st_id]
	local w  : char _dta[st_w]
	local wt : char _dta[st_wt]

					/* identify estimation subsample */
	tempvar touse
	st_smpl `touse' `"`if'"' "`in'" "`cluster'"
	markout `touse' `varlist'
	if _by() {
		version 7
		local byind "`_byindex'"
		version 6
		qui replace `touse'=0 if `byind'!=_byindex()
		local byind
	}

					/* determine command arguments 	*/

	if `"`wt'"'=="pweight" {
		local robust `"robust"'
	}

	if "`robust'"!="" & "`cluster'"=="" & "`id'"!="" {
		local cluster "`id'"
	}
	if "`cluster'"!="" {
		local cluster "cluster(`cluster')"
	}
	if "`frailty'"!="" {
		local frailty "frailty(`frailty')"
	}


	st_show `show'

	di

	if "`cmd'"!="" {
		di _n in gr `"-> `ecmd' _t `varlist' `w' `if' `in', "' /*
		*/ `"`etime' `otime' `robust' `cluster' `t0' `d' `score'"' /*
		*/ `"`frailty' `shared' `options'"'
		exit
	}
		`ecmd' _t `varlist' `w' if `touse', `frailty'  /*
		*/ `shared' nocoef /*
		*/`etime' `otime' `robust' `cluster' `t0' `d' `score' `options'

	if `class'==2 {
		est local frm2 "hazard"
	}
	else if `class'==3 {
		est local frm2 "time"
	}
	st_hc `touse'
	global S_E_cmd2 streg 		/* Double save */
	est local cmd2 streg
	Display, `rotime'
end


program define Display
	syntax [, noHR TR *]
	if "`e(frm2)'" == "hazard" {
		if "`tr'"!="" {
			di in red "tr not allowed"
			exit 198
		}
		if "`hr'"=="" {
			local hr "hr"
		}
		else	local hr
	}
	else {
		if "`hr'"!="" {
			di in red "nohr not allowed"
			exit 198
		}
	}

	di
	*Form
	Title
	st_hcd
	di
	`e(cmd)', `hr' `tr' `options' nohead
end

program define Title
	if "`e(frm2)'"=="hazard" {
		local h1 "log relative-hazard form"
	}
	else if "`e(frm2)'"=="time" {
		local h1 "accelerated failure-time form"
	}
	if "`e(cmd)'"=="weibull" {
		di in gr "Weibull regression -- `e(title)' "
		exit
	}
	if "`e(cmd)'"=="ereg" {
		di in gr "Exponential regression -- `h1' "
		exit
	}
	if "`e(cmd)'"=="lnormal" {
		di in gr "Log-normal regression -- `h1' "
		exit
	}
	if "`e(cmd)'"=="llogistic" {
		di in gr "Log-logistic regression -- `h1' "
		exit
	}
	if "`e(cmd)'"=="gamma" {
		di in gr "Gamma regression -- `h1' "
		exit
	}
	if "`e(cmd)'"=="gompertz" {
		di in gr "Gompertz regression -- `h1' "
		exit
	}
	if "`e(cmd)'"=="weibullhet2" {
		di in gr "Weibull regression -- `h1' "
		di in gr "                      `e(fr_title)'"
		exit
	}
	if "`e(cmd)'"=="ereghet2" {
		di in gr "Exponential regression -- `h1' "
		di in gr "                          `e(fr_title)'"
		exit
	}
	if "`e(cmd)'"=="lnormalhet2" {
		di in gr "Log-normal regression -- `h1' "
		di in gr "                         `e(fr_title)'"
		exit
	}
	if "`e(cmd)'"=="llogistichet2" {
		di in gr "Log-logistic regression -- `h1' "
		di in gr "                           `e(fr_title)'"
		exit
	}
	if "`e(cmd)'"=="gammahet" {
		di in gr "Gamma regression -- `h1' "
		di in gr "                    `e(fr_title)'"
		exit
	}
	if "`e(cmd)'"=="gompertzhet2" {
		di in gr "Gompertz regression -- `h1' "
		di in gr "                       `e(fr_title)'"
		exit
	}
end

program define GetCmd, sclass
	args dist frailty
	if "`dist'"=="" {
		/* default command */

		if "`e(cmd2)'"=="streg" & "`e(fr_title)'"~="" {
			if "`frailty'"=="" {
				local frailty=lower(substr("`e(fr_title)'",1,1))
			}
			sret local cmd "`e(cmd)'"
			sret local frailty "`frailty'"
			exit
		}

		if "`e(cmd2)'"=="streg" {
			sret local cmd "`e(cmd)'"
			sret local frailty "`frailty'"
			exit
		}
		di in red "must specify distribution -dist()-"
		exit 198
	}

	local l = length("`dist'")
	if substr("exponential",1,max(1,`l')) == "`dist'" {
		if "`frailty'"=="" {
			sret local cmd "ereg"
			sret local frailty "`frailty'"
		}
		else {
			sret local cmd "ereghet2"
			sret local frailty "`frailty'"
		}
		exit
	}
	local l = length("`dist'")
	if substr("ereg",1,max(1,`l')) == "`dist'" {
		if "`frailty'"=="" {
			sret local cmd "ereg"
			sret local frailty "`frailty'"
		}
		else {
			sret local cmd "ereghet2"
			sret local frailty "`frailty'"
		}
		exit
	}
	if substr("weibull",1,max(1,`l')) == "`dist'" {
		if "`frailty'"=="" {
			sret local cmd "weibull"
			sret local frailty "`frailty'"
		}
		else {
			sret local cmd "weibullhet2"
			sret local frailty "`frailty'"
		}
		exit
	}
 	if substr("lognormal",1,max(`l',4)) == "`dist'" | /*
 		*/ substr("lnormal",1,max(`l',2)) == "`dist'" {
		if "`frailty'"=="" {
 			sret local cmd "lnormal"
			sret local frailty "`frailty'"
		}
		else {
 			sret local cmd "lnormalhet2"
			sret local frailty "`frailty'"
		}
 		exit
 	}
 	if substr("loglogistic",1,max(`l',4))  == "`dist'"  | /*
 		*/ substr("llogistic",1,max(`l',2)) == "`dist'"  {
		if "`frailty'"=="" {
			sret local cmd "llogistic"
			sret local frailty "`frailty'"
		}
		else {
			sret local cmd "llogistichet2"
			sret local frailty "`frailty'"
		}
 		exit
 	}
 	if substr("gompertz",1,max(3,`l')) == "`dist'" {
		if "`frailty'"=="" {
			sret local cmd "gompertz"
			sret local frailty "`frailty'"
		}
		else {
			sret local cmd "gompertzhet2"
			sret local frailty "`frailty'"
		}
 		exit
 	}
 	if substr("gamma",1,max(3,`l')) == "`dist'" {
		if "`frailty'"=="" {
			sret local cmd "gamma"
			sret local frailty "`frailty'"
		}
		else {
			sret local cmd "gammahet"
			sret local frailty "`frailty'"
		}
 		exit
 	}
	di in red "unknown distribution dist(`dist')"
	exit 198
end


program define GetClass, sclass
	args cmd
	if "`cmd'"=="ereg" | "`cmd'"=="weibull"  | "`cmd'"=="weibullhet2" /*
		*/  | "`cmd'"=="ereghet2" {
		sret local class 1
		exit
	}
	if "`cmd'"=="gompertz" | "`cmd'"=="gompertzhet2" {
		sret local class 2
		exit
	}
	if "`cmd'"=="lnormal" | "`cmd'"=="llogistic" | "`cmd'"=="gamma" {
		sret local class 3
		exit
	}
	if "`cmd'"=="lnormalhet2" | "`cmd'"=="llogistichet2" | /*
		*/ "`cmd'"=="gammahet" {
		sret local class 3
		exit
	}

	error 301
end

program define Opt_1, sclass
	syntax [, noHR TIme TR]
	if "`time'"!="" | "`tr'"!="" {
		sret local etime /*nothing*/
		sret local otime `tr'
		sret local rotime `tr'
	}
	else {
		sret local etime hazard
		if "`hr'"=="" {
			sret local otime hr
			sret local rotime
		}
		else {
			sret local otime /*nothing*/
			sret local rotime nohr
		}
	}
end

program define Opt_2, sclass
	syntax [, noHR]
	sret local etime /*nothing*/
	if "`hr'"=="" {
		sret local otime hr
		sret local rotime /*nothing*/
	}
	else {
		sret local otime
		sret local rotime nohr
	}
end


program define Opt_3, sclass
	syntax [, TIme TR]
	sret local etime /*nothing*/
	sret local otime `tr'
	sret local rotime `tr'
end


exit

Concerning GetClass returns

        s(class)        1 ln time/ln hazard command
			2 ln hazard command
                        3 ln time command

A class-1 command:
     1)  defaults to estimating results in ln time metric;
         a)  reports coefficients by default
         b)  the -tr- option will report coefficients as ratios
     2)  the -hazard- option will estimate in the ln hazard metric;
         a)  reports coefficients by default
         b)  the -hr- option will report coefficients as ratios;
     3)  A class-1 command fills in e(frm2) with "hazard" or "time"
         depending on metric.
     4)  e(t0) contains t0 variable or 0
Examples are -weibull- and -ereg-

A class-2 command:
    1)  estimates in the ln hazard metric
        a) by default, reports coefficients
        b) the -hr- option will report coefficients as ratios
    2)  A class-2 command does NOT fill in e(frm2)
    3)  e(t0) contains t0 variable or 0
Examples are -gompertz-

A class-3 command:
    1)  estimates in the ln time metric
        a) by default, reports coefficients
        b) the -tr- option will report coefficients as ratios
    2)  A class-3 command does NOT fill in e(frm2)
    3)  e(t0) contains t0 variable or 0
Examples are -lnormal-.

-streg- works like this:

    1)  Estimates in the ln hazard metric if possible.
        a)  reports coefficients as hazard ratios by default
        b)  the -nohr- option will report coefficients

        c)  estimates in ln time metric if option -time- is specified
        d)  reports coefficients by default
        e)  reports ratios if option -tr-

    2)  If only ln hazard is allowed:
        a)  reports coefficients as hazard ratios by default
        b)  the -nohr- option will report coefficients

    3)  If only ln time is allowed:
        a)  may specify option -time- or not; it makes no difference
        d)  reports coefficients by default
        c)  reports ratios if option -tr-

So, options are -nohr-, -time-, and -tr-.  They map like this:

    Class 1:            Est options            Display options
         <none>         hazard                 hr
         nohr           hazard
         time
         tr                                    tr
	 time tr                               tr
    Class 2:
         <none>                                hr
         nohr
    Class 3:
         <none>
         time
         tr                                    tr
         time tr                               tr