📄 svy.ado
字号:
*! version 1.2.15 27jun2005
program svy, sortpreserve prop(irr or rrr)
version 9
local version : di "version " string(_caller()) ":"
capture _on_colon_parse `0'
if c(rc) | `"`s(after)'"' == "" {
if !c(rc) {
local 0 `"`s(before)'"'
}
if replay() {
if "`e(prefix)'" != "svy" {
error 301
}
Display `0'
exit
}
}
`version' SvyEst `0'
end
program SvyEst, eclass
version 9
local version : di "version " string(_caller()) ":"
// <my_stuff> : <command>
_on_colon_parse `0'
local command `"`s(after)'"'
local 0 `"`s(before)'"'
syntax [anything(name=vcetype equalok)] ///
[if] [in] [, ///
Level(passthru) ///
COEF /// for -logistic-
VCE(string) /// VCE type
STRata(passthru) /// see _svy_newrule.ado
PSU(passthru) /// see _svy_newrule.ado
FPC(passthru) /// see _svy_newrule.ado
* /// other options
]
_get_eformopts, soptions eformopts(`options') allowed(__all__)
local options `"`s(options)'"'
local efopt = cond(`"`s(opt)'"'=="",`"`s(eform)'"',`"`s(opt)'"')
// check that vcetype and vce() option do not conflict
if `"`vcetype'"' != "" & `"`vce'"' != "" {
gettoken vcetype vcerest : vcetype
_svy_check_vce `vcetype'
local vcetype `s(vce)'
_svy_check_vce `vce'
local vce `s(vce)'
if !`:list vce === vcetype' {
di as err ///
`"option vce(`vce') is not valid with svy `vcetype'"'
exit 198
}
}
else if `"`vcetype'"' != "" {
gettoken vcetype vcerest : vcetype
local vce `"`vcetype'"'
}
if `"`vce'"' != "" {
local vceopt vce(`vce')
}
// check for illegal options; and that the data is -svyset-
_svy_newrule , `strata' `psu' `fpc'
// parse the command and check for conflicts
`version' _prefix_command svy `if' `in' , ///
svy checkvce `coef' `efopt' `level': `command'
local vce2 `s(vce)'
if `"`vce'"' == "" {
local vce `vce2'
}
else if `"`vce2'"' != "" {
_svy_check_vce `vce'
local vce `s(vce)'
_svy_check_vce `vce2'
local vce2 `s(vce)'
if !`:list vce === vce2' {
di as err ///
`"option vce() of `r(cmdname)' conflicts with svy {it:vcetype}"'
exit 198
}
local vce2 // use the equivalent `vce'
}
local command `"`s(command)'"'
local version `"`s(version)'"'
local cmdname `"`s(cmdname)'"'
local cmdargs `"`s(anything)'"'
local wgt `"`s(wgt)'"'
local wtype `"`s(wtype)'"'
local wexp `"`s(wexp)'"'
local cmdif `"`s(if)'"'
local cmdin `"`s(in)'"'
local cmdopts `"`s(options)'"'
local rest `"`s(rest)'"'
local efopt `"`s(efopt)'"'
local level `"`s(level)'"'
local cmdmeff `"`cmdname' `cmdargs'"'
local cmd `"`cmdname' `cmdargs' \`cmdif' \`wgt'"'
// svy:tabulate handles the vce() option itself
if "`cmdname'" == "tabulate" {
if `"`vceopt'"' != "" {
local vceopt vce(`vce', `mse')
}
if `"`vcerest'"' != "" {
di as err ///
"{it:exp_list} is not allowed with svy: tabulate"
exit 198
}
// NOTE: `rest' should be empty, but pass it along for an
// error message
`version' ///
_svy_tabulate ///
`cmdargs' ///
`cmdif' ///
`cmdin', ///
`cmdopts' ///
level(`level') ///
`vceopt' ///
`options' ///
`rest'
exit
}
// cmdname is a svy replication VCE type
// Called with:
// . svy [, <options>] : <vcetype> <exp_list> [, <options>] : ...
capture _svy_check_vce `cmdname'
if !c(rc) {
if inlist("`cmdname'", "jknife", "jackknife") {
local cmdname jackknife
local jkopts svy
}
`version' ///
`cmdname' `vcerest' `cmdargs' ///
`cmdif' `cmdin', ///
`options' ///
`cmdopts' ///
`mse' ///
`jkopts' ///
level(`level') ///
`rest'
exit
}
if "`s(replay)'" != "" {
if "`e(cmdname)'" != "`cmdname'" {
error 301
}
Display, `options' `cmdopts' level(`level') `rest'
exit
}
// VCE -- variance covariance estimation type
if "`vce'" == "" {
quietly svyset
if "`r(vce)'" != "" {
local vce `r(vce)'
}
else local vce linearized
}
_svy_check_vce `vce'
if "`s(vce)'" != "" {
local vce `s(vce)'
// this will only be used by replication VCE type
local mse `s(mse)'
}
if `"`vcerest'"' == "" {
// check for unsupported commands
if "`vce'" == "brr" {
local prop svyb
}
else if "`vce'" == "jackknife" {
local prop svyj
}
else local prop svyr
local cmdprops : properties `cmdname'
if "`cmdprops'" == "" | ! `:list prop in cmdprops' {
di as err "{p}" ///
"`cmdname' is not supported by {cmd:svy} " ///
"with {cmd:vce(`vce')}; " ///
"see help {help survey##|_new:survey} for a " ///
"list of Stata estimation commands that are " ///
"supported by {cmd:svy}{p_end}"
exit 322
}
}
if "`vce'" != "linearized" {
// This command only computes linearized VCE, so call another
// prefix command for the other vcetypes.
if "`vce'" == "jackknife" {
local jkopts svy
}
`version' ///
`vce' `vcerest' ///
`cmdif' `cmdin', ///
`options' ///
`mse' ///
`jkopts' ///
level(`level') ///
: `cmdname' ///
`cmdargs', ///
`cmdopts' ///
`rest'
exit
}
_prefix_note `cmdname', `dots'
// The -_svy2- routines get special treatment.
is_svysum `cmdname'
if r(is_svysum) {
`version' ///
_svy_summarize ///
`cmdname' ///
`cmdargs' ///
`cmdif' ///
`cmdin', ///
`cmdopts' ///
level(`level') ///
svy /// -svy- switch
`options' /// -svy- options
// blank
exit
}
// -_svy_check_cmdopts- resets -s()-
_svy_check_cmdopts `cmdname', `cmdopts'
local cmdlog `s(log)'
local cmdopts1 `"`s(cmdopts1)'"'
local cmdopts2 `"`s(cmdopts2)'"'
local first `"`s(first)'"'
local 0 `", `options'"'
syntax [, ///
NOIsily ///
TRace ///
SUBpop(passthru) ///
noHeader /// -Display- options
noLegend /// ignored
noADJust ///
]
// -Display- options
local diopts `header' `legend' level(`level') `efopt'
// debug options
if "`trace'" != "" {
local noisily noisily
local traceon set trace on
local traceoff set trace off
}
if "`cmdlog'" != "" {
local noisily noisily
}
local qui = cond("`noisily'"=="", "*", "noisily")
if "`noisily'" != "" {
local dots nodots
}
// identify estimation sample
tempname touse
mark `touse' `wgt' `cmdif' `cmdin', zeroweight
if "`cmdname'" == "intreg" {
local vlist `anything'
gettoken y1 vlist : vlist
gettoken y2 vlist : vlist
markout `touse' `vlist'
quietly replace `touse' = 0 if missing(`y1') & missing(`y2')
}
else markout `touse' `anything'
// check/get svy settings
tempvar subuse wvar
_svy_setup `touse' `subuse', ///
cmdname(`cmdname') ///
svy ///
`subpop' ///
// blank
if !missing(r(N_strata_omit)) {
local N_omit = r(N_strata_omit)
}
if `"`subpop'"' != "" {
local subopt subpop(`subuse')
local subpop `"`r(subpop)'"'
}
if "`r(wtype)'" != "" {
local wtype `"`r(wtype)'"'
local wexp `"`r(wexp)'"'
local wgted " weighted"
}
if "`r(poststrata)'" != "" {
local posts `r(poststrata)'
svygen post double `wvar' ///
[`wtype'`wexp'] ///
if `touse', ///
posts(`r(poststrata)') ///
postw(`r(postweight)') ///
// blank
}
else if "`wtype'" != "" {
quietly gen double `wvar' `wexp'
}
else {
quietly gen double `wvar' = `touse'
}
local wgt [iw=`wvar']
local cmdif "if `subuse'"
// Execute command to get "observed" values
tempname b V Vsuest cns Vsrs npop
ClearE
// Compute point estimates
`traceon'
capture noisily quietly `noisily' ///
`version' `cmd' `cmdopts1' `cmdopts2' `rest'
`traceoff'
if c(rc) {
ClearAndExit `c(rc)' `cmdname'
}
// -markout- information only available from e()
MarkOut `touse'
// for e(N_pop)
sum `wvar' if `touse', mean
scalar `npop' = r(sum)
// Estimation results
matrix `b' = e(b)
matrix `V' = e(V)
// get constraints if present
capture mat `cns' = get(Cns)
if (c(rc)) local cns
`qui' di _n as txt "Computing scores..."
if inlist("`cmdname'", "ologit", "oprobit") & missing(e(version)) {
_prefix_relabel_eqns `b' `V'
}
tempname x
capture noisily quietly ///
predict double `x'* if e(sample), scores
if c(rc) {
di as err ///
"an error occured while attempting to compute scores"
exit 322
}
unab scvars : `x'*
foreach sc of local scvars {
quietly replace `sc' = 0 if missing(`sc')
}
if "`cmdname'" == "ivreg" {
preserve
quietly _ivreg_project `wgt' `cmdif'
}
matrix `Vsuest' = `V'
tempname nobs nstr npsu
quietly _robust2 `scvars' if `touse', svy `subopt' v(`V') vsrs(`Vsrs')
local wtype `"`r(wtype)'"'
local wexp `"`r(wexp)'"'
if "`cmdname'" == "ivreg" {
restore
}
local df = r(df_r)
scalar `nobs' = r(N)
scalar `nstr' = r(N_strata)
scalar `npsu' = r(N_clust)
local census = r(census)
local single = r(singleton)
if "`subopt'" != "" {
tempname nsub nsubpop
scalar `nsub' = r(N_sub)
scalar `nsubpop' = r(sum_wsub)
}
if "`posts'" != "" {
tempname npost
scalar `npost' = r(N_poststrata)
}
local cmdname `"`e(cmd)'"'
_prefix_title `"`e(cmd)'"' ""
if `"`r(title)'"' != "" {
local title `"Survey: `r(title)'"'
}
else local title "Survey data analysis"
// Get information from estimation command.
// At the very least, the following should be defined:
// e(depvar)
// e(predict)
// Some of the following may be defined when certain options are
// supplied to `cmdname':
// e(offset)
// make a copy of what is in -e()-, with some eXclusions
local xmac cmd _estimates_name chi2type
local xsca F chi2 chi2_c p p_c ll ll_c ll0 ll_0 df_m ///
r2_p r2_a rmse rss mss
local xmat b V
if "`cmdname'" == "heckman" {
local xsca `xsca' selambda
}
if "`cmdname'" == "intreg" {
local xsca `xsca' se_sigma
}
_e2r, xmac(`xmac') xsca(`xsca') xmat(`xmat')
if `"`e(depvar)'"' != "" {
local depvar `e(depvar)'
if `:word count `depvar'' == 1 {
local depname depname(`depvar')
}
}
local ncols = colsof(`b')
local cons _cons
local k_eq : coleq `b', quote
local k_eq : list uniq k_eq
local k_eq : word count `k_eq'
// Post -svy- results
ereturn clear
ereturn post `b' `V' `cns' , dof(`df') esample(`touse') `depname'
// restore the copied elements back to -e()-
_r2e
quietly svyset
_r2e
ereturn local settings
ereturn local brrweight
ereturn local jkrweight
// Summary information
ereturn scalar N_pop = `npop'
if "`nsub'`nsubpop'" != "" {
ereturn scalar N_sub = `nsub'
ereturn scalar N_subpop = `nsubpop'
}
ereturn scalar N = `nobs'
ereturn scalar N_strata = `nstr'
ereturn scalar N_psu = `npsu'
ereturn scalar df_r = `npsu' - `nstr'
ereturn scalar N_strata_omit = `N_omit'
if "`npost'" != "" {
ereturn scalar N_poststrata = `npost'
}
ereturn scalar singleton = `single'
ereturn scalar census = `census'
// number of equations
ereturn scalar k_eq = `k_eq'
// svy characteristics
ereturn local wtype `"`wtype'"'
ereturn local wexp `"`wexp'"'
ereturn local subpop `"`subpop'"'
ereturn local adjust `adjust'
ereturn local title `"`title'"'
ereturn local vcetype Linearized
ereturn local vce linearized
ereturn local estat_cmd svy_estat
// the model test should be one of the last things performed; it
// depends upon some things in -e()-
_prefix_model_test `cmdname', svy `adjust'
_svy_check_predict `cmdname'
// design & misspecification effects
if "`e(poststrata)'" == "" {
ereturn matrix V_srs `Vsrs'
if `"`subpop'"' != "" {
ereturn matrix V_srssub `Vsrs'sub
}
if `"`e(fpc1)'"' != "" {
ereturn matrix V_srswr `Vsrs'wr
if `"`subpop'"' != "" {
ereturn matrix V_srssubwr `Vsrs'subwr
}
}
}
ereturn matrix V_modelbased `Vsuest'
// NOTE: this must be the last thing posted to -e()-
ereturn local command `"`command'"'
ereturn local cmdname `cmdname'
ereturn local prefix svy
ereturn local cmd `cmdname'
`qui' di _n as txt "Survey results:"
if "`e(cmd)'`first'" == "ivregfirst" {
_svy_ivreg_first, `diopts'
}
Display, `diopts'
end
program MarkOut
args touse
local coleq : coleq e(b)
local coleq : list uniq coleq
local neq : word count `coleq'
if `"`e(offset)'"' != "" {
_get_offopt `e(offset)'
local vlist `s(offvar)'
}
if `neq' > 1 {
local colna : colna e(b)
local colna : list uniq colna
local cons _cons
local colna : list colna - cons
local vlist `vlist' `colna'
forval i = 1/`neq' {
if `"`e(offset`i')'"' != "" {
_get_offopt `e(offset`i')'
local vlist `vlist' `s(offvar)'
}
}
}
if "`e(cmd)'" == "ivreg" {
local vlist `vlist' `e(instd)' `e(insts)'
}
local vlist : list uniq vlist
markout `touse' `vlist'
end
program Display
// forward to display routines for special cases
is_svysum `e(cmd)'
if r(is_svysum) {
_svy_summarize `e(cmd)' `0'
exit
}
if "`e(cmdname)'" == "tabulate" {
_svy_tabulate `0'
exit
}
// general purpose -svy- results displayer
_prefix_display `0'
end
program ClearAndExit // rc cmdname
ClearE
_prefix_run_error `1' svy `2'
end
program ClearE, eclass
ereturn clear
end
exit
NOTES:
All updates to this file may require similar changes to the following files as
well; they perform -svy- compatible estimation on their own.
brr.ado
jackknife.ado
_jk_sum.ado -- uses _svy
ml_max.ado -- uses _robust2
_svy_summarize.ado -- uses _svy2
_svy_tabulate.ado -- uses _svy2
suest.ado -- uses _robust2
<end>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -