Search code examples
rsurvey

confidence intervals of svyby proportion


Is there an existing function that creates confidence intervals from a svyby object for proportions (in my case a crosstab for a binary item in the survey package). I often compare proportions across groups, and it would be very handy to have a function that can extract confidence intervals (with the survey function svyciprop rather than confint). The example below shows what I'd like to achieve.

Load data

library(survey)
library(weights)
data(api)
apiclus1$both<-dummify(apiclus1$both)[,1]#Create dummy variable
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)

Create a svyby object which compares proportion of variable "both" across stype

b<-svyby(~both, ~stype, dclus1, svymean)
confint(b)#This works, but svyciprop is best in  other cases, especially when proportion is close to 0 or 1
svyciprop(b)#This requires that you specify each level and a design object

Would it be possible to create a function (e.g. byCI(b,method="likelihood") which achieves the same as confint(b) but using svyciprop? It would basically have to go through each level of the svyby object and create a confidence interval. My attempts have been unsuccessful up to now.

There may be another way around this, but I like using svyby() as it's quick and intuitive.


Solution

  • svyby() has a vartype= argument to specify how you want the sampling uncertainty specified. Use vartype="ci" to get confidence intervals, eg

    svyby(~I(ell>0),~stype,design=dclus1, svyciprop,vartype="ci",method="beta")
    

    It's easy to check that this gives the same as doing each level by hand, eg,

    confint(svyciprop(~I(ell>0), design=subset(dclus1,stype=="E"),method="beta"))