-2

I created a grouped proportion table. What is the easiest way to include a margin of error at a specified confidence interval?

data.frame(prop.table(table(df$variable, df$group),2))

SummarySE() should work but my variable is a factor with three levels, not a numeric value.

Kasey
  • 173
  • 2
  • 11
  • I think confidence intervals don't make sense in the context of frequency tables of binary variables. You might consider asking on stats.stackexchange.com If asking here, you should probably make a reproducible example including references to packages (like wherever SummarySE comes from). Some guidance: http://stackoverflow.com/a/28481250/ – Frank Dec 09 '16 at 23:02
  • My mistake, should have said three levels. – Kasey Dec 09 '16 at 23:14

1 Answers1

0

You really should specify what you mean by confidence intervals, but assuming you want binomial confidence intervals for proportions, this is not necessarily pretty but it works. Take it as a starting point and adapt to your needs:

ci.table <- function(tbl, margin = NULL) {
    binom_ci <- function(x, n) {
        paste(round(binom.test(x, n)$conf.int, 3), collapse = " - ")
    }
    sweep_ci <- function(xx, nn) { mapply(FUN = binom_ci, xx, nn) }

    if (length(margin))
        result <- sweep(tbl, margin, margin.table(tt, margin),
                        "sweep_ci", check.margin = FALSE)       
    else
        result <- sweep_ci(tbl, sum(tbl))

    dim(result) <- dim(tbl)
    as.table(result)
}

The binom_ci function handles the formatting. I don't particularly like "x - y" for confidence intervals, but I have found that most people understand this better than for example "(x, y)"...

MWE:

ddff <- data.frame(
    A = sample(c("A", "B", "C"), 20, replace = TRUE),
    B = sample(c("C", "D"), 20, replace = TRUE) 
)
tt <- table(ddff$A, ddff$B)
ci.table(tt)
ci.table(tt, 1)
ci.table(tt, 2)
Zé Loff
  • 275
  • 1
  • 8