📄 svytab_7.ado
字号:
*/ "observation in subpopulation" _n /*
*/ "subpop() = 0 indicates observation not in " /*
*/ "subpopulation"
local samemsg 1
}
qui count if `subpop'!=1 & `subpop'!=0 & `doit'
if _result(1) > 0 {
if "`samemsg'"=="" {
di in blu _n "Note: subpop() takes on " /*
*/ "values other than 0 and 1" _n /*
*/ "subpop() ~= 0 indicates subpopulation"
}
global S_VYsub "`subpop'~=0"
}
else global S_VYsub "`subpop'==1"
qui gen byte `subvar' = (`subpop'!=0) if `doit'
global S_VYopt "$S_VYopt by(`subvar') nby(1)"
end
/*--------------------------- Save results program ---------------------------*/
program define SvySave, eclass
est scalar N = $S_VYnobs
est scalar N_strata = $S_VYnstr
est scalar N_psu = $S_VYnpsu
est scalar N_pop = `1'
if "`2'"!="" {
est scalar N_sub = $S_VYosub
est scalar N_subpop = `2'
}
est local setype = cond("$S_VYse"!="", "$S_VYse", "cell")
est local wtype `"$S_VYwgt"'
est local wexp `"$S_VYexp"'
est local strata `"$S_VYstr"'
est local psu `"$S_VYpsu"'
est local fpc `"$S_VYfpc"'
est local subpop `"$S_VYsub"'
est local rowvar `"$S_VYvar1"'
est local colvar `"$S_VYvar2"'
local vlab1 : variable label $S_VYvar1
local vlab2 : variable label $S_VYvar2
est local rowvlab `"`vlab1'"'
est local colvlab `"`vlab2'"'
est local rowlab `"$S_VYlab1"'
est local collab `"$S_VYlab2"'
est local tab `"$S_VYtab"'
est scalar r = `3' /* # of rows */
est scalar c = `4' /* # of columns */
est scalar total = `5' /* e(N_pop) or total of tab variable */
if `6' {
est local zero "zero marginal"
}
est scalar mgdeff = `7' /* mean deff eigenvalue */
est scalar cvgdeff = `8' /* c.v. of deff eigenvalues */
est matrix Obs `9' /* cell # of observations */
if "$S_VYsub" != "" {
est matrix ObsSub `18' /* cell # of observations in Sub */
}
VectoMat `10' e(r) e(c) /* convert cell proportions to matrix */
est matrix Prop `10' /* proportions */
est matrix Row `11' /* row stub */
est matrix Col `12' /* column stub */
est matrix V_row `13' /* variance of row margins */
est 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) */
est local cmd "svytab"
end
program define 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 define 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
}
est matrix Deff`name' `deff'
est matrix Deft`name' `deft'
end
/*---------------------------- Display programs ------------------------------*/
program define 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 /*
*/ CELLWidth(passthru) /* tabdisp opts
*/ CSEPwidth(passthru) /*
*/ STUBWidth(passthru) /*
*/ ]
/* Check for syntax errors. */
if "`null'"!="" & "`pearson'`lr'"=="" & "`wald'`llwald'"!="" {
di in red "null option modifies pearson and lr options only"
exit 198
}
if "`unadjus'"!="" { local adjust "noadjust" }
if "`adjust'"!="" & "`wald'`llwald'"=="" {
di in red /*
*/ "noadjust option modifies wald and llwald options only"
exit 198
}
if "`wald'"!="" & "`e(F_Wald)'"=="" {
di in red "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 in red "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 in red "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 in red "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 in red "`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'"!="" & "`proport'"!="" {
di in red "both percent and proportion cannot be specified"
exit 198
}
/* Display header. */
svy_head e(N) e(N_strata) e(N_psu) e(N_pop) "`e(N_sub)'" /*
*/ "`e(N_subpop)'" "`e(wtype)'" "`e(wexp)'" "`e(strata)'" /*
*/ "`e(psu)'" "`e(fpc)'" "`e(subpop)'"
/* Display tabulation. */
DispTab "`label'" "`se'" "`deff'" "`deft'" "`margina'" "`percent'" /*
*/ "`proport'" "`cell'" "`count'" "`row'" "`column'" "`level'" "`ci'" /*
*/ "`format'" "`vertica'" "`obs'" /*
*/ "`cellwid'" /*
*/ "`csepwid'" /*
*/ "`stubwid'" /*
*/
/* Display error and warning messages. */
if "`e(zero)'"!="" {
di _n in blu " Table contains a zero in the marginals." _n /*
*/ " Statistics cannot be computed."
}
if e(r) == 1 {
di _n in blu " Only one row category." _n /*
*/ " Statistics cannot be computed."
}
if e(c) == 1 {
di _n in blu " Only one column category." _n /*
*/ " Statistics cannot be computed."
}
if e(N_psu) - e(N_strata) < (e(r) - 1)*(e(c) - 1) {
di _n in blu " Note: variance estimate degrees of freedom " /*
*/ "= " in ye e(N_psu) - e(N_strata) _n in blu /*
*/ " are less than" _n /*
*/ " nominal table degrees of freedom = " /*
*/ in ye (e(r) - 1)*(e(c) - 1)
}
/* Display statistics. */
if "`pearson'`lr'`wald'`llwald'`fuller'"=="" {
local pearson "pearson"
}
if "`pearson'"!="" {
if "`null'"!="" {
DispStat "Pearson:" Pear Penl
}
else DispStat "Pearson:" Pear
}
if "`lr'"!="" {
if "`null'"!="" {
DispStat "Likelihood ratio:" LR LRnl
}
else DispStat "Likelihood ratio:" LR
}
if "`fuller'"!="" {
DispStat "Pearson (Fuller version):" Full
}
if "`wald'"!="" {
DispWald "Wald (Pearson):" Wald `adjust'
}
if "`llwald'"!="" {
DispWald "Wald (log-linear):" LLW `adjust'
}
/* Display mean generalized deff and cv. */
if "`deff'`deft'"!="" {
di _n in gr " Mean generalized deff" _col(35) "= " in ye /*
*/ %9.4f e(mgdeff) _n in gr " CV of generalized deffs" /*
*/ _col(35) "= " in ye %9.4f e(cvgdeff)
}
/* Display FPC warning. */
if "`e(fpc)'"!="" { /* print FPC note */
di _n in blu "Finite population correction (FPC) assumes " /*
*/ "simple random sampling without " _n /*
*/ "replacement of PSUs within each stratum with no " /*
*/ "subsampling within PSUs."
}
end
program define DispTab
args label se deff deft margina percent proport cell count row /*
*/ column level ci format vertica obs /*
*/ cellwid /*
*/ csepwid /*
*/ stubwid /*
*/
if "`format'"=="" { local format "%6.0g" }
/* Preserve, then make data set for -tabdisp-. */
preserve
drop _all
tempname b matr matc Row Col
local ncat = e(r)*e(c)
local iii "int((_n-1)/e(c))+1" /* row position */
local jjj "mod(_n-1,e(c))+1" /* column position */
mat `b' = e(Prop)
local bij "`b'[`iii',`jjj']"
mat `matr' = J(1,e(c),1)
mat `matr' = `matr'*`b'' /* row marginals */
mat `matc' = J(1,e(r),1)
mat `matc' = `matc'*`b' /* column marginals */
mat `Row' = e(Row)
mat `Col' = e(Col)
quietly {
/* Set obs. */
if "`margina'"=="" {
local Nr1 = `ncat' + 1 /* start row marginals */
local Nr2 = `ncat'+e(r) /* end row marginals */
local Nc1 = `Nr2' + 1 /* start column marginals */
local Nc2 = `Nr2' + e(c) /* end column marginals */
local N = `Nc2' + 1 /* end */
local total "total"
}
else local N `ncat'
set obs `N'
/* Make stubs of table. */
gen double `e(rowvar)' = `Row'[1,`iii'] in 1/`ncat'
gen double `e(colvar)' = `Col'[1,`jjj'] in 1/`ncat'
if "`margina'"=="" {
replace `e(rowvar)' = `Row'[1,_n-`Nr1'+1] in `Nr1'/`Nr2'
replace `e(colvar)' = `Col'[1,_n-`Nc1'+1] in `Nc1'/`Nc2'
}
/* Make variable labels and value labels for stub variables. */
if "`label'"=="" {
label variable `e(rowvar)' `"`e(rowvlab)'"'
label variable `e(colvar)' `"`e(colvlab)'"'
if "`e(rowlab)'"!="" {
tempname lblname
MakeLab `lblname' `e(rowvar)' `Row'
}
if "`e(collab)'"!="" {
tempname lblname
MakeLab `lblname' `e(colvar)' `Col'
}
}
/* Handle display of missing values if necessary. */
if `Row'[1,e(r)]>=. {
tempname lblname
FixMiss `lblname' `e(rowvar)' `Row'[1,e(r)-1] /*
*/ `ncat' `Nr1' `Nr2'
}
if `Col'[1,e(c)]>=. {
tempname lblname
FixMiss `lblname' `e(colvar)' `Col'[1,e(c)-1] /*
*/ `ncat' `Nc1' `Nc2'
}
/* Make counts and proportions or percents. */
local keyi 1
if "`count'"!="" {
tempvar xc
gen double `xc' = e(total)*`bij' in 1/`ncat'
if "`margina'"=="" {
replace `xc' = e(total)*`matr'[1,_n-`Nr1'+1] /*
*/ in `Nr1'/`Nr2'
replace `xc' = e(total)*`matc'[1,_n-`Nc1'+1] /*
*/ in `Nc1'/`Nc2'
replace `xc' = e(total) in l
}
if "`e(wtype)'"!="" {
local key`keyi' "weighted counts"
}
else local key`keyi' "counts"
local keyi = `keyi' + 1
}
if "`percent'"!="" {
local porp 100 /* percent or proportion */
local kporp "percentages"
}
else {
local porp 1
local kporp "proportions"
}
if "`cell'"!="" {
tempvar xp
gen double `xp' = `porp'*`bij' in 1/`ncat'
if "`margina'"=="" {
replace `xp' = `porp'*`matr'[1,_n-`Nr1'+1] /*
*/ in `Nr1'/`Nr2'
replace `xp' = `porp'*`matc'[1,_n-`Nc1'+1] /*
*/ in `Nc1'/`Nc2'
replace `xp' = `porp' in l
}
local key`keyi' "cell `kporp'"
local keyi = `keyi' + 1
}
if "`row'"!="" {
tempvar xrow
gen double `xrow' = `porp'*`bij'/`matr'[1,`iii'] /*
*/ in 1/`ncat'
if "`margina'"=="" {
replace `xrow' = `porp' in `Nr1'/`Nr2'
replace `xrow' = `porp'*`matc'[1,_n-`Nc1'+1] /*
*/ in `Nc1'/`Nc2'
replace `xrow' = `porp' in l
}
local key`keyi' "row `kporp'"
local keyi = `keyi' + 1
}
if "`column'"!="" {
tempvar xcol
gen double `xcol' = `porp'*`bij'/`matc'[1,`jjj'] /*
*/ in 1/`ncat'
if "`margina'"=="" {
replace `xcol' = `porp'*`matr'[1,_n-`Nr1'+1] /*
*/ in `Nr1'/`Nr2'
replace `xcol' = `porp' in `Nc1'/`Nc2'
replace `xcol' = `porp' in l
}
local key`keyi' "column `kporp'"
local keyi = `keyi' + 1
}
/* From here to `obs', only one of count, cell, row, or column is
chosen.
*/
local keylast = `keyi' - 1
local item "`key`keylast''"
if "`count'"!="" { local porp 1 }
if "`se'`ci'"!="" {
tempvar xse
tempname V
mat `V' = e(V)
gen double `xse' = `porp'*sqrt(`V'[_n,_n]) /*
*/ in 1/`ncat'
if "`margina'"=="" {
if "`row'"=="" {
mat `V' = e(V_row)
replace `xse' = `porp'* /*
*/ sqrt(`V'[_n-`Nr1'+1, /*
*/ _n-`Nr1'+1]) in `Nr1'/`Nr2'
}
if "`column'"=="" {
mat `V' = e(V_col)
replace `xse' = `porp'* /*
*/ sqrt(`V'[_n-`Nc1'+1, /*
*/ _n-`Nc1'+1]) in `Nc1'/`Nc2'
}
}
if "`se'"!="" {
if "`vertica'"=="" {
local key`keyi' /*
*/ "(standard errors of `item')"
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -