[R] Flat tables for confidence intervals with "survey" package

Duncan Murdoch murdoch.duncan at gmail.com
Wed Feb 4 17:18:15 CET 2015


On 04/02/2015 10:16 AM, Michael.Laviolette at dhhs.state.nh.us wrote:
> I'm preparing some reports for substate regions from BRFSS survey data. I
> can get estimates easily enough, but am having problems putting the results
> in convenient form. Here's some code using the New Hampshire portion of the
> public BRFSS "SMART" data:
>
> library(foreign)
> library(survey)
> # download and extract http://www.cdc.gov/brfss/smart/2012/CNTY12XPT.zip
> nh.smart <- svydesign(ids = ~0, strata = ~X_STSTR, weights = ~X_CNTYWT,
>                        data = subset(read.xport("CNTY12.xpt"), X_STATE ==
> 33))
> # using asthma status as example
> nh.smart <- update(nh.smart,
>    X_ASTHMS1 = factor(X_ASTHMS1, levels = 1:3,
>                       labels = c("Current","Former","Never")),
>    X_CNTY = factor(X_CNTY, labels = c("Belknap","Carroll","Cheshire","Coos",
>                                       "Grafton","Hillsborough","Merrimack",
>                                       "Rockingham","Strafford")))
> a <- svyby(~X_ASTHMS1, ~X_CNTY, nh.smart, svymean, na.rm = TRUE,
>             vartype = "ci")
>
> Is there a convenient way to get a flat table similar to the following? I'm
> not having much success with "ftable."
>
> Thanks in advance,
> Michael L.
>
> #                    Percent   LCI   UCI
> # Belknap   Current     10.4   5.6  15.3
> #           Former       1.5   0.4   2.7
> #           Never       88.1  83.1  93.0
> # Carroll   Current      7.5   4.9  10.1
> #           Former       2.9   1.3   4.5
> #           Never       89.6  86.5  92.7
> # ...
>

What do you mean by a "flat table"?  You can get a table that's suitable 
for conversion to LaTeX or HTML (or maybe some other format) from the 
tables package.  I haven't tried this, but I would think the syntax 
would be something like

tabular(Factor(X_CNTY, "County")*Factor(X_ASTHMS1, "Asthma")
                    ~ Percent(denom=Equal(X_CNTY)) +
                       Percent(denom=Equal(X_CNTY), fn=LCI) +
                       Percent(denom=Equal(X_CNTY), fn=UCI), data=nh.smart)

You'll need to write the LCI and UCI functions to compute the confidence 
limits.  I don't know how to extract those from the objects you've got; 
you may need to add an "analysis variable" to the tabular() call to get 
the required inputs.

Duncan Murdoch



More information about the R-help mailing list