You need the in dev version of dplyr to use group_map
#library(devtools)
#install_github("tidyverse/dplyr")
library(tidyverse)
library(tidymodels)
calculate_ci_boot <- function(d, x, ...) {
x <- ensym(x)
d_estimate <- d %>%
group_by(...) %>%
group_map(
~ .x %>%
specify(expr(!!x ~ NULL)) %>%
calculate(stat = "mean")
)
d_ci <- d %>%
group_by(...) %>%
group_map(
~ .x %>%
specify(expr(!!x ~ NULL)) %>%
generate(reps = 10000, type = "bootstrap") %>%
calculate(stat = "mean") %>%
get_confidence_interval()
)
left_join(d_estimate, d_ci)
}
mtcars_boot_ci <- mtcars %>%
calculate_ci_boot(mpg, gear, am) %>%
rename(estimate = stat, conf.low = `2.5%`, conf.high = `97.5%`) %>%
mutate(type = "boot")
## Joining, by = c("gear", "am")
calculate_ci <- function(d, x, ...) {
d %>%
group_by(...) %>%
group_map(~t.test(.x %>% pull(!!enquo(x)), conf.int = TRUE) %>% tidy())
}
mtcars_ttest_ci <- mtcars %>%
calculate_ci(mpg, gear, am) %>%
mutate(type = "t-test")
mtcars_boot_ci %>%
bind_rows(mtcars_ttest_ci) %>%
ggplot(aes(x = gear, y = estimate, ymin = conf.low, ymax = conf.high,
color = type)) +
facet_wrap(~ am) +
geom_pointrange(position = position_dodge(.2))