Reputation: 3
I've been following https://r4ds.had.co.nz/many-models.html to create a tibble with individual GAMs for each row. The data column lists the data that was used to generate the GAM. Now I've having trouble trying to use the predict.gam function to generate a new column with the predicted values.
# A tibble: 2,157 x 3
# Groups: Site [2,157]
Site data model
<fct> <list> <list>
1 Abana Rock North 1 <tibble [7 x 6]> <gam>
2 Abana Rock North 2 <tibble [7 x 6]> <gam>
3 Abana Rock South 1 <tibble [7 x 6]> <gam>
4 Abana Rock South 2 <tibble [7 x 6]> <gam>
5 Ampa Marker East <tibble [7 x 6]> <gam>
6 Ampa Marker West <tibble [7 x 6]> <gam>
7 Ampa Patches Southwest 1 <tibble [7 x 6]> <gam>
8 Ampa Patches Southwest 2 <tibble [7 x 6]> <gam>
9 Brunei Patches 1 <tibble [7 x 6]> <gam>
10 Brunei Patches 2 <tibble [7 x 6]> <gam>
# ... with 2,147 more rows
# A tibble: 7 x 6
Country Location Year Population100km
<fct> <fct> <dbl> <int>
1 Brunei Inshore Brunei 1990 431102
2 Brunei Inshore Brunei 1995 492958
3 Brunei Inshore Brunei 2000 545008
4 Brunei Inshore Brunei 2005 602691
5 Brunei Inshore Brunei 2010 660197
6 Brunei Inshore Brunei 2015 715266
7 Brunei Inshore Brunei 2020 766133
The code thus far are as follows:
data <- rawdata %>%
group_by(Site) %>%
nest()
model_function <- function(df) {
gam(Population100km ~ Year, data = df)
}
models <- data %>%
mutate(model = map(data, mode_function))
years <- data.frame(Year=1990:2020)
Now I'm basically trying to run the following for each model and save it as another column using mutate.
predict.gam(models$model, predict.years)
Any help would be appreciated. Thank you!
Upvotes: 0
Views: 511
Reputation: 462
You should be able to use similar syntax for the predictions. I have here used gapminder to make it reproducible.
library(tidyverse)
library(mgcv)
#> Loading required package: nlme
#>
#> Attaching package: 'nlme'
#> The following object is masked from 'package:dplyr':
#>
#> collapse
#> This is mgcv 1.8-31. For overview type 'help("mgcv-package")'.
rawdata <- gapminder::gapminder
data <- rawdata %>%
group_by(country) %>%
nest()
years <- data.frame(year=1990:2020)
models <- data %>%
mutate(
model = map(data, ~ gam(lifeExp ~ year, data = .x)),
predicted = map(model, ~ predict(.x, newdata = years))
)
unnest(models, predicted)
#> # A tibble: 4,402 x 4
#> # Groups: country [142]
#> country data model predicted
#> <fct> <list> <list> <dbl>
#> 1 Afghanistan <tibble [12 × 5]> <gam> 40.4
#> 2 Afghanistan <tibble [12 × 5]> <gam> 40.6
#> 3 Afghanistan <tibble [12 × 5]> <gam> 40.9
#> 4 Afghanistan <tibble [12 × 5]> <gam> 41.2
#> 5 Afghanistan <tibble [12 × 5]> <gam> 41.5
#> 6 Afghanistan <tibble [12 × 5]> <gam> 41.7
#> 7 Afghanistan <tibble [12 × 5]> <gam> 42.0
#> 8 Afghanistan <tibble [12 × 5]> <gam> 42.3
#> 9 Afghanistan <tibble [12 × 5]> <gam> 42.6
#> 10 Afghanistan <tibble [12 × 5]> <gam> 42.8
#> # … with 4,392 more rows
Created on 2020-04-10 by the reprex package (v0.3.0)
Upvotes: 1