-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNE.R
387 lines (312 loc) · 12.5 KB
/
MNE.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
#' BeeTool SDM script
# Libraries ----
library(terra)
library(magrittr)
library(fs)
library(readr)
library(dplyr)
library(purrr)
library(stringr)
library(fuzzySim)
box::use(./sdm/utils)
# Environment config ----
set.seed(1049)
# Load config
config <- config::get()
# Preprocessing ----
## Load data ----
if (config$regional$use_regional_cutoff) {
regional_data <- vect(config$regional$shapefile_region_path)
}
## Read args ----
# args = commandArgs(trailingOnly = TRUE)
# if (length(args) == 0) {
# stop("Please enter a single parameter (input file).\n", call. = FALSE)
# } else if (length(args) == 1) {
# print(paste("Processing model for file ", args[1]))
# } else {
# stop("Single parameter is needed (input file).\n", call. = FALSE)
# }
#
# inputDataFile <- args[1]
input_data_file <- "./data/BOMHUN.csv"
output_folder <- input_data_file %>%
path_file() %>%
path_ext_remove()
if (!dir_exists(output_folder)) {
dir_create(output_folder)
}
### Load occurrence data ----
crs_wgs84 <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
occs_data <- read_csv(input_data_file)
# Coordinates columns are X and Y
occs_data <- vect(
as.matrix(select(occs_data, X, Y)),
crs=crs_wgs84,
atts=as.data.frame(select(occs_data, -X, -Y))
)
### Create records datasets ----
# Sample records
sampled_occs_data <- utils$sample_occurrences(
occs_data,
grid_res = config$spatial_resolution)
utils$write_points(
sampled_occs_data,
path_join(c(output_folder, "data_clean.csv"))
)
## Select regional data of interest ----
regions_of_interest <- NULL
if (config$regional$use_regional_cutoff) {
regions_of_interest <- extract(regional_data, sampled_occs_data)
}
## Load covariables ----
if (config$covariables$is_worldclim) {
covar_file_list <- str_c(
config$covariables$path,
"/wc2.1_30s_",
config$covariables$variables,
".tif")
}
covar_rasters <- rast(covar_file_list)
if (!is.null(regions_of_interest)) {
covar_rasters <- crop(covar_rasters, ext(regions_of_interest))
}
sampled_occs_data_covar <- terra::extract(
covar_rasters,
sampled_occs_data,
bind=TRUE)
## Tal vez sea necesario que se quiten los puntos que tengan valores NA
utils$write_points(
sampled_occs_data_covar,
path_join(c(output_folder, "data_clean_covar.csv"))
)
# Variable selection ----
# Add presence variable
presence_col <- "presence"
sampled_occs_data_covar[,presence_col] <- 1
covar_names <- names(covar_rasters)
covar_selection <- corSelect(
data = sampled_occs_data_covar,
sp.cols = presence_col,
var.cols = covar_names
)
# TODO: create a function to save results of corSelect
# Old ----
####SELECCION DE VARIABLES####
# speciesCol <- match("Presence", names(occsData))
varCols <- ncol(occsData) + 1
correlacion <- corSelect(
data = covarData@data,
sp.cols = speciesCol,
var.cols = varCols:ncol(covarData),
cor.thresh = 0.8,
use = "pairwise.complete.obs"
)
select_var <- correlacion$selected.vars
write(select_var, file = file.path(outputFolder, "selected_variables.txt"))
selectedVariables <- enviromentalVariables[[select_var]]
selectedVariablesAOI <- enviromentalVariablesAOI[[select_var]]
# Selects the M of the species, base on Olson´s ecoregions
# Download: https://www.worldwildlife.org/publications/terrestrial-ecoregions-of-the-world
# Intersects the occurrence data with polygons
ecoregionsOfInterest <- sp::over(occsData, regionalizacion) %>%
filter(!is.na(ECO_ID))
idsEcoRegions <- unique(ecoregionsOfInterest$ECO_ID)
polygonsOfInterest <- regionalizacion[regionalizacion$ECO_ID %in% idsEcoRegions, ]
writeOGR(polygonsOfInterest, layer = 'ecoregionsOI', outputFolder, driver="ESRI Shapefile")
# Mask present rasters with ecoregions of interest
selectedVariablesCrop <- raster::crop(selectedVariables, polygonsOfInterest)
env <- raster::mask(selectedVariablesCrop,
polygonsOfInterest) #Species variables delimited by M
dir.create(file.path(outputFolder, "Presente"))
writeRaster(env,
file.path(outputFolder, "Presente/.tif"),
bylayer = T, suffix='names',
overwrite = TRUE)
#### Calibration ####
# Divides your data into trainining and test data sets. 70/30 %
sampleDataPoints <- sample.int(
nrow(covarData),
size = floor(0.7*nrow(covarData))
)
selectedValues <- rep(0, nrow(covarData)) %>% inset(sampleDataPoints, 1)
covarData$isTrain <- selectedValues
write.csv(cbind(covarData@data, coordinates(covarData)), file = file.path(outputFolder,
paste0(outputFolder,
"_",
"presencias",
".csv")),
row.names = FALSE)
# MAXENT calibration
# We used ENMeval package to estimate optimal model complexity (Muscarrella et al. 2014)
# Modeling process, first separate the calibration and validation data
occsCalibracion <- covarData %>%
as.data.frame() %>%
dplyr::filter(isTrain == 1) %>%
dplyr::select(Long, Lat)
write.csv(occsCalibracion, file = file.path(outputFolder,paste0(outputFolder,
"_","Calibracion",".csv")),
row.names = FALSE)
occsValidacion <- covarData %>%
as.data.frame() %>%
dplyr::filter(isTrain == 0) %>%
dplyr::select(Long, Lat)
write.csv(occsValidacion, file = file.path(outputFolder,paste0(outputFolder,
"_","Validacion",".csv")),
row.names = FALSE)
# Background
bg.df <- dismo::randomPoints(env[[1]], n = 10000) %>% as.data.frame()
#Divide backgeound into train and test
sample.bg <- sample.int(
nrow(bg.df),
size = floor(0.7*nrow(bg.df))
)
selectedValues.bg <- rep(0, nrow(bg.df)) %>% inset(sample.bg, 1)
bg.df$isTrain <- selectedValues.bg
sp::coordinates(bg.df) <- c("x", "y")
sp::proj4string(bg.df) <- crs.wgs84
bg.dfbio <- raster::extract(enviromentalVariables, bg.df)
bg.df<-as.data.frame(bg.df)
bg.dfbio <- cbind(bg.df, bg.dfbio) %>% as.data.frame()
write.csv(bg.dfbio, file = file.path(outputFolder, paste0(outputFolder,
"_",
"background_points",
".csv")))
#training background
bg.df.cal <- bg.df %>%
dplyr::filter(isTrain == 1) %>%
dplyr::select(x, y)
write.csv(bg.df.cal, file = file.path(outputFolder,paste0(outputFolder,
"_","back_calibracion",".csv")),
row.names = FALSE)
#testing back
bg.df.val <- bg.df %>%
dplyr::filter(isTrain == 0) %>%
dplyr::select(x, y)
write.csv(bg.df.val, file = file.path(outputFolder,paste0(outputFolder,
"_","back_validacion",".csv")),
row.names = FALSE)
# ENMeval
sp.models <- ENMevaluate(occsCalibracion, env, bg.df.cal, RMvalues = seq(0.5, 4, 0.5),
fc = c("L", "LQ", "H", "LQH", "LQHP", "LQHPT"),
method = "randomkfold", kfolds = 4, bin.output = TRUE,
parallel = TRUE, numCores = parallel::detectCores()-1,
updateProgress = TRUE)
resultados_enmeval <- sp.models@results
saveRDS(sp.models@models,
file=file.path(outputFolder,"Maxent_models.Rds"))
write.csv(resultados_enmeval,
file = file.path(outputFolder, "enmeval_results.csv"),
row.names = FALSE)
sp.models_p<-sp.models@predictions
dir.create(file.path(outputFolder, "Outputs_todos"))
writeRaster(sp.models_p, file = file.path(outputFolder, "Outputs_todos/", paste0(outputFolder)),
suffix='names',
format = "GTiff",
bylayer=TRUE,
overwrite= TRUE)
# delta_aic <- which(resultados_enmeval$delta.AICc == 0)
modelsAIC0 <- resultados_enmeval %>%
mutate(index = rownames(resultados_enmeval)) %>%
filter(delta.AICc == 0) %>%
select(index, settings) %>%
mutate(index = as.numeric(index), settings = as.character(settings))
aic.opt <- sp.models@models[[which(sp.models@results$delta.AICc==0)]]
importa <- var.importance(aic.opt)
write.csv( importa,
file = file.path(outputFolder, "varImportance.csv"),
row.names = FALSE)
# save species niche (raw output) model over raster
saveRasterWithSettings <- function(models, predictions, prefix) {
raster::writeRaster(predictions[[models["settings"]]],
file.path(outputFolder, paste0(prefix,
models["settings"],
".tif")),
overwrite = TRUE)
}
apply(modelsAIC0, 1, saveRasterWithSettings,
predictions = sp.models@predictions, prefix = "ENM_prediction_M_raw_")
####ENMTest####
#source("funciones_LAE.R")
#Threslhold independent
#AUC
aucCalculator <- function(prediction, occs, bgPoints) {
data <- rbind(occs, setNames(bgPoints, names(occs)))
labels <- c(rep(1, nrow(occs)),
rep(0, nrow(bgPoints)))
scores <- raster::extract(prediction, data)
pred <- ROCR::prediction(scores, labels)
# perf <- performance(pred, "tpr", "fpr")
auc <- performance(pred, "auc")@y.values[[1]]
return(auc)
}
aucStatistcs <- function(model, models, env, occs, bgPoints) {
result <- apply(model, 1, function(x, models, env, occs, bgPoints){
choicedModel <- models[[as.integer(x["index"])]]
prediction <- dismo::predict(choicedModel, env)
auc <- aucCalculator(prediction, occs, bgPoints)
return(c(x["settings"], auc))
},
models = models,
env = env,
occs = occs,
bgPoints = bgPoints)
result <- data.frame(
matrix(unlist(result), nrow = nrow(model), byrow = TRUE),
stringsAsFactors = FALSE
)
names(result) <- c("settings", "AUC")
result <- result %>% mutate(AUC = as.numeric(AUC))
return(result)
}
# Testing background
bg.df.test <- bg.df %>%
dplyr::filter(isTrain == 0) %>%
dplyr::select(x, y)
resultsAUC <- aucStatistcs(modelsAIC0, sp.models@models, env, occsValidacion, bg.df.test)
write.csv(resultsAUC,
file = file.path(outputFolder, "data_auc.csv"),
row.names = FALSE)
#### Projections ####
# predict choicemodel over current climate variables
predictAndSave <- function(model, models, data, prefix, occs) {
choicedModel <- models[[as.integer(model["index"])]]
predictions <- dismo::predict(choicedModel, data)
raster::writeRaster(predictions,
file.path(outputFolder, paste0(prefix,
"log_",
model["settings"],
"_",
outputFolder,
"_",
".tif")),
overwrite = TRUE)
#Threshold prection using minimum traning (min) and 10 percentil (q10) values
occsValues <- raster::extract(predictions, occs)
minValOcc <- min(occsValues, na.rm = TRUE)
raster::writeRaster(reclassify(predictions,
c(-Inf, minValOcc, 0, minValOcc, Inf, 1)),
file.path(outputFolder, paste0(prefix,
"bin_min_",
model["settings"],
"_",
outputFolder,
"_",
".tif")),
overwrite = TRUE)
q10ValOcc <- quantile(occsValues, 0.1, na.rm = TRUE)
raster::writeRaster(reclassify(predictions,
c(-Inf, q10ValOcc, 0, q10ValOcc, Inf, 1)),
file.path(outputFolder, paste0(prefix,
"bin_q10_",
model["settings"],
"_",
outputFolder,
"_",
".tif")),
overwrite = TRUE)
}
# log
apply(modelsAIC0, 1, predictAndSave,
models = sp.models@models, data = env, prefix = "ENM_",
occs = occsCalibracion)