1

I have been looking at how to calculate the CI for proportions across two categorical variables. I have seen this answer, which gets close to what I am looking for, but I am wanting the proportions to be calculated between all possible combinations of the two variables. I have gotten it to work using svymeans and confint and this is the output I am looking for, but using svyciprop. This example comes from the api data in the survey package using the apiclus1 data frame.

library(survey)
data(api)
dclus1<-svydesign(id=~dnum, fpc=~fpc, data=apiclus1)
cbind(svymean(~interaction(sch.wide, stype), design = dclus1),
  confint(svymean(~interaction(sch.wide, stype), design = dclus1)))      
                                                   2.5 %     97.5 %
interaction(sch.wide, stype)No.E  0.06557377  0.031454436 0.09969311
interaction(sch.wide, stype)Yes.E 0.72131148  0.635732262 0.80689069
interaction(sch.wide, stype)No.H  0.01639344 -0.002458860 0.03524575
interaction(sch.wide, stype)Yes.H 0.06010929  0.018347771 0.10187081
interaction(sch.wide, stype)No.M  0.04371585  0.005196326 0.08223537
interaction(sch.wide, stype)Yes.M 0.09289617  0.050121072 0.13567128
jamesguy0121
  • 1,124
  • 11
  • 28

1 Answers1

1

thanks for the minimal reproducible example. there might be a cleaner way to do this, but i think it's what you're after? thanks

library(survey)

data(api)

dclus1<-svydesign(id=~dnum, fpc=~fpc, data=apiclus1)

# figure out the levels
levels <- svytable( ~ interaction(sch.wide, stype) , dclus1 )


# calculate svyciprop zero/one for each possible level
svyciprop_fun <-
    function( this_level , ci = FALSE , ... ){
        this_formula <- as.formula( paste0( "~as.numeric( '" , this_level , "' == interaction(sch.wide, stype))"))

        res <- svyciprop( this_formula , dclus1 , ... )
        if( ci ) res <- confint( res )
        res
    }


# use the default method
cbind(
    do.call( rbind , lapply( names( levels ) , svyciprop_fun ) ) ,
    do.call( rbind , lapply( names( levels ) , svyciprop_fun , ci = TRUE ) )
)


# use a different method=
cbind(
    do.call( rbind , lapply( names( levels ) , svyciprop_fun , method = 'asin' ) ) ,
    do.call( rbind , lapply( names( levels ) , svyciprop_fun , ci = TRUE , method = 'asin' ) )
)
Anthony Damico
  • 5,779
  • 7
  • 46
  • 77