【发布时间】:2020-12-25 12:06:17
【问题描述】:
我正在尝试为分组数据框中的每个样本创建平滑样条曲线。为此,我使用了嵌套和地图方法和 mgcv gam(遵循此示例 https://smu095.github.io/2019/02/16/2019-02-16-tidytuesday-fitting-multiple-time-series-models-using-purrr/)。
运行游戏后,我想使用 broom::augment 来提取拟合数据并计算置信区间。
此代码使用 broom 0.5.6 工作,但使用新的 broom 0.7 版本会引发错误。 broom::tidy 和 broom:glance 仍然可以使用这种格式,但扩充停止时出现“错误:mutate() 输入 augment_spline 出现问题。
未找到 x 对象“年份””
下面的示例代码
library(tidyverse)
library(dslabs)
#Use the gapminder dataset that comes with dslabs as an example
glimpse(gapminder)
gapminder_nest <- gapminder %>%
group_by(country) %>%
nest()%>%
mutate(splined =map(data, ~mgcv::gam(population ~ s(year, k=5, bs="tp"), data=.x))) %>%
mutate(augment_spline= map(splined, broom::augment))%>%
unnest(augment_spline)%>%
dplyr::select(country, population,.fitted,.se.fit)
如果使用 broom 0.5.6,运行相同的代码
devtools::install_version("broom", version = "0.5.6", repos = "http://cran.us.r-project.org")
我能找到的所有在线教程都提供了似乎无法使用 broom 0.7 工作的类似代码
【问题讨论】: