Skip to content

Commit

Permalink
2-parameter gamma distribution
Browse files Browse the repository at this point in the history
Add 2-paramter gamma distribution for distoutput. Note ggamma and gamma now used for 3 and 2 parameter gamma distributions, respectively.
  • Loading branch information
davidtrueman committed Sep 8, 2022
1 parent 4e7f830 commit 3402af9
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 137 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ For `distoutput`:
### Example use

sysuse cancer.dta, clear
global dlist "gamma weibull gompertz exponential lognormal loglogistic"
global dlist "gamma ggamma weibull gompertz exponential lognormal loglogistic"
distfind age i.drug, dlist($dlist) timevar(studytime) failure(died)
matrix a = e(diags)
matrix list a
Expand Down
4 changes: 2 additions & 2 deletions distanalysis.sthlp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{smcl}
{* *! version 1.2.1 07mar2013}{...}
{* *! version 1.3.0 08Aug2022}{...}
{findalias asfradohelp}{...}
{vieweralsosee "" "--"}{...}
{vieweralsosee "[R] help" "help help"}{...}
Expand Down Expand Up @@ -53,7 +53,7 @@
{dlgtab:Main}

{phang}
{cmd:sdist({it:distribution})} distribution to use. All lowercase. Legal names are "weibull" "gompertz" "gamma" "lognormal" "loglogistic" "exponential".
{cmd:sdist({it:distribution})} distribution to use. All lowercase. Legal names are "weibull" "gompertz" "ggamma" "lognormal" "loglogistic" "exponential". The 2-parameter gamma distribution is not supported (this uses mestreg and the likelihoods are not comparable to those from streg).

{phang}
{cmd:doctitle()} variable defining name of output files, e.g. "myoutput".
Expand Down
45 changes: 25 additions & 20 deletions distfind.ado
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
program define distfind, eclass
version 12.1
version 17.0
syntax [varlist(default=none fv)] [if], dlist(string) timevar(varname) ///
failure(varname) [GRaphs] [SUPpress] [strata(varlist)]

Expand All @@ -10,27 +10,32 @@ if "`graphs'"!="" & "`suppress'"=="" {

*Loop through distributions, estimate AIC/BIC and Cox-snell residuals
foreach d in `dlist' {
qui stset `timevar' `failure'
if "`strata'"!="" {
cap noisily streg `varlist' `if', dist(`d') strata(`strata') iter(100)
if "`d'"=="gamma" {
display as error "Diagnostics not available for 2-parameter gamma. If you wanted the 3-paramter gamma, use 'ggamma'"
}
else {
cap noisily streg `varlist' `if', dist(`d') iter(100)
}
if e(converged) == 1 {
local dlist2="`dlist2' `d'"
estimates store `d'
if "`suppress'"=="" {
predict double cs, csnell
stset, clear
qui stset cs `failure'
qui sts gen km = s `if'
qui gen double H = -ln(km) `if'
qui line H cs cs, sort title(`d') leg(off) ///
ytitle("Cumulative Hazard", size(small) margin(medsmall)) ///
xtitle(Cox-Snell Residuals, size(small) margin(medsmall)) ///
saving(`d', replace) name(`d', replace)
drop cs km H cs
qui stset `timevar' `failure'
if "`strata'"!="" {
cap noisily streg `varlist' `if', dist(`d') strata(`strata') iter(100)
}
else {
cap noisily streg `varlist' `if', dist(`d') iter(100)
}
if e(converged) &_rc==0 == 1 {
local dlist2="`dlist2' `d'"
estimates store `d'
if "`suppress'"=="" {
predict double cs, csnell
stset, clear
qui stset cs `failure'
qui sts gen km = s `if'
qui gen double H = -ln(km) `if'
qui line H cs cs, sort title(`d') leg(off) ///
ytitle("Cumulative Hazard", size(small) margin(medsmall)) ///
xtitle(Cox-Snell Residuals, size(small) margin(medsmall)) ///
saving(`d', replace) name(`d', replace)
drop cs km H cs
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions distfind.sthlp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{smcl}
{* *! version 1.2.1 07mar2013}{...}
{* *! version 1.3.0 08Aug2022}{...}
{findalias asfradohelp}{...}
{vieweralsosee "" "--"}{...}
{vieweralsosee "[R] help" "help help"}{...}
Expand Down Expand Up @@ -56,7 +56,7 @@
{dlgtab:Main}

{phang}
{cmd:dlist({it:distributions})} list of distributions to try. All lowercase. Typically defined in a macro (see example). Legal names are "weibull" "gompertz" "gamma" "lognormal" "loglogistic" "exponential".
{cmd:dlist({it:distributions})} list of distributions to try. All lowercase. Typically defined in a macro (see example). Legal names are "weibull" "gompertz" "ggamma" "lognormal" "loglogistic" "exponential". The two-parameter gamma distribtuion is not supported.

{phang}
{cmd:timevar({it:timevar})} variable defining time-to-event. Used in the same way as {it:stset}
Expand All @@ -72,6 +72,6 @@


{phang}{cmd:. sysuse cancer.dta}{p_end}
{phang}{cmd:. global dlist "gamma weibull gompertz exponential lognormal loglogistic"}{p_end}
{phang}{cmd:. global dlist "ggamma weibull gompertz exponential lognormal loglogistic"}{p_end}
{phang}{cmd:. distfind age i.drug, dlist($dlist) timevar(studytime) failure(died)}{p_end}
{phang}{cmd:. distanalysis age i.drug, sdist(gompertz) doctitle(test) tabcaption("Gompertz")}{p_end}
84 changes: 42 additions & 42 deletions distoutput.ado
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void nozero(Q) {
end

program define distoutput
version 17.0
syntax [varlist(default=none fv)] [if], dlist(string) fname(string) ///
sname(string) [note(string) MODify]

Expand All @@ -45,52 +46,51 @@ putexcel C`trow'="Coef." D`trow'="Std. Err" E`trow'="ll" ///

foreach dist in `dlist' {

if "`dist'"=="gamma" {
local dist="ggamma"
if "`dist'"!="gamma" {
*Run regression and store output
cap noisily streg `varlist' `if', d(`dist') nohr iter(100)
if _rc!=0 {
cap noisily streg `varlist' `if', d(`dist') iter(100)
}
}

*Run regression and store output
cap noisily streg `varlist' `if', d(`dist') nohr iter(100)
if _rc!=0 {
cap noisily streg `varlist' `if', d(`dist') iter(100)
else if "`dist'"=="gamma" {
cap noisily mestreg `varlist' `if', d(`dist') iter(100)
}
if _rc==0 {
if e(converged) == 1 {
matrix A = e(V)
local nvars = rowsof(A) //Includes ommitted variables
matrix B = r(table)'
local rownms: rown B //Row names

*Get variance-covriance matrix without zeros
mata: nozero("A")
if e(converged) == 1 &_rc==0 {
matrix A = e(V)
local nvars = rowsof(A) //Includes ommitted variables
matrix B = r(table)'
local rownms: rown B //Row names

*Get variance-covriance matrix without zeros
mata: nozero("A")

if !missing("`j'") {
local myrow = `myrow' + `j'
}
qui putexcel A`myrow' = ("`dist'")
local j = 0

forvalues i = 1/`nvars' {
if !missing(B[`i', 2]) {
local rowname: word `i' of `rownms'
local mynewrow = `myrow' + `j'
local mycoef = B[`i', 1]
local myse = B[`i', 2]
local myll = B[`i', 5]
local myul = B[`i', 6]

quietly {
putexcel B`mynewrow' = "`rowname'"
putexcel C`mynewrow' = `mycoef'
putexcel D`mynewrow' = `myse'
putexcel E`mynewrow' = `myll'
putexcel F`mynewrow' = `myul'
}
local j = `j' + 1
}
}
qui putexcel G`myrow' = matrix(VarCovar)
if !missing("`j'") {
local myrow = `myrow' + `j'
}
qui putexcel A`myrow' = ("`dist'")
local j = 0

forvalues i = 1/`nvars' {
if !missing(B[`i', 2]) {
local rowname: word `i' of `rownms'
local mynewrow = `myrow' + `j'
local mycoef = B[`i', 1]
local myse = B[`i', 2]
local myll = B[`i', 5]
local myul = B[`i', 6]

quietly {
putexcel B`mynewrow' = "`rowname'"
putexcel C`mynewrow' = `mycoef'
putexcel D`mynewrow' = `myse'
putexcel E`mynewrow' = `myll'
putexcel F`mynewrow' = `myul'
}
local j = `j' + 1
}
}
qui putexcel G`myrow' = matrix(VarCovar)
}
}
putexcel close
Expand Down
2 changes: 1 addition & 1 deletion distoutput.sthlp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{dlgtab:Main}

{phang}
{cmd:dlist({it:distributions})} list of distributions to try. All lowercase. Note use of "ggamma" or "gamma". Typically defined in a macro (see example). Legal names are "weibull" "gompertz" "ggamma"/"gamma" "lognormal" "loglogistic" "exponential".
{cmd:dlist({it:distributions})} list of distributions to try. All lowercase. Note use of "ggamma" vs "gamma" to denote 3-paramter and 2-parameter gamma distributions, respectively. Typically defined in a macro (see example). Legal names are "weibull" "gompertz" "ggamma" "gamma" "lognormal" "loglogistic" "exponential".

{phang}
{cmd:sname({it:doctitle})} Excel file to write reuslts to
Expand Down
Loading

0 comments on commit 3402af9

Please sign in to comment.