*! Calculates life tables and related graphs: v1.00 August 2000 (STB-59: ssa14) *! In order to analyse specific causes of death this adofile *! calls lifetabs.ado, another sub-routine which must always *! be kept together with this one. *! Author: Carlos Ramalheira, cramal@ci.uc.pt *! Coimbra Faculty of Medicine & University Hospital cap program drop lifetabl program define lifetabl, nclass version 6.0 syntax [varlist] [if] [in] , Strata(varname) [ Rates(varname) Multiplier(string) /* */ Keep Deaths(varname) Pop(varname) BY(varname) RADIX(int 100000) /* */ Weights(numlist min=1 max=4 >0 <1) Label(varname) NOT SAVing(string) /* */ NYears(varname) GE GP GS GH GSC GRPHS SClist(varlist min=1 max=20) REPLACE /* */ LOGrate NOO ALLRx SCOnly PYLL(numlist >20 <=100 max=1) ALLSCT /* */ LEVel(numlist >20 <=100 max=1) CI NOYLL * ] tokenize "`varlist'" tempvar touse mark `touse' `if' `in' set display line 100 tokenize "`options'" if "`level'"=="" { local level = "95" } if "`level'"~="" { global D_level = "`level'" } local zscore = -invnorm( (1-($D_level/100))/2 ) if "`pyll'"=="" { local pyll = "65" } if "`sconly'"~="" { local noo = "yes" local not = "yes" } if "`saving'"~="" { local end = lower(substr("`saving'",-4,4)) local str = length("`saving'") if "`end'"==".dta" { local last = `str' - 4 local saving = lower(substr("`saving'",1,`last')) local str = `last' } if `str'>8 { di in re " Error: Saving file name bigger than 8 characters" exit 499 } local keep = "keep" } if "`keep'"=="" { local keep = "keep" local fake = "YES" } local namef = "$S_FN" if "`keep'"~="" { qui cap count if `touse'~=1 local n = r(N) if `n'>0 { preserve qui cap drop if `touse'==1 tempfile _F0 qui save `_F0' , replace restore local fsaved = "`_F0'" qui cap drop if `touse'~=1 } else { local fsaved = " " } } cap conf v `strata' local A = "`strata'" if _rc~=0 { di in re " Error: variable `strata', describing strata" /* */ " does not exist or abbreviation is invalid" qui use "`namef'" , clear error 499 } if "`by'"~="" { tempvar h qui egen `h' = group(`strata' `by') if `touse'==1 sort `h' cap by `h' : assert _N==1 if _rc==0 { } else { local Dups = "yes" } } if "`by'"=="" { sort `strata' cap by `strata' : assert _N==1 if _rc==0 { } else { local Dups = "yes" } } if "`Dups'"~="" { di in re " Error: Invalid duplicate or missing values detected in variable `A'." di in re " Check variable `A' coding, or your IF, IN or BY( ) options." qui use "`namef'" , clear error 499 } cap conf numeric v `strata' if _rc==0 { tempvar z qui ge `z'=1 if `strata'==. & `touse'==1 qui count if `z'==1 local n = r(N) if `n'>=1 { di in re " Error: invalid missing values detected in variable `A'" di in re " Check variable `A' coding, or your IF, IN or BY( ) options." qui use "`namef'" , clear error 499 } } cap conf string v `strata' if _rc==0 { local label = "`strata'" global lbl = "`strata'" tempvar z qui ge `z'=1 if `strata'=="" qui count if `z'==1 local n = r(N) if `n'>=1 { di in re " Error: invalid nul values detected in variable `A'" qui use "`namef'" , clear error 499 } } if "`nyears'"~="" { cap conf numeric v `nyears' if _rc==0 { qui su `nyears' if r(min)<1 { di in re " Error: non integer or negative values in variable `nyears'" qui use "`namef'" , clear error 499 } if r(min)~=r(max) | r(min)~=1 { local yearmsg = "yes" } } } else { tempvar nyears qui ge `nyears' = 1 local nyears = "`nyears'" } local numvars : word count `sclist' if `numvars'==1 { if "`deaths'" == "" { di in re " Error: with only one SClist( ) var you must always specify Deaths( )." exit 499 } else { qui cap drop _OtherSC qui gen _OtherSC = `deaths'-`sclist' qui label var _OtherSC "Other causes of death" local sclist = "`sclist' " + "_OtherSC" } } if "`sclist'"~="" & "`pop'"=="" & "`rates'"=="" { if "`allrx'"=="" { di in gr " " di in re " Error: with option SClist( ) you must always specify Pop( ) AND/OR Rates( )." di in gr " " di in gr " Available alternatives: SClist( ) AND Pop( )" di in gr " SClist( ) AND Rates( )" di in gr " SClist( ) AND Pop( ) AND ALLRx" di in gr " SClist( ) AND Rates( ) AND ALLRx" di in gr " " di in gr " > Some of these combinations imply that SClist( ) vars encode a full" di in gr " set (i.e., an exaustive set) of strata- and cause- specific rates." di in gr " " di in gr " -> When Deaths( ) is not declared together with Pop( ) the population" di in gr " total number of whithin strata casualties is estimated by adding" di in gr " (through strata rows) the fatalities coded by SClist( ) vars." di in gr " -> When Rates( ) are not declared with Pop( ) and ALLRx the population" di in gr " whithin strata rates are estimated by adding (through strata rows)" di in gr " the rates declared by means of the SClist( ) vars." di in gr " " di in gr " ->> So, it is the responsibility of the user to assure that the total" di in gr " deaths (or rates) by all causes are adequately described by the" di in gr " particular set of SClist( ) vars included in the command." di in gr " " qui use "`namef'" , clear error 499 } } if "`rates'"=="" { local R = "" if "`sclist'"=="" { if "`pop'"~="" & "`deaths'"~="" { local P = "`pop'" local D = "`deaths'" } if "`pop'"=="" | "`deaths'"=="" { di in re " Error: you must specify option Rates( ) OR, in alternative," di in re " Pop( ) AND Deaths( )" error 499 } if "`allrx'"~="" { di in blu " " di in blu " Info: option ALLRx is only relevant when used with SClist( )." } } if "`sclist'"~="" { if "`pop'"~="" & "`deaths'"~="" { local P = "`pop'" local D = "`deaths'" } if "`pop'"~="" & "`deaths'"=="" { local P = "`pop'" if "`allrx'"=="" { di in blu " " di in blu " Warning: the total number of deaths whithin strata were estimated by means" di in blu " of a (row)sum of all the SClist( ) vars. Is this valid? If not," di in blu " results are incorrect." tempvar deaths qui egen `deaths' = rsum(`sclist') local D = "`deaths'" } if "`allrx'"~="" { di in blu " " di in blu " Warning: the whithin strata rates -- Rates( ) -- were estimated by means" di in blu " of a (row)sum of all the SClist( ) vars. Is this valid? If not," di in blu " results are incorrect." tempvar rates deaths qui egen `rates' = rsum(`sclist') local R = "`rates'" local passD = "yes" } } if "`pop'"=="" { di in re " Error: you must also specify Pop( ) AND/OR Rates( )." qui use "`namef'" , clear error 499 } } qui cap conf v `P' if _rc~=0 { di in re " Error: Pop( ) variable does not exist or abbreviation is invalid" qui use "`namef'" , clear error 499 } local z 1 while `z'<=_N { if `P'[`z']==. { di in blu " " di in blu " Warning: At least one level was dropped because `P' was missing" di in blu " Verify your data: results may be compromised..." local z = _N } local z = `z' + 1 } qui replace `touse'=0 if `P'==. qui cap drop if `touse'==0 qui su `P' if `touse'==1 , meanonly if r(min)==r(max) { local mess = "yes" } if "`passD'" =="" { qui cap conf v `D' if _rc~=0 { di in re " Error: Deaths( ) variable does not exist or abbreviation is invalid" qui use "`namef'" , clear error 499 } local z 1 while `z'<=_N { if `D'[`z']==. { di in blu " " di in blu " Warning: At least one level was dropped because `D' was missing" di in blu " Verify your data: results may be compromised..." local z = _N } local z = `z' + 1 } qui replace `touse'=0 if `D'==. qui cap drop if `touse'==0 qui su `D' if `touse'==1 , meanonly if r(min)==r(max) { local mess = "yes" } } if "`mess'"~="" { di in red " Error: no observations satisfy conditions (due to p( ) or d( ) missing values)" exit 499 } } if "`rates'"~="" { local R = "`rates'" if "`sclist'"=="" { if "`pop'"~="" | "`deaths'"~="" { di in blu " " di in blu " Info: option Pop( ), and/or option Deaths( ), not necessary." } if "`allrx'"~="" { di in blu " " di in blu " Info: option ALLRx is only relevant when used with SClist( )." } } if "`sclist'"~="" { if "`deaths'"~="" { local D = "`deaths'" } if "`pop'"~="" { local P = "`pop'" } } cap conf numeric v `R' if _rc~=0 { di in re " Error: variable `rates', in option Rates( ), does not " /* */ "exist or abbreviation is invalid" error 499 } local z 1 while `z'<=_N { if `R'[`z']==. { di in blu " " di in blu " Warning: At least one level was dropped because `R' was missing" di in blu " Verify your data: results may be compromised..." local z = _N } local z = `z' + 1 } qui replace `touse'=0 if `R'==. qui cap drop if `touse'==0 qui su `R' if `touse'==1 , meanonly if r(min)==r(max) { di in red " Error: no observations satisfy conditions (due to Rates( ) missing values)." qui use "`namef'" , clear exit 499 } } quietly { if "`by'"~="" { cap conf numeric v `by' if _rc ~= 0 { di in re " Error: option by( ) variable (`by') is not numeric" qui use "`namef'" , clear exit 499 } su `by' if `touse'==1 , meanonly if r(N) == 0 { noisily error 2000 } if r(min) == r(max) { noi di in blu " " noi di in blu " Warning: Only 1 by( ) group was found where 2 or more were expected." noi di in blu " Please verify if your by( ), if or in options are appropriate." } qui tab `by' if `touse'==1 local nrows = r(r) tempvar g egen `g' = group(`by') if `touse'==1 label var `g' "`by' groups" summarize `by' if `touse'==1 & `g'==1 , meanonly local n = r(N) local x 1 while `x'<=`nrows' { su `by' if `touse'==1 & `g'==`x' , meanonly if `n'~=r(N) { di in red " Error: by( ) groups are not similarly sized: " /* */ "the number of strata must be equal" qui use "`namef'" , clear exit 499 } local x = `x' + 1 } } else { local nrows = 1 } qui cap drop if `touse'==0 } if "`label'"~="" { cap conf string v `label' if _rc~=0 { di in re " Error: var `label', to label strata levels, must be string type" qui use "`namef'" , clear error 499 } tempvar Labv qui ge str7 `Labv' = substr(`label',1,7) qui label var `Labv' "`label'" local labv : variable label `Labv' } local WW = "`weights'" if "`WW'"=="" & "`yearmsg'"=="" { local w1 = "0.1134" /* old: "0.1134" */ local w2 = "0.43" local w3 = "0.45" local w4 = "0.47" local WW = "0.1134 0.43 0.45 0.47" /* old: "0.1134 0.43 0.45 0.47" */ } else { local nargs : word count `weights' tokenize "`weights'" , parse(", ") local s 1 while `s' <= 4 { if "``s''" ~= "" { local w`s' = "``s''" } else { if `s'== 1 { local w`s' = 0.5 } if `s'== 2 { local w`s' = 0.5 } if `s'== 3 { local w`s' = 0.5 } if `s'== 4 { local w`s' = 0.5 } } local s = `s' + 1 } local WW = "`w1' `w2' `w3' `w4'" } local radx = "`radix'" if "`grphs'" ~= "" { local ge = "ge" local gp = "gp" global gsc = "gsc" local gs = "gs" local gh = "gh" } local baseR = "`multiplier'" if "`baseR'"~="" & "`rates'"=="" { di in re "Option multiplier( ) may only be used when option Rates( ) is also specified" qui use "`namef'" , clear error 499 } if "`baseR'"=="" { local baseR = 100000 } if `baseR'==1 | `baseR'==10 | `baseR'==100 | `baseR'==1000 | `baseR'==10000 | /* */ `baseR'==100000 | `baseR'==1000000 { if `baseR' == 1 { local factor = 100000 } if `baseR' == 10 { local factor = 10000 } if `baseR' == 100 { local factor = 1000 } if `baseR' == 1000 { local factor = 100 } if `baseR' == 10000 { local factor = 10 } if `baseR' == 100000 { local factor = 1 } if `baseR' == 1000000 { local factor = 0.1 } } else { di in re " Error: Allowed multipliers are 1, 10, 100, 1000, 10000, 100000 " /* */ "(default), 1000000" qui use "`namef'" , clear error 499 } qui cap drop _Rx if "`R'"=="" { qui ge _Rx = ((`D'/`P')*100000) } else { qui ge _Rx = `R'*`factor' } label var _Rx "LT Mortality Rate (by 10^5)" * CICLO DE GERAÇÃO DA TABELA VITAL * -------------------------------- * Ciclo de grupos AgeStrata * ------------------------- local c 1 while `c'<=`nrows' { preserve if "`by'"~="" { qui cap drop `g' qui egen `g' = group(`by') if `touse'==1 qui su `by' if `g'==`c' & `touse'==1 local lab = r(min) qui replace `touse' = 0 if `touse'==1 & `g'~=`c' qui count if `touse'==1 if r(N)==0 { restore local c = `c' + 1 } if "`noo'"=="" { if `c'>1 { di in gr " " } di in gr " " if "`sclist'"=="" { di in gr " Global Life Table" } else { di in gr " Global Life Table (all causes of death)" } di in gr " Results for group: `by' = " in ye `lab' } if `nrows'>=1 { qui replace `touse' = 0 if `touse'==1 & `g'~=`c' } qui cap drop if `touse'==0 } qui cap drop `NN' tempvar NN qui sort `touse' `by' `A' qui cap drop if `touse'==0 qui gen byte `NN' = _n if `touse'==1 format `NN' %2.0f qui cap drop _qx qui cap drop _px qui cap drop _dx qui cap drop _lx qui cap drop _Lx qui cap drop _Tx qui cap drop _ExpYL qui cap drop _Surv qui cap drop _HRate sort `NN' tempvar g qui egen `g' = group(`NN') local max = `g'[_N] tempvar s qui ge `s' = 0.5 qui replace `s' = `w1' if `g'==1 qui replace `s' = `w2' if `g'==2 qui replace `s' = `w3' if `g'==3 qui replace `s' = `w4' if `g'==4 qui replace `s' = 1 if `g'==`max' qui ge _qx = . label var _qx "Prob of dying in interval" qui ge _dx = . label var _dx "LT Expected deaths in interval" qui ge _px = . label var _px "Prob of surviving the interval" qui ge _lx = . label var _lx "LT Number people alive at x" qui ge _Lx = . label var _Lx "LT Stationary population" qui ge _Tx = . label var _Tx "LT Total time lived beyond x" qui ge _ExpYL = . label var _ExpYL "Expected years of life at x" * label var _ExpYL "E(years of life) at age x" * Beginning of life table construction * ----------------------------------- qui replace _lx = `radx' if `g'==1 qui replace _qx = ((_Rx/100000)*`nyears')/(1+(`s'*`nyears'*(_Rx/100000))) if `g'==1 qui replace _px = 1 - _qx if `g'==1 qui replace _Lx = (_lx - (_qx*_lx)) + ((_qx*_lx)*`s'[1]) if `g'==1 qui replace _dx = _Lx*(_Rx/100000) if `g'==1 * Alternative Defs * qui replace _qx = (_dx*`nyears')/(_Lx+(`s'*`nyears'*_dx)) if `g'==1 * qui replace _dx = _lx * _qx if `g'==1 * qui replace _qx = ((_Rx/100000) * `nyears') / (1+ ((`s' * `nyears') * (_Rx/100000))) * qui replace _qx = _dx / _lx if `g'==1 /* old - complete tables */ local z 2 while `z'<=`max'{ qui replace _qx = ((_Rx/100000)*`nyears')/(1+(`s'[`z']*`nyears'*(_Rx/100000))) if `g'==`z' qui replace _lx = _lx[_n-1] - _dx[_n-1] if `g'==`z' qui replace _dx = _lx*_qx if `g'==`z' qui replace _Lx = _lx - _dx + (_dx*`s'[`z']) if `g'==`z' qui replace _px = 1 - _qx if `g'==`z' local z = `z' + 1 } qui replace _qx = 1 if `g'==`max' qui replace _px = 0 if `g'==`max' qui replace _Lx = ((_lx*`nyears')/(_Rx/100000)) if `g'==`max' format _Rx %7.1f format _lx %8.0f format _Lx %8.0f format _dx %8.0f format _qx _px %9.5f tempvar zz qui gen `zz' = _Lx*`nyears' qui replace `zz' = sum(`zz') qui replace _Tx =. qui replace _Tx = `zz'[_N] if `g'==1 qui format _Tx %6.0f qui replace `zz' = _Lx*`nyears' local w 2 while `w'<=`max' { local f = `w' - 1 qui replace `zz' =. in 1/`f' qui replace `zz' = sum(`zz') qui replace _Tx = `zz'[_N] in `w' qui replace `zz' = _Lx*`nyears' local w = `w' + 1 } qui replace _Tx = _Lx if `g'==`max' qui replace _dx = _lx if `g'==`max' * l deaths _dx _qx _Tx _lx _Lx * qui replace _ExpYL = _Tx/(_lx) qui replace _ExpYL = 1/(_lx/_Tx) format _ExpYL %7.2f sort `NN' qui gen _Surv =. qui replace _Surv = _lx/`radix' label var _Surv "Survival probability" qui cap drop _SurvVar qui gen _SurvVar = _qx*(_Surv*_Surv)*(1-_Surv) qui format _SurvVar %6.5f label var _SurvVar "Greenwood variance for _Surv" tempvar low hig se qui gen `se' = sqrt(_SurvVar) qui gen `low' = _Surv -(`zscore'*`se') qui gen `hig' = _Surv +(`zscore'*`se') * l `low' `hig' qui replace `low' = 1 if `low'==. qui replace `hig' = 1 if `hig'==. qui replace `low' = 0 if `low'<0 qui replace `hig' = 1 if `hig'>1 qui format `low' %3.2f qui format `hig' %3.2f * gr `low' `hig' _Surv `linf' , c(IIl) s(..o) sort xlab(0(5)90) ylab(0(.1)1) if "`noo'"=="" { if "`not'"=="" { di in gr _dup(81) " " di in gr _col(1) _dup(34)"-" in gr " LIFE TABLE " in gr _dup(35) "-" di in gr _col(3) "Radix: " in ye `radx' di in gr _col(3) "Nr of age strata: " in ye `max' di in gr _col(3) "Weights (first 4 strata): " in ye %5.4g `w1' in gr "," /* */ in ye %5.4g `w2' in gr "," in ye %5.4g `w3' in gr "," in ye %5.4g `w4' /* */ in gr _col(59) "Rx multiplier: " in ye `baseR'*`factor' di in gr _dup(81) "-" di in gr _col(3) "Strata | Rx qx dx lx" /* */ " Lx Ex Sx" di in gr _dup(81) "-" qui compress `NN' local x 1 while `x'<=`max' { if "`label'"=="" { local str = `NN' in `x' local len = 6-length("`str'") } if "`label'"~="" { local str = `Labv' in `x' local len = 8-length("`str'") } local rx = _Rx in `x' local qx = _qx in `x' local dx = _dx in `x' local lx = _lx in `x' local Lx = _Lx in `x' local ex = _ExpYL in `x' local Sx = _Surv in `x' di in ye _dup(`len') " " "`str'" in gr _col(10) "| " in ye %7.1f `rx' /* */ " " %6.5f `qx' " " %6.0f `dx' /* */ " " %8.0f `lx' " " %8.0f `Lx' " " %5.2f `ex' " " %4.3f `Sx' local x = `x' + 1 } * list `strata' _Rx _qx _dx _lx _Lx _Tx _ExpYL , nodisp noobs nol } /* end of if not... */ if "`not'"~="" { di in gr _dup(81) " " if "`by'"=="" { di in gr " Global statistics (all causes of death)" } } di in gr _dup(81) "-" local CMR = _ExpYL in 1 di in gr " Life Expectancy at birth : " in ye %5.3f `CMR' in gr _col(55) "(years)" global LExp_`c' = `CMR' local CMR = (1/`CMR')*1000 global CrMR_`c' = `CMR' di in gr " Crude Mortality Rate : " in ye %6.3f `CMR' in gr _col(55) "(deaths by 10^3 persons)" di in gr _dup(79) " " } /* end of if noo... */ * ROTINA DE GRAFICOS SIMPLES if `c'==1 { if "`rates'"=="" { local name = "`deaths'" } if "`rates'"~="" { local name = "`rates'" } tempvar pm pmx qui ge `pm' = sum(`nyears') qui ge `pmx' = `pm' qui replace `pm' = `pm'/2 if _n==1 qui replace `pm' = (`pm'+`pmx'[_n-1])/2 if _n>=2 & _n<_N qui replace `pm' = `pm'-`nyears' if _n==_N tempvar linf qui gen `linf' = 0 if _n==1 qui replace `linf' = `linf'[_n-1]+`nyears'[_n-1] if _n>=2 qui replace `pmx' = `linf'[_n+1] qui replace `pmx' = `pmx'[_N-1]+((`pmx'[_N-1]-`pmx'[_N-2])) if _n==_N local maxx= `pm'[_N] local uppx = ((int(`maxx'/10))*11) } if "`ge'"~="" & `nrows'==1 { format _ExpYL %3.1f qui su _ExpYL , meanonly local maxy = r(max) local step = `maxy'/10 local maxy = int(`step'*11+(`step'/2)) if "`by'"~="" { gr _ExpYL `linf' , c(m) s(o) sort `options' xlab(0(5)`uppx') /* */ ylab(0(`step')`maxy') b1(Life Expectation Function) /* */ gap(4) b2(Age level) l1(E(x)) t2(Deaths variable: `name') /* */ t1(Expected years of life for `by'=`lab') } else { gr _ExpYL `linf' , c(m) s(o) sort `options' xlab(0(5)`uppx') /* */ ylab(0(`step')`maxy') t1(Life Expectation Function) /* */ gap(4) b2(Age level) l1(E(x)) t2(Deaths variable: `name') } set more 0 more set more 1 } if "`gp'"~="" & `nrows'==1 { tempvar xx qui gen `xx' = sum(_dx) qui su `xx' , meanonly local maxy = r(max) qui replace `xx' = _dx/`maxy' * Proof: /* tempvar dd qui ge `dd' = sum(`xx') local ss=`dd'[_N] noi di "ss: `ss'" */ qui format `xx' %4.3f qui su `xx' , meanonly local maxy = r(max) local step = `maxy'/10 local maxy = (`step'*10)+(`step'/2) if "`by'"~="" { gr `xx' `pm' , c(l) s(o) sort `options' xlab(0(5)`uppx') ylab(0(`step')`maxy') /* */ gap(6) b2(Age level) l1(Density) t2(Deaths variable: `name') /* */ t1(Proportion of total events at age x, for `by'=`lab') /* */ title(" Density function") } else { gr `xx' `pm' , c(l) s(o) sort `options' xlab(0(5)`uppx') ylab(0(`step')`maxy') /* */ gap(6) b2(Age level) l1(Density) t2(Deaths variable: `name') /* */ t1(Proportion of total events at age x) /* */ title(" Density function") } set more 0 more set more 1 tempvar xx qui ge `xx' = 1-_Surv qui format `xx' %3.2f * noi l `xx' _Surv `pmx' `linf' `xx' if "`by'"~="" { gr `xx' `linf' , c(l) s(o) sort `options' xlab(0(5)`uppx') ylab(0(0.1)1) /* */ gap(6) b2(Age level) l1("P(x)= 1-S(x)") /* */ t1(Deaths variable: `name') t2(Group: `by'=`lab') /* */ title("Probability of death before age x") yline(.25,.5,.75) } else { gr `xx' `linf' , c(ll) s(o) sort `options' xlab(0(5)`uppx') ylab(0(0.1)1) /* */ gap(6) b2(Age level) l1("P(x)= 1-S(x)") /* */ t1(Deaths variable: `name') /* */ title("Probability of death before age x") yline(.25,.5,.75) } set more 0 more set more 1 } if "`gs'"~="" & `nrows'==1 { qui format _Surv %3.2f if "`ci'"~="" { if "`by'"~="" { gr `low' `hig' _Surv `linf' , c(IIl) s(..o) sort `options' xlab(0(5)`uppx') ylab(0(.1)1) /* */ gap(4) b2(Age level) l1(S(x)) b1(" Survival Function") /* */ t1(Survival Function with $D_level% conf. intervals for `by'=`lab') /* */ t2("Deaths variable: `name'") yline(.25,.5,.75) } else { gr `low' `hig' _Surv `linf' , c(IIl) s(..o) sort `options' xlab(0(5)`uppx') ylab(0(.1)1) /* */ gap(4) b2(Age level) l1(S(x)) b1(" Survival Function") /* */ t1(Survival Function) t2("with $D_level% confidence intervals")yline(.25,.5,.75) } } if "`ci'"=="" { if "`by'"~="" { gr _Surv `linf' , c(l) s(o) sort `options' xlab(0(5)`uppx') ylab(0(.1)1) /* */ gap(4) b2(Age level) l1(S(x)) b1(" Survival Function") /* */ t1(Survival Function for `by'=`lab') /* */ t2("Deaths variable: `name'") yline(.25,.5,.75) } else { gr _Surv `linf' , c(l) s(o) sort `options' xlab(0(5)`uppx') ylab(0(.1)1) /* */ gap(4) b2(Age level) l1(S(x)) b1(" Survival Function") /* */ t1(Survival Function) yline(.25,.5,.75) } } set more 0 more set more 1 } qui cap drop _HRate * qui ge _HRate = -((_Surv[_n+1]-_Surv)/((_Surv[_n+1]+_Surv)/2)) qui ge _HRate = _dx/(`nyears'*(_lx-(`s'*_dx))) * qui ge _HRate = -log(_px) label var _HRate "Hazard Rate" qui format _HRate %4.3f qui su _HRate , meanonly local maxy = r(max) local step = `maxy'/10 local step = round(`step',0.0001) local maxy = (`step'*10)+(`step'/2) if "`gh'"~="" & `nrows'==1 { if "`by'"~="" { gr _HRate `pm' , c(l) s(o) sort `options' xlab(0(5)`uppx') /* */ ylabel(0(`step')`maxy') b1(" Hazard Function") /* */ gap(6) b2(Age level) l1(lambda(x)) /* */ t1(Hazard Rates for `by'=`lab') t2(Deaths variable: `name') } else { gr _HRate `pm' , c(l) s(o) sort `options' xlab(0(5)`uppx') /* */ ylabel(0(`step')`maxy') b1(" Hazard Function") /* */ gap(6) b2(Age level) l1(lambda(x)) /* */ t1(Hazard Rates) t2(Deaths variable: `name') } if "`lograte'"~="" { set more 0 more set more 1 tempvar HR qui ge `HR' = log(_HRate) qui format `HR' %5.3f qui su `HR' , meanonly local maxyl = r(max) local minyl = r(min) local stepl = (`maxyl'-`minyl')/10 local stepl = round(`stepl',0.0001) local minyl = (`minyl')-(`step'/2) local maxyl = (`minyl'+(`stepl'*11))+(`stepl'/2) if "`by'"~="" { gr `HR' `pm' , c(l) s(o) sort `options' xlab(0(5)`uppx') /* */ ylabel(`minyl'(`stepl')`maxyl') /* */ gap(7) b2(Age level) l1(Log(Rate)) b1(" Log-Hazard Function") /* */ t1(Log-Rates for `by'=`lab') t2(Deaths variable: `name') } else { gr `HR' `pm' , c(l) s(o) sort `options' xlab(0(5)`uppx') /* */ ylabel(`minyl'(`stepl')`maxyl') /* */ gap(7) b2(Age level) l1(Log(lambda(x))) b1(" Log-Hazard Function")/* */ t1(Log-Rates) t2(Deaths variable: `name') } qui cap drop `HR' } } * CICLO DE GERAÇÃO DA TABELA MC * ----------------------------- if "`sclist'"~="" { global nrows = "`nrows'" global max = "`max'" global factor = "`factor'" global yearmsg = "`yearmsg'" global fake = "`fake'" global WW = "`w1' `w2' `w3' `w4'" global w1 = "`w1'" global w2 = "`w2'" global w3 = "`w3'" global w4 = "`w4'" global by = "`by'" global noYLL = "`noyll'" global lab = "`lab'" global warn = "yes" if "`keep'"~="" & "`fake'"=="" | "`saving'"~="" { local numvars : word count `sclist' local v 1 while `v'<=`numvars' { qui cap drop _qx`v' qui ge _qx`v' =. local v = `v' + 1 } local v 1 while `v'<=`numvars' { qui cap drop _Wx`v' qui ge _Wx`v' =. local v = `v' + 1 } local v 1 while `v'<=`numvars' { qui cap drop _Fx`v' qui ge _Fx`v' =. local v = `v' + 1 } } lifetabs `0' } * ----------------------------- if "`keep'"~="" { tempfile _F`c' qui save `_F`c'' , replace local fsaved = "`fsaved' `_F`c''" } restore local c = `c' + 1 } * FIM do ciclo de grupos AgeStrata * -------------------------------- * Apanha de canas (1) * --------------- if "`keep'"~="" { if "`_F0'"~="" { qui use "`_F0'" , clear local i 1 } else { qui use "`_F1'" , clear local i 2 } while "`_F`i''"~="" { qui append using `_F`i'' local i = `i' + 1 } if "`saving'"~="" { qui cap drop _OtherSC local file = "`saving'.dta" qui save "`file'" , `replace' } else { qui cap drop _OtherSC qui save "_SaVeD_.dta" , replace local file = "_SaVeD_.dta" } * ____________________ * ROTINA DE GRAFICOS PARA MULTIPLOS GRUPOS * ---------------------------------------- if ("`ge'"~="" | "`gp'"~="" | "`gs'"~="" | "`gh'"~="") & `nrows'>1 { tempvar t qui egen `t' = group(`by') if `touse'==1 qui lab var `t' "`by' groups" quietly { sort `touse' `t' `strata' tempvar pm pmx linf ge `pm' = . ge `pmx' = . ge `linf' = . by `touse' `t' : replace `pm' = sum(`nyears') if `touse'==1 replace `pmx' = `pm' replace `linf' = `pm'[_n-1] by `touse' `t' : replace `pm' = `pm'/2 if _n==1 by `touse' `t' : replace `pm' = (`pm'+`pmx'[_n-1])/2 if _n>=2 by `touse' `t' : replace `pm' = . if _n==`max' by `touse' `t' : replace `linf' = 0 if _n==1 by `touse' `t' : replace `pmx' = 0 if _n==1 * by `touse' `t' : replace `pmx' = . if _n==`max' by `touse' `t' : replace `pmx' = `linf'+`nyears' } local maxx = `pm'[_N-1]+1 local uppx = ((int(`maxx'/10))*11) } if "`ge'"~="" & `nrows'>1 { qui su _ExpYL , meanonly local maxy = r(max) local step = `maxy'/10 local maxy = int(`step'*11+(`step'/2)) format _ExpYL %2.0f qui sort `by' `strata' qui cap drop _LExp* qui ge _LExp = _ExpYL qui separate _LExp , by(`t') local k 1 while `k'<=`nrows' { qui su `by' if `t'==`k' , meanonly local val = r(mean) label var _LExp`k' "`by' = `val'" qui format _LExp`k' %2.0f local k = `k' + 1 } local k 1 while `k'<=`nrows' { local strvar = "`strvar' " + "_LExp`k'" local cc = "`cc'" + "m" local s = substr("odp.odp.odp.odp.odp.",`k',1) local ss = "`ss'" + "`s'" local k = `k' + 1 } keyplot `strvar' `linf' , c(`cc') s(`ss') sort `options' xlab(0(5)`uppx') /* */ ylab(0(`step')`maxy') varlbl /* */ gap(3) b2(Age level) l1(E(Years of Life)) /* */ title("Life Expectation Functions (death var: `name')") qui drop _LExp* local strvar = "" local cc = "" local ss = "" set more 0 more set more 1 } if "`gp'"~="" & `nrows'>1 { tempvar xx qui gen `xx' = . qui sort `t' `strata' qui { by `t' : replace `xx' = sum(_dx) } qui { by `t' : replace `xx' = _dx / `xx'[_N] } qui { by `t' : replace `xx'=. if _n==_N } qui format `xx' %4.3f qui su `xx' , meanonly local maxy = r(max) local step = `maxy'/10 local maxy = (`step'*10)+(`step'/2) label var `xx' "Density functions" qui cap drop _Dsity* qui ge _Dsity = `xx' qui separate _Dsity , by(`t') local k 1 while `k'<=`nrows' { qui su `by' if `t'==`k' , meanonly local val = r(mean) label var _Dsity`k' "`by' = `val'" local k = `k' + 1 } local k 1 while `k'<=`nrows' { local strvar = "`strvar' " + "_Dsity`k'" qui format _Dsity`k' %4.3f local cc = "`cc'" + "l" local s = substr("odp.odp.odp.odp.odp.",`k',1) local ss = "`ss'" + "`s'" local k = `k' + 1 } keyplot `strvar' `pm' , c(`cc') s(`ss') sort `options' xlab(0(5)`uppx') /* */ ylab(0(`step')`maxy') varlbl /* */ gap(6) b2(Age level) l1(Density) /* */ b1(Density functions : P(x)/dAge (var `name') ) set more 0 more set more 1 qui sort `touse' `t' `strata' qui ge _CumD = 1 - _Surv if `touse'==1 qui replace _CumD = _CumD[_n+1] qui replace _CumD =. if _CumD==0 qui format _CumD %5.4f qui separate _CumD , by(`t') local k 1 while `k'<=`nrows' { qui su `by' if `t'==`k' , meanonly local val = r(mean) label var _CumD`k' "`by' = `val'" local k = `k' + 1 } local strvar = " " local cc = "" local ss = "" local k 1 while `k'<=`nrows' { local strvar = "`strvar' " + "_CumD`k'" qui format _CumD`k' %3.2f local cc = "`cc'" + "l" local s = substr("odp.odp.odp.odp.odp.",`k',1) local ss = "`ss'" + "`s'" local k = `k' + 1 } keyplot `strvar' `linf' , c(`cc') s(`ss') sort `options' xlab(0(5)`uppx') /* */ ylab(0(.1)1) yline(.25,.5,.75) varlbl /* */ gap(6) b2(Age level) l1(Cum(P(d))) /* */ b1(Prob. of death until age, by `by' (var `name')) qui cap drop _Dsity* qui cap drop _CumD* local strvar = "" local cc = "" local ss = "" set more 0 more set more 1 } if "`gs'"~="" & `nrows'>1 { qui sort `t' `strata' qui cap drop _SurvP* qui ge _SurvP = _Surv qui separate _SurvP , by(`t') local k 1 while `k'<=`nrows' { qui su `by' if `t'==`k' , meanonly local val = r(mean) label var _SurvP`k' "`by' = `val'" local k = `k' + 1 } local k 1 while `k'<=`nrows' { local strvar = "`strvar' " + "_SurvP`k'" qui format _SurvP`k' %3.2f local cc = "`cc'" + "l" local s = substr("odp.odp.odp.odp.odp.",`k',1) local ss = "`ss'" + "`s'" local k = `k' + 1 } keyplot `strvar' `linf' , c(`cc') s(`ss') sort `options' xlab(0(5)`uppx') ylab(0(.10)1) /* */ gap(5) b2(Age level) l1(S(x)) varlbl /* */ title("Survival Functions (var `name', by `by')") yline(.25,.5,.75) qui cap drop _SurvP* local strvar = "" local cc = "" local ss = "" set more 0 more set more 1 } if "`gh'"~="" & `nrows'>1 { qui sort `by' `strata' * qui ge _HRate = -((_SurvP[_n+1]-_SurvP)/((_SurvP[_n+1]+_SurvP)/2)) * qui ge _HRate = -log(_SurvP) label var _HRate "Hazard Rate" qui gen _HRATE = _HRate qui su _HRate , meanonly local maxy = r(max) local step = `maxy'/10 local maxy = (`step'*11)+(`step'/2) qui separate _HRATE , by(`t') local k 1 while `k'<=`nrows' { qui su `by' if `t'==`k' , meanonly local val = r(mean) label var _HRATE`k' "`by' = `val'" qui format _HRATE`k' %4.3f local k = `k' + 1 } local k 1 while `k'<=`nrows' { local strvar = "`strvar' " + "_HRATE`k'" local cc = "`cc'" + "m" local s = substr("odp.odp.odp.odp.odp.",`k',1) local ss = "`ss'" + "`s'" local k = `k' + 1 } keyplot `strvar' `pm' , c(`cc') s(`ss') sort `options' xlab(0(5)`uppx') /* */ ylab(0(`step')`maxy') varlbl /* */ gap(5) b2(Age level) l1(lambda(x)) /* */ title("Hazard Rates (var `name', by `by')") qui cap drop _HRATE* local strvar = "" local cc = "" local ss = "" if "`lograte'"~="" { set more 0 more set more 1 qui cap drop _HrLoG qui ge _HrLoG = log(_HRate) qui format _HrLoG %4.2f qui su _HrLoG , meanonly local maxyl = r(max) local minyl = r(min) local stepl = (`maxyl'-(`minyl'))/10 if `stepl'<0 { local stepl = `stepl' * -1 } local stepl = round(`stepl',0.0001) local minyl = (`minyl')-(`step'/2) local minyl = round(`minyl',0.0001) local maxyl = (`minyl'+(`stepl'*12))+(`stepl'/2) local maxyl = round(`maxyl',0.0001) qui separate _HrLoG , by(`t') local k 1 while `k'<=`nrows' { qui su `by' if `t'==`k' , meanonly local val = r(mean) label var _HrLoG`k' "`by' = `val'" qui format _HrLoG`k' %4.2f local k = `k' + 1 } local k 1 while `k'<=`nrows' { local strvar = "`strvar' " + "_HrLoG`k'" local cc = "`cc'" + "m" local s = substr("odp.odp.odp.odp.odp.",`k',1) local ss = "`ss'" + "`s'" local k = `k' + 1 } gr `strvar' `pm' , c(`cc') s(`ss') sort `options' xlabel(0(5)`uppx') /* */ ylabel(`minyl'(`stepl')`maxyl') /* */ gap(5) b2(Age level) l1("Log(lambda(x))") /* */ title("Log Hazard Rates (var `name')") qui cap drop _HrLoG* local strvar = "" } } * ____________________ FIM dos gráficos if "`fake'"=="" { if "`noo'"~="" { di in blu " " } di in blu " Warning: data has changed. The file being used now is `file'" } di in gr _dup(79) " " } if "`fake'"=="YES" { qui use "`namef'" , clear } macro drop _all end