-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGAM_Tests.Rmd
275 lines (202 loc) · 10.2 KB
/
GAM_Tests.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
---
title: "GAM Testing"
output: html_document
date: "2024-05-06"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(here)
library(arrow)
library(neon4cast)
library(yardstick)
library(marginaleffects)
library(mgcv)
library(tidymv)
library(tictoc)
library(doParallel)
library(doSNOW)
source(here("download_target.R"))
```
# Read in forecast metrics
```{r}
crps_scores_df <- read_parquet(here("Data/summarized_crps.parquet"))
nnse_scores_df <- read_parquet(here("Data/summarized_weekly_normedNSE.parquet"))
```
# Data setup
```{r}
nnse_oxygen <- nnse_scores_df|>filter(variable == "oxygen")|>
mutate(site_id = as_factor(site_id),
forecast_week_fct = as_factor(forecast_week)) #important for 'by' variables in mgcv
crps_df <- crps_scores_df|>#filter(variable == "oxygen")|>
group_by(variable)|>
mutate(forecast_month = month(reference_datetime),
forecast_week = week(reference_datetime))|>
group_by(variable, horizon, site_id, forecast_week)|>
summarize(mean_crps_skill = mean(crps_skill, na.rm = TRUE))|>
mutate(site_id = as_factor(site_id),
forecast_week_fct = as_factor(forecast_week),
variable = as_factor(variable))|>
ungroup()|>
arrange(variable, forecast_week, site_id, horizon)|>
group_by(variable, site_id, forecast_week)|>
mutate(horizon_arstart = ifelse(row_number()==1, TRUE, FALSE))
```
# GAM
## CRPS Skill
### Full data set
Really think these have to be done by variable - this takes an enormous amoutn of time to fit and it's not even the whole data set AND it's not even all the components we want to estimate
```{r}
cl <- makeCluster(7)
tic()
crps.m1.full <- bam(data = crps_df|>filter(forecast_week == 35),
mean_crps_skill ~
s(horizon, by = variable)+ # central trend for each variable
s(horizon, site_id, bs = "sz", by = variable), #site-level deviations from central trend
# s(forecast_week, bs = "cc", by = variable)+ #trend for forecast week
#ti(horizon, forecast_week, by = variable), #allow effect of horizon to vary based on week
family = gaussian())
stopCluster(cl)
toc()
plot_predictions(crps.m1.full, condition = c("horizon", "site_id", "forecast_week_fct"), type = "response", points = 0.5)+
facet_wrap(.~variable)
plot_predictions(crps.m1.full, #condition = c("horizon", "site_id"),
by = c("site_id","forecast_week_fct"), type = "response", points = 0.5)
```
### GAM by variable
```{r Oxygen base model}
tic()
crps.oxy.m0 <- bam(data = crps_df|>filter(variable == "oxygen"),
mean_crps_skill ~
s(horizon), # central trend
family = gaussian())
toc() # 0.6 s
plot_predictions(crps.oxy.m0, condition = "horizon")
```
```{r Oxygen add site-level deviations}
cl <- makeCluster(6)
tic()
crps.oxy.m1 <- bam(data = crps_df|>filter(variable == "oxygen"),#|>filter(forecast_week == 35),
mean_crps_skill ~
s(horizon)+ # central trend
s(horizon, site_id, bs = "sz"), #site-level deviations from central trend
# s(forecast_week, bs = "cc", by = variable)+ #trend for forecast week
#ti(horizon, forecast_week, by = variable), #allow effect of horizon to vary based on week
family = gaussian())
stopCluster(cl)
toc()
plot_predictions(crps.oxy.m1, condition = c("horizon", "site_id"), type = "response", points = 0.5)
plot_predictions(crps.m1, #condition = c("horizon", "site_id"),
by = c("site_id","forecast_week_fct"), type = "response", points = 0.5)
```
```{r Oxygen add temporal changing intercept}
cl <- makeCluster(6)
tic()
crps.oxy.m2 <- bam(data = crps_df|>filter(variable == "oxygen"),#|>filter(forecast_week == 35),
mean_crps_skill ~
s(horizon)+ # central trend
s(horizon, site_id, bs = "sz")+ #site-level deviations from central trend
s(forecast_week, bs = "cc"), #trend for forecast week
family = gaussian(), cluster = cl)
stopCluster(cl)
toc() #119 s
plot_predictions(crps.oxy.m2, condition = c("horizon", "site_id"), type = "response", points = 0.5)
plot_predictions(crps.oxy.m2, condition = c("horizon"), type = "response", points = 0.5)
plot_predictions(crps.oxy.m2, condition = c("horizon", "forecast_week"), type = "response", points = 0.5)
```
```{r Oxygen add temporal interaction with horizon}
cl <- makeCluster(6)
tic()
crps.oxy.m3 <- bam(data = crps_df|>filter(variable == "oxygen"),#|>filter(forecast_week == 35),
mean_crps_skill ~
s(horizon)+ # central trend
s(horizon, site_id, bs = "sz")+ #site-level deviations from central trend
s(forecast_week, bs = "cc")+ #trend for forecast week
ti(horizon, forecast_week), #allow effect of horizon to vary based on week
family = gaussian(), cluster = cl)
stopCluster(cl)
toc() # 100 s
plot_predictions(crps.oxy.m3, condition = c("horizon", "site_id"), type = "response")
plot_predictions(crps.oxy.m3, condition = c("horizon"), type = "response", points = 0.5)
plot_predictions(crps.oxy.m3, condition = c("horizon", "forecast_week"), type = "response")
```
```{r Oxygen add temporal interaction at site level}
cl <- makeCluster(6)
tic()
crps.oxy.m4 <- bam(data = crps_df|>filter(variable == "oxygen")|>filter(horizon <=30),#For testing |>filter(forecast_week == 35),
mean_crps_skill ~
s(horizon)+ # central trend
s(horizon, site_id, bs = "sz")+ #site-level deviations from central trend
s(forecast_week, bs = "cc")+ #trend for forecast week
ti(horizon, forecast_week)+ #allow effect of horizon to vary based on week
s(forecast_week, site_id, bs = "fs", xt= list(bs = "cc")), #site-level deviation for forecast week effect
family = gaussian(), cluster = cl)
stopCluster(cl)
toc() #200 s
#AIC(crps.oxy.m0,crps.oxy.m1,crps.oxy.m2,crps.oxy.m3,crps.oxy.m4)
oxy_average_forecast.p<-plot_predictions(crps.oxy.m4, condition = c("horizon"), type = "response") # what is the average oxygen forecast?
oxy_hoorizonXsite.p <- plot_predictions(crps.oxy.m4, condition = c("horizon", "site_id"), type = "response")
oxy_skillXsiteXweek.p <- plot_predictions(crps.oxy.m4, condition = c("site_id", "forecast_week"), type = "response")+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
plotly::ggplotly(oxy_hoorizonXsite.p)
# think wha't sgoing on with many of these is marginal effects is making an even grid for plotting and predicting out of data bounds (splines going crazy) - need to use newdata function
```
### Temp
```{r Temperature full model}
cl <- makeCluster(6)
tic()
crps.temp.m4 <- bam(data = crps_df|>filter(variable == "temperature")|>filter(horizon <= 31),
mean_crps_skill ~
s(horizon)+ # central trend
s(horizon, site_id, bs = "sz")+ #site-level deviations from central trend
s(forecast_week, bs = "cc")+ #trend for forecast week
ti(horizon, forecast_week)+ #allow effect of horizon to vary based on week
s(forecast_week, site_id, bs = "fs", xt= list(bs = "cc")), #site-level deviation for forecast week effect
family = gaussian(), cluster = cl)
stopCluster(cl)
toc() # 164 s
rho <- acf(resid(crps.temp.m4, type = "pearson"), lag.max = 30, plot=TRUE)$acf[2]
itsadug::start_value_rho(crps.temp.m4)
cl <- makeCluster(6)
tic()
crps.temp.m4_rhoar1 <- bam(data = crps_df|>filter(variable == "temperature")|>filter(horizon <= 31),
mean_crps_skill ~
s(horizon)+ # central trend
s(horizon, site_id, bs = "sz")+ #site-level deviations from central trend
s(forecast_week, bs = "cc")+ #trend for forecast week
ti(horizon, forecast_week)+ #allow effect of horizon to vary based on week
s(forecast_week, site_id, bs = "fs", xt= list(bs = "cc")), #site-level deviation for forecast week effect
family = gaussian(), cluster = cl, rho = rho, AR.start = horizon_arstart)
stopCluster(cl)
toc() # 128s
acf(resid(crps.temp.m4, type = "pearson"))
acf(itsadug::resid_gam(crps.temp.m4_rhoar1))
itsadug::acf_resid(crps.temp.m4_rhoar1, split_pred = c("horizon"))
acf(residuals.gam(crps.temp.m4_rhoar1, type = "pearson"))
AIC(crps.temp.m4,crps.temp.m4_rhoar1)
plot_predictions(crps.temp.m4, condition = c("horizon", "site_id"), type = "response")
plot_predictions(crps.temp.m4, condition = c("horizon"), type = "response")+ # what is the average temperature forecast?
scale_y_continuous(limits = c(-4,0))
plot_predictions(crps.temp.m4_rhoar1, condition = c("horizon"), type = "response")
plot_predictions(crps.temp.m4_rhoar1, condition = c("horizon", "site_id"), type = "response")
plot_predictions(crps.temp.m4_rhoar1, condition = c("site_id", "forecast_week"), type = "response")+
scale_y_continuous(limits = c(-5,5))
plot_predictions(crps.temp.m4_rhoar1, condition = c("site_id"), type = "response")
```
```{r Temperature GAMM}
tic()
crps.temp.m4_ar1 <- gamm(data = crps_df|>filter(variable == "temperature")|>filter(horizon <= 31, site_id %in% c("ARIK", "KING")),
mean_crps_skill ~
s(horizon)+ # central trend
s(horizon, site_id, bs = "fs")+ #site-level deviations from central trend
s(forecast_week, bs = "cc")+ #trend for forecast week
ti(horizon, forecast_week)+ #allow effect of horizon to vary based on week
s(forecast_week, site_id, bs = "fs", xt= list(bs = "cc")), #site-level deviation for forecast week effect
correlation = corAR1(form = ~horizon|forecast_week_fct*site_id),
family = gaussian())
toc()
nwemod <-crps.temp.m4_ar1$lme$residuals
acf(nwemod)
acf(resid(crps.temp.m4_ar1$lme))
acf(resid(crps.temp.m4))
```