*! version 1.0.2 24apr1996 program define svy_preg version 4.0 local name "`1'" if "`name'"=="save" { /* save macros */ Ftest /* compute F test of model */ MakeDeff /* make matrices S_E_deff, S_E_deft, S_E_meft */ matrix S_E_Vsrs = $S_VYdeff matrix S_E_Vmsp = $S_VYmeff if "$S_VYfpc"!="" { matrix S_E_Vswr = $S_VYdeft } global S_E_nobs "$S_VYnobs" global S_E_nstr "$S_VYnstr" global S_E_npsu "$S_VYnpsu" global S_E_npop "$S_VYnpop" global S_E_nsub "$S_VYnsub" global S_E_osub "$S_VYosub" global S_E_wgt "$S_VYwgt" global S_E_exp "$S_VYexp" global S_E_str "$S_VYstr" global S_E_psu "$S_VYpsu" global S_E_fpc "$S_VYfpc" global S_E_depv "$S_VYdepv" global S_E_fail "$S_VYfail" global S_E_succ "$S_VYsucc" global S_E_adj "$S_VYadj" } /* Erase macros. */ global S_VYnobs /* number of obs */ global S_VYnstr /* number of strata */ global S_VYnpsu /* number of PSUs */ global S_VYnpop /* sum of weights = population size */ global S_VYnsub /* subpop. size */ global S_VYosub /* number of obs in subpopulation */ global S_VYtype /* double or float for computation */ global S_VYwgt /* user's weight type */ global S_VYexp /* user's weight variable or expression */ global S_VYw /* weight variable */ global S_VYstr /* user's strata variable */ global S_VYpsu /* user's psu variable */ global S_VYfpc /* user's fpc variable */ global S_VYdepv /* user's dependent variable */ global S_VYindv /* user's independent variables */ global S_VYdopt /* user's display options */ global S_VYmopt /* maximize options */ global S_VYopt /* options for svy_reg */ global S_VYcons /* = "nocons" if specified */ global S_VYadj /* = "noadjust" if specified */ global S_VYfail /* no. of completely determined failures */ global S_VYsucc /* ditto successes -- logit/probit only */ global S_VYv /* tempname for covariance matrix */ global S_VYdeff /* tempname for covariance matrix deff */ global S_VYdeft /* tempname for covariance matrix deft */ global S_VYmeff /* tempname for covariance matrix meff */ if "`name'"=="save" | "`name'"=="cleanup" { exit } local doit "`2'" /* mark variable */ local w "`3'" /* weight variable (if needed) */ local subvar "`4'" /* indicator variable for subpop */ macro shift 4 /* Parse. */ if "`name'"=="reg" { local iweight "iweight" } else if "`name'"=="logit" { local or "OR" local mopt "LOg *" } else if "`name'"=="probit" { local mopt "LOg *" } local varlist "req ex" local weight "pweight `iweight' noprefix" local if "opt" local in "opt" #delimit ; local options "STRata(string) PSU(string) FPC(string) SUBpop(string) SRSsubpop FLOAT noCONstant noMINUS noADJust `or' `mopt' Level(int $S_level) Prob CI DEFF DEFT MEFF MEFT" ; /* noWARNing" ; */ #delimit cr parse "`*'" if "`srssubp'"!="" & "`subpop'"=="" { di in red "srssubpop can only be specified when subpop() " /* */ "is specified" exit 198 } /* commented out /* Display warning about the use "if" or "in". */ if "`if'`in'"!="" & "`warning'"=="" { di _n in blu "Note: Use subpop(expression) option to " /* */ "obtain estimates for a subpopulation" _n /* */ "rather than " _quote "if" _quote " or " /* */ _quote "in" _quote "." } end commented out */ /* Set global macros. */ global S_VYdopt /* */ "level(`level') `prob' `ci' `deff' `deft' `meff' `meft' `or'" if "`log'"=="" { local log "nolog" } global S_VYmopt "`log' `options'" global S_VYopt "`srssubp' `float' `minus'" global S_VYcons "`constan'" global S_VYadj "`adjust'" parse "`varlist'", parse(" ") global S_VYdepv "`1'" macro shift global S_VYindv "`*'" /* Get weights. */ if "`exp'"=="" { var_get pweight `exp', optional local exp "$S_1" } else if "`weight'"=="pweight" { /* try to varset pweight variable */ capture confirm variable `exp' if _rc==0 { var_get pweight `exp' local exp "$S_1" } } if "`exp'"!="" { global S_VYexp "`exp'" if "`weight'"!="" { global S_VYwgt "`weight'" } else global S_VYwgt pweight capture confirm variable `exp' if _rc { qui gen double `w' = `exp' global S_VYw "`w'" } else global S_VYw "`exp'" } /* Get strata, psu, and fpc. */ var_get strata `strata', optional global S_VYstr "$S_1" if "$S_VYstr"!="" { global S_VYopt "$S_VYopt strata($S_VYstr)" } capture confirm string variable $S_VYstr if _rc==0 { local strata } else local strata "$S_VYstr" var_get psu `psu', optional global S_VYpsu "$S_1" if "$S_VYpsu"!="" { global S_VYopt "$S_VYopt psu($S_VYpsu)" } capture confirm string variable $S_VYpsu if _rc==0 { local psu } else local psu "$S_VYpsu" var_get fpc `fpc', optional global S_VYfpc "$S_1" if "$S_VYfpc"!="" { global S_VYopt "$S_VYopt fpc($S_VYfpc)" } /* Mark. */ mark `doit' `if' `in' /* Check for negative weights if pweights. */ if "$S_VYwgt"=="pweight" { capture assert $S_VYw >= 0 if `doit' if _rc { error 402 } } /* Markout. */ markout `doit' $S_VYdepv $S_VYindv $S_VYw `strata' `psu' /* */ $S_VYfpc $S_VYby capture confirm string variable $S_VYstr if _rc==0 { /* markout string S_VYstr */ qui replace `doit' = 0 if trim($S_VYstr)=="" } capture confirm string variable $S_VYpsu if _rc==0 { /* markout string S_VYpsu */ qui replace `doit' = 0 if trim($S_VYpsu)=="" } /* Compute total #obs. */ qui count if `doit' if _result(1) == 0 { error 2000 } /* Check fpc variable for sensible ranges. */ if "$S_VYfpc"!="" { CheckFPC `doit' $S_VYfpc } /* If no subpop, `subvar' is markvar. */ if "`subpop'"=="" { qui gen byte `subvar' = `doit' exit } /* `subvar' = 1 for observations satisfying subpop(); otherwise 0. */ capture gen byte `subvar' = (`subpop')*`doit' if _rc { if _rc != 902 { di in red "error in expression in " /* */ "subpop() option" } error _rc } qui count if `subvar' if _result(1) == 0 { error 2000 } global S_VYopt "$S_VYopt subpop(`subvar')" end program define CheckFPC version 4.0 local doit "`1'" local fpc "`2'" capture assert `fpc' >= 0 if `doit' if _rc { di in red "fpc() must be >= 0" exit 499 } capture assert `fpc' >= 2 if `doit' if _rc { capture assert `fpc' <= 1 if `doit' if _rc { di in red "fpc() must be <= 1 if a rate " /* */ "or >= 2 if total PSUs per stratum" exit 499 } } end program define MakeDeff version 4.0 tempname V matrix `V' = get(VCE) matrix `V' = vecdiag(`V') if "$S_VYfpc"!="" { local Vdeft "$S_VYdeft" } else local Vdeft "$S_VYdeff" local dim = colsof(`V') matrix S_E_deff = `V' matrix S_E_deft = `V' matrix S_E_meft = `V' local i 1 while `i' <= `dim' { matrix S_E_deff[1,`i'] = cond($S_VYdeff[`i',`i']!=0, /* */ `V'[1,`i']/$S_VYdeff[`i',`i'], 0) matrix S_E_deft[1,`i'] = cond(`Vdeft'[`i',`i']!=0, /* */ sqrt(`V'[1,`i']/`Vdeft'[`i',`i']), 0) matrix S_E_meft[1,`i'] = cond($S_VYmeff[`i',`i']!=0, /* */ sqrt(`V'[1,`i']/$S_VYmeff[`i',`i']), 0) local i = `i' + 1 } end program define Ftest version 4.0 if "$S_VYindv"=="" { global S_E_f "." exit } tempname V iV matrix `V' = get(VCE) local dim = colsof(`V') capture matrix `iV' = syminv(`V') if _rc { global S_E_f "singular" exit } matrix `V' = `iV'*`V' matrix `iV' = I(`dim') matrix `V' = `V' - `iV' matrix `V' = `V'*`V'' if trace(`V') > 1e-6 { /* matrix is singular */ global S_E_f "singular" exit } capture test $S_VYindv if _rc==0 & _result(3)==$S_E_mdf { if "$S_VYadj"=="" { /* adjusted (default) */ local df = $S_VYnpsu - $S_VYnstr if `df'-$S_E_mdf+1 > 0 { global S_E_f = _result(6) /* */ *(`df'-$S_E_mdf+1)/`df' exit } } else { /* unadjusted */ global S_E_f = _result(6) exit } } global S_E_f "." end