Print Model Summary output from purrr::map

I'd like to access/print regular summary output for a model I've looped with purrr::map, but I'm getting an error: "Error: Input must be list of vectors". Any suggestions?

library(mgcv)
library(dplyr)
library(purrr)
Orange %>%
group_by(Tree) %>%
nest() %>%
mutate(m1_bam = map(data, ~ bam(formula = circumference ~ 1 + age, family = gaussian, data = .x)),
m1_smry = map(m1_bam, ~ summary(.x)),
m1_beta = map(m1_smry, ~ .x$p.table)
) %>%
unnest(m1_smry)
#unnest(m1_beta)

It appears the error is coming from the unnest() function. I'm not sure unnest knows how to handle the class "summary.gam". In the help page, it says unnest is primarily designed to work with lists of data frames. What is your goal with the unnest function? Perhaps I can suggest another function

I can get summary output using

Orange %>%
split(.$Tree) %>%
map(~ summary(bam(formula = circumference ~ 1 + s(age, m=0.5, k=6, bs = "tp"), family = quasipoisson, data = .x)))

I'd like to get this kind of output from the nested data.

I can get the summary information to print by using select(m1_smry) instead of unnest().

Like this?

you can just add in a simple print call to the summary column

library(mgcv)
library(dplyr)
library(purrr)
library(tidyverse)


dat <- Orange %>%
  group_by(Tree) %>%
  nest() %>%
  mutate(
    m1_bam = map(data, ~ bam(formula = circumference ~ 1 + age, family = gaussian, data = .x)),
    m1_smry = map(m1_bam, ~ print(summary(.x))),
    m1_beta = map(m1_smry, ~ .x$p.table)
  )

dat %>% 
  unnest(m1_beta)

Ok. Yes, it looks like I spoke too soon and I have the same output in your screenshot. Sorry! Looks like another user offered a solution. You could also pipe to a dollar sign which is probably not ideal:

Orange %>%
  group_by(Tree) %>%
  nest() %>%
  mutate(m1_bam = map(data, ~ bam(formula = circumference ~ 1 + age, family = gaussian, data = .x)),
         m1_smry = map(m1_bam, ~ tidy(.x))
         m1_beta = map(m1_smry, ~ .x$p.table)
  ) %>%
  .$m1_smry

It looks like folks are working on ways to tidy() the output from a bam model

1 Like

@zac-garland, I'm not sure that your example makes the m1_smry column callable for output. But it does print the output for everything because the print() function is used. I could pipe this output to a text file for later reference.
I was hoping for a simple method for printing that output at-will from the nested table. But the list structure created by map doesn't seem to lend itself to this.

when you say "callable for output" what do you mean?

like this?

library(mgcv)
library(dplyr)
library(purrr)
library(tidyverse)


dat <- Orange %>%
  group_by(Tree) %>%
  nest() %>%
  mutate(
    m1_bam = map(data, ~ bam(formula = circumference ~ 1 + age, family = gaussian, data = .x)),
    m1_smry = map(m1_bam, ~ print(summary(.x))),
    m1_beta = map(m1_smry, ~ .x$p.table)
  )

dat %>% 
  unnest(m1_beta)

dat %>% 
  pull(m1_smry)
1 Like

@zac-garland that pull function did the trick. Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.