I'm using group_map
with geepack::geeglm
. I would like my formula
argument to be a variable (for reasons not included here) but I find that when I try to do so the code throws an error -- unexpectedly to me. I believe there is an issue with group_map
here due to my Example 3 below in my example below. Any thoughts would be appreciated. Thank you!
library(tidyverse)
library(geepack)
# Create fake data
set.seed(1)
foo =
tibble(group = rep(1:2, each = 50),
cluster_id = sample(3, size = 100, replace = T),
outcome = rbinom(100, 1, 0.5),
treatment = rbinom(100, 1, 0.5),
weights = runif(100)) %>%
arrange(group, cluster_id)
# Example 1: when I explicitly write the formula, everything works
foo %>%
group_by(group) %>%
group_map(
~ geeglm(formula = outcome ~ treatment,
data = .x,
id = cluster_id,
family = "binomial",
weights = .x[["weights"]]))
# Example 2: but the code throws an error when I try to input a pre-created formula
geeglm_formula = as.formula("outcome ~ treatment")
foo %>%
group_by(group) %>%
group_map(
~ geeglm(formula = geeglm_formula,
data = .x,
id = cluster_id,
family = "binomial",
weights = .x[["weights"]]))
# Error in eval(extras, data, env) : object '.x' not found
# Example 3: but when I drop the weights argument, it works, even
# with the pre-calculated formula
foo %>%
group_by(group) %>%
group_map(
~ geeglm(formula = geeglm_formula,
data = .x,
id = cluster_id,
family = "binomial"))