95% Confidence Intervals of categorical outcome by categories of a Risk Factor in Survey Data

Use the following program to generate the summary 95% confidence intervals

``````
cap program drop clustercibyRF
program define clustercibyRF , rclass
args outcome  byVar
return clear
return local indicator = "`outcome'"
return local byVar = "`byVar'"
qui svy linearized : proportion `outcome',  over(`byVar')
cap mat drop all
mat all  = r(table)
mat all  = all' //	mat list all
qui svy linearized : proportion `outcome'
cap mat drop all2
mat all2  = r(table)
mat all2  = all2'
qui	levelsof `outcome', local(outcomeLevels)
qui	levelsof `byVar', local(byVarLevels)
local x = 1 // COUNTER FOR OUTCOME RESULTS IN ByVar-WISE ANALYSIS -  ROWS PER OUTCOME LEVEL (ONE FOR EACH SEX)
foreach o of local outcomeLevels  {  // of local levels    in 1
local outcomeName`o' : label(`outcome') `o' // local outcome = ustrregexra(`"`outcomeName'"', " ", "_")
local outcomeCode`o' = `o' 	//di  "`outcomeName'              `outcomeCode'"
foreach byVarLevel of local byVarLevels  {
local byVarLevelName`byVarLevel' : label(`byVar') `byVarLevel'    // local outcome = ustrregexra(`"`outcomeName'"', " ", "_")
local byVarLevelCode`byVarLevel' = `byVarLevel' 	                //di  "`outcomeName'              `outcomeCode'"
qui count if `byVar'==`byVarLevel' & `outcome'!=. //male
local denom`byVarLevel' = r(N)
qui count if `byVar'==`byVarLevel'  //male
local sample`byVarLevel' = r(N)
local prop_`o'_`byVarLevel' : di  %4.1f  all[`x',1]*100 // Cluster-adjusted  prevalence outcome level
local lb_`o'_`byVarLevel' : di  %4.1f  all[`x',5]*100 // Cluster-adjusted  prevalence lower CI
local ub_`o'_`byVarLevel' : di  %4.1f  all[`x',6]*100 // Cluster-adjusted  prevalence lower CI
local x = `x' + 1
}
local y = `y' + 1
}
local levelsOfOutcome = `y'
local levelsOfbyVar = (`x' - 1 )/ `y'

foreach o of local outcomeLevels  {
}
foreach byVarLevel of local byVarLevels  {
local  result "`denom`byVarLevel''  "
foreach o of local outcomeLevels  {
local  result   "`result' `prop_`o'_`byVarLevel'' (`lb_`o'_`byVarLevel'', `ub_`o'_`byVarLevel'')      "
}
local  result " `result'  -  `byVarLevelName`byVarLevel''  "
di "`result'"
}

end```Code language: Stata (stata)```

Usage

``````use "http://www.stata-press.com/data/r15/nhanes2b", clear
svyset psuid [pweight=finalwgt], strata(stratid)

cap drop bmi
gen bmi = weight /  ((height/100) * (height/100))
cap drop bmicat
egen bmicat = cut(bmi), at(0 18.5 25 30   100) lab
tab bmicat race

clustercibyRF  bmicat race
```Code language: JavaScript (javascript)```

RESULTS

``````. clustercibyRF  bmicat race
0-  18.5-  25-  30-
9065    3.3 ( 2.9,  3.7)       51.5 (49.9, 53.1)       31.8 (30.5, 33.1)       13.4 (12.6, 14.4)        -  White
1086    2.6 ( 1.6,  4.4)       43.5 (38.6, 48.4)       30.9 (26.9, 35.2)       23.0 (19.0, 27.5)        -  Black
200    6.8 ( 3.7, 12.1)       59.7 (55.0, 64.2)       23.8 (19.5, 28.7)        9.8 ( 7.1, 13.2)        -  Other
```Code language: CSS (css)```

You get the N for each category of race, and the Percent and 95% CI for each category of BMI. The First row depicts the order of the results