/
sample_pseudoabs.R
352 lines (326 loc) · 13.1 KB
/
sample_pseudoabs.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
#' Sample pseudo-absences
#'
#' @description This function provide several methods for sampling pseudo-absences, for instance
#' totally random sampling method, with the options of using different environmental and or geographical constraints.
#'
#' @param data data.frame or tibble. Database with presences
#' (or presence-absence, or presences-pseudo-absence) records, and coordinates
#' @param x character. Column name with spatial x coordinates
#' @param y character. Column name with spatial y coordinates
#' @param n integer. Number of pseudo-absences to be sampled
#' @param method character. Pseudo-absence allocation method. It is necessary to
#' provide a vector for this argument. The methods implemented are:
#' \itemize{
#' \item random: Random allocation of pseudo-absences throughout the area used for model fitting. Usage method='random'.
#' \item env_const: Pseudo-absences are environmentally constrained to regions with lower suitability values predicted by a Bioclim model. For this method, it is necessary to provide a raster stack or brick object with environmental variables Usage method=c(method='env_const', env = somevar).
#' \item geo_const: Pseudo-absences are allocated far from occurrences based on a geographical buffer. A value of the buffer width in m must be provided if raster (used in rlayer) has a longitude/latitude CRS, or in map units in other cases. Usage method=c('geo_const', width='50000').
#' \item geo_env_const: Pseudo-absences are constrained environmentally (based on Bioclim model) and distributed geographically far from occurrences based on a geographical buffer. For this method, a raster with environmental variables stored as SpatRaster object should be provided. A value of the buffer width in m must be provided if raster (used in rlayer) has a longitude/latitude CRS, or in map units in other cases. Usage method=c('geo_env_const', width='50000', env = somevar).
#' \item geo_env_km_const: Pseudo-absences are constrained using a three-level procedure; it is similar to the geo_env_const with an additional step which distributes the pseudo-absences in environmental space using k-means cluster analysis. For this method, it is necessary to provide a raster stack or brick object with environmental variables and a value of the buffer width in m if raster (used in rlayer) has a longitude/latitude CRS, or map units in other cases. Usage method=c('geo_env_km_const', width='50000', env = somevar).
#' }
#'
#' @param rlayer SpatRaster. A raster layer used for sampling pseudo-absence
#' A layer with the same resolution and extent that environmental variables that will be used for modeling is recommended. In the case use maskval argument, this raster layer must contain the values used to constrain sampling
#' @param maskval integer, character, or factor. Values of the raster layer used for constraining the pseudo-absence sampling
#' @param calibarea SpatVector A SpatVector which delimit the calibration area used for a given species (see \code{\link{calib_area}} function).
#' @param sp_name character. Species name for which the output will be used.
#' If this argument is used, the first output column will have the species name. Default NULL.
#'
#' @importFrom dplyr %>% select mutate
#' @importFrom stats na.exclude kmeans
#' @importFrom terra mask ext extract match as.data.frame
#'
#' @return A tibble object with x y coordinates of sampled pseudo-absence points
#'
#' @export
#'
#' @seealso \code{\link{sample_background}} and \code{\link{calib_area}}.
#'
#' @examples
#' \dontrun{
#' require(terra)
#' require(dplyr)
#' data("spp")
#'
#' somevar <- system.file("external/somevar.tif", package = "flexsdm")
#' somevar <- terra::rast(somevar)
#'
#' regions <- system.file("external/regions.tif", package = "flexsdm")
#' regions <- terra::rast(regions)
#'
#' plot(regions)
#'
#'
#' single_spp <-
#' spp %>%
#' dplyr::filter(species == "sp3") %>%
#' dplyr::filter(pr_ab == 1) %>%
#' dplyr::select(-pr_ab)
#'
#'
#' # Pseudo-absences randomly sampled throughout study area
#' ps1 <-
#' sample_pseudoabs(
#' data = single_spp,
#' x = "x",
#' y = "y",
#' n = nrow(single_spp) * 10,
#' method = "random",
#' rlayer = regions,
#' maskval = NULL,
#' sp_name = "sp3"
#' )
#' plot(regions, col = gray.colors(9))
#' points(single_spp[-1], col = "blue", cex = 0.7, pch = 19) # presences
#' points(ps1[-1], col = "red", cex = 0.7, pch = 19) # absences
#'
#'
#' # Pseudo-absences randomly sampled within a regions where a species occurs
#' ## Regions where this species occurrs
#' samp_here <- terra::extract(regions, single_spp[2:3])[, 2] %>%
#' unique() %>%
#' na.exclude()
#'
#' ps1 <-
#' sample_pseudoabs(
#' data = single_spp,
#' x = "x",
#' y = "y",
#' n = nrow(single_spp) * 10,
#' method = "random",
#' rlayer = regions,
#' maskval = samp_here
#' )
#'
#' plot(regions, col = gray.colors(9))
#' points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
#' points(ps1, col = "red", cex = 0.7, pch = 19)
#'
#'
#' # Pseudo-absences sampled with geographical constraint
#' ps1 <-
#' sample_pseudoabs(
#' data = single_spp,
#' x = "x",
#' y = "y",
#' n = nrow(single_spp) * 10,
#' method = c("geo_const", width = "30000"),
#' rlayer = regions,
#' maskval = samp_here
#' )
#' plot(regions, col = gray.colors(9))
#' points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
#' points(ps1, col = "red", cex = 0.7, pch = 19)
#'
#' # Pseudo-absences sampled with environmental constraint
#' ps1 <-
#' sample_pseudoabs(
#' data = single_spp,
#' x = "x",
#' y = "y",
#' n = nrow(single_spp) * 10,
#' method = c("env_const", env = somevar),
#' rlayer = regions,
#' maskval = samp_here
#' )
#' plot(regions, col = gray.colors(9))
#' points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
#' points(ps1, col = "red", cex = 0.7, pch = 19)
#'
#' # Pseudo-absences sampled with environmental and geographical constraint
#' ps1 <-
#' sample_pseudoabs(
#' data = single_spp,
#' x = "x",
#' y = "y",
#' n = nrow(single_spp) * 10,
#' method = c("geo_env_const", width = "50000", env = somevar),
#' rlayer = regions,
#' maskval = samp_here
#' )
#' plot(regions, col = gray.colors(9))
#' points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
#' points(ps1, col = "red", cex = 0.7, pch = 19)
#'
#' # Pseudo-absences sampled with environmental and geographical constraint and with k-mean clustering
#' ps1 <-
#' sample_pseudoabs(
#' data = single_spp,
#' x = "x",
#' y = "y",
#' n = nrow(single_spp) * 10,
#' method = c("geo_env_km_const", width = "50000", env = somevar),
#' rlayer = regions,
#' maskval = samp_here
#' )
#' plot(regions, col = gray.colors(9))
#' points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
#' points(ps1, col = "red", cex = 0.7, pch = 19)
#'
#' # Sampling pseudo-absence using a calibration area
#' ca_ps1 <- calib_area(
#' data = single_spp,
#' x = "x",
#' y = "y",
#' method = c("buffer", width = 50000),
#' crs=crs(somevar)
#' )
#' plot(regions, col = gray.colors(9))
#' plot(ca_ps1, add = T)
#' points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
#'
#' ps1 <-
#' sample_pseudoabs(
#' data = single_spp,
#' x = "x",
#' y = "y",
#' n = nrow(single_spp) * 50,
#' method = "random",
#' rlayer = regions,
#' maskval = NULL,
#' calibarea = ca_ps1
#' )
#' plot(regions, col = gray.colors(9))
#' plot(ca_ps1, add = T)
#' points(ps1, col = "red", cex = 0.7, pch = 19)
#' points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
#'
#'
#' ps1 <-
#' sample_pseudoabs(
#' data = single_spp,
#' x = "x",
#' y = "y",
#' n = nrow(single_spp) * 50,
#' method = "random",
#' rlayer = regions,
#' maskval = samp_here,
#' calibarea = ca_ps1
#' )
#' plot(regions, col = gray.colors(9))
#' plot(ca_ps1, add = T)
#' points(ps1, col = "red", cex = 0.7, pch = 19)
#' points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
#' }
sample_pseudoabs <- function(data, x, y, n, method, rlayer, maskval = NULL, calibarea = NULL, sp_name = NULL) {
. <- ID <- NULL
if (!any(c(
"random",
"env_const",
"geo_const",
"geo_env_const",
"geo_env_km_const"
) %in% method)) {
stop(
"argument 'method' was misused, available methods random, env_const, geo_const, geo_env_const, and geo_env_km_const"
)
}
rlayer <- rlayer[[1]]
data <- data[, c(x, y)]
if (!is.null(calibarea)) {
rlayer <- rlayer %>% terra::crop(., calibarea) %>% terra::mask(., calibarea)
}
# Random method
if (any(method %in% "random")) {
cell_samp <- sample_background(data = data, x = x, y = y, method = "random", n = n, rlayer = rlayer, maskval = maskval)
}
# env_const method
if (any(method == "env_const")) {
if (is.na(method["env"])) {
stop("Provide a environmental stack/brick variables for env_const method, \ne.g. method = c('env_const', env=somevar)")
}
env <- method[["env"]]
# Test extent
if (!all(as.vector(terra::ext(env)) %in% as.vector(terra::ext(rlayer)))) {
message("Extents do not match, raster layers used were croped to minimum extent")
df_ext <- data.frame(as.vector(terra::ext(env)), as.vector(terra::ext(rlayer)))
e <- terra::ext(apply(df_ext, 1, function(x) x[which.min(abs(x))]))
env <- crop(env, e)
rlayer <- crop(rlayer, e)
}
# Restriction for a given region
envp <- inv_bio(e = env, p = data[, c(x, y)])
envp <- terra::mask(rlayer, envp)
cell_samp <- sample_background(data = data, x = x, y = y, method = "random", n = n, rlayer = envp, maskval = maskval)
}
# geo_const method
if (any(method == "geo_const")) {
if (!"width" %in% names(method)) {
stop("Provide a width value for 'geo_const' method, \ne.g. method=c('geo_const', width='50000')")
}
# Restriction for a given region
envp <- inv_geo(e = rlayer, p = data[, c(x, y)], d = as.numeric(method["width"]))
cell_samp <- sample_background(data = data, x = x, y = y, method = "random", n = n, rlayer = envp, maskval = maskval)
}
# geo_env_const method
if (any(method == "geo_env_const")) {
if (!all(c("env", "width") %in% names(method))) {
stop("Provide a width value and environmental stack/brick variables for 'geo_env_const' method, \ne.g. method=c('geo_env_const', width='50000', env=somevar)")
}
env <- method[["env"]]
# Test extent
if (!all(as.vector(terra::ext(env)) %in% as.vector(terra::ext(rlayer)))) {
message("Extents do not match, raster layers used were croped to minimum extent")
df_ext <- data.frame(as.vector(terra::ext(env)), as.vector(terra::ext(rlayer)))
e <- terra::ext(apply(df_ext, 1, function(x) x[which.min(abs(x))]))
env <- crop(env, e)
rlayer <- crop(rlayer, e)
}
# Restriction for a given region
envp <- inv_geo(e = rlayer, p = data[, c(x, y)], d = as.numeric(method["width"]))
envp2 <- inv_bio(e = env, p = data[, c(x, y)])
envp <- (envp2 + envp)
rm(envp2)
envp <- terra::mask(rlayer, envp)
cell_samp <- sample_background(data = data, x = x, y = y, method = "random", n = n, rlayer = envp, maskval = maskval)
}
# geo_env_km_const
if (any(method == "geo_env_km_const")) {
if (!all(c("env", "width") %in% names(method))) {
stop("Provide a width value and environmental stack/brick variables for 'geo_env_km_const' method, \ne.g. method=c('geo_env_const', width='50000', env=somevar)")
}
env <- method[["env"]]
# Test extent
if (!all(as.vector(terra::ext(env)) %in% as.vector(terra::ext(rlayer)))) {
message("Extents do not match, raster layers used were croped to minimum extent")
df_ext <- data.frame(as.vector(terra::ext(env)), as.vector(terra::ext(rlayer)))
e <- terra::ext(apply(df_ext, 1, function(x) x[which.min(abs(x))]))
env <- crop(env, e)
rlayer <- crop(rlayer, e)
}
# Restriction for a given region
envp <- inv_geo(e = rlayer, p = data[, c(x, y)], d = as.numeric(method["width"]))
envp2 <- inv_bio(e = env, p = data[, c(x, y)])
envp <- (envp2 + envp)
envp <- terra::mask(rlayer, envp)
rm(envp2)
if (!is.null(maskval)) {
if (is.factor(maskval)) {
maskval <-
which(levels(maskval) %in% as.character(maskval))
rlayer <- rlayer * 1
}
filt <- terra::match(rlayer, maskval)
rlayer <- terra::mask(rlayer, filt)
rm(filt)
}
envp <- terra::mask(rlayer, envp)
# K-mean procedure
env_changed <- terra::mask(env, envp)
env_changed <- terra::as.data.frame(env_changed, xy = TRUE)
env_changed <- stats::na.exclude(env_changed)
suppressWarnings(km <- stats::kmeans(env_changed, centers = n))
cell_samp <- km$centers[, 1:2] %>% data.frame()
val <- terra::extract(envp, cell_samp, method = "simple", xy = TRUE) %>%
dplyr::select(-c(ID, x, y))
cell_samp <-
cell_samp %>% dplyr::mutate(val = val[, 1])
cell_samp <- cell_samp[!is.na(cell_samp$val), -3]
cell_samp <- dplyr::tibble(cell_samp)
cell_samp$pr_ab <- 0
}
colnames(cell_samp) <- c(x, y, "pr_ab")
if(!is.null(sp_name)){
cell_samp <- tibble(sp=sp_name, cell_samp)
}
return(cell_samp)
}