*! version 1.0.0 12apr1996 program define svy_logt version 4.0 local cmd "`1'" macro shift if substr("`1'",1,1)=="," | "`*'"=="" { if !(("`cmd'"=="logit" & "$S_E_cmd"=="svylogit") /* */ |("`cmd'"=="probit" & "$S_E_cmd"=="svyprobt")) { error 301 } svy_dreg `*' /* display results */ exit } capture noisily { tempvar doit w subvar score tempname s b D V Vdeff Vdeft Vmeff svy_preg `cmd' `doit' `w' `subvar' `*' /* Ready weights for estimation. */ if "$S_VYwgt"!="" { local awt "[aw=$S_VYw]" local wt "[iw=$S_VYw]" } /* Get logit/probit estimates. */ `cmd' $S_VYdepv $S_VYindv `awt' if `subvar', /* */ $S_VYcons $S_VYmopt nocoef if _result(1)==0 | _result(1)==. { error 2000 } local nobs = _result(1) matrix `b' = get(_b) matrix `D' = get(VCE) quietly { predict double `score' if `subvar' replace `doit' = 0 if `score'==. & `subvar' replace `subvar' = 0 if `doit'==0 /* Note on -if `subvar'- in the -predict-: If there are * dropped obs in subpop, we do not omit obs with same * covariate pattern from the population outside the subpop. * Alternatively, one could omit them using * * predict double `score' if `doit' * replace `doit' = 0 if `score'==. & `doit' * replace `subvar' = 0 if `doit'==0 * * But the former code is consistent with the message that * logit/probit puts out. There would only be a difference * with the variance estimates if the later dropped entire * PSUs from the population outside the subpop. */ /* Count completely determined. */ local eps 1e-7 count if `score' < `eps' & `subvar' if _result(1) > 0 { global S_VYfail = _result(1) } count if `score' > 1-`eps' & `subvar' if _result(1) > 0 { global S_VYsucc = _result(1) } /* Compute score function. */ if "`cmd'"=="logit" { replace `score' = cond(`subvar', /* */ ($S_VYdepv!=0)-`score', 0) } else { /* probit */ drop `score' predict double `score' if `subvar', index replace `score' = cond(`subvar', /* */ cond($S_VYdepv!=0, /* */ exp(-0.5*(`score'^2)) /* */ /(sqrt(2*_pi)*normprob(`score')), /* */ -exp(-0.5*(`score'^2)) /* */ /(sqrt(2*_pi)*normprob(-`score'))), 0) } } /* Use colnames of `b' for indepvars. */ local dim = colsof(`b') global S_VYindv : colnames(`b') parse "$S_VYindv", parse(" ") if "``dim''"=="_cons" { /* there is _cons */ local `dim' global S_VYindv "`*'" local mdf = `dim' - 1 } else { /* no _cons (either not requested or dropped */ global S_VYcons "nocons" local mdf "`dim'" } /* Unwind weight scaling of logit/probit. */ if "$S_VYwgt"!="" { tempvar sw gen double `sw' = sum(`subvar'*$S_VYw) scalar `s' = `nobs'/`sw'[_N] matrix `D' = `s'*`D' } /* Get logit/probit estimates for meff. */ qui `cmd' $S_VYdepv $S_VYindv if `subvar', $S_VYcons $S_VYmopt if _result(1)!=`nobs' { matrix `Vmeff' = J(`dim',`dim',0) } else { matrix `Vmeff' = get(VCE) local dimMeff = colsof(`Vmeff') if `dimMeff'!=`dim' { matrix `Vmeff' = J(`dim',`dim',0) } } global S_VYmeff "`Vmeff'" /* Compute variance estimate. */ svy_reg `D' `score' $S_VYindv `wt' if `doit', $S_VYopt /* */ $S_VYcons var(`V') deff(`Vdeff') deft(`Vdeft') /* Post results. */ local df = $S_VYnpsu - $S_VYnstr /* Note: we do NOT subtract # terms in model. */ matrix post `b' `V', dof(`df') obs($S_VYnobs) depn($S_VYdepv) /* Set macros. */ local doption "$S_VYdopt" global S_E_mdf "`mdf'" svy_preg save /* save macros */ if "`cmd'"=="logit" { global S_E_cmd "svylogit" } else global S_E_cmd "svyprobt" } nobreak { if _rc { local rc = _rc svy_preg cleanup exit `rc' } } svy_dreg, `doption' /* display results */ end