-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataAnalysis.R
510 lines (365 loc) · 13.8 KB
/
DataAnalysis.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
####
# The following code reproduces results of the article:
#
# Heim, Wright, Carnegie, Taylor, Scarth and Oldeland, 2019. Using drones and
# multispectral imagery to detect myrtle rust on a lemon myrtle plantation. Drones
#
# The code is split in two major sections according to our research questions.
#
# A) Is it possible to accurately discriminate fungicide-treated and untreated
# sunlit plants at canopy-level
#
# B) What are important spectral regions for the classification?
####
# Before we start to analyse the data we set up our working environment.
dir.create('output', FALSE, FALSE)
# install.packages(c("rgdal",
# "raster",
# "reshape2",
# "ggplot2",
# "caret",
# "e1071",
# "gdata",
# "rasterVis",
# "corrplot"))
library(rgdal)#extract reflectance values
library(corrplot)#test if predictor vars are correlated
library(raster)#extract reflectance values
library(ggplot2)#figure 3
library(caret)#machine learning library
library(gdata)#required for drop_class()
library(reshape2)#for prep_gg function, reshaping spectra to use ggplot2
library(rasterVis)#modify risk map (Part D)
library(VSURF)#feature selection
source("R/FUN_drop_cat_var.R")#drops factor and according factor level
source("R/FUN_extract_polyclass.R")#extract reflectance and index values
source("R/FUN_prepggwide2long.R")#reshape spectral data for ggplot
source("R/FUN_VSURF_table.R")#create feature selection output
# PART A------------------------Cleaning Data-----------------------------------
# Loading image brick.
img <-
brick("data/20180528_ortho_ground.tif")
# Convert from DN to reflectance.
img <- img/65535
# Calculate indices and rename bands for clarity (MicaSense RedEdge Camera)
names(img) <- c("blue", "green", "red", "re",
"nir", "alpha")
ndvi <- (img$nir-img$red)/(img$nir+img$red)
gr <- img$green/img$red
sipi <- (img$nir-img$blue)/(img$nir-img$red)
ari <- (1/img$green)-(1/img$re)
img <- addLayer(img, c(ndvi, gr, sipi, ari))
names(img) <- c("blue", "green", "red", "re",
"nir", "alpha", "ndvi", "gr", "sipi", "ari")
# Loading QGIS shape file where sample polygons have been defined.
allclasses <- shapefile("data/samplepolygons.shp")
# Check what classes are contained
unique(allclasses@data$Class)
# Extract reflectance values from pixels based on sample polygons.
allclasses.df<- extract_polyclass(allclasses, img)
# Check if all classes were imported
unique(allclasses.df$Class)
# Check for NAs
apply(allclasses.df, 2, function(x) any(is.na(x)))
#full.df.wona <- na.omit(full.df) If NAs contained, they can be removed.
summary(allclasses.df$Class)
#summary(full.df.wona$Class)
#summary(full.df$Class)-summary(full.df.wona$Class)
allclasses.noalpha.df <-
allclasses.df[ , -which(names(allclasses.df) %in% c("alpha"))] #remove alpha
# Write full data to .csv and reload with new name for classification.
write.csv(allclasses.noalpha.df,
'output/droneclassif_plusSHD.csv',
row.names = FALSE)
classif.allclasses <- read.csv("output/droneclassif_plusSHD.csv",
check.names = FALSE)
# Preparing data for a second, mixed shadow, classification
SHDmixdata <- shapefile("data/samplepolygons_noSHD.shp")
unique(SHDmixdata@data$Class) # To check what classes are contained
SHDmixdata.df<- extract_polyclass(SHDmixdata, img)
unique(SHDmixdata.df$Class)
# Check for NAs
apply(SHDmixdata.df, 2, function(x) any(is.na(x)))
#full.df.wona <- na.omit(full.df)
summary(SHDmixdata.df$Class)
#summary(full.df.wona$Class)
#summary(full.df$Class)-summary(full.df.wona$Class)
SHDmixdata.noalpha.df <-
SHDmixdata.df[ , -which(names(SHDmixdata.df) %in% c("alpha"))]
# Write mix shadow data to .csv and reload with new name for classification.
write.csv(SHDmixdata.noalpha.df,
'output/droneclassif_mixSHD.csv',
row.names = FALSE)
classif.mixSHD <- read.csv("output/droneclassif_mixSHD.csv",
check.names = FALSE)
# Plot Figure 3 (Multispectral signatures - TR, UN, SHD)
fig3 <- classif.allclasses[,1:6]
names(fig3) <- c("Type","475", "560", "668", "717", "840")
spectragg <- prep_gg(fig3, agg = TRUE)
C <- ggplot(spectragg, aes(Wavelength, Reflectance*100, colour = Type)) +
geom_line(aes(linetype=Type), size = 1)+
geom_point(aes(shape=Type), size = 2)+
scale_color_manual(values=c("#525252", "#00CD00", "#cd5c00"))+
labs(x = "", y = "")+
theme_minimal(base_size=20)+
theme(legend.title = element_blank(),
legend.key.width = unit(1.5, "cm"))+
annotate("rect",
xmin = 455,
xmax = 495,
ymin = -Inf,
ymax = Inf,
alpha = .2,
fill = 'blue'
) +
annotate(
"rect",
xmin = 540,
xmax = 580,
ymin = -Inf,
ymax = Inf,
alpha = .2,
fill = 'green'
) +
annotate(
"rect",
xmin = 658,
xmax = 678,
ymin = -Inf,
ymax = Inf,
alpha = .2,
fill = c("#FF2626")
) +
annotate(
"rect",
xmin = 707,
xmax = 727,
ymin = -Inf,
ymax = Inf,
alpha = .2,
fill = c("#7A0A0A")
) +
annotate(
"rect",
xmin = 800,
xmax = 880,
ymin = -Inf,
ymax = Inf,
alpha = .2,
fill = c("lightgrey")
) +
annotate(
"text",
x = 475,
y = 80,
label = "B",
fontface = "bold",
size = 5
) +
annotate(
"text",
x = 560,
y = 80,
label = "G",
fontface = "bold",
size = 5
) +
annotate(
"text",
x = 668,
y = 80,
label = "R",
fontface = "bold",
size = 5
) +
annotate(
"text",
x = 717,
y = 80,
label = "RE",
fontface = "bold",
size = 5
) +
annotate(
"text",
x = 840,
y = 80,
label = "NIR",
fontface = "bold",
size = 5
)+
labs(x = "Wavelength [nm]", y = "Reflectance [%]")
ggsave("output/Figure3.allspectra.png",
plot = C,
width = 40,
height = 20,
units = "cm",
dpi = 400
)
# PART B------------------Random Forest Classification--------------------------
# All Classes (TR, UN, SHD) ----------------------------------------------------
set.seed(2019)
# Create test and training data
inTraining <- createDataPartition(classif.allclasses$Class,
p = .75,
list = FALSE)
train <- classif.allclasses[ inTraining,]
test <- classif.allclasses[-inTraining,]
# Define random forest model training parameters
rfControl <- trainControl(
method = "boot",
number = 100)
rfGrid <- expand.grid(mtry = seq(1, ncol(train)-1, 1))
# Train random forest model
rfFit <- train(Class ~ ., data = train,
method = "rf",
importance = TRUE, ntree=500,
trControl = rfControl, tuneGrid = rfGrid,
metric = "Accuracy", maximize = TRUE)
# Validate random forest model on test data
rfPred <-
predict.train(rfFit, test[, !names(test) %in% c("Class")], type = "raw")
# Store relevant random forest results in a list() container for proper output
RFdataall <-
list(fit = rfFit,
pred = predict.train(rfFit,
test[, !names(test) %in% c("Class")],
type = "raw"),
confusion = confusionMatrix(rfPred, test$Class),
varImp = varImp(rfFit, scale = TRUE))
# Use sink() to export list() container contents as text file
sink(file = 'output/I_allclasses_RFclassif_report.txt')
RFdataall
sink()
# Save list() container as R data object to not have to rerun analysis
saveRDS(RFdataall, 'output/I_allclasses_RFclassif_object.rds')
#RFdataall <- readRDS("output/I_allclasses_RFclassif_object.rds")
# Mix Shadow (TR_S, UN_S) ------------------------------------------------------
# Create test and training data
inTraining <- createDataPartition(classif.mixSHD$Class, p = .75, list = FALSE)
train <- classif.mixSHD[ inTraining,]
test <- classif.mixSHD[-inTraining,]
# Train random forest model
rfFit <- train(Class ~ ., data = train,
method = "rf",
importance = TRUE, ntree=500,
trControl = rfControl, tuneGrid = rfGrid,
metric = "Accuracy", maximize = TRUE)
# Validate random forest model on test data
rfPred <-
predict.train(rfFit, test[, !names(test) %in% c("Class")], type = "raw")
# Store relevant random forest results in a list() container for proper output
RFdatamix <-
list(fit = rfFit,
pred = predict.train(rfFit,
test[, !names(test) %in% c("Class")],
type = "raw"),
confusion = confusionMatrix(rfPred, test$Class),
varImp = varImp(rfFit, scale = TRUE))
# Use sink() to export list() container contents as text file
sink(file = 'output/II_mix_RFclassif_report.txt')
RFdatamix
sink()
# Save list() container as R data object to not have to rerun analysis
saveRDS(RFdata, 'output/II_mix_RFclassif_object.rds')
#RFdatamix <- readRDS("output/II_mix_RFclassif_object.rds")
# Only UN and TR ---------------------------------------------------------------
# Drop shadow class
classif.TRUN <- drop_class(classif.allclasses,
classif.allclasses$Class,
"SHD")
# Create test and training data
inTraining <- createDataPartition(classif.TRUN$Class, p = .75, list = FALSE)
train <- classif.TRUN[ inTraining,]
test <- classif.TRUN[-inTraining,]
# Train random forest model
rfFit <- train(Class ~ ., data = train,
method = "rf",
importance = TRUE, ntree=500,
trControl = rfControl, tuneGrid = rfGrid,
metric = "Accuracy", maximize = TRUE)
# Validate random forest model on test data
rfPred <-
predict.train(rfFit, test[, !names(test) %in% c("Class")], type = "raw")
# Store relevant random forest results in a list() container for proper output
RFdata_TRUN <-
list(fit = rfFit,
pred = predict.train(rfFit,
test[, !names(test) %in% c("Class")],
type = "raw"),
confusion = confusionMatrix(rfPred, test$Class),
varImp = varImp(rfFit, scale = TRUE)) #38
# Use sink() to export list() container contents as text file
sink(file = 'output/III_onlyTRUN_RFclassif_report.txt')
RFdata_TRUN
sink()
# Save list() container as R data object to not have to rerun analysis
saveRDS(RFdata_TRUN, 'output/III_onlyTRUN_RFclassif_object.rds') #40
#RFdata_TRUN <- readRDS("output/III_onlyTRUN_RFclassif_object.rds") #41
# PART C----------------------Feature Selection---------------------------------
# Test for all data sets whether predictor variables are correlated.
corr <- round(cor(classif.allclasses[,2:10]), 2)
corr2 <- round(cor(classif.mixSHD[,2:10]), 2)
corr3 <- round(cor(classif.TRUN[,2:10]), 2)
corrplot.mixed(corr)
corrplot.mixed(corr2)
corrplot.mixed(corr3)
# NOTE: As they are correlated, the VSURF package is used for feature
# selection to confirm random forest feature selection.
set.seed(201903)
fs.I <- VSURF(classif.allclasses[,2:10],
classif.allclasses[,1],
clusterType = "FORK",
ntree = 500,mtry = 4)
fs.II <- VSURF(classif.mixSHD[,2:10],
classif.mixSHD[,1],
clusterType = "FORK",
ntree = 500,mtry = 4)
fs.III <- VSURF(classif.TRUN[,2:10],
classif.TRUN[,1],
clusterType = "FORK",
ntree = 500,mtry = 4)
# NOTE: Warning message can be ignored. set mtry = best model reported in
# RFdata_report.txt.
# Save feature selection results as R data object to not have to rerun if requ.
saveRDS(fs.I, 'output/I_fs_allRFdata.rds')
saveRDS(fs.II, 'output/II_fs_mixRFdata.rds')
saveRDS(fs.III, 'output/III_fs_TRUNRFdata.rds')
# Use VSURF_table() to create nice feature selection output.
varimp_I<- VSURF_table(fs.I, classif.allclasses[,-1])
varimp_II<- VSURF_table(fs.II, classif.mixSHD[,-1])
varimp_III<- VSURF_table(fs.III, classif.TRUN[,-1])
# Write VSURF_table() objects as .csv file for manuscript use.
write.csv(varimp_I, 'output/varimp.all_I_RFdata.csv', row.names = TRUE)
write.csv(varimp_II, 'output/varimp.mix_II_RFdata.csv', row.names = TRUE)
write.csv(varimp_III, 'output/varimp.TRUN_III_RFdata.csv', row.names = TRUE)
# PART D------------------Creating a Disease Risk Map --------------------------
# Import raster file similar to initial raster but without any ground pixel.
imgpred <- brick("data/20180528_ortho_no_ground.tif")
# Convert DN into reflectance
imgpred <- imgpred/65535
# Rename raster layers for clarity (must be identical with initial layer names)
names(imgpred) <- c("blue", "green", "red", "re", "nir", "alpha")
ndvi.p <- (imgpred$nir-imgpred$red)/(imgpred$nir+imgpred$red)
gr.p <- imgpred$green/imgpred$red
sipi.p <- (imgpred$nir-imgpred$blue)/(imgpred$nir-imgpred$red)
ari.p <- (1/imgpred$green)-(1/imgpred$re)
imgpred <- addLayer(imgpred, c(ndvi.p, gr.p, sipi.p, ari.p))
names(imgpred) <- c("blue", "green", "red", "re",
"nir", "alpha", "ndvi", "gr", "sipi", "ari")
# Remove alpha channel
riskpre <- imgpred[[c(1,2,3,4,5,7,8,9,10)]]
names(riskpre)
# Start predicting pixel values (risk map) based on random forest model
riskpred <- predict(riskpre, RFdataall$fit)
plot(riskpred)
currentDate <- Sys.Date()
rstFileName <- paste("output/riskmap",currentDate,".tif",sep="")
writeRaster(riskpred,
file=rstFileName,
format = "GTiff",
bylayer=TRUE,
overwrite=TRUE)
# Note: The exported risk map was further processed in QGIS to change class
# colours and have it ready for publication
#END