Darius
Darius

Reputation: 489

How to use map from purrr and mutate from dplyr to produce a glm summary table?

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

Upvotes: 4

Views: 2438

Answers (2)

Darius
Darius

Reputation: 489

Final answer as provided by another Stackoverflow's user:

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)))

# Define a function for a generalized linear model with a poisson family
mod_f <- function(x) {glm(formula = as.formula(x), family = poisson, data = dummy)}

# Make a list of formulas as a column in a new dataframe
# A is yhe response variable 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 using each of the formulas stored in the formulas vector
tbl_2 <- tbl %>% mutate(all = map(formulas, mod_f),
                        gla = all %>% map(glance),
                        tid = all %>% map(tidy),
                        aug = all %>% map(augment),
                        AIC = all%>% map_dbl("AIC"))

Upvotes: 4

NelsonGon
NelsonGon

Reputation: 13309

You made a mistake in your function: You called df instead of dummy. Not sure if you can refactor to generalize it. Here:

   mod_f <- function(x, df = nested) {glm(formula = as.formula(x), family = poisson, data = dummy)}

# 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))

This yields:

forms     mods     
  <chr>     <list>   
1 A ~ 1     <S3: glm>
2 A ~ B     <S3: glm>
3 A ~ C     <S3: glm>
4 A ~ B + C <S3: glm>
    `Map(mod_f,formulas)` 

yields and so on:

$`A ~ 1`

Call:  glm(formula = as.formula(x), family = poisson, data = dummy)

Coefficients:
(Intercept)  
      4.649  

Degrees of Freedom: 49 Total (i.e. Null);  49 Residual
Null Deviance:      1840 
Residual Deviance: 1840     AIC: 2154

Upvotes: 2

Related Questions