📄 svy_est.ado
字号:
*! version 2.0.13 30dec2004
program define svy_est, sortpreserve
/*
Syntax: svy_est svycmd `0'
*/
version 8, missing
if _caller()<8 {
svy_est_7 `0'
exit
}
gettoken svycmd 0 : 0
if replay() {
if `"`e(cmd)'"'!="`svycmd'" {
error 301
}
svy_dreg `0' /* display results */
exit
}
nobreak {
capture noisily break {
/* Create some temps for `svycmd' programs.
Note: S_VYtmp* are NOT used in svy_est.ado.
*/
tempvar doit subvar
tempname tmp1 tmp2 tmp3 tmp4 tmp5 tmp6 tmp7 tmp8 tmp9
macro drop S_VY*
global S_VYtmp1 `tmp1'
global S_VYtmp2 `tmp2'
global S_VYtmp3 `tmp3'
global S_VYtmp4 `tmp4'
global S_VYtmp5 `tmp5'
global S_VYtmp6 `tmp6'
global S_VYtmp7 `tmp7'
global S_VYtmp8 `tmp8'
global S_VYtmp9 `tmp9'
SvyParse `svycmd' `doit' `subvar' `0'
local dopts $S_VYdopt /* save display options */
SvyEst `svycmd' `doit'
}
local rc = _rc
macro drop S_VY*
if `rc' {
ereturn clear
exit `rc'
}
}
svy_dreg, `dopts' /* display results */
end
program define SvyEst, eclass
args svycmd doit
tempvar postit
tempname b V Vdeff npop
/* Create necessary tempvars for scores. */
`svycmd' 0 how_many_scores
if "`svycmd'"=="svymlogit" | /*
*/ "`svycmd'"=="svyologit" | /*
*/ "`svycmd'"=="svyoprobit" {
if $S_VYncat > 50 {
error 149
}
}
if `"$S_VYscore"' != "" {
local k_scores : word count $S_VYscore
if r(k_scores) != 1 & `k_scores' == 1 & /*
*/ substr("$S_VYscore",-1,1)=="*" {
local stub = /*
*/ substr("$S_VYscore",1,length("$S_VYscore")-1)
global S_VYscore
forval i = 1/`r(k_scores)' {
global S_VYscore $S_VYscore `stub'`i'
}
}
else {
if r(k_scores) != `k_scores' {
di as err /*
*/ `"number of variables in score() option must be equal to `r(k_scores)'"'
exit 198
}
}
confirm new var $S_VYscore
local k_scores : word count $S_VYscore
}
local i 1
while `i' <= r(k_scores) {
tempvar s
local sclist `sclist' `s'
local i = `i' + 1
}
if `r(cmdcando)' {
local scopt score(`sclist')
}
/* Are any of the options -log-, -trace-, -gradient-, -hessian-,
or -showstep- specified?
*/
Option "LOg TRace GRADient HESSian SHOWSTEP", $S_VYeopt
if !`r(option)' {
if "`svycmd'"=="svylogit" | "`svycmd'"=="svyprobit" {
local nocoef "nocoef nolog"
}
else local quicmd "quietly"
local quidi "*" /* do not display comment */
local quimeff "quietly" /* do not display meff computation */
}
/* Get D, where V = DMD. */
if "$S_VYwgt"!="" {
local wt [iw=$S_VYexp]
local wgted "weighted "
}
if "`svycmd'"=="svyreg" | "`svycmd'"=="svyivreg" {
local mse1 "mse1"
}
`quidi' di _n as txt "Computing point estimates using `wgted'" /*
*/ as res "$S_VYcmd" as txt ":"
ereturn clear
if inlist(substr("`svycmd'",4,.), /*
*/ "logit","mlogit","ologit","oprobit","probit") {
local crtype "log pseudolikelihood"
local cropt crittype(`"`crtype'"')
}
else {
local crtype
local cropt
}
`quicmd' $S_VYcmd $S_VYdepv $S_VYindv `wt' if $S_VYsub, /*
*/ $S_VYeopt `mse1' `nocoef' `scopt' `cropt'
quietly count if e(sample) /* can't trust commands with iweights
to get #obs right
*/
local nobs `r(N)'
matrix `b' = e(b)
matrix `V' = e(V)
global S_VYb `b'
global S_VYV `V'
/* Compute scores for commands that cannot, or do other work. */
if "`svycmd'"=="svyivreg" {
preserve
}
`svycmd' 0 scores `doit' `sclist'
/* Replace missing scores with zeros. */
tokenize `sclist'
local i 1
while "``i''"!="" {
capture replace ``i'' = 0 if missing(``i'')
if _rc {
local `i' " "
}
local i = `i' + 1
}
/* Compute variance estimates. */
_robust `*' `wt' if `doit', v(`V') vsrs(`Vdeff') /*
*/ $S_VYopt zeroweight
/* Save results from _robust(). */
global S_VYn `r(N)' /* number of obs */
global S_VYnstr `r(N_strata)' /* number of strata */
global S_VYnpsu `r(N_clust)' /* number of PSUs */
scalar `npop' = r(sum_w) /* population size */
if r(N_sub)<. {
tempname nsubpop
global S_VYosub `r(N_sub)' /* # subpop. obs */
scalar `nsubpop' = r(sum_wsub) /* subpop. size */
}
if "`svycmd'"=="svyivreg" {
restore
/* svyivreg changes the data, thus regenerate the scores */
qui _predict double `sclist' if $S_VYsub, residual
qui replace `sclist' = 0 if missing(`sclist')
}
/* Get misspecified estimates if meff or meft specified. */
Option "MEFF MEFT", $S_VYdopt /* was meff or meft specified? */
if `r(option)' {
`quimeff' di _n as txt "Computing misspecified " /*
*/ as res "$S_VYcmd" as txt " model for meff/meft compution:"
ereturn clear
`quimeff' $S_VYcmd $S_VYdepv $S_VYindv if $S_VYsub, /*
*/ $S_VYeopt
tempname Vmeff
capture matrix `Vmeff' = e(V)
if _rc {
matrix `Vmeff' = 0*`V'
}
else if e(N)!=`nobs' | colsof(`Vmeff')!=colsof(`V') {
matrix `Vmeff' = 0*`V'
}
}
/* Post results. */
local df = $S_VYnpsu - $S_VYnstr
gen byte `postit' = `doit' /* may need `doit' later */
ereturn post `b' `V', dof(`df') esample(`postit')
/* Save other results. */
SvySave `npop' "`nsubpop'" `Vdeff' "`Vmeff'" /* save common results */
`svycmd' 0 save /* save special results */
ereturn local cmd `svycmd'
global S_E_cmd `e(cmd)' /* double save */
if inlist(substr("`svycmd'",4,.), /*
*/ "logit","mlogit","ologit","oprobit","probit") {
ereturn local crittype `crtype'
}
if `"`e(cmd)'"' == "svyivreg" {
local 0 , $S_VYdopt
syntax [, FIRST * ]
if `"`first'"' != "" {
`e(cmd)' 0 first `doit' $S_VYcons
}
global S_VYdopt `options'
}
/* save the scores */
if `"$S_VYscore"' != "" {
forval i = 1/`k_scores' {
local ts : word `i' of `sclist'
local us : word `i' of $S_VYscore
rename `ts' `us'
}
}
end
/*----------------------------- Parse programs -------------------------------*/
program define SvyParse
gettoken svycmd 0 : 0
gettoken doit 0 : 0
gettoken subvar 0 : 0
/* S_VY macros:
S_VYdopt display options
S_VYeopt estimation options
S_VYopt options for _robust
S_VYadj "noadjust" if specified
S_VYsrss "srssubpop" if specified
S_VYdepv dependent variable(s)
S_VYindv independent variables
S_VYmodl variable for model test
S_VYwgt weight type
S_VYexp weight expression
S_VYstr strata variable
S_VYpsu psu variable
S_VYfpc fpc variable
S_VYsub subpop. expression = "`doit'" or "`doit' & `subpop'!=0"
*/
/* Call `svycmd' with "syntax" query to get # of dependent variables
`s(k_depvar)', allowed command options `s(okopts)', special display
options `s(dopts)', and whether there are ml options `s(mlopts)'.
*/
`svycmd' 0 syntax `0'
local cmd `s(cmd)'
local k_depvar `s(k_depvar)'
local okopts `s(okopts)'
local dopts `s(dopts)'
local first `s(first)'
if "`s(mlopts)'"=="yes" {
local mlopts "LOg *"
}
if "`s(new0)'"!="" {
local 0 `s(new0)'
}
/* Parse and check for some errors. */
syntax varlist(min=`k_depvar' numeric) /*
*/ [pw iw/] /* see _svy_newrule.ado
*/ [if] [in] /*
*/ [, /*
*/ noCONstant /*
*/ SUBpop(string asis) /*
*/ SRSsubpop /*
*/ noADJust /*
*/ Level(cilevel) /*
*/ Prob /*
*/ CI /*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -