-
Notifications
You must be signed in to change notification settings - Fork 10
/
ET_CP_pred.R
206 lines (165 loc) · 6.68 KB
/
ET_CP_pred.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
# Stacked predictions of Ethiopia cropland observations
# M. Walsh, October 2017
# Required packages
# install.packages(c("devtools","caret","plyr","doParallel")), dependencies=TRUE)
suppressPackageStartupMessages({
require(devtools)
require(caret)
require(plyr)
require(doParallel)
require(dismo)
})
# Data setup --------------------------------------------------------------
# Run this first: https://github.com/mgwalsh/Geosurvey/blob/master/ET_GS_data.R
# or run ...
# SourceURL <- "https://raw.githubusercontent.com/mgwalsh/blob/master/ET_GS_data.R"
# source_url(SourceURL)
rm(list=setdiff(ls(), c("gsdat","grids","glist"))) ## scrub extraneous objects in memory
# set calibration/validation set randomization seed
seed <- 1385321
set.seed(seed)
# split data into calibration and validation sets
gsIndex <- createDataPartition(gsdat$CP, p = 4/5, list = FALSE, times = 1)
gs_cal <- gsdat[ gsIndex,]
gs_val <- gsdat[-gsIndex,]
# GeoSurvey calibration labels
cp_cal <- gs_cal$CP ## Croplands present? (Y/N)
# Raster calibration features
gf_cal <- gs_cal[,9:39] ## grid covariates
# Random forest <randomForest> --------------------------------------------
require(randomForest)
# start doParallel to parallelize model fitting
mc <- makeCluster(detectCores())
registerDoParallel(mc)
# control setup
set.seed(1385321)
tc <- trainControl(method = "cv", classProbs = TRUE,
summaryFunction = twoClassSummary, allowParallel = T)
# model training
tg <- expand.grid(mtry=seq(1, 10, by=1))
CP.rf <- train(gf_cal, cp_cal,
preProc = c("center","scale"),
method = "rf",
ntree = 501,
metric = "ROC",
tuneGrid = tg,
trControl = tc)
# model outputs & predictions
print(CP.rf) ## ROC's accross tuning parameters
plot(varImp(CP.rf)) ## relative variable importance
confusionMatrix(CP.rf) ## cross-validation performance
cprf.pred <- predict(grids, CP.rf, type = "prob") ## spatial predictions
stopCluster(mc)
# Generalized boosting <gbm> ----------------------------------------------
require(gbm)
# start doParallel to parallelize model fitting
mc <- makeCluster(detectCores())
registerDoParallel(mc)
# control setup
set.seed(1385321)
tc <- trainControl(method = "cv", classProbs = TRUE, summaryFunction = twoClassSummary,
allowParallel = T)
# model training
CP.gb <- train(gf_cal, cp_cal,
method = "gbm",
preProc = c("center", "scale"),
trControl = tc,
metric = "ROC")
# model outputs & predictions
print(CP.gb) ## ROC's accross tuning parameters
plot(varImp(CP.gb)) ## relative variable importance
confusionMatrix(CP.gb) ## cross-validation performance
cpgb.pred <- predict(grids, CP.gb, type = "prob") ## spatial predictions
stopCluster(mc)
# Neural network <nnet> ---------------------------------------------------
require(nnet)
# start doParallel to parallelize model fitting
mc <- makeCluster(detectCores())
registerDoParallel(mc)
# control setup
set.seed(1385321)
tc <- trainControl(method = "cv", classProbs = TRUE,
summaryFunction = twoClassSummary, allowParallel = T)
# model training
CP.nn <- train(gf_cal, cp_cal,
method = "nnet",
preProc = c("center","scale"),
trControl = tc,
metric ="ROC")
# model outputs & predictions
print(CP.nn) ## ROC's accross tuning parameters
plot(varImp(CP.nn)) ## relative variable importance
confusionMatrix(CP.nn) ## cross-validation performance
cpnn.pred <- predict(grids, CP.nn, type = "prob") ## spatial predictions
stopCluster(mc)
# Regularized regression <glmnet> -----------------------------------------
require(glmnet)
# start doParallel to parallelize model fitting
mc <- makeCluster(detectCores())
registerDoParallel(mc)
# control setup
set.seed(1385321)
tc <- trainControl(method = "repeatedcv", number=5, classProbs = TRUE,
summaryFunction = twoClassSummary, allowParallel = T)
# model training
CP.rr <- train(gf_cal, cp_cal,
method = "glmnet",
family = "binomial",
preProc = c("center","scale"),
trControl = tc,
metric ="ROC")
# model outputs & predictions
print(CP.rr) ## ROC's accross tuning parameters
plot(varImp(CP.rr)) ## relative variable importance
confusionMatrix(CP.rr) ## cross-validation performance
cprr.pred <- predict(grids, CP.rr, type = "prob") ## spatial predictions
stopCluster(mc)
# Model stacking setup ----------------------------------------------------
preds <- stack(1-cprf.pred, 1-cpgb.pred, 1-cpnn.pred, 1-cprr.pred)
names(preds) <- c("rf","gb", "nn","rr")
plot(preds, axes=F)
# extract model predictions at validation locations
coordinates(gs_val) <- ~x+y
projection(gs_val) <- projection(preds)
gspred <- extract(preds, gs_val)
gspred <- as.data.frame(cbind(gs_val, gspred))
# stacking model validation labels and features
cp_val <- gspred$CP ## subset validation labels
gf_val <- gspred[,40:43] ## subset validation features
# Model stacking ----------------------------------------------------------
# start doParallel to parallelize model fitting
mc <- makeCluster(detectCores())
registerDoParallel(mc)
# control setup
set.seed(1385321)
tc <- trainControl(method = "repeatedcv", repeats = 5, classProbs = TRUE,
summaryFunction = twoClassSummary, allowParallel = T)
# model training
CP.st <- train(gf_val, cp_val,
method = "glmnet",
family = "binomial",
metric = "ROC",
trControl = tc)
# model outputs & predictions
print(CP.st)
confusionMatrix(CP.st)
plot(varImp(CP.st))
cpst.pred <- predict(preds, CP.st, type = "prob") ## spatial predictions
plot(1-cpst.pred, axes=F)
stopCluster(mc)
# Receiver-operator characteristics ---------------------------------------
cp_pre <- predict(CP.st, gf_val, type="prob")
cp_val <- cbind(cp_val, cp_pre)
cpp <- subset(cp_val, cp_val=="Y", select=c(Y))
cpa <- subset(cp_val, cp_val=="N", select=c(Y))
cp_eval <- evaluate(p=cpp[,1], a=cpa[,1]) ## calculate ROC on test set
plot(cp_eval, 'ROC') ## plot ROC curve
# Generate cropland mask --------------------------------------------------
t <- threshold(cp_eval) ## calculate thresholds based on ROC
r <- matrix(c(0, t[,2], 0, t[,2], 1, 1), ncol=3, byrow=TRUE) ## set threshold value <spec_sens>
mask <- reclassify(1-cpst.pred, r) ## reclassify stacked predictions
plot(mask, axes=F)
# Write prediction files --------------------------------------------------
cppreds <- stack(preds, 1-cpst.pred, mask)
names(cppreds) <- c("cprf","cpgb","cpnn","cprr","cpst","cpmk")
writeRaster(cppreds, filename="./Results/ET_cppreds_2017.tif", datatype="FLT4S", options="INTERLEAVE=BAND", overwrite=T)