I am using the packages purrr and broom to produce a series of glm's and build a table with information of the models so I can compare them.
The code is failing when I call map function from purrr. I think the problem relates to the combination of mutate and map. I want to generate a table with a row for each glm and columns for the glm's components.
DATA & CODE
library(broom)
library(tidyverse)
# Produce a dummy dataset
set.seed(123)
dummy <- tibble(ID = 1:50,
A = sample(x = 1:200, size = 50, replace = T),
B = as.factor(sample(x = c("day", "night"), size = 50, replace = T)),
C = as.factor(sample(x = c("blue", "red", "green"), size = 50, replace = T)))
# Nest the data
nested <- dummy %>% select(-ID) %>% nest()
# Define a function for a generalized linear model with a poisson family
mod_f <- function(x, df = nested) {glm(formula = as.formula(x), family = poisson, data = df)}
# Make a list of formulas as a column in a new dataframe
# A is our response variable that we try to predict using B and C
formulas <- c("A ~ 1", "A ~ B", "A ~ C", "A ~ B + C")
tbl <- tibble(forms = formulas)
# Fit the glm's using each of the formulas from the formulas vector
tbl_2 <- tbl %>% mutate(mods = map(formulas, mod_f))
#gla = mods %>% map(glance),
#tid = mods %>% map(tidy),
#aug = mods %>% map(augment),
#AIC = gla %>% map_dbl("AIC"))
ERROR
Error in mutate_impl(.data, dots): Evaluation error: object 'A' not found