简体   繁体   English

从模型中提取模型信息,保存为r中的列表列

[英]extract model info from model saved as list column in r

I'm trying to extract model info from model in a list column. 我正在尝试从列表列中的模型中提取模型信息。 Using mtcars to illustrate my problem: 使用mtcars来说明我的问题:

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. 但可能存在一些差异......非常感谢。

Let's compare the differences between these two approaches. 让我们比较这两种方法之间的差异。 We can run your entire code in addition to the last AIC call and save the results to a and b . 除了最后一次AIC调用之外,我们还可以运行您的整个代码,并将结果保存到ab

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

b <- mtcars %>%  
  group_by(cyl) %>% 
  do(model= lm(mpg~wt, data=.)) 

Now we can print the results in the console. 现在我们可以在控制台中打印结果。

a
# A tibble: 3 x 3
    cyl               data    model
  <dbl>             <list>   <list>
1     6  <tibble [7 x 10]> <S3: lm>
2     4 <tibble [11 x 10]> <S3: lm>
3     8 <tibble [14 x 10]> <S3: lm>

b
Source: local data frame [3 x 2]
Groups: <by row>

# A tibble: 3 x 2
    cyl    model
* <dbl>   <list>
1     4 <S3: lm>
2     6 <S3: lm>
3     8 <S3: lm>

Now we can see dataframe b is grouped by row, while dataframe a is not. 现在我们可以看到数据框b按行分组,而数据框a则不是。 This is the key. 这是关键。

To extract AIC in dataframe a , we can use the rowwise function to group dataframe by each row. 要在数据帧a提取AIC,我们可以使用rowwise函数按每行对数据帧进行分组。

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

Source: local data frame [3 x 4]
Groups: <by row>

# A tibble: 3 x 4
    cyl               data    model      aic
  <dbl>             <list>   <list>    <dbl>
1     6  <tibble [7 x 10]> <S3: lm> 25.65036
2     4 <tibble [11 x 10]> <S3: lm> 61.48974
3     8 <tibble [14 x 10]> <S3: lm> 63.31555

Or we can use the map_dbl function because we know each AIC is numeric. 或者我们可以使用map_dbl函数,因为我们知道每个AIC都是数字。

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

# A tibble: 3 x 4
    cyl               data    model      aic
  <dbl>             <list>   <list>    <dbl>
1     6  <tibble [7 x 10]> <S3: lm> 25.65036
2     4 <tibble [11 x 10]> <S3: lm> 61.48974
3     8 <tibble [14 x 10]> <S3: lm> 63.31555

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM