Updating a linear regression model with update and purrr

牧云@^-^@ 提交于 2019-12-04 13:46:51

This is probably related to the environment where update is being evaluated. A simple workaround is to use map2 and explicitly reference the corresponding data:

library(tidyverse)

mtcars %>% group_by(cyl) %>% 
  nest() %>% 
  mutate(lm1 = map(data, ~lm(mpg ~ wt, data = .x)), 
         lm2 = map2(lm1, data, ~update(object = .x, formula. = .~ . + hp,
                                       data = .y)))
#> # A tibble: 3 x 4
#>     cyl               data      lm1      lm2
#>   <dbl>             <list>   <list>   <list>
#> 1     6  <tibble [7 x 10]> <S3: lm> <S3: lm>
#> 2     4 <tibble [11 x 10]> <S3: lm> <S3: lm>
#> 3     8 <tibble [14 x 10]> <S3: lm> <S3: lm>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!