/
OneHundredBackground.R
84 lines (65 loc) · 2.59 KB
/
OneHundredBackground.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
#' @title Process module: OneHundredBackground
#'
#' @description Process module to generate up to 100 background records at random in
#' cells of ras and return these along with the presence only data.
#'
#' @param .data \strong{Internal parameter, do not use in the workflow function}. \code{.data} is a list of a data frame and a raster object returned from occurrence modules and covariate modules respectively. \code{.data} is passed automatically in workflow from the occurrence and covariate modules to the process module(s) and should not be passed by the user.
#' @param seed Numeric used with \code{\link[base]{set.seed}}
#'
#' @author ZOON Developers, \email{zoonproject@@gmail.com}
#' @section Version: 1.0
#' @section Date submitted: 2015-11-13
#'
#' @section Data type: presence-only
#'
#' @name OneHundredBackground
#' @family process
OneHundredBackground <- function (.data, seed = NULL) {
zoon:::GetPackage('dismo')
occurrence <- .data$df
ras <- .data$ras
# Keep attributes
Atts <- attributes(occurrence)[!names(attributes(occurrence)) %in% c('names', 'class', 'row.names')]
if (!all(occurrence$type == 'presence')) {
stop ('"OneHundredBackground" module only works for presence-only data')
}
# set seed if specified
if(!is.null(seed)){
if(inherits(x = seed, what = c('numeric', 'integer'))){
set.seed(seed)
} else {
stop("'seed' must be numeric or NULL")
}
}
# generate pseudo-absence data
points <- 100
if(ncell(ras) < 100){
points <- ncell(ras)
message(paste0('There are fewer than 100 cells in the environmental raster.',
'\nUsing all available cells (', ncell(ras), ') instead'))
}
pa <- randomPoints(ras, points)
npres <- nrow(occurrence)
npabs <- nrow(pa)
# extract covariates
occ_covs <- as.matrix(extract(ras, occurrence[, c('longitude', 'latitude')]))
pa_covs <- as.matrix(extract(ras, pa))
covs <- rbind(occ_covs, pa_covs)
# combine with the occurrence data
df <- data.frame(value = rep(c(1, 0),
c(npres, npabs)),
type = rep(c('presence', 'background'),
c(npres, npabs)),
fold = rep(1, npres + npabs),
longitude = c(occurrence$lon, pa[, 1]),
latitude = c(occurrence$lat, pa[, 2]),
covs)
names(df)[6:ncol(df)] <- names(ras)
attributes(df) <- c(attributes(df), Atts)
attr(df, 'covCols') <- names(ras)
# remove missing values
if(NROW(na.omit(df)) > 0){
df <- na.omit(df)
}
return(list(df=df, ras=ras))
}