*! phtest2 *! version 1.0.0 William H. Rogers (STB-19: ssa4) program define phtest2 version 3.1 local varlist "req ex" local if "opt" local in "opt" local options "Iact(string) AT(string) DEad(string)" /* we need a test that iact is completely contained within the varlist, linear-algebra wise */ parse "`*'" local vlist "`varlist'" local dv "`1'" preserve quietly { if "`if'`in'"!="" { keep `if' `in' local if local in } if "`if'`in'"!="" { keep `if' `in' } tempvar mdead obs new oldll if "`dead'"=="" { tempvar dead gen byte `dead' = 1 } cox `vlist', dead(`dead') scalar `oldll' = _result(2) local df = _result(3) local j 1 local nat : word count `at' gen `new' = 1 while (`j'<=`nat') { local myat : word `j' of `at' replace `new' = `new' + (`dv'>`myat') local j = `j' + 1 } gen long `obs' = _n expand `new' sort `obs' if "`iact'"=="" { tempvar iact predict `iact' } local varlist "req ex" parse "`iact'" parse "`varlist'", parse(" ") while "`1'"!="" { local j 1 while `j'<=`nat' { tempvar tv by `obs': replace `new' = _n<=`j' gen `tv' = `1' * `new' local nvl "`nvl' `tv'" local j = `j' + 1 } mac shift } local j 1 while `j'<=`nat' { local myat : word `j' of `at' by `obs': replace `new' = (_n==`j' & `dv'>`myat') replace `dead' = 0 if `new'==1 replace `dv' = `myat' if `new'==1 local j = `j' + 1 } cox `vlist' `nvl', dead(`dead') tvid(`obs') tempvar newll scalar `newll' = _result(2) local ndf = _result(3) global S_1 = `ndf'-`df' global S_2 = 2*(`newll'-`oldll') noisily di "Chi-square(" in ye $S_1 in gr ") = " in ye /* */ %8.2f $S_2 in gr ", P = " in ye %8.4f chiprob($S_1,$S_2) } end