📄 svytab.ado
字号:
if `i1' >= 1 {
mat `A' = `A'[1..`i1',.]
mat `A' = `A' \ `Z'
}
else mat `A' = `Z'
`matAB'
end
program IndCon
args r c X1 X2
tempname A X J
local r1 = `r' - 1
local c1 = `c' - 1
/* Build first `r'-1 columns: dummies for main effects of first variable. */
mat `A' = I(`r1')
mat `J' = J(1,`r1',-1)
mat `A' = `A' \ `J'
mat `J' = J(`c',1,1)
mat `X1' = `A' # `J'
/* Build next `c'-1 columns: dummies for main effects of second variable. */
mat `A' = I(`c1')
mat `J' = J(1,`c1',-1)
mat `A' = `A' \ `J'
mat `J' = J(`r',1,1)
mat `X' = `J' # `A'
mat `X1' = `X1' , `X'
/* Build last (`r'-1)*(`c'-1) columns: interaction terms. */
mat `X2' = I(`r1')
mat `X2' = `X2' # `A'
mat `J' = J(1,`r1',-1)
mat `A' = `J' # `A'
mat `X2' = `X2' \ `A'
end
program LabelMat
args nrow ncol
macro shift 2
local i 1
while `i' <= `nrow' {
local j 1
while `j' <= `ncol' {
local names "`names' p`i'`j'"
local j = `j' + 1
}
local i = `i' + 1
}
local i 1
while "``i''"!="" {
mat colnames ``i'' = `names'
if rowsof(``i'') > 1 | `i'==3 {
mat rownames ``i'' = `names'
}
local i = `i' + 1
}
end
program ValueLab
args var stub numb
local dim = colsof(`stub')
local lab : value label `var'
if "`lab'"=="" {
exit
}
local i 1
while `i' <= `dim' {
local x = `stub'[1,`i']
local xlab : label `lab' `x'
if `"`xlab'"'==`"`x'"' {
local list `"`list' hoborxqc"'
/* hoborxqc = flag for not labeled */
}
else {
if `"`numb'"' == "1" {
/* S_VYstw from -tabdisp, stubwidth()- */
local xlab = trim(substr(`"`xlab'"',1,$S_VYstw))
}
else {
/* S_VYclw from -tabdisp, cellwidth()- */
local xlab = trim(substr(`"`xlab'"',1,$S_VYclw))
}
/* Replace periods with commas and colons with
semicolons so -xlab- can safely be matrix names */
local j 1
while `j' <= 8 {
local xpr = index(`"`xlab'"',".")
local xcl = index(`"`xlab'"',":")
if `xpr'==0 & `xcl'==0 {
local j 9 /* exit the loop */
}
if `xpr' ~= 0 {
loc xlab=substr(`"`xlab'"',1,`xpr'-1)/*
*/ + "," + substr(`"`xlab'"',`xpr'+1,.)
}
if `xcl' ~= 0 {
loc xlab=substr(`"`xlab'"',1,`xcl'-1)/*
*/ + ";" + substr(`"`xlab'"',`xcl'+1,.)
}
local j = `j' + 1
}
local list `"`list' "`xlab'""'
}
local i = `i' + 1
}
mat colnames `stub' = `list'
global S_VYlab`numb' "label"
end
program SetMiss
local i 1
while "``i''"!="" {
scalar ``i'' = .
local i = `i' + 1
}
end
program Total
args total y doit subvar
quietly {
if "$S_VYwgt"!="" {
tempvar x
gen double `x' = ($S_VYexp)*`y'
}
else local x "`y'"
if "$S_VYsub"=="" {
summarize `x' if `doit', meanonly
}
else summarize `x' if `doit' & `subvar', meanonly
scalar `total' = _result(18)
}
end
/*----------------------------- Parse program --------------------------------*/
program SvyParse
gettoken doit 0 : 0
gettoken subvar 0 : 0
/* Parse. */
syntax varlist(min=2 max=2 numeric) /*
*/ [pweight iweight/] /* see _svy_newrule.ado
*/ [if] [in] /*
*/ [, /*
*/ SUBpop(string asis) /*
*/ SRSsubpop /*
*/ MISSing /*
*/ TAB(varname numeric) /*
*/ PEArson /*
*/ NULl /*
*/ LR /*
*/ WALD /*
*/ LLWALD /*
*/ noADJust /*
*/ UNADJust /*
*/ FULLER /*
*/ COUnt /*
*/ CELl /*
*/ ROW /*
*/ COLumn /*
*/ SE /*
*/ CI /*
*/ DEFF /*
*/ DEFT /*
*/ OBS /*
*/ noLABel /*
*/ noMARGinals /*
*/ PERcent /*
*/ PROPortion /*
*/ Level(cilevel) /*
*/ FORmat(string) /*
*/ VERTical /*
*/ CELLWidth(integer -1) /* -tabdisp- options
*/ CSEPwidth(passthru) /*
*/ STUBWidth(integer -1) /*
*/ NOTABle /*
*/ STRata(passthru) /* see _svy_newrule.ado
*/ PSU(passthru) /* see _svy_newrule.ado
*/ FPC(passthru) /* see _svy_newrule.ado
*/ ]
_svy_newrule , `weight' `strata' `psu' `fpc'
if "`null'"!="" & "`pearson'`lr'"=="" & "`wald'`llwald'"!="" {
di as err "null option modifies pearson and lr options only"
exit 198
}
if "`unadjust'"!="" {
local adjust "noadjust"
}
if "`adjust'"!="" & "`wald'`llwald'"=="" {
di as err /*
*/ "noadjust option modifies wald and llwald options only"
exit 198
}
local ncell : word count `obs' `se' `deff' `deft'
if "`ci'"!="" & "`vertical'"!="" & `ncell' > 2 {
di as err "only 2 of se, deff, deft, and obs can " /*
*/ "be specified when ci and vertical" _n "specified"
exit 198
}
if "`ci'"!="" {
local ncell = `ncell' + 1
}
if "`vertical'"=="" & `ncell' > 4 {
di as err "only 4 of se, ci, deff, deft, and obs can " /*
*/ "be specified"
exit 198
}
if "`format'"!="" {
quietly di `format' 0
local format "format(`format')"
}
if "`cell'`count'`row'`column'"=="" {
local cell cell
}
if "`se'`ci'`deff'`deft'"!="" {
local nopts : word count `cell' `count' `row' `column'
if `nopts' > 1 {
local opt : word 1 of `se' `ci' `deff' `deft'
di as err "only one of cell, count, row, or column " /*
*/ "can be specified when `opt' specified"
exit 198
}
}
if "`percent'"!="" & "`proportion'"!="" {
di as err "both percent and proportion cannot be specified"
exit 198
}
if "`srssubpop'"!="" & "`subpop'"=="" {
di as err "srssubpop can only be specified when subpop() " /*
*/ "is specified"
exit 198
}
if "`notable'" != "" {
if "`proportion'" != "" {
local noopts proportion
}
if "`percent'" != "" {
local noopts `noopts' percent
}
if "`label'" != "" {
local noopts `noopts' nolabel
}
if "`marginals'" != "" {
local noopts `noopts' nomarginals
}
if "`format'" != "" {
local noopts `noopts' format()
}
if "`vertical'" != "" {
local noopts `noopts' vertical
}
if `cellwidth' > 0 {
local noopts `noopts' cellwidth()
}
if "`csepwidth'" != "" {
local noopts `noopts' csepwidth()
}
if `stubwidth' > 0 {
local noopts `noopts' stubwidth()
}
if "`noopts'" != "" {
di as err ///
"options 'notable `noopts'' may not be combined"
exit 198
}
}
/* Set global macros. */
macro drop S_VY*
global S_VYvar1 : word 1 of `varlist'
global S_VYvar2 : word 2 of `varlist'
if `cellwidth' < 4 {
global S_VYclw 8
local cellwidth
}
else {
global S_VYclw `cellwidth'
local cellwidth cellwidth(`cellwidth')
}
if `stubwidth' < 4 {
global S_VYstw 8
local stubwidth
}
else {
if `stubwidth' > 32 {
local stubwidth 32
}
global S_VYstw `stubwidth'
local stubwidth stubwidth(`stubwidth')
}
global S_VYdopt `pearson' `null' `lr' `wald' `llwald' /*
*/ `adjust' `fuller' /*
*/ `label' `se' `deff' `deft' `marginals' `percent' /*
*/ `proportion' `cell' `count' `row' `column' level(`level') `ci' /*
*/ `format' `vertical' `obs' `cellwidth' `csepwidth' `stubwidth' /*
*/ `notable'
if "`se'`ci'`deff'`deft'"!="" {
global S_VYse "`count'`row'`column'"
}
global S_VYwald "`wald'"
global S_VYmiss "`missing'"
global S_VYtab "`tab'"
global S_VYopt /* erase macro */
/* Get weights, strata, psu, and fpc. */
quietly svyset
if "`r(wtype)'" != "" {
global S_VYexp `r(`r(wtype)')'
global S_VYwgt `r(wtype)'
}
local w $S_VYexp
local wt [`r(wtype)'`r(wexp)']
if "`r(strata)'"!="" {
global S_VYstr `r(strata)'
global S_VYopt $S_VYopt str($S_VYstr)
}
if "`r(psu)'"!="" {
global S_VYpsu `r(psu)'
global S_VYopt $S_VYopt psu($S_VYpsu)
}
if "`r(fpc)'"!="" {
global S_VYfpc `r(fpc)'
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 `w' >= 0 if `doit'
if _rc {
error 402
}
}
/* Markout. */
markout `doit' `w' $S_VYfpc `tab'
markout `doit' $S_VYstr $S_VYpsu, strok
if "`missing'"=="" {
markout `doit' $S_VYvar1 $S_VYvar2
}
/* Compute total #obs. */
qui count if `doit'
if _result(1) == 0 {
error 2000
}
global S_VYnobs = _result(1)
if "`subpop'`srssubpop'"=="" {
exit
}
/* Only here if subpop() specified. */
// WARNING: sort order should not change prior to calling
// svy_sub; it accepts [in range].
svy_sub `doit' ///
`subvar' ///
"$S_VYexp" ///
"$S_VYstr" ///
"" "" /// not byable
"`srssubpop'" ///
`subpop'
global S_VYsrss `r(srssubpop)' /* srssubpop option flag */
global S_VYsub `subpop'
global S_VYsvar "`subvar'"
global S_VYopt "$S_VYopt by(`subvar') nby(1)"
end
/*--------------------------- Save results program ---------------------------*/
program SvySave, eclass
eret scalar N = $S_VYnobs
eret scalar N_strata = $S_VYnstr
eret scalar N_psu = $S_VYnpsu
eret scalar N_pop = `1'
if "`2'"!="" {
eret scalar N_sub = $S_VYosub
eret scalar N_subpop = `2'
}
eret local setype = cond("$S_VYse"!="", "$S_VYse", "cell")
eret local wtype `"$S_VYwgt"'
eret local wexp `"$S_VYexp"'
eret local strata `"$S_VYstr"'
eret local psu `"$S_VYpsu"'
eret local fpc `"$S_VYfpc"'
eret local subpop `"$S_VYsub"'
eret local rowvar `"$S_VYvar1"'
eret local colvar `"$S_VYvar2"'
local vlab1 : variable label $S_VYvar1
local vlab2 : variable label $S_VYvar2
eret local rowvlab `"`vlab1'"'
eret local colvlab `"`vlab2'"'
eret local rowlab `"$S_VYlab1"'
eret local collab `"$S_VYlab2"'
eret local tab `"$S_VYtab"'
eret scalar r = `3' /* # of rows */
eret scalar c = `4' /* # of columns */
eret scalar total = `5' /* e(N_pop) or total of tab variable */
if `6' {
eret local zero "zero marginal"
}
eret scalar mgdeff = `7' /* mean deff eigenvalue */
eret scalar cvgdeff = `8' /* c.v. of deff eigenvalues */
eret matrix Obs `9' /* cell # of observations */
if "$S_VYsub" != "" {
eret matrix ObsSub `18' /* cell # of observations in Sub */
}
VectoMat `10' e(r) e(c) /* convert cell proportions to matrix */
eret matrix Prop `10' /* proportions */
eret matrix Row `11' /* row stub */
eret matrix Col `12' /* column stub */
eret matrix V_row `13' /* variance of row margins */
eret matrix V_col `14' /* variance of column margins */
MakeDeff `15' /* make matrices e(Deff) and e(Deft) */
MakeDeff `16' _row /* matrices e(Deff_row) and e(Deft_row) */
MakeDeff `17' _col /* matrices e(Deff_col) and e(Deft_col) */
eret local cmd "svytab"
end
program VectoMat /* converts `A' from vector to matrix */
args A nrow ncol
tempname row New
mat `New' = `A'[1,1..`ncol']
local i = `ncol' + 1
while `i' <= `nrow'*`ncol' {
local end = `i' + `ncol' - 1
mat `row' = `A'[1,`i'..`end']
mat `New' = `New' \ `row'
local i = `end' + 1
}
mat `A' = `New'
end
program MakeDeff, eclass
args Vdeff name
tempname v f deff deft
matrix `v' = vecdiag(e(V`name'))
matrix `deff' = `v'
matrix `deft' = `v'
if "$S_VYfpc"!="" { /* remove fpc from `Vdeff' for deft (which is wr) */
tempname c
if "$S_VYsrss"=="" {
scalar `c' = 1 - e(N)/e(N_pop)
}
else scalar `c' = 1 - e(N_sub)/e(N_subpop)
}
else local c 1
local dim = colsof(`v')
local i 1
while `i' <= `dim' {
scalar `f' = `v'[1,`i']/`Vdeff'[`i',`i']
matrix `deff'[1,`i'] = cond(`f'<., `f', 0)
scalar `f' = sqrt(`c'*`f')
matrix `deft'[1,`i'] = cond(`f'<., `f', 0)
local i = `i' + 1
}
eret matrix Deff`name' `deff'
eret matrix Deft`name' `deft'
end
/*---------------------------- Display programs ------------------------------*/
program Display
syntax [, PEArson NULl LR WALD LLWALD noADJust UNADJust FULLER /*
*/ COUnt CELl ROW COLumn SE CI DEFF DEFT OBS /*
*/ noLABel noMARGinals PERcent PROPortion /*
*/ Level(cilevel) FORmat(string) VERTical /*
*/ NOTABle /*
*/ CELLWidth(passthru) /* tabdisp opts
*/ CSEPwidth(passthru) /*
*/ STUBWidth(passthru) /*
*/ ]
/* Check for syntax errors. */
if "`null'"!="" & "`pearson'`lr'"=="" & "`wald'`llwald'"!="" {
di as err "null option modifies pearson and lr options only"
exit 198
}
if "`unadjust'"!="" {
local adjust "noadjust"
}
if "`adjust'"!="" & "`wald'`llwald'"=="" {
di as err /*
*/ "noadjust option modifies wald and llwald options only"
exit 198
}
if "`wald'"!="" & "`e(F_Wald)'"=="" {
di as err "wald not available on redisplay" _n /*
*/ "it must be specified at run time"
exit 198
}
if "`format'"!="" {
quietly di `format' 0
}
if "`cell'`count'`row'`column'"=="" {
local cell cell
}
if "`se'`ci'`deff'`deft'"!="" {
local nopts : word count `cell' `count' `row' `column'
if `nopts' > 1 {
local opt : word 1 of `se' `deff' `deft'
di as err "only one of cell, count, row, or column " /*
*/ "can be specified when `opt' specified"
exit 198
}
if "`e(setype)'"!="`cell'`count'`row'`column'" {
if "`se'"!="" {
di as err "standard errors are only " /*
*/ "available for `e(setype)'s" _n /*
*/ "to compute `cell'`count'`row'`column' " /*
*/ "standard errors, rerun command with " /*
*/ "`cell'`count'`row'`column' and se options"
exit 111
}
if "`ci'"!="" {
di as err "confidence intervals are only " /*
*/ "available for `e(setype)'s" _n /*
*/ "to compute `cell'`count'`row'`column' " /*
*/ "confidence intervals, rerun command " /*
*/ "with `cell'`count'`row'`column' and " /*
*/ "ci options"
exit 111
}
local deff : word 1 of `deff' `deft'
di as err "`deff' is only available for `e(setype)'s" /*
*/ _n "to compute `cell'`count'`row'`column' `deff'," /*
*/ " rerun command with `cell'`count'`row'`column' " /*
*/ "and `deff' options"
exit 111
}
}
if "`percent'"!="" & "`proportion'"!="" {
di as err "both percent and proportion cannot be specified"
exit 198
}
if "`notable'" != "" {
if "`proportion'" != "" {
local noopts proportion
}
if "`percent'" != "" {
local noopts `noopts' percent
}
if "`label'" != "" {
local noopts `noopts' nolabel
}
if "`marginals'" != "" {
local noopts `noopts' nomarginals
}
if "`format'" != "" {
local noopts `noopts' format()
}
if "`vertical'" != "" {
local noopts `noopts' vertical
}
if "`cellwidth'" != "" {
local noopts `noopts' cellwidth()
}
if "`csepwidth'" != "" {
local noopts `noopts' csepwidth()
}
if "`stubwidth'" != "" {
local noopts `noopts' stubwidth()
}
if "`noopts'" != "" {
di as err ///
"options 'notable `noopts'' may not be combined"
exit 198
}
local qui quietly
}
/* Display header. */
`qui' svy_header , noftest subexp(`e(subpop)')
/* Display tabulation. */
`qui' DispTab "`label'" "`se'" "`deff'" "`deft'" "`marginals'" /*
*/ "`percent'" /*
*/ "`proportion'" "`cell'" "`count'" "`row'" "`column'" /*
*/ `"`=strsubdp("`level'")'"' /*
*/ "`ci'" "`format'" "`vertical'" "`obs'" /*
*/ "`cellwidth'" /*
*/ "`csepwidth'" /*
*/ "`stubwidth'" /*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -