/
distplot.ado
205 lines (188 loc) · 5.2 KB
/
distplot.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Copyright (C) 2016 Source Market Access Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
program define distplot
version 17.0
syntax [varlist(default=none)] [if], exrange(real) intrange(real) ///
dlist(string) drugnames(string) [ytitle(string) xtitle(string) ///
mtitle(string) GRaphs]
tokenize `drugnames'
if "`graphs'"!="" {
local dir `c(pwd)'
qui cd "`dir'/graphs"
}
*Store a temporary version of the main dataset
tempfile tempresults
save "`tempresults'"
if "`if'"!=""{
keep `if'
}
tempfile tempresults2
save "`tempresults2'"
*Some checks
local varpres:word count `varlist'
if `varpres'>1 {
di in red "Only one variable can be entered in varlist at present"
exit
}
if !missing("`varlist'") {
qui tab `varlist' `if'
local levels = `r(r)'
if `levels' > 2 {
di in red "Only 2 levels for varlist supported at present"
exit
}
}
else {
local levels = 1
}
if !missing("`varlist'") {
qui sum `varlist' `if'
if `r(max)' > 1 {
di in red "Treatment variable must be coded 0/1"
exit
}
}
if `exrange'<0 {
local foo "The argument 'exrange' is the time (in your unit of analysis"
local foo "`foo' time) over which you would like to see extrapolations,"
local foo "`foo' and must be > 0"
di in red "`foo'"
exit
}
*Fit parametric model(s), save stcurve data using outfile
foreach d in `dlist' {
if "`d'"!="gamma" {
cap noisily streg `varlist' `if', dist(`d') iter(100)
if _rc==0 & e(converged)==1 {
local dlist2 = "`dlist2' `d'"
if `levels'==1 {
stcurve, survival range(0 `exrange') outfile(`d', replace)
local myadd "line surv1 _t if dist==1"
}
else if `levels'==2 {
stcurve, at(`varlist'=0) at(`varlist'=1) survival ///
range(0 `exrange') outfile(`d', replace)
local myadd "line surv1 _t if dist==1 || line surv2 _t if dist==1"
}
}
}
else if "`d'"=="gamma" {
cap noisily mestreg `varlist' `if', dist(`d') iter(100)
if _rc==0 & e(converged)==1 {
local dlist2 = "`dlist2' `d'"
if `levels'==1 {
stcurve, survival outfile(`d', replace)
local myadd "line surv1 _t if dist==1"
}
else if `levels'==2 {
stcurve, at(`varlist'=0) at(`varlist'=1) survival ///
outfile(`d', replace)
local myadd "line surv1 _t if dist==1 || line surv2 _t if dist==1"
}
}
}
}
*Create datset containing all extraploations
local y = 1
foreach d in `dlist2' {
if `y'==1 {
use `d', clear
gen dist=1
}
else if `y' > 1 {
append using `d'
replace dist=`y' if missing(dist)
}
local y=`y'+1
}
save combined, replace
*Overlay extrapolations on Kaplan-Meier
use `tempresults2', clear
gen real=1 `if'
append using combined
local y = 1
local x = 1
*Set up legend and axis titles
if `levels'==2 {
local mylegend = `"1 "`1' - Kaplan-Meier" 2 "`2' - Kaplan-Meier" "'
}
else {
local mylegend = `"1 "`1' - Kaplan-Meier" "'
}
if "`ytitle'"=="" {
local ytitle = "Survival"
}
if "`xtitle'"=="" {
local xtitle = "Time"
}
di "$dlist2"
*Create legend and call to addplot
foreach d in `dlist2' {
if `levels'==2 {
local x = `x' + 2
local z = `x' + 1
local mylegend = `"`mylegend'"' + `" `x' "`1' - `d'" `z' "`2' - `d'" "'
if `y' > 1 {
//if "`if'"=="" {
local myadd "`myadd' || line surv1 _t if dist==`y' || line surv2 _t if dist==`y'"
//}
//else {
// local myadd "`myadd' || line surv1 _t `if' & dist==`y' || line surv2 _t `if' & dist==`y'"
//}
}
}
else {
local x = `x' + 1
local mylegend = `"`mylegend'"' + `" `x' "`1' - `d'" "'
if `y' > 1 {
//if "`if'"=="" {
local myadd "`myadd' || line surv1 _t if dist==`y' "
//}
//else {
// local myadd "`myadd' || line surv1 _t `if' & dist==`y' "
//}
}
}
local y=`y'+1
}
di "`myadd'"
*Create sts graph based on 1/2 variables and whether and if statement
if "`if'"!="" & `levels'== 2 {
sts graph `if' & real==1, by(`varlist') addplot(`myadd', sort ///
legend(order(`"`mylegend'"'))) ytitle("`ytitle'") xtitle("`xtitle'") ///
xla(0 (`intrange') `exrange') title("`mtitle'")
}
else if "`if'"=="" & `levels' == 2 {
sts graph if real==1, by(`varlist') addplot(`myadd', sort ///
legend(order(`"`mylegend'"'))) ytitle("`ytitle'") xtitle("`xtitle'") ///
xla(0 (`intrange') `exrange') title("`mtitle'")
}
else if "`if'"!="" & `levels' == 1 {
sts graph `if' & real==1, addplot(`myadd', sort ///
legend(order(`"`mylegend'"'))) ytitle("`ytitle'") xtitle("`xtitle'") ///
xla(0 (`intrange') `exrange') title("`mtitle'")
}
else if "`if'"=="" & `levels' == 1 {
sts graph if real==1, addplot(`myadd', sort ///
legend(order(`"`mylegend'"'))) ytitle("`ytitle'") xtitle("`xtitle'") ///
xla(0 (`intrange') `exrange') title("`mtitle'")
}
*Restore original dataset
use `tempresults', clear
erase "`tempresults'"
erase "`tempresults2'"
if "`graphs'"!="" {
qui cd "`dir'"
}
end