-
Notifications
You must be signed in to change notification settings - Fork 3
/
09-spatialmodelling.Rmd
1112 lines (965 loc) · 45.7 KB
/
09-spatialmodelling.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
# Spatial modeling
------------------------------------------------------------------------
In case you have previosly saved both, the vis-NIR augmented data set and the table of the variance of the residuals into your working directory, and you do not have these data in your `R` enviroment you can execute the code below:
```{r, eval = FALSE}
## vniraugmented.txt is spuppposed to be saved in your working directory
vniraugmented <- read.table(file = "vniraugmented.txt",
header = TRUE,
sep = "\t")
```
If you have saved the table of the variance of the residuals into your working directory you can execute the code below:
```{r, eval = FALSE}
## vnir_residual_variances.txt is spuppposed to be saved in your working directory
residualvariances <- read.table(file = "vnir_residual_variances.txt",
header = TRUE,
sep = "\t")
```
Alternatively, you can clean your `R` enviroment and leave only the data that will be used for the spatial analyses:
```{r, eval = FALSE}
## necessary objects
reqobjects2 <- c("vniraugmented", "residualvariances")
## objects to be removed
o2rm2 <- ls()[!ls() %in% reqobjects2]
## remove the objects
rm(list = o2rm2)
```
Split the data and organize it by layers and spatial fit and validation sets
```{r, eval = FALSE}
vniraugmented <- as_tibble(vniraugmented)
## split the data sets by layer and by spatial fit and spatial validation
fitlayera <- vniraugmented %>% filter(layer == "A", set != "validation")
fitlayerb <- vniraugmented %>% filter(layer == "B", set != "validation")
vallayera <- vniraugmented %>% filter(layer == "A", set == "validation")
vallayerb <- vniraugmented %>% filter(layer == "B", set == "validation")
plot(vallayera$POINT_X, vallayera$POINT_Y,
xlab = "X",
ylab = "Y")
points(fitlayera$POINT_X, fitlayera$POINT_Y,
col = "red",
cex = 1.5)
```
## Robust fitting of the spatial models
Specify some basic aspects/parameters required for fitting the spatial models...
```{r, eval = FALSE}
## Define a lag distance for the estimation of the variagram
lagdist <- seq(0, 1500, by = 100)
## Choose the variogram model
varmodel <- "RMexp"
## Define what parameters need to be adjusted to fit the geo model
fitparam <- default.fit.param(scale = FALSE, alpha = TRUE, variance = FALSE)
## A tuning constant for the robust REML algorithm
## for the spatial models (see tuniing.psi parameter
## of the georob function)
tpsi <- 2000
## Control some aspects of the spatial predictions
gcntrl <- control.predict.georob(extended.output = TRUE,
full.covmat = TRUE)
```
Define the tables where the fitted variogram parameters will be stored
```{r, eval = FALSE}
## The variables for which a spatial model will be fitted
gprops <- c("Ca",
"alr_Clay",
"alr_Silt",
"Ca_spec",
"alr_Clay_spec",
"alr_Silt_spec")
## names of the variogram parameters
varparamames <- c("variance", "snugget", "nugget", "scale")
## Create the table
variogtablea <- data.frame(properties = gprops,
data = rep(c("Laboratory", "vis-NIR augmented"),
each = length(gprops) / 2),
variance = NA,
snugget = NA,
nugget = NA,
scale = NA)
variogtableb <- variogtablea
```
### Laboratory-based data
Here we fit the spatial models of the soil properties whose values comes from conventional laboratory analyes only...
#### Layer A
Exchangeable Ca^2+^...
```{r, eval = FALSE}
## Check the sample variogram
vario_Ca_lab_a <- sample.variogram(object = fitlayera$Ca,
locations = fitlayera[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_Ca_lab_a$lag.dist,
y = vario_Ca_lab_a$gamma,
ylim = c(0, max(vario_Ca_lab_a$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "Ca, laboratory - Layer A")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_Ca_lab_a <- c(variance = 135,
nugget = 10,
scale = 920)
## Fit
Ca_lab_a <- georob(Ca ~ 1,
data = fitlayera,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_Ca_lab_a,
fit.param = fitparam,
tuning.psi = tpsi)
summary(Ca_lab_a)
## Store the variogram parameters
variogtablea[variogtablea$properties == "Ca", varparamames] <- Ca_lab_a$variogram.object[[1]]$param[varparamames]
```
$alr(clay)$
```{r, eval = FALSE}
## Check the sample variogram
vario_alr_Clay_lab_a <- sample.variogram(object = fitlayera$alr_Clay,
locations = fitlayera[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_alr_Clay_lab_a$lag.dist,
y = vario_alr_Clay_lab_a$gamma,
ylim = c(0, max(vario_alr_Clay_lab_a$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "alr(clay) laboratory - Layer A")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_alr_Clay_lab_a <- c(variance = 0.8,
nugget = 0.3,
scale = 1220)
## Fit
alr_Clay_lab_a <- georob(alr_Clay ~ 1,
data = fitlayera,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_alr_Clay_lab_a,
fit.param = fitparam,
tuning.psi = tpsi)
summary(alr_Clay_lab_a)
## Store the variogram parameters
variogtablea[variogtablea$properties == "alr_Clay", varparamames] <- alr_Clay_lab_a$variogram.object[[1]]$param[varparamames]
```
$alr(silt)$
```{r, eval = FALSE}
## Check the sample variogram
vario_alr_Silt_lab_a <- sample.variogram(object = fitlayera$alr_Silt,
locations = fitlayera[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_alr_Silt_lab_a$lag.dist,
y = vario_alr_Silt_lab_a$gamma,
ylim = c(0, max(vario_alr_Silt_lab_a$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "alr(silt) laboratory - Layer A")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_alr_Silt_lab_a <- c(variance = 1.31,
nugget = 0.3,
scale = 812)
## Fit
alr_Silt_lab_a <- georob(alr_Silt ~ 1,
data = fitlayera,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_alr_Silt_lab_a,
fit.param = fitparam,
tuning.psi = tpsi)
summary(alr_Silt_lab_a)
## Store the variogram parameters
variogtablea[variogtablea$properties == "alr_Silt", varparamames] <- alr_Silt_lab_a$variogram.object[[1]]$param[varparamames]
```
#### Layer B
Exchangeable Ca^2+^...
```{r, eval = FALSE}
## Check the sample variogram
vario_Ca_lab_b <- sample.variogram(object = fitlayerb$Ca,
locations = fitlayerb[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_Ca_lab_b$lag.dist,
y = vario_Ca_lab_b$gamma,
ylim = c(0, max(vario_Ca_lab_b$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "Ca, laboratory - Layer B")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_Ca_lab_b <- c(variance = 127,
nugget = 0.1,
scale = 850)
## Fit
Ca_lab_b <- georob(Ca ~ 1,
data = fitlayerb,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_Ca_lab_b,
fit.param = fitparam,
tuning.psi = tpsi)
summary(Ca_lab_b)
## Store the variogram parameters
variogtableb[variogtableb$properties == "Ca", varparamames] <- Ca_lab_b$variogram.object[[1]]$param[varparamames]
```
$alr(clay)$
```{r, eval = FALSE}
## Check the sample variogram
vario_alr_Clay_lab_b <- sample.variogram(object = fitlayerb$alr_Clay,
locations = fitlayerb[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_alr_Clay_lab_b$lag.dist,
y = vario_alr_Clay_lab_b$gamma,
ylim = c(0, max(vario_alr_Clay_lab_b$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "alr(clay) laboratory - Layer B")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_alr_Clay_lab_b <- c(variance = 1.17,
nugget = 0.1,
scale = 973,
alpha = 0.69)
## Fit
alr_Clay_lab_b <- georob(alr_Clay ~ 1,
data = fitlayerb,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_alr_Clay_lab_b,
fit.param = fitparam,
tuning.psi = tpsi)
summary(alr_Clay_lab_b)
## Store the variogram parameters
variogtableb[variogtableb$properties == "alr_Clay", varparamames] <- alr_Clay_lab_b$variogram.object[[1]]$param[varparamames]
```
$alr(silt)$
```{r, eval = FALSE}
## Check the sample variogram
vario_alr_Silt_lab_b <- sample.variogram(object = fitlayerb$alr_Silt,
locations = fitlayerb[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_alr_Silt_lab_b$lag.dist,
y = vario_alr_Silt_lab_b$gamma,
ylim = c(0, max(vario_alr_Silt_lab_b$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "alr(silt) laboratory - Layer B")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_alr_Silt_lab_b <- c(variance = 1.1,
nugget = 0.2,
scale = 423)
## Fit
alr_Silt_lab_b <- georob(alr_Silt ~ 1,
data = fitlayerb,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_alr_Silt_lab_b,
fit.param = fitparam,
tuning.psi = tpsi)
summary(alr_Silt_lab_b)
## Store the variogram parameters
variogtableb[variogtableb$properties == "alr_Silt", varparamames] <- alr_Silt_lab_b$variogram.object[[1]]$param[varparamames]
```
### Augmented vis-NIR data
Here we fit the spatial models of the soil properties whose values come from the vis-NIR augmented data...
#### Layer A
Exchangeable Ca^2+^ (vis-NIR augmented) ...
```{r, eval = FALSE}
# Check the sample variogram
vario_Ca_spec_a <- sample.variogram(object = fitlayera$Ca_spec,
locations = fitlayera[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_Ca_spec_a$lag.dist,
y = vario_Ca_spec_a$gamma,
ylim = c(0, max(vario_Ca_spec_a$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "Ca, vis-NIR augmented - Layer A")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_Ca_spec_a <- c(variance = 112,
nugget = 1,
scale = 1023)
## Fit
Ca_spec_a <- georob(Ca ~ 1,
data = fitlayera,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_Ca_spec_a,
fit.param = fitparam,
tuning.psi = tpsi)
summary(Ca_spec_a)
## Store the variogram parameters
variogtablea[variogtablea$properties == "Ca_spec", varparamames] <- Ca_spec_a$variogram.object[[1]]$param[varparamames]
```
$alr(clay)$ (vis-NIR augmented)
```{r, eval = FALSE}
## Check the sample variogram
vario_alr_Clay_spec_a <- sample.variogram(object = fitlayera$alr_Clay_spec,
locations = fitlayera[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_alr_Clay_spec_a$lag.dist,
y = vario_alr_Clay_spec_a$gamma,
ylim = c(0, max(vario_alr_Clay_spec_a$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "alr(clay) vis-NIR augmented - Layer A")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_alr_Clay_spec_a <- c(variance = 1.134,
nugget = 0.1,
scale = 955)
## Fit
alr_Clay_spec_a <- georob(alr_Clay_spec ~ 1,
data = fitlayera,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_alr_Clay_spec_a,
fit.param = fitparam,
tuning.psi = tpsi)
summary(alr_Clay_spec_a)
## Store the variogram parameters
variogtablea[variogtablea$properties == "alr_Clay_spec", varparamames] <- alr_Clay_spec_a$variogram.object[[1]]$param[varparamames]
```
$alr(silt)$ (vis-NIR augmented)
```{r, eval = FALSE}
## Check the sample variogram
vario_alr_Silt_spec_a <- sample.variogram(object = fitlayera$alr_Silt_spec,
locations = fitlayera[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_alr_Silt_spec_a$lag.dist,
y = vario_alr_Silt_spec_a$gamma,
ylim = c(0, max(vario_alr_Silt_spec_a$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "alr(silt) vis-NIR augmented - Layer A")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_alr_Silt_spec_a <- c(variance = 0.845,
nugget = 0.1,
scale = 1282)
## Fit
alr_Silt_spec_a <- georob(alr_Silt_spec ~ 1,
data = fitlayera,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_alr_Silt_spec_a,
fit.param = fitparam,
tuning.psi = tpsi)
summary(alr_Silt_spec_a)
## Store the variogram parameters
variogtablea[variogtablea$properties == "alr_Silt_spec", varparamames] <- alr_Silt_spec_a$variogram.object[[1]]$param[varparamames]
```
#### Layer B
Exchangeable Ca^2+^ (vis-NIR augmented) ...
```{r, eval = FALSE}
## Check the sample variogram
vario_Ca_spec_b <- sample.variogram(object = fitlayerb$Ca_spec,
locations = fitlayerb[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_Ca_spec_b$lag.dist,
y = vario_Ca_spec_b$gamma,
ylim = c(0, max(vario_Ca_spec_b$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "Ca, vis-NIR augmented - Layer B")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_Ca_spec_b <- c(variance = 142,
nugget = 0.1,
scale = 1025)
## Fit
Ca_spec_b <- georob(Ca ~ 1,
data = fitlayerb,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_Ca_spec_b,
fit.param = fitparam,
tuning.psi = tpsi)
summary(Ca_spec_b)
## Store the variogram parameters
variogtableb[variogtableb$properties == "Ca_spec", varparamames] <- Ca_spec_b$variogram.object[[1]]$param[varparamames]
```
$alr(clay)$ (vis-NIR augmented)
```{r, eval = FALSE}
## Check the sample variogram
vario_alr_Clay_spec_b <- sample.variogram(object = fitlayerb$alr_Clay_spec,
locations = fitlayerb[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_alr_Clay_spec_b$lag.dist,
y = vario_alr_Clay_spec_b$gamma,
ylim = c(0, max(vario_alr_Clay_spec_b$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "alr(clay) vis-NIR augmented - Layer B")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_alr_Clay_spec_b <- c(variance = 1.21,
nugget = 0.1,
scale = 1000,
alpha = 0.68)
## Fit
alr_Clay_spec_b <- georob(alr_Clay_spec ~ 1,
data = fitlayerb,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_alr_Clay_spec_b,
fit.param = fitparam,
tuning.psi = tpsi)
summary(alr_Clay_spec_b)
## Store the variogram parameters
variogtableb[variogtableb$properties == "alr_Clay_spec", varparamames] <- alr_Clay_spec_b$variogram.object[[1]]$param[varparamames]
```
$alr(silt)$ (vis-NIR augmented)
```{r, eval = FALSE}
## Check the sample variogram
vario_alr_Silt_spec_b <- sample.variogram(object = fitlayerb$alr_Silt_spec,
locations = fitlayerb[,c("POINT_X", "POINT_Y")],
lag.dist.def = lagdist,
estimator = "matheron")
## Plot the sample variogram
plot(x = vario_alr_Silt_spec_b$lag.dist,
y = vario_alr_Silt_spec_b$gamma,
ylim = c(0, max(vario_alr_Silt_spec_b$gamma)),
xlab = "lag distance",
ylab = "gamma",
pch = 16,
col = "red",
main = "alr(silt) vis-NIR augmented - Layer B")
grid()
## Check the plot above to select some starting values of the
## variogram parameters
startp_alr_Silt_spec_b <- c(variance = 1.43,
nugget = 0.1,
scale = 850,
alpha = 0.63)
## Fit
alr_Silt_spec_b <- georob(alr_Silt_spec ~ 1,
data = fitlayerb,
locations = ~ POINT_X + POINT_Y,
variogram.model = varmodel,
param = startp_alr_Silt_spec_b,
fit.param = fitparam,
tuning.psi = tpsi)
summary(alr_Silt_spec_b)
## Store the variogram parameters
variogtableb[variogtableb$properties == "alr_Silt_spec", varparamames] <- alr_Silt_spec_b$variogram.object[[1]]$param[varparamames]
```
## Accounting for vis-NIR model errors in the spatial models
Our vis-NIR models (used to create the vis-NIR augmented data) have an error which we estimated in previous sections and which is stored in the `residualvariances` object. The uncertainty of these models was propagated through spatial predictions in order to obtain more realistic performance results of the spatial predictions. We followed the approach given in Viscarra Rossel et al. (2016b) where the variance of the residuals of the vis-NIR predictions at each layer is used for propagating the vis-NIR erros (see section _Spectroscopicmodel error_ in our paper).
For the vis-NIR augmented data we assume that the uncertainty of the whole set is given by the variances of the predictions. However, this dataset contains both, laboratory data (aprox. 26%) and vis-NIR predicted data (aprox. 74%), therefore the variance we assume here is expected to be larger than the actual one in the augmented data. You can try to account for this.
```{r, eval = FALSE}
## Add the residual variances to the snugget parameter
## of the variograms corresponding to the vis-NIR augmented
## data
## Note that snugget was 0 for all the fitted variograms
## Ca (layer A)
variogtablea$snugget[variogtablea$properties == "Ca_spec"] <- residualvariances$layerA[residualvariances$Property == "Ca"]
## alr(clay) (layer A)
variogtablea$snugget[variogtablea$properties == "alr_Clay_spec"] <- residualvariances$layerA[residualvariances$Property == "alr_Clay"]
## alr(silt) (layer A)
variogtablea$snugget[variogtablea$properties == "alr_Silt_spec"] <- residualvariances$layerA[residualvariances$Property == "alr_Silt"]
## Ca (layer B)
variogtableb$snugget[variogtableb$properties == "Ca_spec"] <- residualvariances$layerB[residualvariances$Property == "Ca"]
## alr(clay) (layer B)
variogtableb$snugget[variogtableb$properties == "alr_Clay_spec"] <- residualvariances$layerB[residualvariances$Property == "alr_Clay"]
## alr(silt) (layer B)
variogtableb$snugget[variogtableb$properties == "alr_Silt_spec"] <- residualvariances$layerB[residualvariances$Property == "alr_Silt"]
```
## Validation of the spatial models
Create a `data.frame` for each layer to store the predictions in the validation set and another two to store the validation results...
```{r, eval = FALSE}
## The final variables to be predicted. In the case of
## Clay, silt and Sand they are estimated from the
## predictions of alr(clay) and alr(silt)
bprops <- c("Ca",
"Clay",
"Silt",
"Sand",
"Ca_spec",
"Clay_spec",
"Silt_spec",
"Sand_spec")
## a quick function to create columns of a given length (lg)
idfcol <- function(x, lg){
data.frame(lg, fix.empty.names = FALSE)
}
## object where the spatial predictions will be stored
sppredsvala <- sapply(c("ID", bprops),
FUN = idfcol,
lg = rep(NA, nrow(vallayera)))
sppredsvala <- data.frame(do.call("cbind", sppredsvala))
sppredsvala$ID <- vallayera$ID
sppredsvalb <- sapply(c("ID", bprops),
FUN = idfcol,
lg = rep(NA, nrow(vallayerb)))
sppredsvalb <- data.frame(do.call("cbind", sppredsvalb))
sppredsvalb$ID <- vallayerb$ID
## object where the validation of the spatial predictions will be stored
spvala <- data.frame(properties = bprops,
data = rep(c("Laboratory", "vis-NIR augmented"),
each = length(bprops) / 2),
R2 = NA,
RMSE = NA,
ME = NA)
spvalb <- spvala
```
### Laboratory-based data
#### Layer A
Exchangeable Ca^2+^...
```{r, eval = FALSE}
pred_Ca_lab_a <- predict(Ca_lab_a,
newdata = as.data.frame(vallayera),
control = gcntrl)
sppredsvala$Ca <- pred_Ca_lab_a$pred$pred
spvala$R2[spvala$properties == "Ca"] <- cor(vallayera$Ca, sppredsvala$Ca)^2
spvala$RMSE[spvala$properties == "Ca"] <- mean((vallayera$Ca - sppredsvala$Ca)^2)^0.5
spvala$ME[spvala$properties == "Ca"] <- mean(vallayera$Ca - sppredsvala$Ca)
```
Estimation of clay, silt and sand contents from the predictions of $alr(clay)$ and $alr(silt)$...
```{r eval = FALSE}
## predict alr(clay)
pred_alr_Clay_lab_a <- predict(alr_Clay_lab_a,
newdata = as.data.frame(vallayera),
control = gcntrl)
## predict alr(silt)
pred_alr_Silt_lab_a <- predict(alr_Silt_lab_a,
newdata = as.data.frame(vallayera),
control = gcntrl)
## Back-transfrom to clay, silt and sand contents
dvr.Silt <- exp(pred_alr_Silt_lab_a$pred$pred + (0.5 * (pred_alr_Silt_lab_a$pred$var.target - pred_alr_Silt_lab_a$pred$cov.pred.target)))
dvr.Clay <- exp(pred_alr_Clay_lab_a$pred$pred + (0.5 * (pred_alr_Clay_lab_a$pred$var.target - pred_alr_Clay_lab_a$pred$cov.pred.target)))
dvn.pred <- 1 + dvr.Silt + dvr.Clay
## Store the back-transformed values
sppredsvala$Clay <- 100 * (dvr.Clay/dvn.pred)
sppredsvala$Silt <- 100 * (dvr.Silt/dvn.pred)
sppredsvala$Sand <- 100/dvn.pred
## Estimate the validation parameters for
## Clay
spvala$R2[spvala$properties == "Clay"] <- cor(vallayera$Clay, sppredsvala$Clay)^2
spvala$RMSE[spvala$properties == "Clay"] <- mean((vallayera$Clay - sppredsvala$Clay)^2)^0.5
spvala$ME[spvala$properties == "Clay"] <- mean(vallayera$Clay - sppredsvala$Clay)
## Silt
spvala$R2[spvala$properties == "Silt"] <- cor(vallayera$Silt, sppredsvala$Silt)^2
spvala$RMSE[spvala$properties == "Silt"] <- mean((vallayera$Silt - sppredsvala$Silt)^2)^0.5
spvala$ME[spvala$properties == "Silt"] <- mean(vallayera$Silt - sppredsvala$Silt)
## Sand
spvala$R2[spvala$properties == "Sand"] <- cor(vallayera$Sand, sppredsvala$Sand)^2
spvala$RMSE[spvala$properties == "Sand"] <- mean((vallayera$Sand - sppredsvala$Sand)^2)^0.5
spvala$ME[spvala$properties == "Sand"] <- mean(vallayera$Sand - sppredsvala$Sand)
```
#### Layer B
Exchangeable Ca^2+^...
```{r, eval = FALSE}
pred_Ca_lab_b <- predict(Ca_lab_b,
newdata = as.data.frame(vallayerb),
control = gcntrl)
sppredsvalb$Ca <- pred_Ca_lab_b$pred$pred
spvalb$R2[spvala$properties == "Ca"] <- cor(vallayerb$Ca, sppredsvalb$Ca)^2
spvalb$RMSE[spvala$properties == "Ca"] <- mean((vallayerb$Ca - sppredsvalb$Ca)^2)^0.5
spvalb$ME[spvala$properties == "Ca"] <- mean(vallayerb$Ca - sppredsvalb$Ca)
```
Estimation of clay, silt and sand contents from the predictions of $alr(clay)$ and $alr(silt)$...
```{r, eval = FALSE}
## predict alr(clay)
pred_alr_Clay_lab_b <- predict(alr_Clay_lab_b,
newdata = as.data.frame(vallayerb),
control = gcntrl)
## predict alr(silt)
pred_alr_Silt_lab_b <- predict(alr_Silt_lab_b,
newdata = as.data.frame(vallayerb),
control = gcntrl)
## Back-transfrom to clay, silt and sand contents
dvr.Silt <- exp(pred_alr_Silt_lab_b$pred$pred + (0.5 * (pred_alr_Silt_lab_b$pred$var.target - pred_alr_Silt_lab_b$pred$cov.pred.target)))
dvr.Clay <- exp(pred_alr_Clay_lab_b$pred$pred + (0.5 * (pred_alr_Clay_lab_b$pred$var.target - pred_alr_Clay_lab_b$pred$cov.pred.target)))
dvn.pred <- 1 + dvr.Silt + dvr.Clay
## Store the back-transformed values
sppredsvalb$Clay <- 100 * (dvr.Clay/dvn.pred)
sppredsvalb$Silt <- 100 * (dvr.Silt/dvn.pred)
sppredsvalb$Sand <- 100/dvn.pred
## Estimate the validation parameters for
## Clay
spvalb$R2[spvala$properties == "Clay"] <- cor(vallayerb$Clay, sppredsvalb$Clay)^2
spvalb$RMSE[spvala$properties == "Clay"] <- mean((vallayerb$Clay - sppredsvalb$Clay)^2)^0.5
spvalb$ME[spvala$properties == "Clay"] <- mean(vallayerb$Clay - sppredsvalb$Clay)
## Silt
spvalb$R2[spvala$properties == "Silt"] <- cor(vallayerb$Silt, sppredsvalb$Silt)^2
spvalb$RMSE[spvala$properties == "Silt"] <- mean((vallayerb$Silt - sppredsvalb$Silt)^2)^0.5
spvalb$ME[spvala$properties == "Silt"] <- mean(vallayerb$Silt - sppredsvalb$Silt)
## Sand
spvalb$R2[spvala$properties == "Sand"] <- cor(vallayerb$Sand, sppredsvalb$Sand)^2
spvalb$RMSE[spvala$properties == "Sand"] <- mean((vallayerb$Sand - sppredsvalb$Sand)^2)^0.5
spvalb$ME[spvala$properties == "Sand"] <- mean(vallayerb$Sand - sppredsvalb$Sand)
```
### Vis-NIR augmented-based data
Here, for the spatial predictions in the vis-NIR augmented dataset we use the variogram parameters which include the residual variances of the vis-NIR models. For this we use the `param`argument of the `predict` function in the `georob` pacakge.
#### Layer A
Exchangeable Ca^2+^...
```{r, eval = FALSE}
pred_Ca_spec_a <- predict(Ca_spec_a,
newdata = as.data.frame(vallayera),
control = gcntrl,
param = unlist(variogtablea[variogtablea$properties == "Ca_spec", varparamames]))
sppredsvala$Ca_spec <- pred_Ca_spec_a$pred$pred
spvala$R2[spvala$properties == "Ca_spec"] <- cor(vallayera$Ca, sppredsvala$Ca_spec)^2
spvala$RMSE[spvala$properties == "Ca_spec"] <- mean((vallayera$Ca - sppredsvala$Ca_spec)^2)^0.5
spvala$ME[spvala$properties == "Ca_spec"] <- mean(vallayera$Ca - sppredsvala$Ca_spec)
```
Estimation of clay, silt and sand contents from the predictions of $alr(clay)$ and $alr(silt)$...
```{r, eval = FALSE}
## predict alr(clay)
pred_alr_Clay_spec_a <- predict(alr_Clay_spec_a,
newdata = as.data.frame(vallayera),
control = gcntrl,
param = unlist(variogtablea[variogtablea$properties == "alr_Clay_spec", varparamames]))
## predict alr(silt)
pred_alr_Silt_spec_a <- predict(alr_Silt_spec_a,
newdata = as.data.frame(vallayera),
control = gcntrl,
param = unlist(variogtablea[variogtablea$properties == "alr_Silt_spec", varparamames]))
## Back-transfrom to clay, silt and sand contents
dvr.Silt <- exp(pred_alr_Silt_spec_a$pred$pred + (0.5 * (pred_alr_Silt_spec_a$pred$var.target - pred_alr_Silt_spec_a$pred$cov.pred.target)))
dvr.Clay <- exp(pred_alr_Clay_spec_a$pred$pred + (0.5 * (pred_alr_Clay_spec_a$pred$var.target - pred_alr_Clay_spec_a$pred$cov.pred.target)))
dvn.pred <- 1 + dvr.Silt + dvr.Clay
## Store the back-transformed values
sppredsvala$Clay_spec <- 100 * (dvr.Clay/dvn.pred)
sppredsvala$Silt_spec <- 100 * (dvr.Silt/dvn.pred)
sppredsvala$Sand_spec <- 100/dvn.pred
## Estimate the validation parameters for
## Clay
spvala$R2[spvala$properties == "Clay_spec"] <- cor(vallayera$Clay, sppredsvala$Clay_spec)^2
spvala$RMSE[spvala$properties == "Clay_spec"] <- mean((vallayera$Clay - sppredsvala$Clay_spec)^2)^0.5
spvala$ME[spvala$properties == "Clay_spec"] <- mean(vallayera$Clay - sppredsvala$Clay_spec)
## Silt
spvala$R2[spvala$properties == "Silt_spec"] <- cor(vallayera$Silt, sppredsvala$Silt_spec)^2
spvala$RMSE[spvala$properties == "Silt_spec"] <- mean((vallayera$Silt - sppredsvala$Silt_spec)^2)^0.5
spvala$ME[spvala$properties == "Silt_spec"] <- mean(vallayera$Silt - sppredsvala$Silt_spec)
## Sand
spvala$R2[spvala$properties == "Sand_spec"] <- cor(vallayera$Sand, sppredsvala$Sand_spec)^2
spvala$RMSE[spvala$properties == "Sand_spec"] <- mean((vallayera$Sand - sppredsvala$Sand_spec)^2)^0.5
spvala$ME[spvala$properties == "Sand_spec"] <- mean(vallayera$Sand - sppredsvala$Sand_spec)
```
#### Layer B
Exchangeable Ca^2+^...
```{r, eval = FALSE}
pred_Ca_spec_b <- predict(Ca_spec_b,
newdata = as.data.frame(vallayerb),
control = gcntrl,
param = unlist(variogtableb[variogtableb$properties == "Ca_spec", varparamames]))
sppredsvalb$Ca_spec <- pred_Ca_spec_b$pred$pred
spvalb$R2[spvala$properties == "Ca_spec"] <- cor(vallayerb$Ca, sppredsvalb$Ca_spec)^2
spvalb$RMSE[spvala$properties == "Ca_spec"] <- mean((vallayerb$Ca - sppredsvalb$Ca_spec)^2)^0.5
spvalb$ME[spvala$properties == "Ca_spec"] <- mean(vallayerb$Ca - sppredsvalb$Ca_spec)
```
Estimation of clay, silt and sand contents from the predictions of $alr(clay)$ and $alr(silt)$...
```{r eval = FALSE}
## predict alr(clay)
pred_alr_Clay_spec_b <- predict(alr_Clay_spec_b,
newdata = as.data.frame(vallayerb),
control = gcntrl,
param = unlist(variogtableb[variogtableb$properties == "alr_Clay_spec", varparamames]))
## predict alr(silt)
pred_alr_Silt_spec_b <- predict(alr_Silt_spec_b,
newdata = as.data.frame(vallayerb),
control = gcntrl,
param = unlist(variogtableb[variogtableb$properties == "alr_Silt_spec", varparamames]))
## Back-transfrom to clay, silt and sand contents
dvr.Silt <- exp(pred_alr_Silt_spec_b$pred$pred + (0.5 * (pred_alr_Silt_spec_b$pred$var.target - pred_alr_Silt_spec_b$pred$cov.pred.target)))
dvr.Clay <- exp(pred_alr_Clay_spec_b$pred$pred + (0.5 * (pred_alr_Clay_spec_b$pred$var.target - pred_alr_Clay_spec_b$pred$cov.pred.target)))
dvn.pred <- 1 + dvr.Silt + dvr.Clay
## Store the back-transformed values
sppredsvalb$Clay_spec <- 100 * (dvr.Clay/dvn.pred)
sppredsvalb$Silt_spec <- 100 * (dvr.Silt/dvn.pred)
sppredsvalb$Sand_spec <- 100/dvn.pred
## Estimate the validation parameters for
## Clay
spvalb$R2[spvala$properties == "Clay_spec"] <- cor(vallayerb$Clay, sppredsvalb$Clay_spec)^2
spvalb$RMSE[spvala$properties == "Clay_spec"] <- mean((vallayerb$Clay - sppredsvalb$Clay_spec)^2)^0.5
spvalb$ME[spvala$properties == "Clay_spec"] <- mean(vallayerb$Clay - sppredsvalb$Clay_spec)
## Silt
spvalb$R2[spvala$properties == "Silt_spec"] <- cor(vallayerb$Silt, sppredsvalb$Silt_spec)^2
spvalb$RMSE[spvala$properties == "Silt_spec"] <- mean((vallayerb$Silt - sppredsvalb$Silt_spec)^2)^0.5
spvalb$ME[spvala$properties == "Silt_spec"] <- mean(vallayerb$Silt - sppredsvalb$Silt_spec)
## Sand
spvalb$R2[spvala$properties == "Sand_spec"] <- cor(vallayerb$Sand, sppredsvalb$Sand_spec)^2
spvalb$RMSE[spvala$properties == "Sand_spec"] <- mean((vallayerb$Sand - sppredsvalb$Sand_spec)^2)^0.5
spvalb$ME[spvala$properties == "Sand_spec"] <- mean(vallayerb$Sand - sppredsvalb$Sand_spec)
```
## Mapping
Now that we have validated the spatial models for both laboratory data and vis-NIR augmented data, we proceed to produce the maps of each property at each layer.
First we have to read the polygon corresponding to the study area. Download the polygon to your working directory.
This is a `R` object of class (`SpatialPolygonsDataFrame` of the package `sp`) which contains the polygon of the study area. Click [here](https://github.com/l-ramirez-lopez/VNIR_spectroscopy_for_robust_soil_mapping/raw/master/polygon.rds) to download it. If you saved the file in your working directory you can:
```{r, eval = FALSE}
polyfile <- file("polygon.rds")
shape <- readRDS(polyfile)
shape
```
or...
```{r, eval = FALSE}
polyfile <- file("https://github.com/l-ramirez-lopez/VNIR_spectroscopy_for_robust_soil_mapping/raw/master/polygon.rds")
shape <- readRDS(polyfile)
shape
```
Now create a template for the spatial predictions
```{r eval = FALSE}
## function to convert polygon to raster
pol2raster <-function(r, nrows = 10, ncols = 10){
ext<-raster(extent(r), nrows, ncols)
crs(ext) <- crs(r)
fr <- rasterize(r, ext, field = 1, update = TRUE)
return(fr)
}
rncol <- (extent(shape)@ymax - extent(shape)@ymin)/10
rnrow <- (extent(shape)@xmax - extent(shape)@xmin)/10
rasg <- pol2raster(shape, rncol, rnrow)
## resolution
## here you can choose the resolution for the predicted maps
## for our paper we set this value to 10, however here we
## will set it to 50 for the sake of memory
## if you have a decent machine you can go finer
mresolution <- 50
rasg <- raster::resample(rasg,
raster(resolution = mresolution, ext = extent(shape)),
method="ngb")
rasg
plot(rasg)
## and here we have our template
sppx <- as(rasg, "SpatialPixelsDataFrame")
colnames(sppx@coords) <- c("POINT_X", "POINT_Y")
```
Create a `data.frame` for each layer to store the predicted values for the maps
```{r, eval = FALSE}
spatialpredsa <- data.frame(matrix(NA, nrow(sppx), length(bprops)))
colnames(spatialpredsa) <- bprops
spatialpredsb <- spatialpredsa
```
### Laboratory-based data
#### Layer A
Exchangeable Ca^2+^...
```{r, eval = FALSE}
spatialpredsa$Ca <- predict(Ca_lab_a, newdata = sppx)$pred
```
Clay, sand and silt contents..
```{r, eval = FALSE}
alr_Clay_lab_a_map <- predict(alr_Clay_lab_a, newdata = sppx)
alr_Silt_lab_a_map <- predict(alr_Silt_lab_a, newdata = sppx)
spatialpredsa$Clay <- 100 * exp(alr_Clay_lab_a_map$pred)/(1 + exp(alr_Silt_lab_a_map$pred) + exp(alr_Clay_lab_a_map$pred))
spatialpredsa$Silt <- 100 * exp(alr_Silt_lab_a_map$pred)/(1 + exp(alr_Silt_lab_a_map$pred) + exp(alr_Clay_lab_a_map$pred))
spatialpredsa$Sand <- 100/(1 + exp(alr_Silt_lab_a_map$pred) + exp(alr_Clay_lab_a_map$pred))
```
#### Layer B
Exchangeable Ca^2+^...
```{r, eval = FALSE}
spatialpredsb$Ca <- predict(Ca_lab_b, newdata = sppx)$pred
```
Clay, sand and silt contents..
```{r, eval = FALSE}
alr_Clay_lab_b_map <- predict(alr_Clay_lab_b, newdata = sppx)
alr_Silt_lab_b_map <- predict(alr_Silt_lab_b, sppx)
spatialpredsb$Clay <- 100 * exp(alr_Clay_lab_b_map$pred)/(1 + exp(alr_Silt_lab_b_map$pred) + exp(alr_Clay_lab_b_map$pred))
spatialpredsb$Silt <- 100 * exp(alr_Silt_lab_b_map$pred)/(1 + exp(alr_Silt_lab_b_map$pred) + exp(alr_Clay_lab_b_map$pred))
spatialpredsb$Sand <- 100/(1 + exp(alr_Silt_lab_b_map$pred) + exp(alr_Clay_lab_b_map$pred))
```
### Vis-NIR augmented-based data
#### Layer A
Exchangeable Ca^2+^...
```{r, eval = FALSE}
spatialpredsa$Ca_spec <- predict(Ca_spec_a,
newdata = sppx,
param = unlist(variogtablea[variogtablea$properties == "Ca_spec", varparamames]))$pred
```
Clay, sand and silt contents..
```{r, eval = FALSE}
alr_Clay_spec_a_map <- predict(alr_Clay_spec_a,
newdata = sppx,
param = unlist(variogtablea[variogtablea$properties == "alr_Silt_spec", varparamames]))
alr_Silt_spec_a_map <- predict(alr_Silt_spec_a,
newdata = sppx,
param = unlist(variogtablea[variogtablea$properties == "alr_Clay_spec", varparamames]))
spatialpredsa$Clay_spec <- 100 * exp(alr_Clay_spec_a_map$pred)/(1 + exp(alr_Silt_spec_a_map$pred) + exp(alr_Clay_spec_a_map$pred))
spatialpredsa$Silt_spec <- 100 * exp(alr_Silt_spec_a_map$pred)/(1 + exp(alr_Silt_spec_a_map$pred) + exp(alr_Clay_spec_a_map$pred))
spatialpredsa$Sand_spec <- 100/(1 + exp(alr_Silt_spec_a_map$pred) + exp(alr_Clay_spec_a_map$pred))
```
#### Layer B
Exchangeable Ca^2+^...
```{r, eval = FALSE}
spatialpredsb$Ca_spec <- predict(Ca_spec_b,
newdata = sppx,
param = unlist(variogtableb[variogtableb$properties == "Ca_spec", varparamames]))$pred
```
Clay, sand and silt contents..
```{r, eval = FALSE}
alr_Clay_spec_b_map <- predict(alr_Clay_spec_b,
newdata = sppx,
param = unlist(variogtableb[variogtableb$properties == "alr_Silt_spec", varparamames]))
alr_Silt_spec_b_map <- predict(alr_Silt_spec_b,
newdata = sppx,
param = unlist(variogtableb[variogtableb$properties == "alr_Clay_spec", varparamames]))
spatialpredsb$Clay_spec <- 100 * exp(alr_Clay_spec_b_map$pred)/(1 + exp(alr_Silt_spec_b_map$pred) + exp(alr_Clay_spec_b_map$pred))
spatialpredsb$Silt_spec <- 100 * exp(alr_Silt_spec_b_map$pred)/(1 + exp(alr_Silt_spec_b_map$pred) + exp(alr_Clay_spec_b_map$pred))
spatialpredsb$Sand_spec <- 100/(1 + exp(alr_Silt_spec_b_map$pred) + exp(alr_Clay_spec_b_map$pred))
```
### Plots
```{r, eval = FALSE}
## compute the differences between maps (layer)
spatialpredsa$Cadiff <- spatialpredsa$Ca - spatialpredsa$Ca_spec
spatialpredsa$Claydiff <- spatialpredsa$Clay - spatialpredsa$Clay_spec
spatialpredsa$Siltdiff <- spatialpredsa$Silt - spatialpredsa$Silt_spec
spatialpredsa$Sanddiff <- spatialpredsa$Sand - spatialpredsa$Sand_spec
## compute the differences between maps (layer B)
spatialpredsb$Cadiff <- spatialpredsb$Ca - spatialpredsb$Ca_spec
spatialpredsb$Claydiff <- spatialpredsb$Clay - spatialpredsb$Clay_spec
spatialpredsb$Siltdiff <- spatialpredsb$Silt - spatialpredsb$Silt_spec
spatialpredsb$Sanddiff <- spatialpredsb$Sand - spatialpredsb$Sand_spec
```
Create a new `data.frame` for plotting purposes
```{r, eval = FALSE}
pred_layer_a <- data.frame(rbind(sppx@coords,
sppx@coords,
sppx@coords),
layer = "Depth A (0 - 0.2 m)",
method = c(rep("Laboratory-based", nrow(spatialpredsa)),