-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0_Functions.R
1503 lines (1184 loc) · 57.1 KB
/
0_Functions.R
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-Script - 0_Functions.R ##
## author: Javier Lopatin ##
## mail: javierlopatin@gmail.com ##
## ##
## Manuscript: Mapping plant species in mixed grassland communities using ##
## close range imaging spectroscopy ##
## ##
## description: This R-code provide most of the functions applied in the paper##
## ##
################################################################################
##----------------------------------------------------------------------------##
## ##
## Apply the functions tuningModels, BootsClassification and obstainCovers ##
## in a row to obtain the classification results per dataset ##
## ##
## Arguments: ##
## - valData classes to classify. I this case the species list per subplot##
## - potVa- reflectance obtained with the pot validation method ##
## - rf reflectance obtained with the rip-it-off validation method ##
## - raster_List list of the plots rasters ##
## - wl wavelength vector ##
## - modelTag tag to assign to the exported results ##
## - boots number of bootstraps to perform ##
## ##
##----------------------------------------------------------------------------##
ApplyModels <- function(valData, potVal, rf, raster_List, wl, modelTag, boots){
for (i in 1:length(raster_List)){
# obtain the validation data per plot
raster = raster_List[[i]]
names(raster) <- paste0( rep("B", nlayers(raster)), seq(1, nlayers(raster), 1) )
plot = unique(na.omit(as.numeric(unlist(strsplit( names( raster_List )[[i]], "[^0-9]+")))))
plot_name = paste0("plot_", plot)
x = grep( plot, valData$Plot )
data = valData[x, ]
data$Species <- factor( data$Species ) # reset species Levels
# get species to classify in the plot
classes = unique( data$Species )
# obtain subset of data of potVal and rf that include these species
x = grep( paste(classes, collapse = "|") , potVal$Species )
data_potVal = potVal[x, ]
data_potVal$Species <- factor(data_potVal$Species)
x = grep( paste(classes, collapse = "|") , rf$Species )
data_rf = rf[x, ]
data_rf$Species <- factor(data_rf$Species)
####################################
### Apply tuningModels function ###
####################################
print(paste("### Tuning", plot_name, modelTag, "###"))
print("")
print("Tuning pot Validation model")
print("")
fit_potVal <- tuningModels(classes = data_potVal$Species,
spectra = data_potVal[, 3:length( data_potVal )],
wl = wl)
print("Tuning rip-it-off model")
print("")
fit_rf <- tuningModels(classes = data_rf$Species,
spectra = data_rf[, 3:length( data_rf )],
wl = wl)
# save tuning models
dir.create(file.path(home, "tuningOutputs"), showWarnings = FALSE)
save(fit_potVal, file=paste0(home, "/tuningOutputs/", "potVal_",
plot_name, "_", modelTag, ".RData"))
save(fit_rf, file=paste0(home, "/tuningOutputs/", "rf_",
plot_name, "_", modelTag, ".RData"))
print("Done!")
print("")
#################################
### Apply BootsClassification ###
#################################
dir.create(file.path(home, "BootsClass_out"), showWarnings = FALSE)
print("### Starting bootstrap predictions ###")
print("")
BootsClassification(classes = data_potVal$Species,
spectra = data_potVal[, 3:length( data_potVal )],
en = fit_potVal,
raster = raster,
boots = boots,
outDir = file.path(home, "BootsClass_out"),
modelTag = paste0("potVal_", modelTag),
plotName = plot_name)
BootsClassification(classes = data_rf$Species,
spectra = data_rf[, 3:length( data_potVal )],
en = fit_rf,
raster = raster,
boots = boots,
outDir = file.path(home, "BootsClass_out"),
modelTag = paste0("rf_", modelTag),
plotName = plot_name)
print("Done!")
print("")
#####################################
### Apply obstainCovers function ####
#####################################
print("### Starting Cover estimation ###")
print("")
# for PLS-DA potVal
obstainCovers(ObservedSpecies = valData,
rasterDir = paste0( home, "/BootsClass_out/", plot_name, "_PLS_",
paste0("potVal_", modelTag) ),
subplotDir = subplotDir,
shpMaskName = plot_name,
plotNumber = plot,
Iter = boots,
algorithm = "PLS_potVal")
# for PLS-DA rf
obstainCovers(ObservedSpecies = valData,
rasterDir = paste0( home, "/BootsClass_out/", plot_name, "_PLS_",
paste0("rf_", modelTag) ),
subplotDir = subplotDir,
shpMaskName = plot_name,
plotNumber = plot,
Iter = boots,
algorithm = "PLS_rf")
print("PLS-DA done!")
# for RF potVal
obstainCovers(ObservedSpecies = valData,
rasterDir = paste0( home, "/BootsClass_out/", plot_name, "_RF_",
paste0("potVal_", modelTag) ),
subplotDir = subplotDir,
shpMaskName = plot_name,
plotNumber = plot,
Iter = boots,
algorithm = "RF_potVal")
# for RF rf
obstainCovers(ObservedSpecies = valData,
rasterDir = paste0( home, "/BootsClass_out/", plot_name, "_RF_",
paste0("rf_", modelTag) ),
subplotDir = subplotDir,
shpMaskName = plot_name,
plotNumber = plot,
Iter = boots,
algorithm = "RF_rf")
print("RF done!")
# for SVM potVal
obstainCovers(ObservedSpecies = valData,
rasterDir = paste0( home, "/BootsClass_out/", plot_name, "_SVM_",
paste0("potVal_", modelTag) ),
subplotDir = subplotDir,
shpMaskName = plot_name,
plotNumber = plot,
Iter = boots,
algorithm = "SVM_potVal")
# for SVM rf potVal
obstainCovers(ObservedSpecies = valData,
rasterDir = paste0( home, "/BootsClass_out/", plot_name, "_SVM_",
paste0("rf_", modelTag) ),
subplotDir = subplotDir,
shpMaskName = plot_name,
plotNumber = plot,
Iter = boots,
algorithm = "SVM_rf")
print("SVM done!")
print("")
}
}
##----------------------------------------------------------------------------##
## ##
## tuningModels function ##
## ##
## This function performs a tuning procidure on the models and ##
## a band selection based on a multi-method ensemble ##
## assessment of the variable importance and classification coefficients of ##
## three different model types: Partial Least Squares Discriminant Analysis, ##
## Random Forest and Support Vector Machine classifications (not presented in ##
## the paper) ##
## ##
## Arguments: ##
## - x Numeric matrix containing the spectra (samples as rows) ##
## - y Numeric vector containing the response variable ##
## - wl Numeric vector containing the wavelength information of the bands ##
## ##
##----------------------------------------------------------------------------##
tuningModels <- function(classes, spectra, wl=NA){
## load required libraries
library(caret)
library(e1071)
library(doParallel)
# set data
data2 <- data.frame(classes = classes, spectra)
data2 <- na.omit(data2)
# Set the random number seed so we can reproduce the results
set.seed(123)
# Split data in training and test
forTraining <- createDataPartition(data2$classes, p = 0.6, list=F)
train <- data2 [ forTraining,]
test<- data2 [-forTraining,]
# Each model used 5 repeated 10-fold cross-validation. Use AUC to pick the best model
controlObject <- trainControl(method = "cv", number = 5, classProbs=TRUE,
allowParallel = TRUE, seeds = set.seed(123))
# initialize parallel processing
cl <- makeCluster(detectCores())
registerDoParallel(cl)
#############################
### PLS-DA classification ###
#############################
print("Tuning PLS-DA Model...")
# apply classification
set.seed(123)
plsClas <- train(x=train[, 2:length(train)], y=make.names( train$classes ), method = "pls",
tuneLength=20, preProc = c("center", "scale"), trControl = controlObject)
# predict
pls.pred <- predict(plsClas, test[, 2:length(train)])
# confusion matix
preMatrix <- function(pred, test){ # functionn to prevent caret error for different length
u = union(pred, test)
t = table(factor(pred, u), factor(test, u))
return(t)
}
conf.pls <- confusionMatrix(preMatrix(pls.pred, test$classes))
# get accuracies
PA.pls <- conf.pls$byClass[,3]
UA.pls <- conf.pls$byClass[,4]
OA.pls <- conf.pls$overall["Accuracy"]
kappa.pls <- conf.pls$overall["Kappa"]
### variable importance
plscf <- as.vector(rowMeans(plsClas$finalModel$coefficients)) ## extract coeff.
plscf <- plscf / sd (plscf) ## scale regression coefficients
print("Done!")
#########################
### RF classification ###
#########################
print("Tuning RF model...")
set.seed(123)
rfClas <- train(x=train[, 2:length(train)], y=make.names( train$classes ), method = "rf",
tuneLength=15, trControl = controlObject)
# predict
rf.pred <- predict(rfClas, test[,2:length(train)])
# confusion matix
conf.rf <- confusionMatrix(preMatrix(rf.pred, test$classes))
# get accuracies
PA.rf <- conf.rf$byClass[,3]
UA.rf <- conf.rf$byClass[,4]
OA.rf <- conf.rf$overall["Accuracy"]
kappa.rf <- conf.rf$overall["Kappa"]
### variable importance
rfcf <- varImp(rfClas$finalModel)[[1]]
rfcf <- as.vector (rfcf / sd(rfcf))
print("Done!")
##########################
### SVM classification ###
##########################
print("Tuning SVM Model...")
set.seed(123)
svmClas <- train(x=train[, 2:length(train)], y=make.names( train$classes ), method = "svmLinear2",
tuneLength=10, preProc = c("center", "scale"), trControl = controlObject)
# predict
svm.pred <- predict(svmClas, test[,2:length(train)])
# confusion matix
conf.svm <- confusionMatrix(preMatrix(svm.pred, test$classes))
# get accuracies
PA.svm <- conf.svm$byClass[,3]
UA.svm <- conf.svm$byClass[,4]
OA.svm <- conf.svm$overall["Accuracy"]
kappa.svm <- conf.svm$overall["Kappa"]
### variable importance
svr.alpha <- t(svmClas$finalModel$coefs) ## extract alpha vector
svr.alpha <- colMeans(svr.alpha)
svr.index <- svmClas$finalModel$index ## extract alpha index
## calculate pseudo-regression coefficients from the alpha vector
svrcf <- numeric (ncol (spectra))
for(i in 1:ncol(spectra))
svrcf[i] <- svr.alpha %*% spectra[svr.index, i]
svrcf <- svrcf / sd (svrcf) ## scale pseudo-coefficients
print("Done!")
# stop parallel process
stopCluster(cl)
#####################################################################
### get ensemble from all models and identify important variables ###
#####################################################################
## get ensemble from all models and identify important variables
ensemblecf <- abs(plscf) * OA.pls + abs(rfcf) * OA.rf + abs(svrcf) * OA.svm
th <- mean(ensemblecf) + sd(ensemblecf) ## calculate threshold
selbands <- ensemblecf > th ## apply threshold
######################
### prepare output ###
######################
cf <- rbind (wl, plscf, rfcf, svrcf, ensemblecf, selbands)
colnames(cf) <- colnames(spectra)
fit <- c (OA.pls, OA.rf, OA.svm)
names (fit) <- c ("PLS-DA OA", "RF OA", "SVR OA")
output <- list (cf, fit, th, plsClas, rfClas, svmClas, conf.pls, conf.rf, conf.svm)
names (output) <- c ("selection", "fits", "threshold", "PLS", "RF", "SVM", "confusionMatrix.PLS", "confusionMatrix.RF", "confusionMatrix.SVM")
class (output) <- "ensemble"
output
}
##----------------------------------------------------------------------------##
## ##
## Apply the best model from classificationEnsemble to the plots using ##
## a bootstrapping procidure. ##
## ##
## Arguments: ##
## - spectra spectral information. Used to create the quantiles of spectra ##
## - en classificationEnsemble object ##
## ##
##----------------------------------------------------------------------------##
BootsClassification <- function(classes, spectra, en, raster, boots,
outDir, modelTag, plotName){
library(raster)
library(rgdal)
library(caret)
library(e1071)
library(randomForest)
library(pls)
library(doParallel)
# extract the data from the classification Ensamble function
data2 <- data.frame(classes = classes, spectra)
data2 <- na.omit(data2)
data2$classes <- factor(data2$classes)
ncomp = en$PLS$finalModel$ncomp
probMethod = en$PLS$finalModel$probMethod
bestNtree = en$RF$finalModel$ntree
bestMtry = en$RF$finalModel$mtry
bestCost = en$SVM$finalModel$cost
bestGamma = en$SVM$finalModel$gamma
## apply funtion to predict species cover with SVM
# list of accuracies
PA.PLS <- list()
UA.PLS <- list()
OA.PLS <- list()
kappa.PLS <- list()
predict.PLS <- list()
PA.RF <- list()
UA.RF <- list()
OA.RF <- list()
kappa.RF <- list()
predict.RF <- list()
PA.SVM <- list()
UA.SVM <- list()
OA.SVM <- list()
kappa.SVM <- list()
predict.SVM <- list()
OBS <- list()
# initialize parallel processing
cl <- makeCluster(detectCores())
registerDoParallel(cl)
# progress bar
print(paste0(plotName, " ", modelTag))
pb <- txtProgressBar(min = 0, max = boots, style = 3)
for (i in 1:boots){
# progress bar
Sys.sleep(0.1)
setTxtProgressBar(pb, i)
# stratify samplig. All species get selected at least once
samp <- stratifySampling(data2, classes)
train <- samp$train
val <- samp$validation
# store and select the observations
obs <- val$classes
OBS[[i]]<-obs
#################
### Apply PLS ###
#################
PLS <- plsda(x = train[, 2:length(train)], y = train$classes, ncomp = ncomp,
probMethod = probMethod)
# predict
pred_pls <- predict(PLS, val[, 2:length(val)])
predict.PLS[[i]] <- pred_pls
# confusion matix
conf <- confusionMatrix(pred_pls, val$classes)
# get accuracies
PA.PLS[[i]] <- conf$byClass[,3]
UA.PLS[[i]] <- conf$byClass[,4]
OA.PLS[[i]] <- conf$overall["Accuracy"]
kappa.PLS[[i]] <- conf$overall["Kappa"]
#################
### Apply SVM ###
#################
RF <- randomForest( y = train$classes, x = train[, 2:length(train)],
ntree= bestNtree, mtry = bestMtry)
# predict
pred_rf <- predict(RF, val[, 2:length(val)])
predict.RF[[i]] <- pred_rf
# confusion matix
conf <- confusionMatrix(pred_rf, val$classes)
# get accuracies
PA.RF[[i]] <- conf$byClass[,3]
UA.RF[[i]] <- conf$byClass[,4]
OA.RF[[i]] <- conf$overall["Accuracy"]
kappa.RF[[i]] <- conf$overall["Kappa"]
#################
### Apply SVM ###
#################
SVM <- svm(train[, 2:length(train)], train$classes, kernel = "linear",
gamma = bestGamma, cost = bestCost, probability = TRUE)
# predict
pred_svm <- predict(SVM, val[, 2:length(val)])
predict.SVM[[i]] <- pred_svm
# confusion matix
conf <- confusionMatrix(pred_svm, val$classes)
# get accuracies
PA.SVM[[i]] <- conf$byClass[,3]
UA.SVM[[i]] <- conf$byClass[,4]
OA.SVM[[i]] <- conf$overall["Accuracy"]
kappa.SVM[[i]] <- conf$overall["Kappa"]
##############################
### Apply models to raster ###
##############################
# NDVI mask to spectral images
if ( nlayers(raster)==61 ){
names(raster) <- paste( rep("B", 61), seq(1,61,1), sep="" )
# mask out raster zones with NDVI below 0.3
red <- raster[[31]]
Ired <- raster[[43]]
NDVI <- (Ired-red)/(Ired+red)
NDVI[NDVI < 0.3]<- NA
# apply mask
raster <- mask(raster, NDVI)
} else { # for MNF components
names(raster) <- paste( rep("B", 10), seq(1,10,1), sep="" )
}
### Predict PLS DA
r_PLS <- predict(raster, PLS, type="class")
### Predict RF
r_RF <- predict(raster, RF, type="class")
#### Predict SVM
r_SVM <- predict(raster, SVM, type="class")
### export rasters
# create a folder per plot to store results
plotName_PLS = paste( plotName, "_PLS_", modelTag, sep="" )
dir.create(file.path(outDir, plotName_PLS), showWarnings = FALSE)
outdir_PLS = file.path(outDir, plotName_PLS)
plotName_RF = paste(plotName, "_RF_", modelTag, sep="" )
dir.create(file.path(outDir, plotName_RF), showWarnings = FALSE)
outdir_RF = file.path(outDir, plotName_RF)
plotName_SVM = paste( plotName, "_SVM_", modelTag, sep="" )
dir.create(file.path(outDir, plotName_SVM), showWarnings = FALSE)
outdir_SVM = file.path(outDir, plotName_SVM)
out_PLS = paste( plotName, "_PLS_", i, ".tif", sep="" )
out_RF = paste( plotName, "_RF_", i, ".tif", sep="" )
out_SVM = paste( plotName, "_SVM_", i, ".tif", sep="" )
out_PLS = file.path(outdir_PLS, out_PLS)
out_RF = file.path(outdir_RF, out_RF)
out_SVM = file.path(outdir_SVM, out_SVM)
# Export rasters
writeRaster(r_PLS, filename=out_PLS, format="GTiff", overwrite = T)
writeRaster(r_RF, filename=out_RF, format="GTiff", overwrite = T)
writeRaster(r_SVM, filename=out_SVM, format="GTiff", overwrite = T)
}
# close progress bar
close(pb)
# stop parallel process
stopCluster(cl)
######################
### prepare output ###
######################
fits <- data.frame( unlist(OA.PLS), unlist(OA.RF), unlist(OA.SVM),
unlist(kappa.PLS), unlist(kappa.RF), unlist(kappa.SVM))
names(fits) <- c("OA_PLS", "OA_RF", "OA_SVM", "Kappa_PLS", "Kappa_RF", "Kapa_SVM")
fits_2 <- data.frame( PLS$obsLevels , unlist(PA.PLS), unlist(PA.RF), unlist(PA.SVM),
unlist(OA.PLS), unlist(OA.RF), unlist(OA.SVM))
names(fits_2) <- c("Species", "PA_PLS", "PA_RF", "PA_SVM", "OA_PLS", "OA_RF", "OA_SVM")
predict_all <- data.frame( unlist(OBS), unlist(predict.PLS), unlist(predict.RF), unlist(predict.SVM) )
names(predict_all) <- c("Observed", "PLS", "RF", "SVM")
### Export
write.table(fits, file = file.path(outDir, paste("Fits_", plotName, "_", modelTag, ".txt", sep="")),
row.names = F, col.names = T)
write.table(fits_2, file = file.path(outDir, paste("FitsPA_OA_", plotName, "_", modelTag, ".txt", sep="")),
row.names = F, col.names = T)
write.table(predict_all, file = file.path(outDir, paste("Predicts_", plotName, "_", modelTag, ".txt", sep="")),
row.names = F, col.names = T)
}
##----------------------------------------------------------------------------##
## ##
## obstainCovers: Obtain the covers prediction values per plot ##
## ##
## Arguments: ##
## - rasterDir folder of the predicted plots of ApplyBootsClassification ##
## - shpDir folder of the shapefiles used to mask the image ##
## - maskName Name of the plot to use as a mask ##
## ##
##----------------------------------------------------------------------------##
obstainCovers <- function(ObservedSpecies, rasterDir, subplotDir, maskName,
plotNumber, Iter, algorithm){
library(raster)
library(rgdal)
#######################################
### obtain observed covers per plot ###
#######################################
x = grep(plotNumber, ObservedSpecies$Plot)
obs_plot <- ObservedSpecies[x,]
obs_plot$Species = factor(obs_plot$Species)
store_plot <- matrix(nrow = length( levels(obs_plot$Species)), ncol = 2)
store_plot[,1] <- levels(obs_plot$Species)
colnames(store_plot) <- c("Species", "Covers")
for (i in 1:length( levels(obs_plot$Species))){
sp = levels(obs_plot$Species)[i]
z = grep(sp, obs_plot$Species)
z <- obs_plot[z,]
sumCov = sum(z$Cover)
cov = (sumCov*100)/1600 # 1600: ~ N° of pixels per subplot
store_plot[[i,2]] <- cov
}
rownames(store_plot) <- store_plot[,1]
############################################
### estimate predicted cover per subplot ###
############################################
# make a rasterlist from the rasterDir folder
rstLisr <- rasterList(fileExtantion = ".tif", folder = ".", dir = rasterDir, select=NULL)
levs <- levels(rstLisr[[1]])
levelsNumber <- length( levs[[1]][,1] )
levels_plot <- levs[[1]][2:length(levs[[1]][,1]), 2]
# load subplots from to copy the extation
subplots <- rasterList(fileExtantion = ".tif", folder = "subplots", dir = home)
x = grep(plotNumber, names( subplots ))
subplots <- subplots[x]
# subplot names
subplotNames <- c("A1","A2","A3","A4","B1","B2","B3","B4","C1", "C2","C3","C4","D1","D2","D3","D4")
### start loop to obtain covers ###
areasList <- list()
# loop through the prediction maps
for (i in 1:length( rstLisr )){
raster = rstLisr[[i]]
#store_areas <- as.data.frame(levs[[1]][2:length(levs[[1]][,1]),])
store_areas <- as.data.frame(levs)
colnames(store_areas) <- c("ID", "Species")
rownames( store_areas ) <- store_areas$category
# loop through the subplots
for (i2 in 1:16){
# get subplot extent
ext = extent( subplots[[i2]] )
clip <- crop(raster, ext)
# count number of pixels
area = ncell(clip)
# count number os of pixels per class
count_class = freq(clip)
# estimate area of per class
percent = (count_class[[1]][,2]*100)/area
percent = data.frame(ID = count_class[[1]][,1], Cover = percent)
#percent = data.frame(ID = count_class[[1]][,1]-1, Cover = percent)
## correct the shift
#if ( is.na( percent$ID[length(percent[,1])] ) ){
# percent[length(percent[,1]), 1] <- levelsNumber
#}
# add value to store_areas matrix
store_areas <- merge(store_areas, percent, by.x = "ID", all = TRUE)
### Merge species with flowers
store_areas <- mergeSpecies(store_areas)
# aggregate merge
store_areas <- aggregate( . ~ Species, data=store_areas, sum, na.action = na.pass)
colnames( store_areas )[2+i2] <- subplotNames[i2]
}
store_areas$Species <- factor(store_areas$Species)
areasList [[i]] <- store_areas
}
# unlist
areasPlots <- do.call( "rbind", areasList )
# get the median value per specie
dummy_matrix <- matrix( ncol = ncol(areasList[[1]]), nrow = nrow(areasList[[1]]) )
colnames(dummy_matrix) <- colnames(areasList[[1]])
dummy_matrix[,1] <- as.character( areasList[[1]][,1] )
dummy_matrix[,2] <- areasList[[1]][,2]
for (i in 1:length( unique(areasList[[1]]$Species) )){#(levelsNumber-1)
x = grep(areasList[[1]]$Species[i], areasPlots$Species)
sp = areasPlots[x, ]
for (i2 in 1:15){
y = sp[,i2+2]
y = na.omit(y)
med = median(y)
dummy_matrix[i,i2+2] <- as.numeric(med)
}
}
MedianCover = as.data.frame(dummy_matrix)
# observed subplot covers
Species_plot <- as.data.frame(levs)
colnames(Species_plot) <- c("ID", "Species")
for (i in 1:16){
x = grep( subplotNames[i], obs_plot$Subplot )
obs_subplot <- obs_plot[x, ]
obs_subplot <- obs_subplot[, 4:5]
Species_plot <- merge(Species_plot, obs_subplot, by.x = "Species", all = TRUE)
colnames( Species_plot )[2+i] <- subplotNames[i2]
}
x =grep("flowers", Species_plot$Species)
Species_plot <- Species_plot[-x, ]
x = duplicated(Species_plot$Species)
Species_plot <- Species_plot[!x,]
################################
### Export results to tables ###
################################
# create folder to store results
dir.create( file.path(home, "Covers_results"), showWarnings = FALSE)
# create output names
boot_covers = paste0(algorithm, "_Boot_Covers_", maskName, "_", modelTag, ".txt")
boot_covers = file.path( home, "Covers_results", boot_covers )
median_covers = paste0( algorithm, "_Median_Covers_", maskName, "_", modelTag, ".txt")
median_covers = file.path( home, "Covers_results", median_covers )
observed_covers = paste0( algorithm, "_Obs_covers", maskName, "_", modelTag, ".txt")
observed_covers = file.path( home, "Covers_results", observed_covers )
# write results
write.table(areasPlots, file = boot_covers, sep = " ", row.names = F, col.names = T )
write.table(MedianCover, file = median_covers, sep = " ", row.names = F, col.names = T )
write.table(Species_plot, file = observed_covers, sep = " ", row.names = F, col.names = T )
}
##------------------------------------------------------------------------------##
## ##
## plots: visualization of spectras with the 5, 25, 50, 75 and 95 percentiles ##
## ##
## Arguments: ##
## - spectra spectral information. Used to create the quantiles of spectra ##
## - wl classificationEnsemble object ##
## - xaxis if TRUE the X axis is ploted ##
## - ylab if TRUE the Y axis label is ploted ##
## - ylabside if TRUE the Y axis label is ploted at the left side of the plot ##
## - ymax maximum value of the Y axis ##
## ##
##------------------------------------------------------------------------------##
plot.spectra <- function(spectra, wl, xaxis=TRUE, ylab = TRUE, ymax, ...){
# obtain the quantiles of the spectras
quant <- apply(spectra, 2, quantile, probs =c(0.05, 0.25, 0.5, 0.75, 0.95))
if (xaxis == TRUE){
plot(wl, quant[1,], type="l", ylim = c(0,ymax), xlim = c(min(wl)-10, max(wl)+10),
xaxs = "i", ylab= NA, las=1, xlab=expression(lambda(nm)), axes=F, cex.lab = 1.3)
lines(wl, quant[2,], type="l")
lines(wl, quant[3,], type="l")
lines(wl, quant[4,], type="l")
lines(wl, quant[5,], type="l")
polygon(c(wl, rev(wl)), c(quant[2,], rev(quant[1,])), col = "grey70")
polygon(c(wl, rev(wl)), c(quant[3,], rev(quant[2,])), col = "grey50")
polygon(c(wl, rev(wl)), c(quant[4,], rev(quant[3,])), col = "grey50")
polygon(c(wl, rev(wl)), c(quant[5,], rev(quant[4,])), col = "grey70")
axis(side = 1, pos = 0, las=1, cex.axis = 1.3)
if (ylab == TRUE){
axis(side = 2, las=1, pos=min(wl), cex.axis = 1.3)
mtext(side = 2, line = 3, 'Reflectance', cex=1.3)
}
} else {
plot(wl, quant[1,], type="l", ylim = c(0,ymax), xlim = c(min(wl)-10, max(wl)+10),
xaxs = "i", axes=F, ylab = NA, xlab=NA, las=1, cex.lab = 1.3)
lines(wl, quant[2,], type="l")
lines(wl, quant[3,], type="l")
lines(wl, quant[4,], type="l")
lines(wl, quant[5,], type="l")
polygon(c(wl, rev(wl)), c(quant[2,], rev(quant[1,])), col = "grey70")
polygon(c(wl, rev(wl)), c(quant[3,], rev(quant[2,])), col = "grey50")
polygon(c(wl, rev(wl)), c(quant[4,], rev(quant[3,])), col = "grey50")
polygon(c(wl, rev(wl)), c(quant[5,], rev(quant[4,])), col = "grey70")
axis(side = 2, pos=min(wl), labels=F, lwd.ticks=0)
if (ylab == TRUE){
axis(side = 2, las=1, pos=min(wl), cex.axis = 1.3)
mtext(side = 2, line = 3, 'Reflectance', cex=1.3)
}
}
}
##------------------------------------------------------------------------------##
## ##
## plots: the MRPP variable importance and select the values over 0.3 ##
## ##
## Arguments: ##
## - spectra spectral information. Used to create the quantiles of spectra ##
## - wl classificationEnsemble object ##
## ##
##------------------------------------------------------------------------------##
plot.importance <- function(varImport, wl, xaxis=TRUE, ...){
library(grDevices)
library(RColorBrewer)
# variables from ensemble
A <- varImport[1, ]
imp = A
imp[imp > 0.3] = 1 ; imp[imp < 0.3] = 0
imp <- as.logical(imp)
# matices of varImport
z1 <- matrix (rep (A, 100), ncol=100)
z1[,0:50] <- NA
# selection
z2 <- matrix (rep (imp, 100), ncol=100)
z2[z2==0] <- NA
z2[,50:100] <- NA
# add coefficients
blueish <- colorRampPalette(brewer.pal(9,"Blues"))(100)
# MRFF coefficients
if (xaxis == TRUE){
image(wl, seq(0, 100, 1), z1, xlim = c(min(wl)-10, max(wl)+10), xlab=expression(lambda(nm)), col=blueish, ylab="", axes=F, cex.lab = 1.3)
} else {
image(wl, seq(0, 100, 1), z1, xlim = c(min(wl)-10, max(wl)+10), xlab="", col=blueish, ylab="", axes=F, cex.lab = 1.3)
}
# selection
image(wl, seq(0, 100, 1), z2, xlim = c(min(wl)-10, max(wl)+10), col=1, ylab="", axes=F, add=T, cex.lab = 1.3)
if (xaxis == TRUE){
axis(side = 1, las=1, cex.axis = 1.3)
}
abline(h = 49.5)
box()
}
##----------------------------------------------------------------------------##
## ##
## significanceTest_CanopyLevel: Apply one-side bootstrap significance test to##
## canopy-level data ##
## ##
## Arguments: ##
## - data: input dataset ##
## - fitASD: classificationEnsemble ASD object ##
## - fitAISA: classificationEnsemble AISA object ##
## ##
## Function based on: ##
## Lopatin, J., Dolos, K., Hernández, H. J., Galleguillos, M., & Fassnacht, ##
## F. E. (2016). Comparing Generalized Linear Models and random forest to ##
## model vascular plant species richness using LiDAR data in a natural forest ##
## in central Chile. Remote Sensing of Environment, 173, 200-210. ##
## http://doi.org/10.1016/j.rse.2015.11.029 ##
## ##
##----------------------------------------------------------------------------##
significanceTest_CanopyLevel <- function(model1, model2){
### PLS
### compute the differences between ASD and AISA band settings
### OA of rip-it-off should be larger. So, if OA(m1) - OA(m2) is positive, rip-it-off is significantly better.
PLS_OA <- model1$OA_PLS - model2$OA_PL
### Kappa of rip-it-off should be larger. So, if OA(m1) - OA(m2) is positive, rip-it-off is significantly better.
PLS_kappa <- model1$Kappa_PLS - model2$Kappa_PLS
### RF
RF_OA <- model1$OA_RF - model2$OA_RF
RF_kappa <- model1$Kappa_RF - model2$Kappa_RF
### SVM
SVM_OA <- model1$OA_SVM - model2$OA_SVM
SVM_kappa <- model1$Kappa_SVM - model2$Kappa_SVM
# prepare output
output <- list(PLS_OA, RF_OA, SVM_OA, PLS_kappa, RF_kappa, SVM_kappa)
names(output) <- c("PLS_OA", "RF_OA", "SVM_OA", "PLS_kappa", "RF_kappa", "SVM_kappa")
class(output) <- "boot_test"
output
}
##----------------------------------------------------------------------------##
## ##
## Small functions to help in repetitive tasks ##
## ##
## ##
##----------------------------------------------------------------------------##
###############################################
## stratified bootstrap selection of samples ##
###############################################
# So each species get selected for training and validation
stratifySampling <- function(data, classes){
TRAIN <- list()
VAL <- list()
for (i in 1:length( levels(data$classes ))){
x = grep( levels(data$classes)[i], data$classes )
x <- data[x, ]
# random sampling
if ( length(x[, 1])==1 ){
TRAIN[[i]] <- x
VAL[[i]] <- x
}
else {
idx = sample(1:length(x[,1]), length(x[,1]), replace=TRUE)
TRAIN[[i]] <- x[idx, ]
VAL[[i]] <- x[-idx, ]
}
}
# unlist
TRAIN2 <- do.call("rbind", TRAIN)
VAL2 <- do.call("rbind", VAL)
# prepare exit
output <- list(TRAIN2, VAL2)
names(output) <- c("train", "validation")
output
}
###############################################
## List the names of the rasters in a folder ##
###############################################
rasterListNames <- function(fileExtantion, folder, dir=NULL){
# if dir = NULL, set it to "home" by default
if (is.null(dir)){
dir = home
}
setwd(dir)
# make a list of all fileExtantion files
rast_list = list.files(folder, pattern = fileExtantion)
# delete the ".dat" from the name
x = grep(".tif.aux.xml", rast_list)
if ( length(x) > 0 ){ rast_list <- rast_list[-x] }
rast_list = gsub('.{4}$', '', rast_list)
return(rast_list)
}
#####################################################
## List and load the rasters contained in a folder ##
#####################################################
rasterList <- function(fileExtantion, folder, dir=NULL, select=NULL){
# if dir = NULL, set it to "home" by default
if (is.null(dir)){
dir = home
}
setwd(dir)
# make a list of all fileExtantion files
rast_list = list.files(folder, pattern = fileExtantion)
x = grep(".tif.aux.xml", rast_list)
if ( length(x) > 0 ){ rast_list <- rast_list[-x] }
# select only rasters with a especific pattern
if (!is.null(select)){
rast_list <- rast_list[ grep(select, rast_list) ]
}
# raster names
rasterNames = gsub('.{4}$', '', rast_list)
# import rasters
setwd(file.path(dir, folder))
rasterlist <- list()
for(i in 1:length(rast_list)){
rast <- stack(rast_list[i])