extract model info from model saved as list column in r

Viewed 723

I'm trying to extract model info from model in a list column. Using mtcars to illustrate my problem:

mtcars %>%  
    nest(-cyl) %>% 
    mutate(model= map(data, ~lm(mpg~wt, data=.))) %>% 
    mutate(aic=AIC(model))

what I got is error message:

Error in mutate_impl(.data, dots) : 
  Evaluation error: no applicable method for 'logLik' applied to an object of class "list".

But when I do it this way, it works.

mtcars %>%  
    group_by(cyl) %>% 
    do(model= lm(mpg~wt, data=.)) %>% 
    mutate(aic=AIC(model))

Can anyone explain why? Why the second way works? I could not figure it out. In both cases, the list column 'model' contains model info . But there might be some differences... Thanks a lot.

1 Answers
Related