-
Notifications
You must be signed in to change notification settings - Fork 0
/
004-paper-m-reduce-underfitting.Rmd
1182 lines (1081 loc) · 39.6 KB
/
004-paper-m-reduce-underfitting.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
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
```{r m-reduce-underfitting-1}
### reduce undrfitting by including additional variables provided by the algorithm from Hodgkins et al. 2018 into the models
## Holocellulose
# same as m1.3, but with additional predictors
m1.5 <-
brms::brm(hol ~ carb + arom15 + arom16 + acids + aliph28 + aliph29 + trough16 + trough28,
data = d %>% dplyr::filter(sample_type != "old magazines"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors,
chains = chains,
seed = seed,
iter = iter,
warmup = warmup)
# same as m1.5, but more regularizing priors
brms_beta_priors1 <-
c(
brms::prior_string("normal(0, 0.5)", class = "b"),
brms::prior_string("normal(0, 2.5)", class = "Intercept"),
brms::prior_string("gamma(0.01, 0.01)", class = "phi")
)
m1.6 <-
update(m1.5, prior = brms_beta_priors1,
seed = seed)
# compute loos
m1.5_loo <- loo(m1.5, reloo = TRUE)
m1.6_loo <- loo(m1.6, reloo = TRUE)
## Klason lignin
# Same as m2.3, but with additional predictors
m2.5 <-
brms::brm(kl ~ carb + arom15 + arom16 + acids + aliph28 + aliph29 + trough16 + trough28,
data = d %>% dplyr::filter(sample_type != "office paper"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors,
chains = chains,
seed = seed,
iter = iter,
warmup = warmup)
# same as m2.5, but more regularizing priors
m2.6 <- update(m2.5, prior = brms_beta_priors1,
seed = seed)
# compute loos
m2.5_loo <- loo(m2.5, reloo = TRUE)
m2.6_loo <- loo(m2.6, reloo = TRUE)
```
```{r m-reduce-underfitting-2}
### reduce underfitting by using the complete initial spectrum binned with varying bin widths ranging from 10 to 100 and using regularizing priors
# define bin widths
bin_widths <- c(10, 20, 30, 50, 100)
# create binned datasets
d_underfitting_flat <-
purrr::map(bin_widths, function(x) {
d %>%
ir::ir_bin(width = x) %>%
ir::ir_flatten()
})
d_underfitting <-
purrr::map(d_underfitting_flat, function(x) {
d %>%
dplyr::select(hol, kl) %>%
dplyr::bind_cols(
x %>%
dplyr::select(-1) %>%
t() %>%
as.data.frame() %>%
scale(center = TRUE, scale = TRUE) %>%
as.data.frame()
)
})
# define regularized horsheshoe prior
prior_hs_tau0 <-
purrr::map_dbl(d_underfitting, function(x) {
n <- nrow(x) - 4L # in each case, four observations are removed
p <- ncol(x) - 2L # two target variables have to be subtracted
p0 <- 8L # prior guess for the number of relevant variables
p0/(p-p0) * 1/sqrt(n) # tau0
})
```
```{r m-reduce-underfitting-3}
## Holocellulose
# fit models
brms_beta_priors2 <-
c(
brms::prior_string(paste0("horseshoe(df = 1, scale_global = ", prior_hs_tau0[[1]], ", df_global = 1, autoscale = TRUE)"),
class = "b"),
brms::prior_string("normal(0, 2.5)", class = "Intercept"),
brms::prior_string("gamma(0.01, 0.01)", class = "phi")
)
m1.7 <-
brms::brm(
hol ~ .,
data =
d_underfitting[[1]] %>%
dplyr::select(-kl) %>%
dplyr::filter(d$sample_type != "old magazines"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors2,
cores = chains,
chains = chains,
warmup = warmup,
iter = iter,
seed = seed ,
control = list(adapt_delta = 0.99, max_treedepth = 15) # with default adapt_delta (0.8): 6 divergent transitions, low bulk ESS; with adapt_delta = 0.99: There were 183 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10, low bulk ESS
)
brms_beta_priors2 <-
c(
brms::prior_string(paste0("horseshoe(df = 1, scale_global = ", prior_hs_tau0[[2]], ", df_global = 1, autoscale = TRUE)"),
class = "b"),
brms::prior_string("normal(0, 2.5)", class = "Intercept"),
brms::prior_string("gamma(0.01, 0.01)", class = "phi")
)
m1.8 <-
brms::brm(
hol ~ .,
data =
d_underfitting[[2]] %>%
dplyr::select(-kl) %>%
dplyr::filter(d$sample_type != "old magazines"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors2,
cores = chains,
chains = chains,
warmup = warmup,
iter = iter,
seed = seed,
control = list(adapt_delta = 0.99, max_treedepth = 15) # with default adapt_delta (0.8): 5 divergent transitions; with adapt_delta = 0.99: There were 3994 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10
)
brms_beta_priors2 <-
c(
brms::prior_string(paste0("horseshoe(df = 1, scale_global = ", prior_hs_tau0[[3]], ", df_global = 1, autoscale = TRUE)"),
class = "b"),
brms::prior_string("normal(0, 2.5)", class = "Intercept"),
brms::prior_string("gamma(0.01, 0.01)", class = "phi")
)
m1.9 <-
brms::brm(
hol ~ .,
data =
d_underfitting[[3]] %>%
dplyr::select(-kl) %>%
dplyr::filter(d$sample_type != "old magazines"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors2,
cores = chains,
chains = chains,
warmup = warmup,
iter = iter,
seed = seed,
control = list(adapt_delta = 0.99, max_treedepth = 15) # with default adapt_delta (0.8): 11 divergent transitions; adapt_delta = 0.99: There were 2000 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10
)
brms_beta_priors2 <-
c(
brms::prior_string(paste0("horseshoe(df = 1, scale_global = ", prior_hs_tau0[[4]], ", df_global = 1, autoscale = TRUE)"),
class = "b"),
brms::prior_string("normal(0, 2.5)", class = "Intercept"),
brms::prior_string("gamma(0.01, 0.01)", class = "phi")
)
m1.10 <-
brms::brm(
hol ~ .,
data =
d_underfitting[[4]] %>%
dplyr::select(-kl) %>%
dplyr::filter(d$sample_type != "old magazines"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors2,
cores = chains,
chains = chains,
warmup = warmup,
iter = iter,
seed = seed,
control = list(adapt_delta = 0.99, max_treedepth = 15) # with default adapt_delta (0.8): 8 divergent transitions; adapt_delta = 0.99: There were 1840 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10.
)
brms_beta_priors2 <-
c(
brms::prior_string(paste0("horseshoe(df = 1, scale_global = ", prior_hs_tau0[[5]], ", df_global = 1, autoscale = TRUE)"),
class = "b"),
brms::prior_string("normal(0, 2.5)", class = "Intercept"),
brms::prior_string("gamma(0.01, 0.01)", class = "phi")
)
m1.11 <-
brms::brm(
hol ~ .,
data =
d_underfitting[[5]] %>%
dplyr::select(-kl) %>%
dplyr::filter(d$sample_type != "old magazines"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors2,
cores = chains,
chains = chains,
warmup = warmup,
iter = iter,
seed = seed,
control = list(adapt_delta = 0.99, max_treedepth = 15) # with default adapt_delta (0.8): 17 divergent transitions; adapt_delta = 0.99: no sampling issues
)
```
```{r m-reduce-underfitting-4}
## Klason lignin
# fit models
brms_beta_priors2 <-
c(
brms::prior_string(paste0("horseshoe(df = 1, scale_global = ", prior_hs_tau0[[1]], ", df_global = 1, autoscale = TRUE)"),
class = "b"),
brms::prior_string("normal(0, 2.5)", class = "Intercept"),
brms::prior_string("gamma(0.01, 0.01)", class = "phi")
)
m2.7 <-
brms::brm(
kl ~ .,
data =
d_underfitting[[1]] %>%
dplyr::select(-hol) %>%
dplyr::filter(d$sample_type != "office paper"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors2,
cores = chains,
chains = chains,
warmup = warmup,
iter = iter,
seed = seed,
control = list(adapt_delta = 0.99, max_treedepth = 15) # with default adapt_delta (0.8): 49 divergent transitions; adapt_delta = 0.99: There were 7136 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. Low bulk ESS
)
brms_beta_priors2 <-
c(
brms::prior_string(paste0("horseshoe(df = 1, scale_global = ", prior_hs_tau0[[2]], ", df_global = 1, autoscale = TRUE)"),
class = "b"),
brms::prior_string("normal(0, 2.5)", class = "Intercept"),
brms::prior_string("gamma(0.01, 0.01)", class = "phi")
)
m2.8 <-
brms::brm(
kl ~ .,
data =
d_underfitting[[2]] %>%
dplyr::select(-hol) %>%
dplyr::filter(d$sample_type != "office paper"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors2,
cores = chains,
chains = chains,
warmup = warmup,
iter = iter,
seed = seed,
control = list(adapt_delta = 0.99, max_treedepth = 15) # with default adapt_delta (0.8): 136 divergent transitions and low tail ESS; adapt_delta = 0.99: 3 divergent transitions. There were 2000 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10.
)
brms_beta_priors2 <-
c(
brms::prior_string(paste0("horseshoe(df = 1, scale_global = ", prior_hs_tau0[[3]], ", df_global = 1, autoscale = TRUE)"),
class = "b"),
brms::prior_string("normal(0, 2.5)", class = "Intercept"),
brms::prior_string("gamma(0.01, 0.01)", class = "phi")
)
m2.9 <- brms::brm(kl ~ .,
data =
d_underfitting[[3]] %>%
dplyr::select(-hol) %>%
dplyr::filter(d$sample_type != "office paper"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors2,
cores = chains,
chains = chains,
warmup = warmup,
iter = iter,
seed = seed,
control = list(adapt_delta = 0.99, max_treedepth = 15) # with default adapt_delta (0.8): 452 divergent transitions and low tail ESS; adapt_delta = 0.99: 5 divergent transitions. There were 1631 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10.
)
brms_beta_priors2 <-
c(
brms::prior_string(paste0("horseshoe(df = 1, scale_global = ", prior_hs_tau0[[4]], ", df_global = 1, autoscale = TRUE)"),
class = "b"),
brms::prior_string("normal(0, 2.5)", class = "Intercept"),
brms::prior_string("gamma(0.01, 0.01)", class = "phi")
)
m2.10 <- brms::brm(kl ~ .,
data =
d_underfitting[[4]] %>%
dplyr::select(-hol) %>%
dplyr::filter(d$sample_type != "office paper"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors2,
cores = chains,
chains = chains,
warmup = warmup,
iter = iter,
seed = seed,
control = list(adapt_delta = 0.99, max_treedepth = 15) # with default adapt_delta (0.8): 267 divergent transitions, low tail ESS; adapt_delta = 0.99: 7 divergent transitions
)
brms_beta_priors2 <-
c(
brms::prior_string(paste0("horseshoe(df = 1, scale_global = ", prior_hs_tau0[[5]], ", df_global = 1, autoscale = TRUE)"),
class = "b"),
brms::prior_string("normal(0, 2.5)", class = "Intercept"),
brms::prior_string("gamma(0.01, 0.01)", class = "phi")
)
m2.11 <- brms::brm(kl ~ .,
data =
d_underfitting[[5]] %>%
dplyr::select(-hol) %>%
dplyr::filter(d$sample_type != "office paper"),
family = Beta(link = "logit", link_phi = "log"),
prior = brms_beta_priors2,
cores = chains,
chains = chains,
warmup = warmup,
iter = iter,
seed = seed,
control = list(adapt_delta = 0.99, max_treedepth = 15) # with default adapt_delta (0.8): 81 divergent transitions; adapt_delta = 0.99: no sampling issues
)
```
```{r m-reduce-underfitting-5-1}
# compute loos
m1.7_loo <- loo(m1.7, reloo = TRUE, cores = chains, recompile = FALSE)
```
```{r m-reduce-underfitting-5-2}
m1.8_loo <- loo(m1.8, reloo = TRUE, cores = chains, recompile = FALSE)
```
```{r m-reduce-underfitting-5-3}
m1.9_loo <- loo(m1.9, reloo = TRUE, cores = chains, recompile = FALSE)
```
```{r m-reduce-underfitting-5-4}
m1.10_loo <- loo(m1.10, reloo = TRUE, cores = chains, recompile = FALSE)
```
```{r m-reduce-underfitting-5-5}
m1.11_loo <- loo(m1.11, reloo = TRUE, cores = chains, recompile = FALSE)
```
```{r m-reduce-underfitting-6-1}
# compute loos
m2.7_loo <- loo(m2.7, reloo = TRUE, cores = chains, recompile = FALSE)
```
```{r m-reduce-underfitting-6-2}
m2.8_loo <- loo(m2.8, reloo = TRUE, cores = chains, recompile = FALSE)
```
```{r m-reduce-underfitting-6-3}
m2.9_loo <- loo(m2.9, reloo = TRUE, cores = chains, recompile = FALSE)
```
```{r m-reduce-underfitting-6-4}
m2.10_loo <- loo(m2.10, reloo = TRUE, cores = chains, recompile = FALSE)
```
```{r m-reduce-underfitting-6-5}
m2.11_loo <- loo(m2.11, reloo = TRUE, cores = chains, recompile = FALSE)
```
```{r m-reduce-underfitting-7}
### create the tables summarizing the predictive performance comparison
# table with loo results
loo_comparison <-
tibble::tibble(
variably_y = c("Holocellulose", "Klason lignin"),
loo_comparison =
list(
loo::loo_compare(list(m1.2_loo, m1.3_loo, m1.5_loo, m1.6_loo, m1.7_loo, m1.8_loo, m1.9_loo, m1.10_loo, m1.11_loo)),
loo::loo_compare(list(m2.2_loo, m2.3_loo, m2.5_loo, m2.6_loo, m2.7_loo, m2.8_loo, m2.9_loo, m2.10_loo, m2.11_loo))
)
)
## create table for printing
loo_comparison_code <-
tibble::tibble(
model_code = paste0("m1.", c(2, 3, 5:11)),
bin_width = c(rep("-", 4), 10, 20, 30, 50, 100),
family = c("Gaussian", rep("Beta", 8)),
original_model = c(rep(TRUE, 2), rep(FALSE, 7)),
prior_scale = c(rep(2.5, 3), 0.5, rep("-", 5)),
variables = ifelse(bin_width == "-", "peaks", "bins")
)
# holocellulose
loo_comparison_hol <-
loo_comparison %>%
dplyr::filter(variably_y == "Holocellulose") %>%
dplyr::pull(loo_comparison) %>%
as.data.frame() %>%
dplyr::select(1:3) %>%
dplyr::mutate(model_code = rownames(.)) %>%
dplyr::mutate(dplyr::across(1:3, round, 1)) %>%
dplyr::left_join(loo_comparison_code, by = "model_code")
# Klason lignin
loo_comparison_kl <-
loo_comparison %>%
dplyr::filter(variably_y == "Klason lignin") %>%
dplyr::pull(loo_comparison) %>%
as.data.frame() %>%
dplyr::select(1:3) %>%
dplyr::mutate(model_code = rownames(.)) %>%
dplyr::mutate(dplyr::across(1:3, round, 1)) %>%
dplyr::left_join(
loo_comparison_code %>%
dplyr::mutate(model_code =
model_code %>% stringr::str_replace(pattern = "^m1", replacement = "m2")),
by = "model_code")
## combine
loo_comparison_res_colindex <-
c("model_code", "original_model", "family", "variables", "prior_scale", "bin_width", "elpd_loo", "elpd_diff", "se_diff")
loo_comparison_res <-
dplyr::bind_rows(
loo_comparison_hol %>%
dplyr::select(dplyr::all_of(loo_comparison_res_colindex)),
loo_comparison_kl %>%
dplyr::select(dplyr::all_of(loo_comparison_res_colindex))
) %>%
dplyr::select(-original_model) %>%
tibble::remove_rownames() %>%
kableExtra::kable(booktabs = TRUE,
col.names = c("Model", "Distribution", "Predictors", "Prior scale", "Bin width", "ELPD", "$\\Delta$ELPD", "$\\Delta$SE"),
escape = FALSE,
caption = 'Overview on the relative predictive performance of the models for holocellulose and Klason lignin content as measured using PSIS-LOO ELPD. For each variable, the model with the best average predictive performance (largest ELPD) is at the top and the other models follow in descending order. Models ending in ".2" and ".3" are the models with the original model structure as developed by \\cite{Hodgkins.2018}. "Distribution" is the distribution assumed for the target variable. "Predictors" indicates if models were fitted with peaks extracted using the procedure of \\cite{Hodgkins.2018} ("peaks") or using binned spectra ("bins"). "Prior" scale indicates the standard deviation for Gaussian coefficients (numeric values), or that flat priors were assumed ("flat") or regularized horseshoe priors were used ("-"). "Bin width" is the width of bins (in wavenumber units). "ELPD" is the PSIS-LOO expected log-predictive density, $\\Delta$ELPD the difference in ELPD relative to the average ELPD of the on average best model, and $\\Delta$SE the standard error in the average $\\Delta$ELPD. \\label{tab:res-tab-loo-comparison-res}') %>%
kableExtra::kable_styling(latex_options = c("hold_position"), position = "center") %>%
kableExtra::pack_rows("Holocellulose", 1, 9) %>%
kableExtra::pack_rows("Klason lignin", 10, 8)
```
```{r m-reduce-underfitting-8}
### compute predictions for peat profiles comparing the "best" model and th original Beta regression model
## create data sets for predictions
# bin width: 20
d_peat_bin20_scale <-
d_underfitting_flat[[2]] %>%
dplyr::select(-1) %>%
t() %>%
as.data.frame() %>%
scale(center = TRUE, scale = TRUE)
d_peat_bin20_scale <-
tibble::tibble(
center = attr(d_peat_bin20_scale, "scaled:center"),
scale = attr(d_peat_bin20_scale, "scaled:scale")
)
d_peat_bin20_flat <-
d_peat %>%
ir::ir_bin(width = 20) %>%
ir::ir_flatten()
d_peat_bin20 <-
d_peat_bin20_flat %>%
dplyr::select(-1) %>%
t() %>%
as.data.frame() %>%
scale(
center = d_peat_bin20_scale$center,
scale = d_peat_bin20_scale$scale
) %>%
as.data.frame()
# bin width: 10
d_peat_bin10_scale <-
d_underfitting_flat[[1]] %>%
dplyr::select(-1) %>%
t() %>%
as.data.frame() %>%
scale(center = TRUE, scale = TRUE)
d_peat_bin10_scale <-
tibble::tibble(
center = attr(d_peat_bin10_scale, "scaled:center"),
scale = attr(d_peat_bin10_scale, "scaled:scale")
)
d_peat_bin10_flat <-
d_peat %>%
ir::ir_bin(width = 10) %>%
ir::ir_flatten()
d_peat_bin10 <-
d_peat_bin10_flat %>%
dplyr::select(-1) %>%
t() %>%
as.data.frame() %>%
scale(
center = d_peat_bin10_scale$center,
scale = d_peat_bin10_scale$scale
) %>%
as.data.frame()
# predictions
d_peat_pred_best <-
dplyr::bind_rows(
m1.8 %>%
rstanarm::posterior_predict(newdata = d_peat_bin20) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Best binned spectra",
variable_y = "Holocellulose"
),
m1.6 %>%
rstanarm::posterior_predict(newdata = d_peat_res) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Best all peaks",
variable_y = "Holocellulose"
),
m1.2 %>%
rstanarm::posterior_predict(newdata = d_peat_res) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Original",
variable_y = "Holocellulose"
),
m2.2 %>%
rstanarm::posterior_predict(newdata = d_peat_res) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Original",
variable_y = "Klason lignin"
),
m2.8 %>%
rstanarm::posterior_predict(newdata = d_peat_bin20) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Best binned spectra",
variable_y = "Klason lignin"
),
m2.6 %>%
rstanarm::posterior_predict(newdata = d_peat_res) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Best all peaks",
variable_y = "Klason lignin"
)
) %>%
dplyr::bind_cols(
purrr::map_df(1:6, function(x) {
tibble::tibble(
sample_id = d_peat$sample_id,
core_label = d_peat$core_label,
sampling_longitude = d_peat$sampling_longitude,
sampling_latitude = d_peat$sampling_latitude,
sampling_altitude = d_peat$sampling_altitude,
sample_depth_lower = d_peat$sample_depth_lower,
sample_depth_upper = d_peat$sample_depth_upper
)
})
)
# summarize across depth values to reproduce key plots in Hodgkins et al. 2018
d_peat_pred_best_summary <-
d_peat_pred_best %>%
dplyr::filter(sample_depth_lower <= 50) %>%
dplyr::group_by(variable_y, model, core_label) %>%
dplyr::summarise(fit = mean(fit),
lwr = mean(lwr),
upr = mean(upr),
sampling_latitude = mean(sampling_latitude))
# data frame to compare fitted with measured values across all models
d_fitted_comparison <-
dplyr::bind_rows(
tibble::tibble(
y =
d %>%
dplyr::filter(sample_type != "old magazines") %>%
dplyr::pull(hol) %>%
rep(4),
yhat =
list(m1.2, m1.5, m1.8) %>%
purrr::map(function(x) {
apply(posterior_predict(x), 2, median)
}) %>%
unlist() %>%
c(m1.1 %>% predict(type = "response")),
model = rep(c("Original", "Best all peaks", "Best binned spectra", "Original non-Bayesian"),
each = d %>% dplyr::filter(sample_type != "old magazines") %>% nrow()),
y_variable = "Holocellulose"
),
tibble::tibble(
y =
d %>%
dplyr::filter(sample_type != "office paper") %>%
dplyr::pull(kl) %>%
rep(4),
yhat =
list(m2.2, m2.6, m2.8) %>%
purrr::map(function(x) {
apply(posterior_predict(x), 2, median)
}) %>%
unlist() %>%
c(m2.1 %>% predict(type = "response")),
model = rep(c("Original", "Best all peaks", "Best binned spectra", "Original non-Bayesian"),
each = d %>% dplyr::filter(sample_type != "office paper") %>% nrow()),
y_variable = "Klason lignin"
)
)
```
```{r m-reduce-underfitting-9}
## plot depth profiles
p1 <-
d_peat_pred_best %>%
dplyr::filter(variable_y == "Holocellulose") %>%
ggplot(aes(y = fit, ymin = lwr, ymax = upr,
x = sample_depth_lower,
colour = model,
fill = model)) +
geom_ribbon(colour = NA,
alpha = 0.2) +
geom_path() +
geom_hline(yintercept = 0, linetype = 2, colour = "grey") +
coord_flip() +
scale_x_continuous(labels = function(x) x * 100) +
scale_y_continuous(labels = function(x) x * 100) +
labs(title = "Holocellulose",
y = expression(Holocellulose~content~"[mass-%]"),
x = "Lower layer boundary depth [cm]") +
facet_wrap(~ core_label,
scales = "free_y",
ncol = 2) +
scale_x_reverse() +
scale_color_manual(values = palette_cb[-1]) +
scale_fill_manual(values = palette_cb[-1])
p2 <-
d_peat_pred_best %>%
dplyr::filter(variable_y == "Klason lignin") %>%
ggplot(aes(y = fit, ymin = lwr, ymax = upr,
x = sample_depth_lower,
colour = model,
fill = model)) +
geom_ribbon(colour = NA,
alpha = 0.2) +
geom_path() +
geom_hline(yintercept = 0, linetype = 2, colour = "grey") +
coord_flip() +
scale_x_continuous(labels = function(x) x * 100) +
scale_y_continuous(labels = function(x) x * 100) +
labs(title = "Klason lignin",
y = expression(Klason~lignin~content~"[mass-%]"),
x = "Lower layer boundary depth [cm]") +
facet_wrap(~ core_label,
scales = "free_y",
ncol = 2) +
scale_x_reverse() +
scale_color_manual(values = palette_cb[-1]) +
scale_fill_manual(values = palette_cb[-1])
# combine plots
p_gaussian_beta_depth_profile_best <-
p1 + p2 +
plot_annotation(tag_levels = "a", tag_prefix = "(", tag_suffix = ")") +
plot_layout(guides = "collect") &
theme(legend.position = "bottom")
### Reproduce plots in Hodgkins et al. 2018
## contents vs latitude across sites
p1 <-
d_peat_pred_best_summary %>%
dplyr::filter(variable_y == "Holocellulose" & model != "Best all peaks") %>%
ggplot(aes(y = fit, x = sampling_latitude, colour = model)) +
geom_errorbar(aes(ymin = lwr, ymax = upr)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, formula = y ~ x) +
labs(title = "Holocellulose",
y = expression(Holocellulose~content~"["*g/g[sample]*"]"),
x = expression("Latitude ["*degree*N*"]")) +
guides(colour = guide_legend(title = "Model"))
p2 <-
d_peat_pred_best_summary %>%
dplyr::filter(variable_y == "Klason lignin" & model != "Best all peaks") %>%
ggplot(aes(y = fit, x = sampling_latitude, colour = model)) +
geom_errorbar(aes(ymin = lwr, ymax = upr)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, formula = y ~ x) +
labs(title = "Klason lignin",
y = expression(Klason~lignin~content~"["*g/g[sample]*"]"),
x = expression("Latitude ["*degree*N*"]")) +
guides(colour = guide_legend(title = "Model"))
# combine
p_sites_latitude <-
p1 + p2 +
plot_annotation(tag_levels = "a", tag_prefix = "(", tag_suffix = ")") +
plot_layout(guides = "collect") &
theme(legend.position = "bottom")
## depth profiles for two latitude classes
p1 <-
d_peat_pred_best %>%
dplyr::arrange(sample_depth_lower) %>%
dplyr::filter(variable_y == "Holocellulose" & model != "Best all peaks") %>%
ggplot(aes(y = fit, ymin = lwr, ymax = upr,
x = sample_depth_lower,
colour = sampling_latitude >45,
fill = sampling_latitude >45)) +
geom_hline(yintercept = c(0.09, 0.14, 0.21, 0.43), colour = "grey", linetype = 2) +
geom_smooth(method = "loess", formula = y ~ x) +
coord_flip() +
labs(title = "Holocellulose",
y = expression(Holocellulose~content~"["*g/g[sample]*"]"),
x = "Lower layer boundary depth [cm]") +
facet_wrap(~ model,
scales = "free_y",
ncol = 1) +
scale_x_reverse() +
guides(colour = guide_legend(title = expression(Latitude>45~degree*N)),
fill = guide_legend(title = expression(Latitude>45~degree*N)))
p2 <-
d_peat_pred_best %>%
dplyr::arrange(sample_depth_lower) %>%
dplyr::filter(variable_y == "Klason lignin" & model != "Best all peaks") %>%
ggplot(aes(y = fit, ymin = lwr, ymax = upr,
x = sample_depth_lower,
colour = sampling_latitude >45,
fill = sampling_latitude >45)) +
geom_hline(yintercept = c(0.22, 0.34, 0.38, 0.53), colour = "grey", linetype = 2) +
geom_smooth(method = "loess", formula = y ~ x) +
coord_flip() +
labs(title = "Klason lignin",
y = expression(Klason~lignin~content~"["*g/g[sample]*"]"),
x = "Lower layer boundary depth [cm]") +
facet_wrap(~ model,
scales = "free_y",
ncol = 1) +
scale_x_reverse() +
guides(colour = guide_legend(title = expression(Latitude>45~degree*N)),
fill = guide_legend(title = expression(Latitude>45~degree*N)))
# combine
p_depth_profiles_latitude <-
p1 + p2 +
plot_annotation(tag_levels = "a", tag_prefix = "(", tag_suffix = ")") +
plot_layout(guides = "collect") &
theme(legend.position = "bottom")
### plot fitted vs measured values
p1 <-
d_fitted_comparison %>%
dplyr::filter(y_variable == "Holocellulose" & model != "Original non-Bayesian") %>%
dplyr::mutate(model = factor(model, levels = c("Original", "Best binned spectra", "Best all peaks"))) %>%
ggplot(aes(y = y, x = yhat)) +
geom_abline(intercept = 0, slope = 1, colour = "grey50") +
geom_point() +
geom_smooth(method = "loess", formula = y ~ x, colour = "grey50") +
labs(title = "Holocellulose",
x = "Fitted",
y = "Measured") +
coord_fixed(ratio = 1) +
scale_x_continuous(labels = function(x) x * 100) +
scale_y_continuous(labels = function(x) x * 100) +
facet_wrap( ~ model)
p2 <-
d_fitted_comparison %>%
dplyr::filter(y_variable == "Klason lignin" & model != "Original non-Bayesian") %>%
dplyr::mutate(model = factor(model, levels = c("Original", "Best binned spectra", "Best all peaks"))) %>%
ggplot(aes(y = y, x = yhat)) +
geom_abline(intercept = 0, slope = 1, colour = "grey50") +
geom_point() +
geom_smooth(method = "loess", formula = y ~ x, colour = "grey50") +
labs(title = "Klason lignin",
x = "Fitted",
y = "Measured") +
coord_fixed(ratio = 1) +
scale_x_continuous(labels = function(x) x * 100) +
scale_y_continuous(labels = function(x) x * 100) +
facet_wrap( ~ model)
# combine plots
p_y_yhat_best <-
p1 / p2 +
plot_annotation(tag_levels = "a", tag_prefix = "(", tag_suffix = ")") +
plot_layout(guides = "collect") &
theme(legend.position = "bottom")
```
```{r m-reduce-underfitting-10}
### compute predictions for vegetation samples comparing the "best" model and th original Gaussian regression model
# bin width: 20
d_veg_bin20_flat <-
d_veg %>%
ir::ir_bin(width = 20) %>%
ir::ir_flatten()
d_veg_bin20 <-
d_veg_bin20_flat %>%
dplyr::select(-1) %>%
t() %>%
as.data.frame() %>%
scale(center = d_peat_bin20_scale$center,
scale = d_peat_bin20_scale$scale) %>%
as.data.frame()
# bin width: 10
d_veg_bin10_flat <-
d_veg %>%
ir::ir_bin(width = 10) %>%
ir::ir_flatten()
d_veg_bin10 <-
d_veg_bin10_flat %>%
dplyr::select(-1) %>%
t() %>%
as.data.frame() %>%
scale(center = d_peat_bin10_scale$center,
scale = d_peat_bin10_scale$scale) %>%
as.data.frame()
# predictions
d_veg_pred_best <-
dplyr::bind_rows(
m1.8 %>%
rstanarm::posterior_predict(newdata = d_veg_bin20) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Best binned spectra",
variable_y = "Holocellulose"
),
m1.6 %>%
rstanarm::posterior_predict(newdata = d_veg_res) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Best all peaks",
variable_y = "Holocellulose"
),
m1.2 %>%
rstanarm::posterior_predict(newdata = d_veg_res) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Original",
variable_y = "Holocellulose"
),
m2.2 %>%
rstanarm::posterior_predict(newdata = d_veg_res) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Original",
variable_y = "Klason lignin"
),
m2.8 %>%
rstanarm::posterior_predict(newdata = d_veg_bin20) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Best binned spectra",
variable_y = "Klason lignin"
),
m2.6 %>%
rstanarm::posterior_predict(newdata = d_veg_res) %>%
as.data.frame() %>%
purrr::map_df(function(x) {
tibble::tibble(
fit = median(x),
lwr = quantile(x, prob = 0.05),
upr = quantile(x, prob = 0.95)
)
}) %>%
dplyr::mutate(
model = "Best all peaks",
variable_y = "Klason lignin"
)
) %>%
dplyr::bind_cols(
purrr::map_df(1:6, function(x) {
tibble::tibble(
sample_id = d_veg$sample_id,
site_name = d_veg$site_name,
species_name = d_veg$species_name,
plant_part = d_veg$plant_part
)
})
)
# plot comparing vegetation predictions
p_veg_species <-
d_veg_pred_best %>%
dplyr::filter(model != "Best all peaks") %>%
dplyr::mutate(