Error when using broom (augment) and dplyr with loess fit

匆匆过客 提交于 2019-12-06 01:54:24

This appears to be a problem related to the do() operator: when we check the model.frame() on one of the LOESS model objects, we get back all 32 rows rather than the subset corresponding to that model.

A workaround is to hold on to the data and not just the model, and pass that as the second argument to augment():

regressions2 <- mtcars %>%
  group_by(cyl) %>%
  do(fit = loess(wt ~ mpg, .),
     data = (.)) %>%
   augment(fit, data)

This is usually recommended with augment() anyway, since model.frame() doesn't get all the original columns.


Incidentally, I'm the maintainer of broom and I generally no longer recommend the do() approach (since dplyr has mostly been moving away from it).

Instead, I suggest using tidyr's nest() and purrr's map(), as described in this chapter of R4DS. This makes it a little bit easier to hold on to the data and incorporate into augment().

library(tidyr)
library(purrr)

mtcars %>%
  nest(-cyl) %>%
  mutate(fit = map(data, ~ loess(wt ~ mpg, .))) %>%
  unnest(map2(fit, data, augment))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!