📄 suest_8.ado
字号:
*! version 2.1.1 08dec2004
program suest_8
version 8
if replay() {
if "`e(cmd)'" != "suest" {
di as err "estimation results for suest not found"
exit 301
}
Display `0'
}
else {
Estimate `0'
}
end
program Estimate, eclass
version 8
syntax [anything] [, CLuster(varname) SVY minus(passthru) regressml * ]
local diopt `options'
est_expand `"`anything'"', min(1) default(.)
// code doesn't allow duplicates
DropDup names : "`r(names)'"
local nnames : word count `names'
if "`svy'" != "" & `"`cluster'"' != "" {
di as err "options cluster() and svy may not be combined"
exit 198
}
if "`svy'" != "" {
foreach key in pweight psu strata fpc {
svy_get `key' , optional
local svy_`key' `"`s(varname)'"'
}
if "`svy_psu'`svy_pweight" == "" {
di as err "data are not svy; neither psu nor pweight were set"
exit 198
}
}
// I may later have to add a cluster() spec -- eg clogit
local cluster_var `cluster' `svy_psu'
// extract information for selected models in -names-
tempvar touse esamplei esample
tempname hcurrent rank IVi V Vi b bi
_est hold `hcurrent' , restore nullok estsystem
scalar `rank' = 0
local i 0
foreach name of local names {
local ++i
nobreak {
if "`name'" != "." {
est_unhold `name' `esample'
}
else {
_est unhold `hcurrent'
}
capture noisily break {
local cmdi `e(cmd)'
local cmd2i `e(cmd2)'
local clustervari `e(clustvar)'
local vcetypei `e(vcetype)'
local wtypei `e(wtype)'
local wexpi `"`e(wexp)'"'
local scoresi `e(scorevars)'
GetMat `name' `bi' `Vi'
capt drop `esamplei'
gen byte `esamplei' = e(sample)
// fix some irregularities in
// Stata's handling of estimators
if "`cmdi'" == "regress" {
tempvar sc`i'_1 sc`i'_2
qui Fix_regress `bi' `Vi' "`regressml'" /*
*/ `sc`i'_1' `sc`i'_2'
local scoresi `sc`i'_1' `sc`i'_2'
}
else if inlist("`cmdi'", "oprobit", "ologit") ///
& missing(e(version)) {
Fix_order `bi' `Vi'
}
else if "`cmdi'" == "clogit" {
tempvar sc`i'
Fix_clogit "`sc`i''" "`cluster_var'"
local cluster_var `r(cluster_var)'
local scoresi `sc`i''
}
}
local rc = _rc
if "`name'" != "." {
est_hold `name' `esample'
}
else {
_est hold `hcurrent' , restore nullok estsystem
}
}
if `rc' {
exit `rc'
}
NotSupported "`cmdi'" "`cmd2i'"
capt assert `esamplei' == 0
if !_rc {
di as err "estimation sample of the model saved under `name' could not be restored"
exit 198
}
if substr("`cmdi'",1,3) == "svy" {
local nonsvy_name = substr("`cmdi'",4,.)
di as err "`name' was estimated as a survey command. "
di as err "re-estimate with nonsvy command with iweight's "
di as err "and score(), but no cluster()."
exit 498
}
if "`clustvari'" != "" {
di as err "`name' was estimated with cluster(`clustvari'). "
di as err "re-estimate without the cluster() option, and "
di as err "specify the cluster() option with suest."
exit 498
}
if "`vcetypei'" != "" {
di as err ///
"`name' was estimated with a non-standard vce (`vcetypei')"
exit 498
}
// modifies equation names into name_eq or name#
if `nnames' > 1 {
FixEquationNames `name' `bi' `Vi'
}
else {
NoFixEquationNames `name' `bi' `Vi'
}
local neq`i' `r(neq)'
local eqnames`i' `r(eqnames)'
local newfullnames `"`newfullnames' `:colfullnames `bi''"'
// check score vars
if "`scoresi'" == "" {
di as err "score variables were not saved by model `name'"
di as err "re-estimate with the score() option"
exit 198
}
capt confirm var `scoresi'
if _rc {
di as err ///
"score variables for model `name' no longer exist in the data"
exit 198
}
local nscoresi : word count `scoresi'
if `neq`i'' != `nscoresi' {
di as err "number of score variables does not match " ///
"number of equations in model `name'"
exit 198
}
foreach v of local scoresi {
// scores should not be missing in e(sample)
capt assert !missing(`v') if `esamplei'
if _rc {
di as err "score variable(s) for model `name' " ///
"contain missing values"
exit 498
}
// out of sample values of score variables are set zero
qui replace `v' = 0 if missing(`v')
}
// check svy weights
if "`svy'" != "" {
Check_svy_weight "`name'" "`svy_pweight'" "`wtypei'"
}
// store-append b/V/weight
// necessary if Vi was constrained estimator
mat `IVi' = syminv(`Vi')
scalar `rank' = `rank' + (colsof(`Vi') - diag0cnt(`IVi'))
if `i' == 1 {
gen byte `touse' = `esamplei'
local wtype `wtypei'
local wexp `"`wexpi'"'
mat `b' = `bi'
mat `V' = `Vi'
}
else {
// union of samples of models
qui replace `touse' = `touse' | `esamplei'
// the weights in e() should be the same as in model 1
CheckWeight "`wtype'" "`wexp'" "`wtypei'" "`wexpi'"
// append the bi and Vi
mat `b' = `b' , `bi'
local nv = colsof(`V')
local nvi = colsof(`Vi')
mat `V' = (`V', J(`nv',`nvi',0) \ J(`nvi',`nv',0), `Vi')
}
// score vars all models
local scores `scores' `scoresi'
} // loop over models
// use _robust to compute the sandwich estimator
mat colnames `b' = `newfullnames'
mat colnames `V' = `newfullnames'
mat rownames `V' = `newfullnames'
if "`wtype'" != "" {
local wght `"[`wtype'`wexp']"'
}
if "`svy'" != "" {
local svyopt "zeroweight"
// if "`svy_psu'" != "" {
// local svyopt `svyopt' psu(`svy_psu')
// }
if "`svy_strata'" != "" {
local svyopt `svyopt' strata(`svy_strata')
}
if "`svy_fpc'" != "" {
local svyopt `svyopt' fpc(`svy_fpc')
}
local iftouse
local subpop subpop(`touse')
}
else {
local iftouse "if `touse'"
local subpop
}
if "`cluster_var'" != "" {
local clopt `"cluster(`cluster_var')"'
}
// _robust is the work horse for suest !
capt noi _robust `scores' `iftouse' `wght', ///
var(`V') `clopt' `svyopt' `subpop' `minus'
local rc = _rc
if `rc' {
if `rc' == 1 {
error 1
}
di as err "_robust failed to compute cross model sandwich"
di as err "likely an internal error in suest"
exit `rc'
}
if "`svy'" != "" {
tempname N N_strata N_psu N_pop
scalar `N' = r(N) // number of obs
scalar `N_strata' = r(N_strata) // number of strata
scalar `N_psu' = r(N_clust) // number of PSU
scalar `N_pop' = r(sum_w) // population size
if r(N_sub) != . {
tempname N_sub N_subpop
scalar `N_sub' = r(N_sub) // # subpop. obs
scalar `N_subpop' = r(sum_wsub) // subpop. size
}
local Nobs = scalar(`N')
}
else {
qui count if `touse'
local Nobs = r(N)
}
// post results
eret post `b' `V', esample(`touse') obs(`Nobs')
if "`svy'" != "" {
eret local svy svy
eret local pweight `svy_pweight'
eret local psu `svy_psu'
eret local strata `svy_strata'
eret local fpc `svy_fpc'
eret scalar N_strata = `N_strata' // number of strata
eret scalar N_psu = `N_psu' // number of PSUs
eret scalar N_pop = `N_pop' // population size
if "`N_sub'" != "" {
eret scalar N_sub = `N_sub' // # subpop. obs
eret scalar N_subpop = `N_subpop' // subpop. size
}
eret scalar df_r = `N_psu' - `N_strata'
}
else if "`cluster_var'" != "" {
eret local vcetype Robust
eret local clustvar `cluster_var'
}
else {
eret local vcetype Robust
}
eret scalar rank = `rank'
eret local wtype `wtype'
eret local wexp `"`wexp'"'
eret local names `names'
forvalues ieq = `nnames'(-1)1 {
eret local eqnames`ieq' `eqnames`ieq''
}
eret local cmd suest
_est unhold `hcurrent', not
Display , `diopt'
end
// ============================================================================
// display routines
// ============================================================================
program Display_svy_header
args nnames clicktxt adjust
if `nnames'>1 {
di as txt "Simultaneous survey results for `clicktxt'"
}
else {
di as txt "Survey results for `clicktxt'"
}
svy_head `e(N)' `e(N_strata)' `e(N_psu)' `e(N_pop)' . . ///
"" "`e(pweight)'" "`e(strata)'" "`e(psu)'" "`e(fpc)'" ///
"" "" ""
if `e(N_sub)' != . {
di
di as txt "Subpopulation no. of obs =" as res %10.0f e(N_sub)
di as txt "Subpopulation size =" as res %10.4f e(N_subpop)
}
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -