confidence intervals of svyby proportion
Asked Answered
T

3

16

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.

These answered 1/1, 2013 at 17:21 Comment(0)
N
17

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"))
Nullification answered 2/1, 2013 at 21:19 Comment(1)
@These i didn't realize this was possible - this obviously makes a lot more sense, you should change the accepted answer :)Dogmatist
D
2

interesting.. these two commands should not give the same result.. the first should probably throw an error or a warning:

svyby( ~both , ~stype , dclus1 , svyciprop , method = 'likelihood' )
svyby( ~both , ~stype , dclus1 , svymean )

you might want to alert Dr. Lumley to this issue - the code near line 80 of surveyby.R could probably be slightly modified to get svyciprop working inside svyby too.. but i may be overlooking something (and he may have noted it somewhere in the documentation), so be sure to read everything carefully before contacting him about this

anyway, here's a temporary solution that might solve your problem

# create a svyby-like function specific for svyciprop
svyciby <- 
    function( formula , by , design , method = 'likelihood' , df = degf( design ) ){

        # steal a bunch of code from the survey package's source
        # stored in surveyby.R..
        byfactors <- model.frame( by , model.frame( design ) , na.action = na.pass )
        byfactor <- do.call( "interaction" , byfactors )
        uniquelevels <- sort( unique( byfactor ) )
        uniques <- match( uniquelevels , byfactor )
        # note: this may not work for all types..
        # i only tested it out on your example.

        # run the svyciprop() function on every unique combo
        all.cis <-
            lapply( 
                uniques , 
                function( i ){

                    svyciprop( 
                        formula , 
                        design[ byfactor %in% byfactor[i] ] ,
                        method = method ,
                        df = df
                    )
                }
            )

        # transpose the svyciprop confidence intervals
        t.cis <- t( sapply( all.cis , attr , "ci" ) )

        # tack on the names
        dimnames( t.cis )[[1]] <- as.character( sort( unique( byfactor ) ) )

        # return the results
        t.cis
    }

# test out the results
svyciby( ~both , ~stype , dclus1 , method = 'likelihood' )
# pretty close to your b, but not exact (as expected)
confint(b)
# and this one does match (as it should)
svyciby( ~both , ~stype , dclus1 , method = 'mean' , df = Inf )
Dogmatist answered 1/1, 2013 at 18:51 Comment(0)
P
0

Unfortunately, I can't reproduce the suggested answer.

I however create custom function to achieve this.

# create a svyby-like function specific for svyciprop
svyciprop_by <- function(x, design, by, method) {
  # extract the levels in by
  by_var <- all.vars(by)[1]
  by_data <- model.frame(by, data = design$variables)
  by_levels <- unique(by_data[[by_var]])
 
  # run the svyciprop() functions on each levels in by
  calculate_ci <- function(stratum) {
    subset_design <- subset(design, 
                            design$variables[[by_var]] == stratum)
    result <- svyciprop(x, 
                        design = subset_design, 
                        method = method, 
                        df = degf(design))
    return(attr(result, "ci"))
  }

  # tabulate the result
  ci_results <- lapply(by_levels, calculate_ci)
  results <- data.frame(subset = by_levels, 
                        ci = do.call(rbind, ci_results))

  return(results)
}

# example
svyciprop_by(x = ~both, design = dclus1, 
             by = ~stype, method = "xl")
Prater answered 30/1 at 7:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.