📄 sts.ado
字号:
if "`adjustf'"!="" {
if length("`adjustf'")>50 {
local adjustf = substr("`adjustf'",1,47)
local adjustf "`adjustf'..."
}
local ttl2 "adjusted for `adjustf'"
}
if "`ylog'" != "" {
local varcnt: word count `svars'
local i 1
while `i' <= `varcnt' {
local varn: word `i' of `svars'
qui replace `varn'=. if `varn'<=0
local i=`i'+1
}
local ysca yscale(log)
}
/*** new by mac *****/
if "`origin'"=="" {
tempvar last flg
local N = _N
if "`by'"=="" & "`strata'"=="" {
qui gen `last'=2 if _n==_N
qui expand `last'
qui gen `flg'=1 if _n>`N'
qui replace _t=0 if `flg'==1
if "`failure'" == "" {
qui replace `s'=1 if `flg'==1
}
else qui replace `s'=0 if `flg'==1
}
else {
sort `by' `strata'
qui by `by' `strata': gen `last'=2 if _n==_N
qui expand `last'
qui gen `flg'=1 if _n>`N'
qui replace _t=0 if `flg'==1
local varcnt: word count `svars'
local i 1
while `i' <= `varcnt' {
local varn: word `i' of `svars'
if "`failure'" == "" {
qui replace `varn'=1*`per' if _t==0
}
else qui replace `varn'=0 if _t==0
local i=`i'+1
}
}
if `"`sttl'"' != "" {
qui replace `sttl' = "" if `flg'==1
}
if "`gwood'"!="" {
qui replace `lb'=. if `flg'==1
qui replace `ub'=. if `flg'==1
}
if "`notreal'"=="" {
if "`lost'" !="" | "`enter'" !="" {
tempvar tempce
qui gen str8 `tempce' = string(`cens')
qui replace `tempce'="" if `flg'==1
qui drop `cens'
qui rename `tempce' `cens'
qui replace `cens'=trim(`cens')
}
}
}
if "`na'"~="" | "`origin'"~="" {
tempvar flg
qui gen int `flg'=.
}
if `"`censt'"'== "numbered" | `"`censt'"'=="single" {
tempvar tmvars tsym expw tu nextt mins
qui gen double `mins'=1-`s'
sort `mins' _t
qui gen double `nextt'=_t[_n+1]
qui by `mins': replace `nextt'=`nextt'[_N]
qui sum _t, meanonly
local adjd=(r(max)-r(min))/450
qui gen int `expw'=2 if `cens'>0 & `cens'<. & `flg'>=.
local N=_N
qui expand `expw'
qui replace `expw'=cond(_n>`N',2,.)
sort _t `s' `expw'
qui by _t: replace _t=_t+`adjd' if `expw'==2 & _n==1 & /*
*/ _t+`adjd'<`nextt'
qui gen double `tmvars'=`s' if `expw'==2
format `tmvars' %9.2f
label var `tmvars' "Censored"
if "`na'"=="" & "`cna'"=="" {
qui gen `tu'=`tmvars'+.02 if `expw'==2
}
else {
noi sum `s', meanonly
qui gen `tu'=`tmvars'+ (r(max)-r(min))/45 if `expw'==2
}
label var `tu' "Censored"
local tmvars="`tmvars' `tu'"
if "`sttl'"!="" {
qui replace `sttl'="" if `expw'==2
}
if `"`censt'"'=="numbered" {
local N=_N
qui expand `expw'
qui replace `expw'=cond(_n>`N',2,.)
qui replace `s'=. if `expw'==2
qui replace `cens'=. if `expw'~=2
local ctgraph ///
(scatter `tu' _t, ///
sort ///
connect(none) ///
msymbol(none) ///
mlabel(`cens') ///
mlabpos(12) ///
pstyle(p1) ///
`ysca' ///
)
if "`sttl'"!="" {
qui replace `sttl'="" if `expw'==2
}
}
local ctgraph ///
(rspike `tmvars' _t, ///
sort ///
`ysca' ///
) ///
`ctgraph' ///
// blank
}
else if `"`censt'"'=="multiple" {
tempvar tmvars tsym expw tu nextt mins
qui gen double `mins'=1-`s'
sort `mins' _t
qui gen double `nextt'=_t[_n+1]
qui by `mins': replace `nextt'=`nextt'[_N]
qui sum _t, meanonly
local adjd=(r(max)-r(min))/350
qui gen int `expw'=`cens'+1 if `cens'>0 & `cens'<. & `flg'>=.
local N=_N
qui expand `expw'
qui replace `expw'=cond(_n>`N',2,.)
qui gen double `tmvars'=`s' if `expw'==2
sort _t `s' `expw'
tempvar move ttime
qui by _t: gen int `move'=1 if _t+`adjd'*_n<=`nextt' /*
*/ & `expw'==2
qui by _t: replace `move'=2 if `move'>=. & /*
*/ _t-`adjd'*_n>=_t[1] & `expw'==2
qui by _t: gen double `ttime'= _t+`adjd'*_n if `move'==1
qui by _t: replace `ttime'= _t-`adjd'*_n if `move'==2
qui replace _t= `ttime' if `ttime'<.
drop `ttime' `move'
sort `s' _t `expw'
format `tmvars' %9.2f
if "`na'"=="" & "`cna'"=="" {
qui gen `tu'=`tmvars'+.02 if `expw'==2
}
else {
noi sum `s', meanonly
qui gen `tu'=`tmvars'+ (r(max)-r(min))/45 if `expw'==2
}
label var `tmvars' "Censored"
local tmvars="`tmvars' `tu'"
label var `tu' "Censored"
if "`sttl'"!="" {
qui replace `sttl'="" if `expw'==2
}
local ctgraph ///
(rspike `tmvars' _t, ///
sort ///
`ysca' ///
) ///
// blank
}
if `"`cigraph'"' == "" {
local lb
local ub
}
local nv : word count `svars' `lb'
if `nv' > 1 {
numlist "1/`nv'"
local legend legend(order(`r(numlist)'))
}
else if `"`plot'`addplot'"' == "" {
local legend legend(nodraw)
}
if `"`separat'"' != "" {
if "`sb'"!="" {
local byopt `"by(`sb' , \`byann' `byopts')"'
}
}
if `"`byopt'"' != "" {
if `"`byttl'"' == "" {
local byttl `"title(`"`ttl'"' `"`ttl2'"')"'
}
if `"`bylgnd'"' == "" {
local bylgnd `"`legend'"'
local legend
}
local byann `"`byttl' `bylgnd'"'
}
else {
local title `"title(`"`ttl'"' `"`ttl2'"')"'
}
if `"`plot'`addplot'"' != "" {
local draw nodraw
}
local nvars : word count `svars'
local nvars = min(`nvars',15)
forval i = 1/`nvars' {
local pseries `"`pseries' p`i'line"'
}
quietly replace `s' = `s'*`per'
if `"`tmin'"' != "" {
foreach svar of local svars {
qui replace `svar' = . if _t < `tmin'
}
}
version 8: graph twoway ///
`cigraph' ///
(line `svars' _t, ///
sort ///
`conopt' ///
`ylabopt' ///
ytitle(`""') ///
`ysca' ///
xtitle(`"analysis time"') ///
pstyle(`pseries') ///
`title' ///
`legend' ///
`draw' ///
`byopt' ///
`options' ///
) ///
`mgraph' /// labels censored, lost
`ctgraph' /// censor ticks
// blank
if `"`plot'`addplot'"' != "" {
restore
version 8: graph addplot `plot' || `addplot' || , norescaling
}
end
program define DoAdjust /* by strata adjustf if in -> haz s */
local by "`1'" /* optional */
local strata "`2'" /* optional */
local adjustf "`3'" /* required */
local if "`4'" /* optional */
local in "`5'" /* optional */
/* */
local haz "`7'" /* optional */
local s "`8'" /* optional */
if "`strata'"!="" {
local stopt "strata(`strata')"
}
if "`by'"=="" {
if "`s'" != "" {
local sopt "bases(`s')"
}
if "`haz'" != "" {
local hopt "basehc(`haz')"
}
stcox `adjustf' `if' `in', /*
*/ `stopt' `sopt' `hopt' estimate norobust
exit
}
if "`haz'" != "" {
tempvar hi
local hopt "basehc(`hi')"
gen double `haz' = .
}
if "`s'" != "" {
tempvar si
local sopt "bases(`si')"
gen double `s' = .
}
if !(`"`if'"'=="" & "`in'"=="") {
tempvar touse
mark `touse' `if' `in'
local cond "if `touse'"
local andcond "& `touse'"
}
tempvar grp
sort `touse' `by'
by `touse' `by': gen int `grp' = 1 if _n==1 `andcond'
replace `grp'=sum(`grp') `cond'
local ng = `grp'[_N]
local i 1
while `i' <= `ng' {
capture stcox `adjustf' if `grp'==`i', /*
*/ `stopt' `sopt' `hopt' estimate norobust
if _rc==0 {
if "`hopt'"!="" {
replace `haz' = `hi' if `grp'==`i'
drop `hi'
}
if "`sopt'"!="" {
replace `s' = `si' if `grp'==`i'
drop `si'
}
}
local i = `i' + 1
}
end
program define KeepDead /* strata */
args strata
local d : char _dta[st_d]
if `"`_dta[st_d]'"'!="" {
/* keep if `_dta[st_d]' */
drop `_dta[st_d]'
}
sort `strata' _t
by `strata' _t: keep if _n==1
end
program define MarkPt /* t strata s -> ttl s2 */
args t strata s ARROW ttl s2
quietly {
tempvar mark marksum ls
summarize `t', mean
local tval = r(min) + (r(max)-r(min))*2/3
gen byte `mark' = cond(`t'<`tval', 1, 0)
by `strata': replace `mark'=0 if `mark'[_n+1]==1
by `strata': gen byte `marksum' = sum(`mark')
by `strata': replace `mark'=1 if _n==_N & `marksum'==0
drop `marksum'
summarize `s', mean
local eps = 0 // this use to be "= max( (r(max)-r(min))/20, 0)"
gen float `ls' = `s'
by `strata': replace `ls' = `ls'[_n-1] if `ls'>=.
gen float `s2' = `ls'+`eps' if `mark'
replace `s2' = `ls'[_n-1]+`eps' if `mark' & `strata'==`strata'[_n-1]
summarize `s2', mean
capture confirm string variable `strata'
if _rc {
gen str20 `ttl' = "`strata' " + trim(string(`strata')) if `mark'
local lab : value label `strata'
if "`lab'" != "" {
tempvar delab
decode `strata', gen(`delab') maxlen(20)
replace `ttl' = `delab' if `mark'
}
}
else gen str20 `ttl' = trim(`strata') if `mark'
compress `ttl'
} // quietly
end
program define ByStAdj, sclass
args by strata adjustf
sret clear
if "`strata'"!="" {
if "`adjustf'"=="" {
di in red /*
*/ "strata() requires adjustfor(); perhaps you mean by()"
exit 198
}
}
if !("`by'"=="" & "`strata'"=="") {
if "`by'"!="" & "`strata'"!="" {
sret local sb "`by' `strata'"
}
else if "`by'"!="" {
sret local sb "`by'"
}
else sret local sb "`strata'"
}
end
program define GetGroupLabel, rclass
syntax varlist [if] , id(varname)
sum `id' `if', mean
local n = r(min)
foreach var of local varlist {
cap confirm numeric var `var'
if _rc { // string variable
local ll = substr(`var'[`n'],1,20)
}
else { // numeric variable
sum `var' `if', mean
local ll `"`var' = `: label (`var') `=r(min)''"'
}
local lab `"`lab'`sep'`ll'"'
local sep "/"
}
return local label `"`lab'"'
end
program define SmoothHazard
args time deltah deltaVh kernel width tmin tmax sb ub lb level
local by `"`sb'"'
drop if `deltah' == 0 | `deltah' == .
tempvar tvar group id
if `"`width'"' != "" {
foreach j of local width {
if "`j'"!="." {
confirm number `j'
}
}
}
if "`by'" != "" {
qui egen `group' = group(`by')
qui summarize `group', meanonly
local ngroup = r(max)
qui gen `id' = _n
}
else {
qui gen `group' = `time'<.
local ngroup = 1
}
if _N < 101 {
set obs 101
}
qui summarize `time'
if `tmin' == -1 {
local tmin = r(min)
}
if `tmax' == -1 {
local tmax = r(max)
}
qui gen `tvar' = `tmin' + (_n-1)/100*(`tmax'-`tmin') in 1/101
forvalues i = 1/`ngroup' {
local w : word `i' of `width'
if `"`w'"' != "" {
if `"`w'"' == "." {
local wopt
}
else {
local wopt width(`w')
}
}
else {
local wopt
}
version 8: qui kdensity `time' [iw=`deltah'] ///
if `group'==`i', ///
`kernel' `wopt' ///
gen(__y`i') at(`tvar') ///
nograph
qui summ `time' if `group'==`i'
qui replace __y`i'=. if `tvar'<r(min) | `tvar'>r(max)
qui gen __yy`i' = .
if `"`wopt'"' == "" {
quietly summ `time' if ///
`group'==`i', detail
local wid2 = min(r(sd), (r(p75)-r(p25))/1.349)
if `wid2' <= 0.0 {
local wid2 = r(sd)
}
local wid2 = 0.9*`wid2'/(r(N)^.20)
local wopt width(`wid2')
}
di `" --wopt is--`wopt'"'
_KDE2 `time' [iw=`deltaVh'] if `group'==`i', `kernel' ///
at(`tvar') kde(__yy`i') `wopt'
if "`by'"!="" {
qui summ `id' if `group'==`i'
local index r(min)
version 8: qui gen __by`i' = `by'[`index'] in 1/101
}
local z = invnorm(1-(1-`level'/100)/2)
quietly generate __lb`i'= __y`i'* ///
exp(-`z'*sqrt(__yy`i')/__y`i') if __y`i'<.
quietly generate __ub`i' = __y`i'* ///
exp(`z'*sqrt(__yy`i')/__y`i') if __y`i'<.
}
keep in 1/101
if "`by'"!="" {
qui replace `id' = _n
reshape long __y __yy __ub __lb __by, i(`id')
}
drop `time' `deltah' `deltaVh'
if "`by'"!="" {
drop `by'
rename __y `deltah'
rename __yy `deltaVh'
rename __by `by'
rename __ub `ub'
rename __lb `lb'
}
else {
rename __y1 `deltah'
rename __yy1 `deltaVh'
rename __ub `ub'
rename __lb `lb'
}
rename `tvar' `time'
end
program define ForbidOpt
if `"`1'"'!="" {
di in red `"`1' not allowed with hazard plots"'
exit 198
}
end
exit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -