Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling prop.test function in R with dplyr

Tags:

r

dplyr

I am trying to calculate several binomial proportion confidence intervals. My data are in a data frame, and though I can successfully extract the estimate from the object returned by prop.test, the conf.int variable seems to be null when run on the data frame.

library(dplyr)

cases <- c(50000, 1000, 10, 2343242)
population <- c(100000000, 500000000, 100000, 200000000)

df <- as.data.frame(cbind(cases, population))
df %>% mutate(rate = prop.test(cases, population, conf.level=0.95)$estimate)

This appropriately returns

    cases population       rate
1   50000      1e+08 0.00050000
2    1000      5e+08 0.00000200
3      10      1e+05 0.00010000
4 2343242      2e+08 0.01171621

However, when I run

df %>% mutate(confint.lower= prop.test(cases, pop, conf.level=0.95)$conf.int[1])

I sadly get

Error in mutate_impl(.data, dots) : 
  Column `confint.lower` is of unsupported type NULL

Any thoughts? I know alternative ways to calculate the binomial proportion confidence interval, but I would really like to learn how to use dplyr well.

Thank you!

like image 706
PBB Avatar asked Dec 17 '22 22:12

PBB


1 Answers

You can use dplyr::rowwise() to group on rows:

df %>%
    rowwise() %>%
    mutate(lower_ci = prop.test(cases, pop, conf.level=0.95)$conf.int[1])

By default dplyr takes the column names and treats them like vectors. So vectorized functions, like @Jake Fisher mentioned above, just work without rowwise() added.

This is what I would do to catch all of the confidence interval components at once:

df %>%
    rowwise %>%
    mutate(tst = list(broom::tidy(prop.test(cases, pop, conf.level=0.95)))) %>%
    tidyr::unnest(tst)
like image 128
Nate Avatar answered Jan 03 '23 12:01

Nate