📄 _svy2.ado
字号:
*! version 1.0.8 15jun2005
program _svy2, rclass sort
version 9
syntax varlist(numeric) [if] [in] ///
[pw iw], ///
TYPE(name) ///
[ ///
SVY ZEROweight ///
over(passthru) ///
SUBpop(passthru) ///
B(name) /// output matrices
V(name) ///
VSRS(name) ///
CLuster(varname) ///
STDize(varname) ///
STDWeight(varname numeric) ///
noSTDRescale ///
TOUSE1(varname) ///
* ///
]
if "`svy'" != "" {
if "`cluster'" != "" {
di as err "option cluster() is not allowed with svy"
exit 198
}
if "`weight'" != "" {
di as err ///
"weights can only be supplied to {help svyset##|_new:svyset}"
exit 198
}
}
else if `"`options'"' != "" {
local 0 `", `options'"'
syntax [, BADOPTION ]
di as err "option badoption not allowed"
exit 198
}
if !inlist("`type'","mean","ratio","total") {
di as err "option type(`type') invalid"
exit 198
}
if "`type'" == "ratio" {
if mod(`:word count `varlist'',2) {
di as err ///
"invalid number of variables for type(ratio)"
exit 198
}
}
if "`svy'`zeroweight'" != "" | "`weight'" == "iweight" {
local zero zeroweight
}
marksample touse, `zero'
if "`cluster'" != "" {
markout `touse' `cluster', strok
if substr("`:type `cluster''",1,3) == "str" {
tempvar clvar
quietly egen `clvar' = group(`cluster') if `touse'
}
else local clvar `cluster'
}
// get the -svyset- information
tempvar subuse
_svy_setup `touse' `subuse', ///
`svy' ///
cmdname(`type') ///
`over' ///
`subpop' ///
// blank
return add
return local settings
return local brrweight
return local jkrweight
if "`svy'" != "" {
local stages = return(stages)
if missing(`stages') {
local stages 0
}
forval i = 1/`stages' {
if "`return(strata`i')'" == "" {
if "`strm'" == "" {
tempvar strm
quietly gen byte `strm' = . in 1
}
local strata `strata' `strm'
}
else {
local stri `return(strata`i')'
if substr("`:type `stri''",1,3) == "str" {
tempvar strtmp
qui egen `strtmp' = group(`stri') ///
if `touse'
local stri `strtmp'
}
local strata `strata' `stri'
}
if "`return(su`i')'" == "" {
tempvar obsid
quietly gen byte `obsid' = . in 1
local su `su' `obsid'
}
else {
local sui `return(su`i')'
if substr("`:type `sui''",1,3) == "str" {
tempvar sutmp
qui egen `sutmp' = group(`sui') ///
if `touse'
local sui `sutmp'
}
local su `su' `sui'
}
local fpc `fpc' `return(fpc`i')'
}
local posts `return(poststrata)'
local postw `return(postweight)'
local wtype `return(wtype)'
local wexp `"`return(wexp)'"'
gettoken equal wvar : wexp, parse(" =")
local wvar : list retok wvar
}
else {
local su `clvar'
local wtype `weight'
if "`wtype'" != "" {
tempvar wvar
quietly gen double `wvar' `exp'
local wexp `"`:list retok exp'"'
}
}
// poststratification
if "`posts'" != "" {
tempvar postid
sort `touse' `posts', stable
local by `touse' `posts'
quietly by `by': gen `postid' = _n==1 if `touse'
quietly replace `postid' = sum(`postid')
// do not return vsrs() for poststratfication
local vsrs
}
// standardization
if "`stdize'`stdweight'" != "" {
if "`type'" == "total" {
di as err ///
"options stdize() and stdweight() are not allowed for totals"
exit 198
}
if `:word count `stdize' `stdweight'' == 1 {
if "`stdweight'" == "" {
di as err ///
"option stdize() requires stdweight()"
}
if "`stdize'" == "" {
di as err ///
"option stdweight() requires stdize()"
}
exit 198
}
tempvar stdid stdw
sort `touse' `stdize', stable
capture by `touse' `stdize': ///
assert `stdweight' == `stdweight'[1] if `touse'
if c(rc) {
di as err ///
"{p 0 0 2}standardization weights must be constant within groups " ///
"identified by the stdize() option{p_end}"
exit 459
}
capture assert `stdweight' > 0 if `touse'
if c(rc) {
di as err "standardization weights must be positive"
exit 459
}
quietly by `touse' `stdize': gen `stdid' = _n==1 if `touse'
sum `stdweight' if `stdid' == 1, mean
quietly gen double `stdw' = `stdweight'/r(sum) if `touse'
quietly replace `stdid' = sum(`stdid')
return local stdize `stdize'
return local stdweight `stdweight'
if "`svy'" != "" {
// do not return vsrs() for svy standardization
local vsrs
}
}
else if "`stdrescale'" != "" {
di as err "option nostdrescale requires option stdize()"
exit 198
}
capture noisily quietly ///
mata: _svy2( ///
"`varlist'", ///
"`b'", "`v'", ///
"`vsrs'", ///
"`type'", ///
"`touse'", ///
"`subuse'", ///
"`stdid'", ///
"`stdw'", ///
"`stdrescale'", ///
"`wvar'", ///
"`fpc'", ///
"`strata'", ///
"`su'", ///
"`postid'", ///
"`postw'" ///
)
if c(rc) error c(rc)
// saved results
local sclist : r(scalars)
foreach sc of local sclist {
return scalar `sc' = r(`sc')
}
local matlist : r(matrices)
tempname m
foreach mat of local matlist {
matrix `m' = r(`mat')
return matrix `mat' `m'
}
if "`svy'" != "" {
return scalar df_r = r(N_psu) - r(N_strata)
if `"`return(subpop)'"' == "" {
return local N_sub
return local N_subpop
}
}
else {
if inlist("`wtype'", "fweight", "iweight") {
return scalar N = r(N_pop)
return scalar df_r = r(N_pop) - 1
}
else return scalar df_r = r(N) - 1
if "`cluster'" != "" {
return local cluster `cluster'
return scalar df_r = r(N_psu) - 1
return scalar N_clust = r(N_psu)
}
if "`wtype'" != "" {
return local wtype `wtype'
return local wexp `"`wexp'"'
}
return local N_sub
return local N_subpop
return local N_pop
return local N_strata
return local N_psu
}
if "`touse1'" != "" {
quietly replace `touse1' = `touse'
}
end
exit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -