forked from anitas-giraldo/GB_Habitat_Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.RF_DTV_dominant.R
698 lines (518 loc) · 20.7 KB
/
1.RF_DTV_dominant.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
########################################################
###### Script -- Random Forest - BRUVs data - v1.0 ##############
### Load libraries ----
library(ggplot2)
library(ggthemes)
library(cowplot)
library(randomForest)
library(sp)
library(rgdal)
library(raster)
library(caTools)
library(reshape2)
library(tidyr)
library(dplyr)
library(car)
library(lattice)
library(dplyr)
library(raster)
library(rasterVis)
library(zoo)
library(sf)
library(fields)
library(ROCR)
library(caret)
library(geoR)
library(gstat)
#library(elsa)
install.packages("corrplot")
library(corrplot)
library(broman)
library(VSURF)
# Clear memory ----
rm(list=ls())
### Set directories ----
w.dir<- dirname(rstudioapi::getActiveDocumentContext()$path)
d.dir <- paste(w.dir, "data", sep='/')
s.dir <- paste(w.dir, "spatial_data", sep='/')
p.dir <- paste(w.dir, "plots", sep='/')
o.dir <- paste(w.dir, "outputs", sep='/')
### Load data ----
df <- read.csv(paste(d.dir, "tidy", "GB_dtv_fine_bathy_habitat_dominant_broad.csv", sep='/'))
head(df)
str(df) # check the factors and the predictors
any(is.na(df)) # check for NA's in the data
which(is.na(df))
df <- na.omit(df)
str(df) # 2663 obs
p <- stack(paste(s.dir, "predictors.tif", sep='/'))
namesp <- read.csv(paste(s.dir, "namespredictors.csv", sep='/'))
namesp
names(p) <- namesp[,2]
names(p)
## Prepare data ----
# remove unneeded columns ---
names(df)
df2 <- df[,c(10, 14:22)] # remove X, sample, lat, long
head(df2)
# change name of class
names(df2)
colnames(df2)[colnames(df2)=="dominant"] <- "Class"
names(df2)
str(df2)
df2$Class <- as.factor(df2$Class)
levels(df2$Class) # "Consolidated" "Macroalgae" "total.seagrass" "Turf.algae" "Unconsolidated"
summary(df2)
head(df2)
## Plot predictors correlations by class -----
# matrix scatterplot of just these 13 variables --
scatterplotMatrix(df2[2:10], col = df2$Class)
plot(df2[2:10], col = df2$Class)
legend("center",
legend = levels(df2$Class))
## using corrplot ----
# compute correlation matrix --
C <- cor(df2[2:10], method = "pearson")
head(round(C,2))
# correlogram : visualizing the correlation matrix --
# http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram#:~:text=Correlogram%20is%20a%20graph%20of%20correlation%20matrix.&text=In%20this%20plot%2C%20correlation%20coefficients,corrplot%20package%20is%20used%20here.
#Positive correlations are displayed in blue and negative correlations in red color.
#Color intensity and the size of the circle are proportional to the correlation coefficients
corrplot(C, method="circle")
corrplot(C, method="pie")
corrplot(C, method="color")
corrplot(C, method="number", type = "upper")
corrplot(C, method="color", type = "lower", order="hclust") # “hclust” for hierarchical clustering order is used in the following examples
# compute the p-value of correlations --
# mat : is a matrix of data
# ... : further arguments to pass to the native R cor.test function
cor.mtest <- function(mat, ...) {
mat <- as.matrix(mat)
n <- ncol(mat)
p.mat<- matrix(NA, n, n)
diag(p.mat) <- 0
for (i in 1:(n - 1)) {
for (j in (i + 1):n) {
tmp <- cor.test(mat[, i], mat[, j], ...)
p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
}
}
colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
p.mat
}
# matrix of the p-value of the correlation
p.mat <- cor.mtest(df2[2:10])
head(p.mat[, 1:5])
# customize correlogram --
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot(C, method="color", col=col(100),
type="upper", order="hclust",
addCoef.col = "black", # Add coefficient of correlation
tl.col="black", tl.srt=45, #Text label color and rotation
# Combine with significance
p.mat = p.mat, sig.level = 0.01, insig = "blank",
# hide correlation coefficient on the principal diagonal
diag=FALSE
)
### Check Predicitor correlations ---
# define function mosthighlycorrelated --
# https://little-book-of-r-for-multivariate-analysis.readthedocs.io/en/latest/src/multivariateanalysis.html
# linear correlation coefficients for each pair of variables in your data set,
# in order of the correlation coefficient. This lets you see very easily which pair of variables are most highly correlated.
mosthighlycorrelated <- function(mydataframe,numtoreport)
{
# find the correlations
cormatrix <- cor(mydataframe)
# set the correlations on the diagonal or lower triangle to zero,
# so they will not be reported as the highest ones:
diag(cormatrix) <- 0
cormatrix[lower.tri(cormatrix)] <- 0
# flatten the matrix into a dataframe for easy sorting
fm <- as.data.frame(as.table(cormatrix))
# assign human-friendly names
names(fm) <- c("First.Variable", "Second.Variable","Correlation")
# sort and print the top n correlations
head(fm[order(abs(fm$Correlation),decreasing=T),],n=numtoreport)
}
mosthighlycorrelated(df2[2:10], 20) # This results in only depth, rough and slope 4 not being correlated above 0.95
## MAKE BETTER PLOT -- TO do still -----
### Get train and test data ----
set.seed(777)
sample <- caTools::sample.split(df2$flowdir, SplitRatio = 0.75)
train <- subset(df2, sample == TRUE)
test <-subset(df2, sample == FALSE)
dim(train) # [1] 1997 10
dim(test) # [1] 666 10
levels(train$Class) # "Consolidated" "Macroalgae" "total.seagrass" "Turf.algae" "Unconsolidated"
# remove classes if needed classes ----
#train <- train[train$Class != "Other",]
#train <- train[train$Class != "Stony.corals",]
train <- droplevels(train)
summary(train)
levels(train$Class)
#test <- test[test$Class != "Other",]
#test <- test[test$Class != "Stony.corals",]
test <- droplevels(test)
summary(test)
### MODEL 1 ----
### RF - 5 habitat classes ---
# this is using all the habitat classes = 5 in total
# Used only the preds that were not correlated: depth, tri, roughness
model <- randomForest(Class ~ ., data=train %>% mutate(Class = car::recode(Class, "c('Unconsolidated', 'Consolidated')='Unvegetated';'total.seagrass' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")),
ntree=1001, proximity=TRUE, mtry = 3)
#model <- randomForest(Class ~ ., data=train %>% select(c(Class, depth, tri, roughness)) , ntree=501, proximity=TRUE)
model # OOB = 41.63%
model$importance
model$classes
ptest <- p
names(ptest)
#ptest <- dropLayer(p, c(3:7,9))
## Predict ----
pred <- raster::predict(ptest, model)
## Plot ----
plot(pred)
e <- drawExtent()
testx <- crop(pred, e)
plot(testx)
# basic plot using lattice --
# https://pjbartlein.github.io/REarthSysSci/rasterVis01.html
lp <- levelplot(testx)
lp
#class(lp) # trellis
#### MODEL 2 ----
### RF - 5 habitat classes ---
# Using depth, tri and roughness ---
# remove all the Classes that are not SG or MA
levels(train$Class)
model2 <- randomForest(Class ~ ., data=train %>% mutate(Class = car::recode(Class, "c('Unconsolidated', 'Consolidated')='Unvegetated';'total.seagrass' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")) %>%
select(c(Class, depth, aspect4, roughness, tpi)),
ntree=1001, proximity=TRUE, mtry = 3, importance = TRUE)
#model2 <- randomForest(Class ~ ., data=train %>% select(c(Class, depth, tri, roughness)) , ntree=501, proximity=TRUE)
#model2 <- randomForest(Class ~ ., data=df2, ntree=501, proximity=T, mtry=3)
model2 # this is OOB = 56.94% for 2001 trees / OOB = 56.94% for 501 trees
model2$importance
# Predict ----
ptest <- p
names(ptest)
ptest <- dropLayer(p, c(2,3,5,7,9))
names(ptest)
pred <- raster::predict(ptest, model2)
# plot ----
plot(pred)
#e <- drawExtent()
e <- extent(115.1187, 115.5686 , -33.6169, -33.32534)
testx <- crop(pred, e)
plot(testx)
# save prediction ---
writeRaster(testx, paste(o.dir, "GBpred-Fine-DTV.tif", sep='/'))
# basic plot using lattice --
# https://pjbartlein.github.io/REarthSysSci/rasterVis01.html
# https://stat.ethz.ch/pipermail/r-sig-geo/2013-March/017893.html
#pick colors --
# <- brocolors("crayons")["Jungle Green"] # "#78dbe2"
#sg <- brocolors("crayons")["Forest Green"] # "#78dbe2"
sg <- brocolors("crayons")["Fern"] # "#78dbe2"
alg <- brocolors("crayons")["Raw Umber"] # "#1dacd6"
sand <- brocolors("crayons")["Unmellow Yellow"] # "#f75394"
# read gb cmr
gb <- readOGR(dsn="C:/Users/00093391/Dropbox/UWA/Research Associate/PowAnalysis_for1sParksMeeting/Desktop/shapefiles")
plot(gb)
lp <- levelplot(testx, col.regions=c(alg, sg, sand))
lp
class(lp) # trellis
# https://oscarperpinan.github.io/rastervis/FAQ.html
# plot without CMR--
lp2 <- levelplot(testx, col.regions=c(alg, sg, sand), xlab = list("Longitude", fontface = "bold"),
ylab = list("Latitude", fontface = "bold"))
lp2
trellis.device(device ="png", filename = paste(p.dir, "DTV-fine.png", sep='/'), width = 1000, height = 670, res = 200)
print(lp2)
dev.off()
# with the CMR polygon
lp3 <- levelplot(testx, col.regions=c(alg, sg, sand), xlab = list("Longitude", fontface = "bold"),
ylab = list("Latitude", fontface = "bold")) + layer(sp.polygons(gb))
lp3
#print(lp2)
trellis.device(device ="png", filename = paste(p.dir, "DTV-fine-CMR.png", sep='/'), width = 1000, height = 670, res = 200)
print(lp3)
dev.off()
### UP TO HERE ####
### MODEL 3 ----
### RF - 5 habitat classes ---
# this is using all the habitat classes = 5 in total
# Use only depth and tri
model3 <- randomForest(Class ~ ., data=train %>% select(c(Class, depth, tri)) , ntree=501, proximity=TRUE)
model3 # OOB = 53.21%
model3$importance
ptest <- p
names(ptest)
ptest <- dropLayer(p, c(2:6,8:9))
## Predict ----
pred <- raster::predict(ptest, model3)
## Plot ----
plot(pred)
e <- drawExtent()
testx <- crop(pred, e)
plot(testx)
# basic plot using lattice --
# https://pjbartlein.github.io/REarthSysSci/rasterVis01.html
lp <- levelplot(testx)
lp
class(lp) # trellis
### MODEL 4 ----
### RF - 3 habitat classes : unvegetated, seagrass, macroalgae ---
# Using all preds
# to manipulate factors: https://stackoverflow.com/questions/35088812/combine-sets-of-factors-in-a-dataframe-with-dplyr
model4 <- randomForest(Class ~ ., data=train %>% mutate(Class = car::recode(Class, "c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")),
ntree=1001, proximity=TRUE, mtry = 3)
model4 # OOB = 53.21%
model4$importance
# Remove predictors if needed --
#ptest <- p
#names(ptest)
#ptest <- dropLayer(p, c(9))
## Predict ----
pred <- raster::predict(p, model4)
## Plot ----
plot(pred)
e <- drawExtent()
testx <- crop(pred, e)
plot(testx)
# basic plot using lattice --
# https://pjbartlein.github.io/REarthSysSci/rasterVis01.html
lp <- levelplot(testx)
lp
#class(lp) # trellis
### MODEL 5 ----
### RF - 3 habitat classes : unvegetated, seagrass, macroalgae ---
# use all preds except flowdir
# to manipulate factors: https://stackoverflow.com/questions/35088812/combine-sets-of-factors-in-a-dataframe-with-dplyr
model5 <- randomForest(Class ~ ., data=train %>% mutate(Class = car::recode(Class, "c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")) %>%
select(-flowdir),
ntree=501, proximity=TRUE, mtry = 3)
model5 # OOB = 40.33%
model5$importance
# Remove predictors if needed --
ptest <- p
names(ptest)
ptest <- dropLayer(p, c(9))
## Predict ----
pred <- raster::predict(ptest, model5)
## Plot ----
plot(pred)
e <- drawExtent()
testx <- crop(pred, e)
plot(testx)
# basic plot using lattice --
# https://pjbartlein.github.io/REarthSysSci/rasterVis01.html
lp <- levelplot(testx)
lp
#class(lp) # trellis
### MODEL 6 ----
### RF - 3 habitat classes : unvegetated, seagrass, macroalgae ---
# use depth, aspect 4, tri and tpi
# to manipulate factors: https://stackoverflow.com/questions/35088812/combine-sets-of-factors-in-a-dataframe-with-dplyr
model6 <- randomForest(Class ~ ., data=train %>% mutate(Class = car::recode(Class, "c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")) %>%
select(Class, depth, aspect4, tri, tpi),
ntree=501, proximity=TRUE, mtry = 3)
model6 # OOB = 40.33%
model6$importance
# Remove predictors if needed --
ptest <- p
names(ptest)
ptest <- dropLayer(p, c(2:3,5,8,9))
## Predict ----
pred <- raster::predict(ptest, model6)
## Plot ----
plot(pred)
e <- drawExtent()
testx <- crop(pred, e)
plot(testx)
# basic plot using lattice --
# https://pjbartlein.github.io/REarthSysSci/rasterVis01.html
lp <- levelplot(testx)
lp
#class(lp) # trellis
### MODEL 7 ----
### RF - 3 habitat classes : unvegetated, seagrass, macroalgae ---
# use depth, aspect 4, tri and tpi
# to manipulate factors: https://stackoverflow.com/questions/35088812/combine-sets-of-factors-in-a-dataframe-with-dplyr
model7 <- randomForest(Class ~ ., data=train %>% mutate(Class = car::recode(Class, "c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")) %>%
select(Class, depth, aspect4, tpi, roughness),
ntree=501, proximity=TRUE, mtry = 3)
model7 # OOB = 39.84%
model7$importance
# Remove predictors if needed --
ptest <- p
names(ptest)
ptest <- dropLayer(p, c(2:3,5,7,9))
## Predict ----
pred <- raster::predict(ptest, model7)
## Plot ----
plot(pred)
e <- drawExtent()
testx <- crop(pred, e)
plot(testx)
# basic plot using lattice --
# https://pjbartlein.github.io/REarthSysSci/rasterVis01.html
lp <- levelplot(testx)
lp
#class(lp) # trellis
### MODEL 8 ----
# like model 6 but using the caret package
### RF - 3 habitat classes : unvegetated, seagrass, macroalgae ---
# Using all preds
# to manipulate factors: https://stackoverflow.com/questions/35088812/combine-sets-of-factors-in-a-dataframe-with-dplyr
# using different code --
# https://www.edureka.co/blog/random-forest-classifier/
# Training using ‘random forest’ algorithm
# Converting ‘Survived’ to a factor
train$Class <- factor(train$Class)
# Set a random seed
set.seed(51)
# had to use less classes, otherwise it wouldn't run, I think because not enough replicates per class
t=train %>% mutate(Class = car::recode(Class, "c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'"))
head(t)
TrainData <- t[,c(2,5,7,8)]
TrainClasses <- t[,1]
model8 <- caret::train(TrainData, TrainClasses, # Class is a function of the variables we decided to include
#data = train, # Use the train data frame as the training data
#preProcess = c("center", "scale"),
method = 'rf',# Use the 'random forest' algorithm
trControl = trainControl(method = 'cv', # Use cross-validation
search = 'random')) # Use 5 folds for cross-validation
model8
model8$finalModel
model8
#model7$importance
v<- varImp(model8, scale =F)
v
varImp(model8)
plot(v, top = 9)
# AREA UNDER THE CURVE --
roc_imp <- filterVarImp(x = train[, 2:10], y = train$Class)
roc_imp
# Remove predictors if needed --
ptest <- p
names(p)
ptest <- dropLayer(p, c(2,3,5,8,9))
names(ptest)
#ptest2 <- dropLayer(p, c(3:5,9))
#names(ptest2)
## Predict ----
pred <- raster::predict(ptest, model8)
pred2 <- raster::predict(ptest, model6)
## Plot ----
plot(pred)
e <- drawExtent()
testx <- crop(pred, e)
plot(testx)
plot(pred2)
e <- drawExtent()
testx2 <- crop(pred2, e)
plot(testx2)
# basic plot using lattice --
# https://pjbartlein.github.io/REarthSysSci/rasterVis01.html
lp <- levelplot(testx)
lp
lp2 <- levelplot(testx2)
lp2
#### Validation model 2: looking at confusion matrix ----
# model 6
#prediction_for_table <- raster::predict(model6, test[,-c(1,4:8,10)])
prediction_for_table2 <- raster::predict(model2, test %>% mutate(Class = car::recode(Class, "c('Unconsolidated', 'Consolidated')='Unvegetated';'total.seagrass' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")) %>%
select(c(Class, depth, aspect4,tpi, roughness)))
#table(observed=test[,-c(2:10)], predicted=prediction_for_table)
table(observed=test$Class %>%
car::recode("c('Unconsolidated', 'Consolidated')='Unvegetated';'total.seagrass' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'"),
predicted=prediction_for_table2)
# confusion matrix model 2 ----
caret::confusionMatrix(test$Class %>%
car::recode("c('Unconsolidated', 'Consolidated')='Unvegetated';'total.seagrass' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'"),
prediction_for_table2)
# Validation set assessment #2: ROC curves and AUC
# Needs to import ROCR package for ROC curve plotting:
install.packages("ROCR")
library(ROCR)
# Calculate the probability of new observations belonging to each class
# prediction_for_roc_curve will be a matrix with dimensions data_set_size x number_of_classes
prediction_for_roc_curve <- predict(model6,
test %>% mutate(Class = car::recode(Class, "c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")) %>%
select(c(Class, depth, aspect4, tri, tpi)),
type="prob")
# Plot ROC curve ----
# Use pretty colours:
pretty_colours <- c("#F8766D","#00BA38","#619CFF")
# Specify the different classes
classes <- levels(test$Class %>%
car::recode("c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'"))
# For each class
for (i in 1:3)
{
# Define which observations belong to class[i]
true_values <- ifelse(test$Class %>%
car::recode("c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")==classes[i],1,0)
# Assess the performance of classifier for class[i]
pred <- prediction(prediction_for_roc_curve[,i],true_values)
perf <- performance(pred, "tpr", "fpr")
if (i==1)
{
plot(perf,main="ROC Curve",col=pretty_colours[i])
}
else
{
plot(perf,main="ROC Curve",col=pretty_colours[i],add=TRUE)
}
# Calculate the AUC and print it to screen
auc.perf <- performance(pred, measure = "auc")
print([email protected])
}
# model 8 --
#prediction_for_table <- raster::predict(model6, test[,-c(1,4:8,10)])
prediction_for_table8 <- raster::predict(model8, test %>% mutate(Class = car::recode(Class, "c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")) %>%
select(c(Class, depth, aspect4, tri, tpi)))
#table(observed=test[,-c(2:10)], predicted=prediction_for_table)
table(observed=test$Class %>%
car::recode("c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'"),
predicted=prediction_for_table8)
# confusion matrix model 8 ----
caret::confusionMatrix(test$Class %>%
car::recode("c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'"),
prediction_for_table8)
# Validation set assessment #2: ROC curves and AUC
# Needs to import ROCR package for ROC curve plotting:
install.packages("ROCR")
library(ROCR)
# Calculate the probability of new observations belonging to each class
# prediction_for_roc_curve will be a matrix with dimensions data_set_size x number_of_classes
prediction_for_roc_curve <- predict(model8,
test %>% mutate(Class = car::recode(Class, "c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")) %>%
select(c(Class, depth, aspect4, tri, tpi)),
type="prob")
# Plot ROC curve ----
# Use pretty colours:
pretty_colours <- c("#F8766D","#00BA38","#619CFF")
# Specify the different classes
classes <- levels(test$Class %>%
car::recode("c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'"))
# For each class
for (i in 1:3)
{
# Define which observations belong to class[i]
true_values <- ifelse(test$Class %>%
car::recode("c('Unconsolidated', 'Consolidated')='Unvegetated';'Seagrasses' = 'Seagrass'; c('Turf.algae','Macroalgae')='Algae'")==classes[i],1,0)
# Assess the performance of classifier for class[i]
pred <- prediction(prediction_for_roc_curve[,i],true_values)
perf <- performance(pred, "tpr", "fpr")
if (i==1)
{
plot(perf,main="ROC Curve",col=pretty_colours[i])
}
else
{
plot(perf,main="ROC Curve",col=pretty_colours[i],add=TRUE)
}
# Calculate the AUC and print it to screen
auc.perf <- performance(pred, measure = "auc")
print([email protected])
}