Here are presented, in small and unlinked examples, secundary functions (starting with bm_[...]
) of biomod2
. Most of these functions are internally called by some main functions (starting with BIOMOD_[...]
) of biomod2
, but can be easily used as such.
## Generate a binary vector -------------------------------------------------------------
vec.a <- sample(c(0, 1), 100, replace = TRUE)
## Generate a 0-1000 vector (random drawing) --------------------------------------------
vec.b <- runif(100, min = 0, max = 1000)
## Generate a 0-1000 vector (biased drawing) --------------------------------------------
BiasedDrawing <- function(x, m1 = 300, sd1 = 200, m2 = 700, sd2 = 200) {
return(ifelse(x < 0.5, rnorm(1, m1, sd1), rnorm(1, m2, sd2)))
}
vec.c <- sapply(vec.a, BiasedDrawing)
vec.c[which(vec.c < 0)] <- 0
vec.c[which(vec.c > 1000)] <- 1000
## Generate a 0-1000 vector (normal distribution) ---------------------------------------
vec.d <- rnorm(100, 500, 100)
library(biomod2)
library(terra)
## Create simple simulated data ---------------------------------------------------------
myResp.s <- sample(c(0, 1), 20, replace = TRUE)
myExpl.s <- data.frame(var1 = sample(c(0, 1), 100, replace = TRUE),
var2 = rnorm(100),
var3 = 1:100)
## Create raster data -------------------------------------------------------------------
ras.1 <- ras.2 <- mask.out <- rast(nrows = 10, ncols = 10)
ras.1[] <- as.factor(rep(c(1, 2, 3, 4, 5), each = 20))
ras.2[] <- rnorm(100)
stk <- c(ras.1, ras.2)
names(stk) <- c("varFact", "varNorm")
## define a mask for already sampled points
mask.out[1:40] <- 1
## define a list of masks where we want to sample in priority
mask.in <- list(ras.1, ras.1)
mask.in[[1]][1:80] <- NA ## only level 5 should be sampled in this mask
mask.in[[1]][21:80] <- NA ## only levels 1 and 5 should be sampled in this mask
## Load real data ---------------------------------------------------------
data("DataSpecies")
myResp.r <- as.numeric(DataSpecies[, 'GuloGulo'])
data("bioclim_current")
myExpl.r <- rast(bioclim_current)
myRespXY <- DataSpecies[which(myResp.r == 1), c('X_WGS84', 'Y_WGS84')]
myResp.v <- reclassify(subset(myExpl.r, 1, drop = TRUE), c(-Inf, Inf, 0))
myResp.v[cellFromXY(myResp.v, myRespXY)] <- 1
bm_SampleBinaryVector(ref = vec.a, ratio = 0.7)
bm_FindOptimStat(metric.eval = 'TSS', obs = vec.a, fit = vec.b)
bm_FindOptimStat(metric.eval = 'TSS', obs = vec.a, fit = vec.c, nb.thresh = 100)
vec.d_bin <- bm_BinaryTransformation(data = vec.d, threshold = 500)
vec.d_filt <- bm_BinaryTransformation(data = vec.d, threshold = 500, do.filtering = TRUE)
cbind(vec.d, vec.d_bin, vec.d_filt)
bm_MakeFormula(resp.name = 'myResp.s',
expl.var = head(myExpl.s),
type = 'quadratic',
interaction.level = 0)
samp1 <- bm_SampleFactorLevels(expl.var = stk, mask.out = mask.out)
samp2 <- bm_SampleFactorLevels(expl.var = stk, mask.in = mask.in)
samp3 <- bm_SampleFactorLevels(expl.var = stk, mask.out = mask.out, mask.in = mask.in)
# Compute SRE for several quantile values
sre.100 <- bm_SRE(resp.var = myResp.v,
expl.var = myExpl.r,
new.env = myExpl.r,
quant = 0)
sre.095 <- bm_SRE(resp.var = myResp.v,
expl.var = myExpl.r,
new.env = myExpl.r,
quant = 0.025)
sre.090 <- bm_SRE(resp.var = myResp.v,
expl.var = myExpl.r,
new.env = myExpl.r,
quant = 0.05)
# Visualize results
res <- c(myResp.v, sre.100, sre.095, sre.090)
names(res) <- c("Original distribution", "Full data calibration", "Over 95 percent", "Over 90 percent")
plot(res, zlim = c(0, 1))
mod <- glm(var1 ~ var2 + var3, data = myExpl.s)
bm_VariablesImportance(bm.model = mod,
expl.var = myExpl.s[, c('var2', 'var3')],
method = "full_rand",
nb.rep = 3)