你如何对嵌套在 tibbles 中的许多模型进行 predict.gam?

How do you do predict.gam on many models nested within tibbles?

我一直在关注 https://r4ds.had.co.nz/many-models.html 为每一行创建一个带有单独 GAM 的标题。数据列列出了用于生成 GAM 的数据。现在,我在尝试使用 predict.gam 函数生成具有预测值的新列时遇到了问题。

# 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

目前代码如下:

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)

现在我基本上是在尝试 运行 每个模型的以下内容,并使用 mutate 将其另存为另一列。

predict.gam(models$model, predict.years)

如有任何帮助,我们将不胜感激。谢谢!

您应该能够对预测使用类似的语法。我在这里使用 gapminder 使其可重现。

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

reprex package (v0.3.0)

于 2020 年 4 月 10 日创建