使用嵌套的newdata进行预测

问题描述 投票:0回答:1

想象一下在许多天(站)在许多地方拍摄的高分辨率温度和光照时间序列。除此以外,在每个站点上,温度和光都是由不同的传感器拍摄的,从而导致时间戳集略有不同。

[将它们合并为一个data.frame,我一直试图在df1的每个站点上建立每天的光照模型。然后,我想预测温度读数精确时间戳下的光值,它们以相同的方式嵌套在df2(温度数据集)中。

station <- rep(1:5, each=36500)
dayofyear <- rep(1:365, 5, each=100)

hourofday.light <- runif(182500, min=0, max=24)
light <- runif(182500, min=0, max=40)

hourofday.temp <- runif(182500, min=0, max=24)
temp <- runif(182500, min=0, max=40)

df.light <- df.temp <- data.frame(station, dayofyear, hourofday.light, light)    
df.temp <- data.frame(station, dayofyear, hourofday.temp, temp)

> head(df.light)
  station dayofyear hourofday.light     light
1       1         1       10.217349  0.120381
2       1         1       12.179213 12.423694
3       1         1       16.515400  7.277784
4       1         1        3.775723 31.793782
5       1         1        7.719266 30.578220
6       1         1        9.269916 16.937042
> tail(df.light)
       station dayofyear hourofday.light      light
182495       5       365        4.712285 19.2047471
182496       5       365       11.190919 39.5921675
182497       5       365       18.710969 11.8182347
182498       5       365       20.288101 11.6874453
182499       5       365       15.466373  0.3264828
182500       5       365       12.969125 29.4429034
> head(df.temp)
  station dayofyear hourofday.temp      temp
1       1         1     12.1298554 30.862308
2       1         1     23.6226076  9.328942
3       1         1      9.3699831 28.970397
4       1         1      0.1814767  1.405557
5       1         1     23.6300014 39.875743
6       1         1      7.6999984 39.786182

我可以制作灯光模型,例如GAM,使用df1dplyr中每个站点的每一天。但是我不知道如何将嵌套的newdatadf2馈入模型,以生成每站每天的预测。

library("mgcv")
library("tidyverse")

data <- as_tibble(df.light) %>%
  group_by(station, dayofyear) %>%
  nest()

models <- data %>%
  mutate(
    model = map(data, ~ gam(light ~ s(hourofday.light), data = .x)),
    predicted = map(model, ~ predict.gam(.x, newdata = hourofday.temp)) # newdata doesn't look nested
  )

predicted开头的最后一行不起作用

,因为未嵌套newdata ...我认为。请帮忙。我猜想这可能是合并多个来源生成的时间序列中的常见问题。

想象一下在许多天(站)在许多地方拍摄的高分辨率温度和光照时间序列。除此之外,在每个站点上,温度和光都是由不同的传感器拍摄的,从而导致...

r dplyr nested time-series predict
1个回答
0
投票

您可以先准备数据。

© www.soinside.com 2019 - 2024. All rights reserved.