-
Notifications
You must be signed in to change notification settings - Fork 1
/
03_climate-correction-layer-for-downscaling.Rmd
478 lines (412 loc) · 13.8 KB
/
03_climate-correction-layer-for-downscaling.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
---
editor_options:
chunk_output_type: console
---
# Building a correction layer for climate predictions
In this section we focus on quantifying the error over the entire study site between model-predicted climate and climate measures derived from remote sensing data (from BIOCLIM, and referred to as CHELSA data).
We then use these differences to create a 'correction layer', that allows us to scale the model predictions for historical data.
The assumption is that the correction layer is applicable to historical climate model predictions.
## Load libraries
```{r}
library(tidyverse)
library(ggplot2)
library(colorspace)
library(patchwork)
library(terra)
library(stars)
```
```{r}
error_data <- read_csv("results/climate/data_gam_comparison_mae.csv")
survey_clim <- read_csv("results/climate/data_gam_compare_survey_sites.csv")
# load saved object from previous script
load("results/climate/model_pred_climate.Rds")
```
## Plot model error
Here we plot the mean absolute error (MAE) between CHELSA data and model predicted data in each season (dry or rainy) using boxplots.
Recall that this is for the 10,000 sampled locations split into 10 groups of 1,000 locations to calculate MAE.
```{r}
# Nest data by climate variable
error_data <- nest(
error_data,
data = !c("climvar")
)
# Get the distinct values of the model formulae
forms <- distinct(survey_clim, form = forms)
# Map over each climate variable, producing a list of ggplots
plots <- map2(
error_data$data, error_data$climvar,
function(df, cl) {
ggplot(df) +
geom_boxplot(
aes(
forms, mae
),
width = 0.5
) +
geom_text(
data = forms,
aes(
form, 1,
label = form
),
angle = 90,
hjust = "inward",
nudge_x = -0.5,
col = "steelblue",
alpha = 0.6,
fontface = "italic"
) +
scale_y_log10() +
facet_wrap(
~season,
labeller = label_both
) +
theme_grey(base_size = 10) +
theme(
strip.background = element_blank(),
axis.text.y = element_text(
angle = 90,
hjust = 0.5
),
axis.text.x = element_blank(),
panel.border = element_rect(
fill = NA, colour = "black"
)
) +
labs(
x = "GAM formula",
y = "Mean absolute error",
title = ifelse(
cl == "ppt",
"Precipitation",
"Mean temperature"
)
)
}
)
# Combine into a single plot to save
plots <- wrap_plots(
plots,
ncol = 1
) +
plot_annotation(
tag_levels = "A"
) &
theme(
plot.tag = element_text(
face = "bold"
)
)
# Save the plot for future reference
ggsave(
plots,
filename = "figs/fig_compare_models_general.png",
width = 6,
height = 8
)
dev.off()
```
![Comparing the mean absolute errors for the precipitation and temperature models](figs/fig_compare_models_general.png)
## Compare climate predictions at survey sites
Next we plot the model-predicted climate variable values against the equivalent remote-sensing derived climate values at each of the modern resurvey locations, separating by model formulation and season.
```{r}
# Nest the data by climate variable and season
survey_clim <- group_by(survey_clim, climvar, season) |>
nest()
# For each climate variable and season, produce a separate plot
# Plots form a list of ggplot objects
plots_survey_pts <- Map(
survey_clim$climvar, survey_clim$data, survey_clim$season,
f = function(cvar, df, season) {
ggplot(
df
) +
geom_abline(
slope = 1
) +
geom_point(
aes(bioclim_val, pred_val),
shape = 1,
alpha = 0.8
) +
scale_y_continuous() +
facet_grid(
~forms,
scales = "free",
labeller = labeller(
.multi_line = F,
season = label_both
)
) +
theme_grey(
base_size = 8
) +
theme(
legend.position = "top",
panel.border = element_rect(
fill = NA, colour = "black"
)
) +
labs(
colur = "Model",
title = ifelse(
cvar == "ppt",
glue::glue("Precipation; season: {season}"),
glue::glue("Mean temperature; season: {season}")
)
)
}
)
# Combine plots into a single plot
plots_survey_pts <- wrap_plots(
plots_survey_pts,
ncol = 1
) +
plot_annotation(
tag_levels = "A"
)
# Save the plot for future reference
ggsave(
plots_survey_pts,
filename = "figs/fig_survey_site_model_comparison.png",
height = 10, width = 6
)
```
![Comparing predicted values vs. data from Bioclim for rainy and dry seasons for both temperature and precipitation data](figs/fig_survey_site_model_comparison.png)
## Visualise model predictions as proportion of remote sensing data
Here, we obtain the model-predicted climate data (for each variable in each season) and express it as a proportion of the CHELSA value for the same variable, which we take as the canonical value.
```{r}
# Read in the raster data of model predictions
gam_validation_pred <- terra::rast("results/climate/gam_pred_valid_avg.tif") |>
as.list()
# Read in the data for season, climate variable, and model formulation
gam_validation_id <- read_csv("results/climate/gam_model_formulas.csv")
# Select only data for climate variables precip. and temp. mean
# and assign raster data as a list column
gam_validation_pred <- gam_validation_pred[gam_validation_id$climvar %in%
c("ppt", "t_mean")]
gam_validation_id <- filter(gam_validation_id, climvar %in% c("ppt", "t_mean"))
gam_validation_data <- mutate(
gam_validation_id,
prediction = gam_validation_pred
)
```
Next we load the BIOCLIM data and prepare variables that allow it to be merged with the model-predictions dataframe.
```{r}
# Read in BIOCLIM/CHELSA rasters
chelsa_temp <- terra::rast("results/climate/chelsa_temp_stack.tif") |>
as.list()
chelsa_ppt <- terra::rast("results/climate/chelsa_ppt_stack.tif") |>
as.list()
# Make a dataframe with combinations of season and climate variable
# and assing the raster data as a list column
chelsa_data <-
crossing(
season = c("rainy", "dry"),
climvar = c("t_mean", "ppt")
) |>
arrange(desc(climvar), desc(season)) |>
mutate(
chelsa_rast = append(chelsa_temp, chelsa_ppt)
)
```
We merge the CHELSA data with the model-prediction and get the scaling layer (called 'residual'), which is the prediction as a proportion of the canonical CHELSA data.
```{r}
# link prediction and residual
gam_validation_data <- gam_validation_data |>
left_join(chelsa_data)
gam_validation_data <- mutate(
gam_validation_data,
residual = map2(chelsa_rast, prediction, function(ch, pr) {
pr <- terra::resample(pr, ch) # resampling required
pr / ch
})
)
```
## Temperature scaling layer
We plot the scaling layer for temperature. The general pattern is that all models overestimate temperature in hilly regions of the study area; higher elevations have a higher ratio of prediction to CHELSA in all models.
In contrast, lower elevations have a prediction to CHELSA ratio close to 1.0 in all models, i.e., the model prediction is relatively good in the non-hilly areas.
```{r}
plots_temp_resid <- filter(gam_validation_data, climvar == "t_mean") |>
select(-prediction, -chelsa_rast)
plots_temp_resid <- pmap(
plots_temp_resid,
.f = function(season, climvar, forms, residual) {
residual <- st_as_stars(residual)
ggplot() +
geom_stars(
data = residual
) +
scale_fill_continuous_diverging(
palette = "Blue-Red 3",
mid = 1,
na.value = "transparent",
name = glue::glue("Prediction / BIOCLIM
{climvar}"),
limits = c(0.5, 2),
labels = scales::percent,
trans = ggallin::ssqrt_trans
) +
theme_test(base_size = 6) +
theme(
legend.position = "right",
legend.key.height = unit(10, "mm"),
legend.key.width = unit(2, "mm"),
axis.title = element_blank()
) +
coord_sf(
expand = F
) +
labs(
title = glue::glue("variable: {climvar} season: {season}
model: {forms}")
)
}
) |> wrap_plots(
guides = "collect"
) &
theme(
legend.position = "right"
)
plots_temp_resid[[1]]
```
## Precipitation scaling layer
We plot the scaling layer for precipitation. The general pattern is that all models overestimate precipitation on the leeward side of the study area, i.e., in the rain shadow of the Western Ghats, while underestimating precipitation on the windward side.
This makes sense as the driver of canonical variation is largely the blocking of monsoon winds and associated rainfall by the Ghats; while this could be expected to be captured by the distance-to-coast variable, it does not appear to do so well.
The underestimation of precipitation on the windward side is more pronounced in the rainy season, which is expected.
Higher elevations also have a lower prediction to CHELSA ratio than lower elevations on the windward side.
```{r}
plots_ppt_resid <- filter(gam_validation_data, climvar == "ppt") |>
select(-prediction, -chelsa_rast)
plots_ppt_resid <- pmap(
plots_ppt_resid,
.f = function(season, climvar, forms, residual) {
residual <- st_as_stars(residual)
ggplot() +
geom_stars(
data = residual
) +
scale_fill_continuous_diverging(
palette = "Vik",
rev = TRUE, mid = 1,
na.value = "transparent",
name = glue::glue("Prediction / BIOCLIM
{climvar}"),
limits = c(0.1, 5.5),
labels = scales::percent,
breaks = c(0.01, 0.5, seq(0.0, 5.5, 1))
) +
theme_test(base_size = 6) +
theme(
legend.position = "right",
legend.key.height = unit(10, "mm"),
legend.key.width = unit(2, "mm"),
axis.title = element_blank()
) +
coord_sf(
expand = F
) +
labs(
title = glue::glue("variable: {climvar} season: {season}
model: {forms}")
)
}
) |> wrap_plots(
guides = "collect"
) &
theme(
legend.position = "right"
)
plots_ppt_resid[[1]]
```
```{r}
# Save the scaling layer plots for future reference
ggsave(
plots_temp_resid,
filename = "figs/fig_temp_resid.png",
width = 9, height = 7
)
ggsave(
plots_ppt_resid,
filename = "figs/fig_ppt_resid.png",
width = 9, height = 7
)
```
![Bioclimatic prediction across GAM models for temperature](figs/fig_temp_resid.png)
![Bioclimatic predictions for precipitation](figs/fig_ppt_resid.png)
## Save climate scaling layers
Looking at the GAM predictions as proportions of the BIOCLIM layers, we can choose model formulas for each season and each variable that lead to predictions that are closest to the real BIOCLIM values. We choose formulas on the basis of observed deviation from the true value, as well as spatial contiguity of deviations, basically, are nearby areas similarly different from true values. For example, there is an odd north east regional deviation for rainfall in the wet season for the forumla $\text{ppt} ~ s(\text{elevation}, k = 3) + s(\text{distance to coast, latitude}, k = 5)$, so we prefer to chose another formula.
This means we pick the simple $\text{temp} ~ s(\text{elevation}, k = 3)$ formula for mean monthly temperature, in both dry and wet seasons, and the $\text{ppt} ~ s(\text{elevation}, k = 3) + \text{distance to coast} + \text{latitude}$ formula for total monthly rainfall in both dry and wet seasons.
We then save the inverse proportion, BIOCLIM / prediction, as a correction layer --- one layer per season and variable.
This allows us to fit GAMs to chunks of historical climate data, using physical predictors as covariates, and to then correct the resulting spatial prediction using the correction layer.
Hence the 'true' historical value of a climate variable is $\text{GAM prediction} \times \text{correction factor}$, where the correction factor is the cell-specific value from the correction layers.
## Seasonal mean temperature correction layer
We prepare the season-specific mean temperature correction layer.
```{r}
# Subset validation dataset
temp_correction_layer <- gam_validation_data %>%
filter(
climvar == "t_mean",
forms == "value~s(elev, k = 3)"
)
# Get the correction layer as CHELSA / prediction
temp_correction_layer <- mutate(
temp_correction_layer,
correction_layer = map2(
chelsa_rast, prediction, function(ch, pr) {
pr <- terra::resample(pr, ch) # resampling required
ch / pr
}
)
)
# Make the correction layer a single raster stack object and name correctly
temp_correction_layer <- Reduce(
temp_correction_layer$correction_layer,
f = c
)
# Set names for layers
names(temp_correction_layer) <- c(
"correction_layer_temp_dry",
"correction_layer_temp_wet"
)
# Save the correction layer
terra::writeRaster(
temp_correction_layer,
filename = "results/climate/raster_correction_layers_temp.tif"
)
```
## Seasonal total rainfall correction layer
We prepare the season-specific total precipitation correction layer.
```{r}
# Subset validation dataset
ppt_correction_layer <- gam_validation_data %>%
filter(
climvar == "ppt",
forms == "value~s(elev, k = 3) + coast + lat"
)
# Now get the correction layer as CHELSA / prediction
ppt_correction_layer <- mutate(
ppt_correction_layer,
correction_layer = map2(
chelsa_rast, prediction, function(ch, pr) {
pr <- terra::resample(pr, ch) # resampling required
ch / pr
}
)
)
# Make the correction layer a single raster stack object and name correctly
ppt_correction_layer <- Reduce(
ppt_correction_layer$correction_layer,
f = c
)
# Set names for layers
names(ppt_correction_layer) <- c(
"correction_layer_ppt_dry",
"correction_layer_ppt_wet"
)
# Save the layer
terra::writeRaster(
ppt_correction_layer,
filename = "results/climate/raster_correction_layers_ppt.tif"
)
```