-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_3_ml.qmd
1406 lines (1202 loc) · 58.8 KB
/
chapter_3_ml.qmd
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
editor:
markdown:
wrap: 80
---
# A data driven approach to predict GPP from VIs through machine learning methods
## Introduction
The field of Earth Science has witnessed a transformative shift with the
integration of Machine Learning (ML) methods, which has led to a deeper
understanding of our planet's complex ecosystems and processes
[@reichstein_deep_2019]. ML methods are now well established in environmental
sciences [@lary_machine_2016], including their use within studies aimed at
mapping and quantifying vegetation characteristics and ecological processes such
as vegetation cover, structure, and disturbances, among others
[@lehnert_retrieval_2015; @verrelst_machine_2012].
The increasing number of EC sites [@tramontana_predicting_2016] coupled with the
continuously growing amount of Earth system data surpassing dozens of petabytes
[@reichstein_deep_2019], has led to an emergence of purely data-driven
methodologies for quantifying ecosystem status and fluxes. These approaches have
shown promise for the quantification of global terrestrial photosynthesis
[@jung_global_2011; @tramontana_predicting_2016] and have resulted in good
progress in the estimation of biogeo-physical parameters using remotely sensed
reflectance data, both at local and global scales [@coops_prediction_2003;
@verrelst_retrieval_2012].
Furthermore, these data-driven approaches have contributed significantly to the
scientific community by providing spatial, seasonal, and interannual variations
in predicted fluxes. These predictions, generated through machine learning
methodologies, are now serving as important benchmarks for evaluating the
performance of physical land-surface and climate models [@jung_recent_2010;
@bonan_improving_2011; @anav_spatiotemporal_2015].
Some of the differences between data driven models and process-based methods are
the inherent observational character of data driven models and that functional
relationships emerge from the patterns found in the data, rather than being
stated before [@tramontana_predicting_2016]. So functional relationships between
in-situ measured fluxes with the explanatory variables can emerge
[@tramontana_predicting_2016]. This paradigm shift toward data-driven modeling
to extract patterns represents an opportunity to come up with new ideas and
question established theories in earth system models.
In contrast to process based models, data driven models inherently possess an
observational nature where functional relationships emerge from the patterns
found in the data, rather than being predefined, such as the relationships
between in-situ measured fluxes and the explanatory variables.
[@tramontana_predicting_2016]. For example, the application of spatially
explicit global data driven methods, has unveiled discrepancies in the
estimation of photosynthesis within tropical rainforests when compared to
climate models [@beer_terrestrial_2010]. This overestimation, has led to the
creation of hypothesis for a better understanding of radiative transfer in
vegetation canopies [@bonan_improving_2011] which can result in better
photosynthesis estimates. This paradigm shift toward data-driven modeling to
extract patterns, represents an opportunity to explore novel ideas and question
established theories in earth system models [@reichstein_deep_2019].
Predicting dynamics in the biosphere is challenging due to biologically mediated
processes [@reichstein_deep_2019]. The term "prediction" should not be confused
with forecasting, as most models are not aiming at predicting into the future.
Instead, the focus of these data-driven models is to improve historical
estimates or enable the use of reflectance values to predict GPP when no in-situ
data is available in the present times [@meyer_improving_2018].
These predictions can include many forms of uncertainty [@reichstein_deep_2019].
One form is that individual ML methods can have different responses, especially
when these models are applied beyond the conditions presented in the training
dataset. [@jung_towards_2009; @papale_effect_2015]. Another form is related to
the explanatory variables used in ML methods derived from satellite remote
sensing, which are partial in providing information about the vegetation state
[@tramontana_predicting_2016]. Consequently, they lack the information required
to explain the complete variability in fluxes [@tramontana_uncertainty_2015].
For instance, if a model is created using only reflectance data to estimate GPP
without meteorological data, phenomena such as drought could lead to predicted
values with large errors, given that stomata closure during water deficit have
an immediate effect on the fluxes that can not be detected by the reflectance
values only until later when the stress conditions persist
[@tramontana_uncertainty_2015].
This chapter evaluates data drive approaches for estimating GPP given MODIS
surface reflectance data for temperate broadleaf forests in North America. Two
state-of-the-art approaches previously used for GPP estimation are tested:
regression random forests [@sohil_introduction_2022] and AutoML [@ledell2020h2o]
to understand how the ML algorithm impacts prediction uncertainty. Both
approaches were tested locally and using pooled data from three sites to
quantify their ability to be applied over larger spatial extents.
\newpage
## Methods
```{r libraries and sources}
#| echo: false
#| message: false
#| warning: false
# Libraries
library(ggplot2)
library(cowplot)
library(lubridate)
library(purrr)
library(broom)
library(gt)
library(tidymodels)
library(broom)
library(usemodels)
library(vip)
library(h2o)
library(stringr)
library(DALEX)
library(DALEXtra)
library(forcats)
library(ranger)
# Source files
# Source the objects created for the complete GPP trends.
# This file will source the code and load objects to memory.
source("scripts/trend_plots.R")
# Source the objects created for the complete GPP trends
source("scripts/models_data_preparation.R")
# Source file with functions to plot rf predictions
source("R/plot_exploratory.R")
```
<!-- - Sites -->
<!-- - ONEFlux for GPP -->
<!-- - Satellite imagery -->
<!-- - indices calculation -->
<!-- - Here I'm using all the bands available -->
<!-- - Cleaning satellite data -->
<!-- - Filter -->
<!-- - scaling -->
<!-- - join -->
### Eddy Covariance sites
For this study, we selected three deciduous broadleaf forest forests sites:
University of Michigan Biological Station located in northern Michigan, USA
(45°350 N 84°430 W), Bartlett experimental forest in New Hampshire, USA (44°06
N, 71°3 W), and the Borden Forest Research Station (44°19 N, 79°56 W) in
Ontario, Canada. These sites were selected to ensure they represented a single
ecosystem type, characterized by shared environmental features. This approach
allowed us to treat the dataset as a representation of a specific vegetation
type terrestrial ecosystem.
In-situ data such as GPP was obtained utilizing the ONEFlux estimation
processing by Ameriflux. Here, we selected GPP estimation done by the daytime
method [@pastorello2020fluxnet2015] on a daily, weekly, and monthly basis.
To capture seasonal variations and long-term trends, GPP data was collected over
a minimum of 2 years. Specifically, University of Michigan Biological Station
collected data spanned from January 2015 to January 2018, Bartlett experimental
forest data ranges from January 2015 to December 2018, and Borden Forest
Research Station from January 2015 to January 2022.
<!-- From ameriflux: -->
<!-- https://ameriflux.lbl.gov/sites/siteinfo/US-Bar -->
<!-- https://ameriflux.lbl.gov/sites/siteinfo/CA-Cbo -->
<!-- https://ameriflux.lbl.gov/sites/siteinfo/US-UMB -->
<!-- All sites are Dfb -->
<!-- The code "Dfb" in the Köppen system refers to a specific climate type, which can be described as follows: -->
<!-- "D" stands for the warm-summer continental or hemiboreal climate. -->
<!-- "f" indicates that this climate has significant precipitation in all seasons. -->
<!-- "b" indicates that the warmest month has an average temperature between 22°C and 28°C. -->
### Satellite imagery
We used Google Earth Engine (GEE) to retrieve the Terra Moderate Resolution
Imaging Spectroradiometer (MODIS), specifically the collection MOD09GA Version
6.1 daily 500m resolution surface reflectance products (MODIS/Terra Surface
Reflectance Daily L2G Global 1 km and 500 m SIN Grid). A square polygon with an
area of 3km surrounding the EC tower was defined for each of the study sites,
and all daily data pixel values within this polygon were extracted for analysis.
MOD09GA contains the surface spectral reflectance from bands 1 through 7
(@tbl-MODIS_500_complete_bands) with a spatial resolution of 500m, with
corrections for atmospheric conditions such as aerosols, gasses, and Rayleigh
scattering [@vermote2021modis].
We selected the highest quality pixels according to the 1km Reflectance Data
State QA (`state_1km`) (@tbl-state_1km_bitstrings) and Surface Reflectance 500m
Quality Assurance (`qc_500m`) (@tbl-qc_scan_bit_strings) variables. Once we had
just the highest quality pixels, all the band values were scaled by a factor of
0.0001. If any value fell outside the range of 0 to 1 after the scaling, it was
discarded.
Once all the band values were scaled, we calculated 4 Vegetation Indices: `NDVI`
(@eq-ndvi), `NIRv` (@eq-nirv), `EVI` (@eq-evi), and `CCI` (@eq-cci). Then all
the MODIS bands values and VIs were summarized on a daily, weekly, and monthly
basis to be merged with the GPP values from ONEFlux.
| **Name** | **Description** | **Resolution** | **Wavelength** |
|-------------|-----------------|----------------|----------------|
| sur_refl_01 | Red | 500 meters | 620-670nm |
| sur_refl_02 | NIR | 500 meters | 841-876nm |
| sur_refl_03 | Blue | 500 meters | 459-479nm |
| sur_refl_04 | Green | 500 meters | 545-565nm |
| sur_refl_05 | Red Edge | 500 meters | 1230-1250nm |
| sur_refl_06 | SWIR 1 | 500 meters | 1628-1652nm |
| sur_refl_07 | SWIR 2 | 500 meters | 2105-2155nm |
: MODIS (MOD09GA.061 product) bands used for ML methods
{#tbl-MODIS_500_complete_bands}
### Random Forests
A random forest is an ensemble learning technique that leverages the power of
multiple decision trees to improve predictive accuracy and robustness. It
combines regression and classification trees, constructing each tree from random
subsets of both trained data and features [@sohil_introduction_2022]. The final
prediction is obtained by aggregating individual tree predictions, which are
then averaged to produce the ultimate estimate. Additionally, at each split, the
best predictor from the random subset is selected to effectively partition the
data and in the case of the regression random forest, the process is adapted to
predict a continuous numeric outcome[@sohil_introduction_2022;
@meyer_improving_2018].
Regression random forests were used as an approach to predict GPP as a function
of all available MOD09GA bands values (from B01 to B07) and the calculated VIs.
Three distinct models were developed, each one tailored for a specific time
scale (daily, weekly, and monthly). This approach allowed us to assess the
prediction performance of GPP at different time scales.
Each model was calibrated using a random data splitting procedure, dividing the
data into a training set with 70% of the observations and a test set with the
remaining 30%. Due to the varying number of observations for each site, to avoid
an imbalanced training dataset, we employed a stratified data split to ensure a
proportional representation of each site category in both sets. To ensure
reproducibility, we used a consistent random number generator state throughout
the process.
To implement the RF models, we use the `ranger` package in R
[@wright_ranger_2017], utilizing 1000 trees within the forest ensemble. The
models were trained using the bootstrap resampling technique with 100 folds,
which helps to improve the robustness and accuracy of the predictions.
We calculated the variable of importance (VIP) to understand which MODIS bands
or VIs are driving the predictions in each of the regression random forest
models. To measure the influence of each feature on the overall model's
predictive performance, we quantify how this performance deteriorates when a
particular variable is permuted while keeping others constant.
To understand which features contributed the most on average to a particular GPP
prediction in different coalitions [@molnar2020interpretable], we calculated the
Shapley values [@lundberg_unified_nodate]. These values were computed with the
DALEX package in R [@biecek2018dalex] for both low GPP and high GPP scenarios
within each of the temporally aggregated models.
### AutoML
The AutoML approach is designed to identify the most optimal ML pipeline for a
specific problem and available training data by evaluating different
combinations of data processing steps, ML models, and hyperparameters settings
[@gaber2023using]. To create a stacked ensemble of models and evaluate the
performance, we utilized the H2O AutoML framework [@ledell2020h2o].
For the data training and evaluation, we randomly split the dataset into a
training and a test set, allocating 80% and 20% of the observations,
respectively. We excluded the `total observations` and `site` variables from
both datasets, while the remaining features (bands and VIs), were considered
predictors. The response variable for prediction was GPP. We transformed the
data into an H2O frame to ensure compatibility with the AutoML process.
After generating machine learning models and hyperparameter configurations, we
imposed a time constraint of a maximum of 2 minutes for model training. The best
performing models were then selected in terms of lower predictions erros and
used to form an ensemble. This trained ensemble model was used to generate
predictions on the test set. Model performance was evaluated using metrics such
as the coefficient of determination and the RMSE.
To asses the variable importance, we determined the influence of each variable
on the model's predictions. The results of this analysis were visually
represented as heatmaps for each of the models within the ensemble.
\newpage
## Results
### Data-Driven GPP Prediction: A Regression Random Forest Approach
The best performing model, based on R² and RMSE was the monthly aggregated
model, achieving a R² of 0.81 and an RMSE of
$2.03 \, \mathrm{gC \, m^{-2} \, d^{-1}}$ (see @fig-monthly_500_rf). The daily
model exhibited the second best performance model with a RMSE of
$3.20 \, \mathrm{gC \, m^{-2} \, d^{-1}}$ and a R² of 0.69 (see
@fig-daily_500_rf). However, the weekly model displayed comparatively lower
performance, with an R² of 0.56 and an RMSE of
$3.23 \, \mathrm{gC \, m^{-2} \, d^{-1}}$ (see @fig-weekly_500_rf)
Moreover, in assesing the importance of predictor variables within each model,
VIs variables held top positions across all models, surpassing the importance of
any other spectral bands alone. Specifically, the CCI contributed most
significantly to the predictive performance in two of the models, the weekly and
monthly models (@fig-vip_weekly_500_rf and @fig-vip_monthly_500_rf). In the case
of the daily model, CCI emerged as the second most influential predictor
variable (as shown in @fig-vip_daily_500_rf).
Despite NDVI being ranked as the second variable in importance for the
monthly model, it was the fifth variable in importance for the daily model and
the fourth one for the weekly model. Meanwhile, the NIRv consistently held the
third most influential position across all models. EVI was found to be most
important for the daily model, the second most important variable in the weekly
model, but fourth in the monthly model.
To evaluate how the predictor variables had an impact on the model's
predictions, we employed the Shapley values. For each of the models created, we
calculated the contributions to each variable in predicting GPP for two cases:
for a known high GPP value and a known low GPP value from the test data set.
In the case of the daily model, we chose a low GPP value from the test dataset,
specifically $0.01 \, \mathrm{gC \, m^{-2} \, d^{-1}}$and obtained a model
prediction of $0.60 \, \mathrm{gC \, m^{-2} \, d^{-1}}$. Our Shapley value
analysis showed that EVI, CCI and NIRv were the most influential attributes
affecting the model prediction (See @fig-shap_daily_500_rf **A**). These VIs
contribute negatively to the prediction, reducing the predicted GPP.
Conversely, the high GPP value selected
($16.21 \, \mathrm{gC \, m^{-2} \, d^{-1}}$) from the test dataset, had a model
prediction of $12.70 \, \mathrm{gC \, m^{-2} \, d^{-1}}$. In this case the
Shapley values analysis indicated that the most influential variables were CCI
and NIRv (See @fig-shap_daily_500_rf **B**). These variables contributed
positively to the prediction.
For the weekly model, we selected high and low GPP values of
$1.21 \, \mathrm{gC \, m^{-2} \, d^{-1}}$ and
$13.17 \, \mathrm{gC \, m^{-2} \, d^{-1}}$, respectively, resulting in
predictions of $1.60 \, \mathrm{gC \, m^{-2} \, d^{-1}}$ and
$11.0 \, \mathrm{gC \, m^{-2} \, d^{-1}}$. For the low GPP value, the most
influential variables were CCI, EVI, NIRv, and NDVI, all contributing negatively
to the prediction (see @fig-shap_weekly_500_rf **A**). Conversely, for the high
GPP value, CCI, EVI, NIRv, B02 and NDVI had the most influence on the prediction
positively (refer to @fig-shap_weekly_500_rf **B**).
In the case of the monthly model, the low GPP value selected was
$1.86 \, \mathrm{gC \, m^{-2} \, d^{-1}}$ and the high GPP value was
$11.87 \, \mathrm{gC \, m^{-2} \, d^{-1}}$, leading to predictions of
$3.31 \, \mathrm{gC \, m^{-2} \, d^{-1}}$ and\
$10.60 \, \mathrm{gC \, m^{-2} \, d^{-1}}$ respectively. In both scenarios, CCI,
NDVI, and NIRv emerged as the most influential variables, albeit contributing
negatively to the low GPP value prediction (See @fig-shap_monthly_500_rf **A**)
and positively to the high GPP value prediction (See @fig-shap_monthly_500_rf
**B**).
```{r data_preparation_rf}
#| echo: false
#| message: false
#| warning: false
# 500
# Dataset to use: daily_500 for all sites
bor <- borden_daily_500 %>%
select(ends_with(c("_mean")),
gpp_dt_vut_ref, total_obs) %>%
mutate(site = "borden")
bar <- bartlett_daily_500 %>%
select(ends_with(c("_mean")),
gpp_dt_vut_ref, total_obs) %>%
mutate(site = "bartlett")
mich <- michigan_daily_500 %>%
select(ends_with(c("_mean")),
gpp_dt_vut_ref, total_obs) %>%
mutate(site = "michigan")
daily_500_rf <- bind_rows(bor, bar, mich) %>%
select(-kndvi_mean)
# Dataset to use: weekly_500 for all sites
bor <- borden_weekly_500 %>%
select(ends_with(c("_mean")),
gpp_dt_vut_ref, total_obs) %>%
mutate(site = "borden")
variables <- names(bor)
bar <- bartlett_weekly_500 %>%
mutate(site = "bartlett") %>%
select(all_of(variables))
mich <- michigan_weekly_500 %>%
mutate(site = "michigan") %>%
select(all_of(variables))
weekly_500_rf <- bind_rows(bor, bar, mich) %>%
select(-kndvi_mean)
## Dataset to use: monthly_500 for all sites
bor <- borden_monthly_500 %>%
select(ends_with(c("_mean")),
gpp_dt_vut_ref, total_obs) %>%
mutate(site = "borden")
variables <- names(bor)
bar <- bartlett_monthly_500 %>%
mutate(site = "bartlett") %>%
select(all_of(variables))
mich <- michigan_monthly_500 %>%
mutate(site = "michigan") %>%
select(all_of(variables))
monthly_500_rf <- bind_rows(bor, bar, mich) %>%
select(-kndvi_mean)
```
<!-- #### Daily 500 -->
```{r xgboost_daily}
#| echo: false
#| message: false
#| warning: false
# set.seed(123)
# daily_500_split <- initial_split(daily_500_rf, strata = site)
# daily_500_train <- training(daily_500_split)
# daily_500_test <- testing(daily_500_split)
#
# set.seed(234)
# # daily_500_folds
# daily_500_folds <- bootstraps(daily_500_train,
# times = 100,
# strata = gpp_dt_vut_ref)
#
# bb_recipe <-
# recipe(formula = gpp_dt_vut_ref ~ ., data = daily_500_train) %>%
# step_select(-site, -total_obs)
#
# xgb_spec <-
# boost_tree(
# trees = tune(),
# min_n = tune(),
# mtry = tune(),
# learn_rate = 0.01
# ) %>%
# set_engine("xgboost") %>%
# set_mode("regression")
#
# xgb_wf <- workflow(bb_recipe, xgb_spec)
#
# set.seed(3156)
#
# library(finetune)
# doParallel::registerDoParallel()
#
# set.seed(345)
# xgb_rs <- tune_race_anova(
# xgb_wf,
# resamples = daily_500_folds,
# grid = 15,
# # metrics = metric_set(mn_log_loss),
# control = control_race(verbose_elim = TRUE)
# )
#
# plot_race(xgb_rs)
#
# show_best(xgb_rs)
#
# xgb_last <- xgb_wf %>%
# finalize_workflow(select_best(xgb_rs)) %>%
# last_fit(daily_500_split)
#
# xgb_last
# metrics <- collect_metrics(xgb_last)
#
# plot_predictions_rf(xgb_last, metrics, 4, 4, 23.5, 22)
```
```{r daily_500_rf}
#| echo: false
#| message: false
#| warning: false
## Make sure that the source of the file "R/plot_exploratory.R" was succesful
set.seed(752)
daily_500_split <- initial_split(daily_500_rf, strata = site)
daily_500_train <- training(daily_500_split)
daily_500_test <- testing(daily_500_split)
set.seed(234)
# daily_500_folds
daily_500_folds <- bootstraps(daily_500_train,
times = 100,
strata = gpp_dt_vut_ref)
ranger_recipe <-
recipe(formula = gpp_dt_vut_ref ~ ., data = daily_500_train) %>%
step_select(-site, -total_obs, skip = TRUE)
ranger_spec <-
rand_forest(mtry = tune(), min_n = tune(), trees = 1000) %>%
set_mode("regression") %>%
set_engine("ranger")
ranger_workflow <-
workflow() %>%
add_recipe(ranger_recipe) %>%
add_model(ranger_spec)
# Conditional to re-run model if no artifac was saved before.
if (fs::file_exists("models/daily_500_fit_site.rds") &
fs::file_exists("models/daily_500_site_ranger_tune.rds")) {
daily_500_fit <- readRDS("models/daily_500_fit_site.rds")
ranger_tune <- readRDS("models/daily_500_site_ranger_tune.rds")
} else {
doParallel::registerDoParallel()
set.seed(6578)
ranger_tune <-
tune_grid(ranger_workflow,
resamples = daily_500_folds,
grid = 12)
# Final fit
final_rf <- ranger_workflow %>%
finalize_workflow(select_best(ranger_tune))
daily_500_fit <- last_fit(final_rf, daily_500_split)
## last_fit is saved if no model has been trained and saved.
saveRDS(ranger_tune, "models/daily_500_site_ranger_tune.rds")
saveRDS(daily_500_fit, "models/daily_500_fit_site.rds")
}
```
```{r predictions_plot_daily_500_rf}
#| label: fig-daily_500_rf
#| fig-cap: GPP observed and predicted values from the Random Forest model for all the sites at a daily basis. The red line represents a 1:1 relation. Metrics units are gC m⁻² d⁻¹
#| fig-width: 6
#| fig-height: 4
#| echo: false
#| message: false
#| warning: false
# Explore RF results
## Check the metrics
metrics <- collect_metrics(daily_500_fit)
## Collect predictions
plot_predictions_rf(daily_500_fit, metrics, 4, 4, 23.5, 22)
```
```{r vip_plot_daily_500_rf}
#| label: fig-vip_daily_500_rf
#| fig-cap: "Variable of importance derived from the Random forest model for the daily values at 500 m spatial resolution model."
#| echo: false
#| message: false
#| warning: false
## Feature importance
imp_spec <- ranger_spec %>%
finalize_model(select_best(ranger_tune)) %>%
set_engine("ranger", importance = "permutation")
workflow() %>%
add_recipe(ranger_recipe) %>%
add_model(imp_spec) %>%
fit(daily_500_train) %>%
extract_fit_parsnip() %>%
vip(aesthetics = list(alpha = 0.8, fill = "midnightblue")) +
theme_light(base_size = 12) +
scale_x_discrete(labels = c("ndvi_mean" = "NDVI",
"nirv_mean" = "NIRv",
"evi_mean" = "EVI",
"cci_mean" = "CCI",
"sur_refl_b01_mean" = "B01",
"sur_refl_b02_mean" = "B02",
"sur_refl_b03_mean" = "B03",
"sur_refl_b04_mean" = "B04",
"sur_refl_b05_mean" = "B05",
"sur_refl_b06_mean" = "B06",
"sur_refl_b07_mean" = "B07"))
```
```{r shapley_values_daily}
#| label: fig-shap_daily_500_rf
#| fig-cap: "Shapley values derived from the Random forest model for the daily values at 500 m spatial resolution model. Predicted value for the low GPP value is 0.59 (A) and 12.7 for the selected high GPP value (B)."
#| echo: false
#| message: false
#| warning: false
# Extract workflow to obtain shapley values (or run predict)
daily_gpp_model <- extract_workflow(daily_500_fit)
# Create an explainer for a regression model
explainer_rf <- explain_tidymodels(
daily_gpp_model,
data = daily_500_train,
y = daily_500_train$gpp_dt_vut_ref,
label = "rf",
verbose = FALSE
)
# Take a low gpp value
low_gpp <- daily_500_train[71, ]
low_gpp_pred <- predict(daily_gpp_model, low_gpp)
rf_breakdown <- predict_parts(explainer = explainer_rf,
new_observation = low_gpp,
type = "shap")
# rf_breakdown
low_value <- rf_breakdown %>%
group_by(variable) %>%
mutate(mean_val = mean(contribution)) %>%
ungroup() %>%
mutate(variable = case_when(
str_detect(variable, "ndvi_mean") ~ str_replace(variable, "ndvi_mean", "NDVI"),
str_detect(variable, "nirv_mean") ~ str_replace(variable, "nirv_mean", "NIRv"),
str_detect(variable, "evi_mean" ) ~ str_replace(variable, "evi_mean", "EVI" ),
str_detect(variable, "cci_mean" ) ~ str_replace(variable, "cci_mean", "CCI" ),
str_detect(variable, "sur_refl_b01_mean") ~ str_replace(variable, "sur_refl_b01_mean", "B01"),
str_detect(variable, "sur_refl_b02_mean") ~ str_replace(variable, "sur_refl_b02_mean", "B02"),
str_detect(variable, "sur_refl_b03_mean") ~ str_replace(variable, "sur_refl_b03_mean", "B03"),
str_detect(variable, "sur_refl_b04_mean") ~ str_replace(variable, "sur_refl_b04_mean", "B04"),
str_detect(variable, "sur_refl_b05_mean") ~ str_replace(variable, "sur_refl_b05_mean", "B05"),
str_detect(variable, "sur_refl_b06_mean") ~ str_replace(variable, "sur_refl_b06_mean", "B06"),
str_detect(variable, "sur_refl_b07_mean") ~ str_replace(variable, "sur_refl_b07_mean", "B07"),
str_detect(variable, "total_obs") ~ str_replace(variable, "total_obs", "Total obs."),
str_detect(variable, "site") ~ str_replace(variable, "site", "Site"),
str_detect(variable, "gpp_dt_vut_ref") ~ str_replace(variable, "gpp_dt_vut_ref", "GPP"),
.default = variable
)) %>%
mutate(variable = fct_reorder(variable, abs(mean_val))) %>%
ggplot(aes(contribution, variable, fill = mean_val > 0)) +
geom_col(data = ~distinct(., variable, mean_val),
aes(mean_val, variable),
alpha = 0.5) +
geom_boxplot(width = 0.5) +
theme_light() +
theme(legend.position = "none") +
scale_fill_viridis_d() +
labs(y = NULL)
# Take a high gpp value
high_gpp <- daily_500_train[578, ]
high_gpp_pred <- predict(daily_gpp_model, high_gpp)
rf_breakdown <- predict_parts(explainer = explainer_rf,
new_observation = high_gpp,
type = "shap")
# rf_breakdown
high_value <- rf_breakdown %>%
group_by(variable) %>%
mutate(mean_val = mean(contribution)) %>%
ungroup() %>%
mutate(variable = case_when(
str_detect(variable, "ndvi_mean") ~ str_replace(variable, "ndvi_mean", "NDVI"),
str_detect(variable, "nirv_mean") ~ str_replace(variable, "nirv_mean", "NIRv"),
str_detect(variable, "evi_mean" ) ~ str_replace(variable, "evi_mean", "EVI" ),
str_detect(variable, "cci_mean" ) ~ str_replace(variable, "cci_mean", "CCI" ),
str_detect(variable, "sur_refl_b01_mean") ~ str_replace(variable, "sur_refl_b01_mean", "B01"),
str_detect(variable, "sur_refl_b02_mean") ~ str_replace(variable, "sur_refl_b02_mean", "B02"),
str_detect(variable, "sur_refl_b03_mean") ~ str_replace(variable, "sur_refl_b03_mean", "B03"),
str_detect(variable, "sur_refl_b04_mean") ~ str_replace(variable, "sur_refl_b04_mean", "B04"),
str_detect(variable, "sur_refl_b05_mean") ~ str_replace(variable, "sur_refl_b05_mean", "B05"),
str_detect(variable, "sur_refl_b06_mean") ~ str_replace(variable, "sur_refl_b06_mean", "B06"),
str_detect(variable, "sur_refl_b07_mean") ~ str_replace(variable, "sur_refl_b07_mean", "B07"),
str_detect(variable, "total_obs") ~ str_replace(variable, "total_obs", "Total obs."),
str_detect(variable, "site") ~ str_replace(variable, "site", "Site"),
str_detect(variable, "gpp_dt_vut_ref") ~ str_replace(variable, "gpp_dt_vut_ref", "GPP"),
.default = variable
)) %>%
mutate(variable = fct_reorder(variable, abs(mean_val))) %>%
ggplot(aes(contribution, variable, fill = mean_val > 0)) +
geom_col(data = ~distinct(., variable, mean_val),
aes(mean_val, variable),
alpha = 0.5) +
geom_boxplot(width = 0.5) +
theme_light() +
theme(legend.position = "none") +
scale_fill_viridis_d() +
labs(y = NULL)
plot_grid(low_value,
high_value,
nrow = 1,
labels = c('A', 'B'),
vjust = 1)
```
<!-- #### Weekly 500 -->
```{r weekly_500_rf}
#| echo: false
#| message: false
#| warning: false
set.seed(125)
weekly_500_split <- initial_split(weekly_500_rf, strata = site)
weekly_500_train <- training(weekly_500_split)
weekly_500_test <- testing(weekly_500_split)
set.seed(2389)
weekly_500_folds <- bootstraps(weekly_500_train,
times = 100,
strata = gpp_dt_vut_ref)
ranger_recipe <-
recipe(formula = gpp_dt_vut_ref ~ ., data = weekly_500_train) %>%
step_select(-site, -total_obs, skip = TRUE)
ranger_spec <-
rand_forest(mtry = tune(), min_n = tune(), trees = 1000) %>%
set_mode("regression") %>%
set_engine("ranger")
ranger_workflow <-
workflow() %>%
add_recipe(ranger_recipe) %>%
add_model(ranger_spec)
# Conditional to re-run model if no artifac was saved before.
if (fs::file_exists("models/weekly_500_fit_site.rds")) {
weekly_500_fit <- readRDS("models/weekly_500_fit_site.rds")
ranger_tune <- readRDS("models/weekly_500_site_ranger_tune.rds")
} else {
doParallel::registerDoParallel()
set.seed(1297)
ranger_tune <-
tune_grid(ranger_workflow,
resamples = weekly_500_folds,
grid = 12)
# Final fit
final_rf <- ranger_workflow %>%
finalize_workflow(select_best(ranger_tune))
weekly_500_fit <- last_fit(final_rf, weekly_500_split)
## last_fit is saved if no model has been trained and saved.
saveRDS(ranger_tune, "models/weekly_500_site_ranger_tune.rds")
saveRDS(weekly_500_fit, "models/weekly_500_fit_site.rds")
}
```
```{r predictions_plot_weekly_500_rf}
#| label: fig-weekly_500_rf
#| fig-cap: "GPP observed and predicted values from the Random Forest for all the sites at a weekly basis. The red line represents a 1:1 relation. Metrics units are gC m⁻² d⁻¹"
#| fig-width: 6
#| fig-height: 4
#| echo: false
#| message: false
#| warning: false
# Explore RF results
## Check the metrics
metrics <- collect_metrics(weekly_500_fit)
## Collect predictions
plot_predictions_rf(weekly_500_fit, metrics, 5, 5, 18, 19)
```
```{r vip_plot_weekly_500_rf}
#| label: fig-vip_weekly_500_rf
#| fig-cap: "Variable of importance derived from the Random forest model for the weekly values at 500 m spatial resolution model."
#| echo: false
#| message: false
#| warning: false
## Feature importance
imp_spec <- ranger_spec %>%
finalize_model(select_best(ranger_tune)) %>%
set_engine("ranger", importance = "permutation")
workflow() %>%
add_recipe(ranger_recipe) %>%
add_model(imp_spec) %>%
fit(weekly_500_train) %>%
extract_fit_parsnip() %>%
vip(aesthetics = list(alpha = 0.8, fill = "midnightblue")) +
theme_classic(base_size = 12) +
scale_x_discrete(labels = c("ndvi_mean" = "NDVI",
"nirv_mean" = "NIRv",
"evi_mean" = "EVI",
"cci_mean" = "CCI",
"sur_refl_b01_mean" = "B01",
"sur_refl_b02_mean" = "B02",
"sur_refl_b03_mean" = "B03",
"sur_refl_b04_mean" = "B04",
"sur_refl_b05_mean" = "B05",
"sur_refl_b06_mean" = "B06",
"sur_refl_b07_mean" = "B07"))
```
```{r shapley_values_weekly}
#| label: fig-shap_weekly_500_rf
#| fig-cap: "Shapley values derived from the Random forest model for the weekly values at 500 m spatial resolution model. Predicted value for the low GPP value is 1.59 (A) and 11.0 for the selected high GPP value (B)."
#| echo: false
#| message: false
#| warning: false
# Extract workflow to obtain shapley values (or run predict)
weekly_gpp_model <- extract_workflow(weekly_500_fit)
# Create an explainer for a regression model
explainer_rf <- explain_tidymodels(
weekly_gpp_model,
data = weekly_500_train,
y = weekly_500_train$gpp_dt_vut_ref,
label = "rf",
verbose = FALSE
)
# Take a low gpp value
low_gpp <- weekly_500_train[16, ]
low_gpp_pred <- predict(weekly_gpp_model, low_gpp)
rf_breakdown <- predict_parts(explainer = explainer_rf,
new_observation = low_gpp,
type = "shap")
# rf_breakdown
low_value <- rf_breakdown %>%
group_by(variable) %>%
mutate(mean_val = mean(contribution)) %>%
ungroup() %>%
mutate(variable = case_when(
str_detect(variable, "ndvi_mean") ~ str_replace(variable, "ndvi_mean", "NDVI"),
str_detect(variable, "nirv_mean") ~ str_replace(variable, "nirv_mean", "NIRv"),
str_detect(variable, "evi_mean" ) ~ str_replace(variable, "evi_mean", "EVI" ),
str_detect(variable, "cci_mean" ) ~ str_replace(variable, "cci_mean", "CCI" ),
str_detect(variable, "sur_refl_b01_mean") ~ str_replace(variable, "sur_refl_b01_mean", "B01"),
str_detect(variable, "sur_refl_b02_mean") ~ str_replace(variable, "sur_refl_b02_mean", "B02"),
str_detect(variable, "sur_refl_b03_mean") ~ str_replace(variable, "sur_refl_b03_mean", "B03"),
str_detect(variable, "sur_refl_b04_mean") ~ str_replace(variable, "sur_refl_b04_mean", "B04"),
str_detect(variable, "sur_refl_b05_mean") ~ str_replace(variable, "sur_refl_b05_mean", "B05"),
str_detect(variable, "sur_refl_b06_mean") ~ str_replace(variable, "sur_refl_b06_mean", "B06"),
str_detect(variable, "sur_refl_b07_mean") ~ str_replace(variable, "sur_refl_b07_mean", "B07"),
str_detect(variable, "total_obs") ~ str_replace(variable, "total_obs", "Total obs."),
str_detect(variable, "site") ~ str_replace(variable, "site", "Site"),
str_detect(variable, "gpp_dt_vut_ref") ~ str_replace(variable, "gpp_dt_vut_ref", "GPP"),
.default = variable
)) %>%
mutate(variable = fct_reorder(variable, abs(mean_val))) %>%
ggplot(aes(contribution, variable, fill = mean_val > 0)) +
geom_col(data = ~distinct(., variable, mean_val),
aes(mean_val, variable),
alpha = 0.5) +
geom_boxplot(width = 0.5) +
theme_light() +
theme(legend.position = "none") +
scale_fill_viridis_d() +
labs(y = NULL)
# Take a high gpp value
high_gpp <- weekly_500_train[167, ]
high_gpp_pred <- predict(weekly_gpp_model, high_gpp)
rf_breakdown <- predict_parts(explainer = explainer_rf,
new_observation = high_gpp,
type = "shap")
# rf_breakdown
high_value <- rf_breakdown %>%
group_by(variable) %>%
mutate(mean_val = mean(contribution)) %>%
ungroup() %>%
mutate(variable = case_when(
str_detect(variable, "ndvi_mean") ~ str_replace(variable, "ndvi_mean", "NDVI"),
str_detect(variable, "nirv_mean") ~ str_replace(variable, "nirv_mean", "NIRv"),
str_detect(variable, "evi_mean" ) ~ str_replace(variable, "evi_mean", "EVI" ),
str_detect(variable, "cci_mean" ) ~ str_replace(variable, "cci_mean", "CCI" ),
str_detect(variable, "sur_refl_b01_mean") ~ str_replace(variable, "sur_refl_b01_mean", "B01"),
str_detect(variable, "sur_refl_b02_mean") ~ str_replace(variable, "sur_refl_b02_mean", "B02"),
str_detect(variable, "sur_refl_b03_mean") ~ str_replace(variable, "sur_refl_b03_mean", "B03"),
str_detect(variable, "sur_refl_b04_mean") ~ str_replace(variable, "sur_refl_b04_mean", "B04"),
str_detect(variable, "sur_refl_b05_mean") ~ str_replace(variable, "sur_refl_b05_mean", "B05"),
str_detect(variable, "sur_refl_b06_mean") ~ str_replace(variable, "sur_refl_b06_mean", "B06"),
str_detect(variable, "sur_refl_b07_mean") ~ str_replace(variable, "sur_refl_b07_mean", "B07"),
str_detect(variable, "total_obs") ~ str_replace(variable, "total_obs", "Total obs."),
str_detect(variable, "site") ~ str_replace(variable, "site", "Site"),
str_detect(variable, "gpp_dt_vut_ref") ~ str_replace(variable, "gpp_dt_vut_ref", "GPP"),
.default = variable
)) %>%
mutate(variable = fct_reorder(variable, abs(mean_val))) %>%
ggplot(aes(contribution, variable, fill = mean_val > 0)) +
geom_col(data = ~distinct(., variable, mean_val),
aes(mean_val, variable),
alpha = 0.5) +
geom_boxplot(width = 0.5) +
theme_light() +
theme(legend.position = "none") +
scale_fill_viridis_d() +
labs(y = NULL)
plot_grid(low_value,
high_value,
nrow = 1,
labels = c('A', 'B'),
vjust = 1)
```
<!-- #### Monthly -->
```{r monthly_500_rf}
#| echo: false
#| message: false
#| warning: false
set.seed(973)
monthly_500_split <- initial_split(monthly_500_rf, strata = site)
monthly_500_train <- training(monthly_500_split)
monthly_500_test <- testing(monthly_500_split)
# monthly_500_folds
set.seed(365)
monthly_500_folds <- bootstraps(monthly_500_train,
times = 100,
strata = gpp_dt_vut_ref)
ranger_recipe <-
recipe(formula = gpp_dt_vut_ref ~ ., data = monthly_500_train) %>%
step_select(-site, -total_obs, skip = TRUE)
ranger_spec <-
rand_forest(mtry = tune(), min_n = tune(), trees = 1000) %>%
set_mode("regression") %>%
set_engine("ranger")
ranger_workflow <-
workflow() %>%
add_recipe(ranger_recipe) %>%
add_model(ranger_spec)
# Conditional to re-run model if no artifac was saved before.
if (fs::file_exists("models/monthly_500_fit_site.rds")) {
monthly_500_fit <- readRDS("models/monthly_500_fit_site.rds")
ranger_tune <- readRDS("models/monthly_500_site_ranger_tune.rds")
} else {
set.seed(3159)
doParallel::registerDoParallel()
ranger_tune <-
tune_grid(ranger_workflow,
resamples = monthly_500_folds,
grid = 12)
# Final fit
final_rf <- ranger_workflow %>%
finalize_workflow(select_best(ranger_tune))
monthly_500_fit <- last_fit(final_rf, monthly_500_split)
## last_fit is saved if no model has been trained and saved.
saveRDS(ranger_tune, "models/monthly_500_site_ranger_tune.rds")
saveRDS(monthly_500_fit, "models/monthly_500_fit_site.rds")
}
```
```{r predictions_plot_monthly_500_rf}
#| label: fig-monthly_500_rf
#| fig-cap: "GPP observed and predicted values from the Random Forest for all the sites at a monthly basis. The red line represents a 1:1 relation. Metrics units are gC m⁻² d⁻¹"
#| fig-width: 6
#| fig-height: 4
#| echo: false
#| message: false
#| warning: false
# Explore RF results
## Check the metrics
metrics <- collect_metrics(monthly_500_fit)
## Collect predictions
plot_predictions_rf(monthly_500_fit, metrics, 5, 5, 13, 14)
```
```{r vip_plot_monthly_500_rf}
#| label: fig-vip_monthly_500_rf
#| fig-cap: "Variable of importance derived from the Random forest model for the monthly values at 500 m spatial resolution model."
#| echo: false
#| message: false
#| warning: false
## Feature importance
imp_spec <- ranger_spec %>%
finalize_model(select_best(ranger_tune)) %>%
set_engine("ranger", importance = "permutation")
workflow() %>%
add_recipe(ranger_recipe) %>%
add_model(imp_spec) %>%
fit(monthly_500_train) %>%