-
Notifications
You must be signed in to change notification settings - Fork 1
/
QRF_comparison_approaches.R
299 lines (239 loc) · 11.9 KB
/
QRF_comparison_approaches.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
#------------------------------------------------------------------------------
# Name: QRF_comparison_approaches.R
# (QRF = quantile regression forest)
#
# Content: - train QRF model and predict newdata with a small representative
# dataset to find out which pkgs etc. to use based on:
# - can models be trained using cross-validation grouped by location?
# - can the predict() fnc yield not just average predictions but also
# quantiles?
#
# Refs: - QRF package and vignettes:
# https://cran.r-project.org/web/packages/quantregForest/quantregForest.pdf
# https://mran.microsoft.com/snapshot/2015-07-15/web/packages/quantregForest/vignettes/quantregForest.pdf
# - Cross validation strategies for spatio-temporal data using "CAST" pkg:
# https://cran.r-project.org/web/packages/CAST/vignettes/CAST-intro.html#model-training-and-prediction
# https://cran.r-project.org/web/packages/CAST/vignettes/AOA-tutorial.html
#
# Inputs: -
#
# Output: -
#
# Runtime: -
#
# Project: BIS+
# Author: Anatol Helfenstein
# Updated: January 2021
#------------------------------------------------------------------------------
### empty memory and workspace; load required packages ----------------------
gc()
rm(list=ls())
pkgs <- c("tidyverse", "raster", "quantregForest", "ranger", "caret", "CAST", "foreach")
lapply(pkgs, library, character.only = TRUE)
### Prepare modelling data --------------------------------------------------
# locate, read in and stack covariates to predict over
v_cov_names <- dir("out/data/covariates/final_stack",
pattern = "\\.grd$", recursive = TRUE)
ls_r_cov <- foreach(cov = 1:length(v_cov_names)) %do%
raster(paste0("out/data/covariates/final_stack/", v_cov_names[[cov]]))
r_stack_cov <- stack(ls_r_cov)
# read in sample data: regression matrix of soil pH
tbl_regmat_target <- read_rds("out/data/model/tbl_regmat_pH_KCl_factor.Rds")
# decrease size of dataset for toy example
tbl_regmat_cal <- tbl_regmat_target %>%
filter(split %in% "train") %>%
.[1:800,]
tbl_regmat_val <- tbl_regmat_target %>%
filter(split %in% "test") %>%
.[1:200,]
### General QRF parameter definition and set up --------------------------------
# QRF parameters:
# - mtry: sets number of variables to try for each split when growing the tree;
# same default is used as in randomForest (one third of the number of predictors)
# - nodesize: fixes minimal number of instances in each terminal node, determining
# how many observations at least lie in the same node (default = 10)
# - ntree: how many trees are grown in RF on which QRF are based on;
# empirical evidence suggests that performance of prediction remains good even
# when using only few trees (default = 100 trees)
# set seed to control randomness of cross-validation (CV)
set.seed(10)
# To prevent overfitting the model and improving separate & independent validation
# results later on, we make use of the "CAST" pkg of Hanna Meyer, embedded in a
# "caret" framework. This allows machine learning (ML) for space-time data,
# for which models should be fit differently to non-spatio-temporal data...
# here, we use a 5-fold CV grouped by location (site_id) using CAST::CreatSpacetimeFolds()
indices <- CreateSpacetimeFolds(x = tbl_regmat_cal,
spacevar = "site_id",
k = 5)
# prepare table of predictors
tbl_predictors <- tbl_regmat_cal %>%
dplyr::select(-(split:hor), -(pH_KCl)) %>%
as.data.frame() # "setting row names on tibble is deprecated"
# set mtry to 1/3 of # or predictors
n_mtry <- tbl_predictors %>%
ncol()/3 %>%
round(., 0) # not sure why round does not work???
n_mtry <- round(n_mtry)
### Fit QRF model using "quantregForest" pkg -----------------------------------
# set seed to control randomness of model training/fitting
set.seed(10)
# train QRF
system.time(
qrf_fit_quantreg <- quantregForest(x = tbl_predictors,
y = tbl_regmat_cal$pH_KCl,
nthreads = 10L, # get NAs with > 10 threads
keep.inbag = TRUE,
importance = TRUE,
quantiles = c(0.05,0.5,0.95),
ntree = 500,
mtry = n_mtry)
)
# time elapse 10 cores: 15 sec
# if newdata = NULL, predict() performs out-of-bag (OOB) prediction on dataset,
# i.e. for each of grown trees prediction for data points which were not used for
# fitting tree is done (no new data is involved)
# Per default only one observation per node is used for prediction. This can be
# set with the input argument all with default all=FALSE (one observation per
# node used) and when setting all=TRUE, all observation per node are used. The
# use of only one observation per node is of advantage especially when working
# with large datasets since the algorithm can be very slow otherwise. Numerical
# experiments suggest that the performance remains good.
# In cases with big datasets & few new sample points as input for newdata,
# setting all = FALSE may be significantly slower than choosing all = TRUE.
# When newdata (test set) is very large, use all=FALSE (default) since much faster.
# control randomness of prediction (not sure if this is necessary for prediction?)
set.seed(10)
# OOB predictions using quantregForest approach
qrf_OOB_quantreg <- predict(qrf_fit_quantreg,
newdata = NULL,
what = c(0.05, 0.5, 0.95),
all = FALSE,
obs = 1)
# predict independent test dataset (LSK or CCNL)
qrf_test_quantreg <- predict(qrf_fit_quantreg,
newdata = tbl_regmat_val %>%
dplyr::select(-(split:hor), -pH_KCl),
what = c(0.05, 0.5, 0.95),
all = FALSE,
obs = 1)
### Fit QRF model using "ranger" pkg -------------------------------------------
set.seed(10)
# train QRF
system.time(
qrf_fit_ranger <- ranger(x = tbl_predictors,
y = tbl_regmat_cal$pH_KCl,
num.tree = 500,
mtry = n_mtry,
importance = "permutation", # cannot choose both options
write.forest = TRUE,
keep.inbag = TRUE,
quantreg = TRUE, # this changes it to QRF instead of RF
oob.error = TRUE,
num.threads = 40L,
verbose = TRUE)
)
# time elapse 40 cores: 3 sec
# OOB predictions using ranger approach
qrf_OOB_ranger <- predict(qrf_fit_ranger,
data = NULL,
type = "quantiles",
quantiles = c(0.05, 0.5, 0.95),
verbose = TRUE)
# test predictions using ranger approach
qrf_test_ranger <- predict(qrf_fit_ranger,
data = tbl_regmat_val %>%
dplyr::select(-(split:hor), -pH_KCl),
type = "quantiles",
quantiles = c(0.05, 0.5, 0.95),
verbose = TRUE)
### Tune QRF model with "CAST" & "caret" & use either "quantregForest" or "ranger"
### ----------------------------------------------------------------------------
set.seed(10)
# train model using caret::train()
# using quantregForest() fnc of Meinhausen pkg
system.time(
qrf_fit_quantreg_caret <- train(x = tbl_predictors,
y = tbl_regmat_cal$pH_KCl,
method = "qrf",
ntree = 500,
importance = TRUE,
keep.inbag = TRUE,
nthreads = 10L,
quantiles = c(0.05,0.5,0.95),
tuneGrid = data.frame("mtry" = n_mtry),
trControl = trainControl(method = "cv",
index = indices$index))
) # time elapse 10 cores: 41 sec
# make sure there are no NAs
qrf_fit_quantreg_caret$finalModel$predicted %>%
is.na() %>%
unique() # should be FALSE
# fit a QRF model using ranger
system.time(
qrf_fit_ranger_caret <- train(x = tbl_predictors,
y = tbl_regmat_cal$pH_KCl,
method = "ranger",
num.trees = 500,
importance = "permutation",
#write.forest = TRUE,
keep.inbag = TRUE,
quantreg = TRUE, # this changes it to QRF instead of RF
oob.error = TRUE,
num.threads = 40L,
#quantiles = c(0.05,0.5,0.95),
tuneGrid = data.frame("mtry" = n_mtry,
"splitrule" = "variance", # use default
"min.node.size" = 5), # use default
trControl = trainControl(method = "cv",
index = indices$index))
) # time elapse 40 cores: 14 sec
# make sure there are no NAs
qrf_fit_ranger_caret$finalModel$predictions %>%
is.na() %>%
unique() # should be FALSE
# OOB predictions using ranger approach
qrf_OOB_ranger_caret <- predict(qrf_fit_ranger_caret$finalModel,
data = NULL,
type = "quantiles",
quantiles = c(0.05, 0.5, 0.95),
verbose = TRUE)
# test predictions using ranger approach
qrf_test_ranger_caret <- predict(qrf_fit_ranger_caret$finalModel,
data = tbl_regmat_val %>%
dplyr::select(-(split:hor), -pH_KCl),
type = "quantiles",
quantiles = c(0.05, 0.5, 0.95),
verbose = TRUE)
### Compare different approaches -----------------------------------------------
# compare summaries of calibrated models (model fit/training)
qrf_fit_quantreg
qrf_fit_ranger
qrf_fit_ffs
### Use CAST's forward feature selection to remove covariates ------------------
set.seed(10)
# use CAST's forward feature selection method to remove variables that cause overfitting!
system.time(
qrf_fit_ffs <- ffs(predictors = tbl_regmat_cal %>%
dplyr::select(-(split:hor), -pH_KCl) %>%
as.data.frame(), # "setting row names on tibble is deprecated"
response = tbl_regmat_cal$pH_KCl,
metric = "Rsquared",
method = "ranger",
num.trees = 500,
importance = "permutation",
#write.forest = TRUE,
keep.inbag = TRUE,
quantreg = TRUE, # this changes it to QRF instead of RF
oob.error = TRUE,
num.threads = 48L,
tuneGrid = data.frame("mtry" = 2,
"splitrule" = "variance", # use default
"min.node.size" = 5), # use default
trControl = trainControl(method = "cv",
index = indices$index),
withinSE = TRUE,
# to favour models with < variables & probably shorten calc time
verbose = TRUE)
)
# Just for this toy example with 800 obs and 183 predictors 33K models need to be fit
# takes several days, probably a week!!!