I am trying to create a smooth spline for each sample in a grouped dataframe. For this I am using a nest and map approach and mgcv gam (following this example https://smu095.github.io/2019/02/16/2019-02-16-tidytuesday-fitting-multiple-time-series-models-using-purrr/).
After running the gam I would like to use broom::augment to extract the fitted data and calculate confidence intervals.
This code works using broom 0.5.6 but throws an error using the new broom 0.7 version. broom::tidy and broom:glance still work with this format but augment stops with "Error: Problem with mutate()
input augment_spline
.
x object 'year' not found"
Example code below
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)
Same code runs if using broom 0.5.6
devtools::install_version("broom", version = "0.5.6", repos = "http://cran.us.r-project.org")
All online tutorials I could find present similar code that doesn't seem to work using broom 0.7
In the newer version I think it also needs data in newdata
argument. You can pass the data as separate argument with map2
.
library(tidyverse)
library(dslabs)
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 = map2(splined, data, ~broom::augment(.x, newdata = .y))) %>%
unnest(augment_spline)
Although this works but this doesn't return all the columns as the 0.5.6 version of broom
does i.e se.fit
,.resid
,.hat
, .sigma
and .cooksd
.This only returns .fitted
column.