📄 svytab_7.ado
字号:
mat `X' = 0.5*(`X'' + `X')
*tempname Y
*mat `Y' = `X''
*mat `Y' = `Y' + `X' /* labels picked from right */
*mat `X' = 0.5*`Y'
end
program define SymInv
args X /* matrix in, replaced with inverse */
Sym `X'
mat `X' = syminv(`X')
end
program define Marginal
args nrow ncol b row col
tempname A B
mat `A' = I(`nrow')
mat `B' = J(1,`ncol',1)
mat `A' = `A' # `B'
mat `row' = `b'*`A''
mat `A' = J(1,`nrow',1)
mat `B' = I(`ncol')
mat `A' = `A' # `B'
mat `col' = `b'*`A''
end
program define VMargin
args nrow ncol V Vrow Vcol
tempname A B
mat `A' = I(`nrow')
mat `B' = J(1,`ncol',1)
mat `A' = `A' # `B'
mat `Vrow' = `V'*`A''
mat `Vrow' = `A'*`Vrow'
Sym `Vrow'
mat `A' = J(1,`nrow',1)
mat `B' = I(`ncol')
mat `A' = `A' # `B'
mat `Vcol' = `V'*`A''
mat `Vcol' = `A'*`Vcol'
end
program define Getb0
args row col b0
tempname A B
/* Compute expected cell proportions. */
mat `A' = `row''*`col'
/* Convert into a row vector. */
mat `b0' = `A'[1,1...]
local nrow = colsof(`row')
local i 2
while `i' <= `nrow' {
mat `B' = `A'[`i',1...]
mat `b0' = `b0' , `B'
local i = `i' + 1
}
end
program define ChkZero
args row col zero
scalar `zero' = 0
local nrow = colsof(`row')
local i 1
while `i' <= `nrow' {
scalar `zero' = `zero' | (`row'[1,`i']==0)
local i = `i' + 1
}
local ncol = colsof(`row')
local i 1
while `i' <= `ncol' {
scalar `zero' = `zero' | (`col'[1,`i']==0)
local i = `i' + 1
}
end
program define AddZeros
args cell
macro shift
local nrow = rowsof(`cell')
local ncol = colsof(`cell')
local k 1
local i 1
while `i' <= `nrow' {
local j 1
while `j' <= `ncol' {
if `cell'[`i',`j'] == 0 {
local m 1
while "``m''"!="" {
PutZero ``m'' `k'
local m = `m' + 1
}
}
local k = `k' + 1
local j = `j' + 1
}
local i = `i' + 1
}
end
program define PutZero
args A i
tempname B Z
if `i' <= colsof(`A') {
mat `B' = `A'[.,`i'...]
local matAB "mat `A' = `A' , `B'"
}
local dim = rowsof(`A')
mat `Z' = J(`dim',1,0)
local i1 = `i' - 1
if `i1' >= 1 {
mat `A' = `A'[.,1..`i1']
mat `A' = `A' , `Z'
}
else mat `A' = `Z'
`matAB'
if `dim' == 1 { exit }
if `i' <= `dim' {
mat `B' = `A'[`i'...,.]
local matAB "mat `A' = `A' \ `B'"
}
local dim = `dim' + 1
mat `Z' = J(1,`dim',0)
if `i1' >= 1 {
mat `A' = `A'[1..`i1',.]
mat `A' = `A' \ `Z'
}
else mat `A' = `Z'
`matAB'
end
program define 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 define 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 define 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 define SetMiss
local i 1
while "``i''"!="" {
scalar ``i'' = .
local i = `i' + 1
}
end
program define 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 define SvyParse
gettoken doit 0 : 0
gettoken subvar 0 : 0
/* Parse. */
syntax varlist(min=2 max=2 numeric) [pweight iweight/] [if] [in] /*
*/ [, STRata(varname) PSU(varname) FPC(varname numeric) /*
*/ SUBpop(varname numeric) 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) /*
*/ ]
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
}
local ncell : word count `obs' `se' `deff' `deft'
if "`ci'"!="" & "`vertical'"!="" & `ncell' > 2 {
di in red "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 in red "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' `deff' `deft'
di in red "only one of cell, count, row, or column " /*
*/ "can be specified when `opt' specified"
exit 198
}
}
if "`percent'"!="" & "`proport'"!="" {
di in red "both percent and proportion cannot be specified"
exit 198
}
if "`srssubp'"!="" & "`subpop'"=="" {
di in red "srssubpop can only be specified when subpop() " /*
*/ "is specified"
exit 198
}
/* Set global macros. */
macro drop S_VY*
global S_VYvar1 : word 1 of `varlist'
global S_VYvar2 : word 2 of `varlist'
if `cellwid' < 4 {
global S_VYclw 8
local cellwid
}
else {
global S_VYclw `cellwid'
local cellwid cellwidth(`cellwid')
}
if `stubwid' < 4 {
global S_VYstw 8
local stubwid
}
else {
global S_VYstw `stubwid'
local stubwid stubwidth(`stubwid')
}
global S_VYdopt `pearson' `null' `lr' `wald' `llwald' /*
*/ `adjust' `fuller' /*
*/ `label' `se' `deff' `deft' `margina' `percent' /*
*/ `proport' `cell' `count' `row' `column' level(`level') `ci' /*
*/ `format' `vertica' `obs' `cellwid' `csepwid' `stubwid'
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_VYsrss "`srssubp'"
global S_VYopt /* erase macro */
global S_VYsvar "`subpop'"
/* Get weights. */
if "`exp'"=="" {
svy_get pweight, optional
local exp "$S_1"
}
else if "`weight'"=="pweight" { /* try to varset pweight variable */
capture confirm variable `exp'
if _rc==0 {
svy_get pweight `exp'
local exp "$S_1"
}
}
if "`exp'"!="" {
global S_VYexp "`exp'"
if "`weight'"!="" { global S_VYwgt "`weight'" }
else global S_VYwgt pweight
capture confirm variable `exp'
if _rc {
tempvar `w'
qui gen double `w' = `exp'
}
else local w "`exp'"
}
/* Get strata, psu, and fpc. */
svy_get strata `strata', optional
global S_VYstr "$S_1"
if "$S_VYstr"!="" {
global S_VYopt "$S_VYopt str($S_VYstr)"
}
svy_get psu `psu', optional
global S_VYpsu "$S_1"
if "$S_VYpsu"!="" {
global S_VYopt "$S_VYopt psu($S_VYpsu)"
}
svy_get fpc `fpc', optional
global S_VYfpc "$S_1"
if "$S_VYfpc"!="" {
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 `subpop' `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'"=="" { exit }
/* Only here if subpop() specified. */
qui count if `subpop'!=0 & `doit'
if _result(1) == 0 {
di in red "no observations in subpop() " /*
*/ "subpopulation" _n /*
*/ "subpop() = 1 indicates observation in " /*
*/ "subpopulation" _n /*
*/ "subpop() = 0 indicates observation not in " /*
*/ "subpopulation"
exit 461
}
if _result(1) == $S_VYnobs {
di in blu _n "Note: subpop() subpopulation is " /*
*/ "same as full population" _n /*
*/ "subpop() = 1 (or nonzero) indicates " /*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -