## Not run:
# # based on examples in the dismo package
#
# # get predictor variables
# library(dismo)
# predictor.files <- list.files(path=paste(system.file(package="dismo"), '/ex', sep=''),
# pattern='grd', full.names=TRUE)
# predictors <- stack(predictor.files)
# # subset based on Variance Inflation Factors
# predictors <- subset(predictors, subset=c("bio5", "bio6",
# "bio16", "bio17", "biome"))
# predictors
# predictors
# predictors@title <- "base"
#
#
# # presence points
# # presence points
# presence_file <- paste(system.file(package="dismo"), '/ex/bradypus.csv', sep='')
# pres <- read.table(presence_file, header=TRUE, sep=',')[,-1]
#
# # choose background points
# ext <- extent(-90, -32, -33, 23)
# background <- randomPoints(predictors, n=1000, ext=ext, extf = 1.00)
#
# # if desired, change working directory where subfolders of "models" and
# # "ensembles" will be created
# # raster layers will be saved in subfolders of /models and /ensembles:
# getwd()
#
# # first calibrate the ensemble
# # calibration is done in two steps
# # in step 1, a k-fold procedure is used to determine the weights
# # in step 2, models are calibrated for all presence and background locations
# # factor is not used as it is not certain whether correct levels will be used
# # it may therefore be better to use dummy variables
#
# # step 1: 4-fold cross-validation
# ensemble.calibrate.step1 <- ensemble.test.splits(x=predictors, p=pres, a=background,
# ext=ext,
# k=4,
# layer.drops=c("biome"),
# SINK=TRUE, species.name="Bradypus",
# MAXENT=1, GBM=1, GBMSTEP=0, RF=1, GLM=1, GLMSTEP=1, GAM=1,
# GAMSTEP=1, MGCV=1, MGCVFIX=1, EARTH=1, RPART=1, NNET=1, FDA=1,
# SVM=1, SVME=1, BIOCLIM=1, DOMAIN=1, MAHAL=0,
# ENSEMBLE.tune=TRUE, PROBIT=TRUE,
# ENSEMBLE.best=0, ENSEMBLE.exponent=c(1, 2, 4, 6, 8),
# ENSEMBLE.min=0.7,
# Yweights="BIOMOD", factors=c("biome"),
# PLOTS=FALSE, formulae.defaults=TRUE,
# GBMSTEP.learning.rate=0.002)
#
# # step 2: create the models that will be used for the raster predictions
# # models with input.weights < 0.05 are excluded
#
# output.weights <- ensemble.calibrate.step1$output.weights.AUC
# output.weights[output.weights < 0.05] <- 0
# output.weights
#
# ensemble.calibrate.step2 <- ensemble.test(x=predictors, p=pres, a=background,
# ext=ext,
# layer.drops=c("biome"),
# SINK=TRUE, species.name="Bradypus",
# models.keep=TRUE,
# input.weights=output.weights,
# AUC.weights=FALSE, ENSEMBLE.tune=FALSE, PROBIT=TRUE,
# Yweights="BIOMOD", factors=c("biome"),
# PLOTS=FALSE, formulae.defaults=TRUE,
# GBMSTEP.learning.rate=0.002)
#
# # step 3: use previously calibrated models
# # re-evaluate the created maps at presence and background locations
# # (note that re-evaluation will be different due to rescaling of model results)
# ensemble.nofactors1 <- ensemble.raster(xn=predictors, ext=ext,
# models.list=ensemble.calibrate.step2$models,
# input.weights=output.weights,
# thresholds=ensemble.calibrate.step2$models$thresholds,
# SINK=TRUE, evaluate=TRUE,
# RASTER.species.name="Bradypus", RASTER.stack.name="base")
#
# # use the base map to check for changes in suitable habitat
# # this type of analysis is typically done with different predictor layers
# # (for example, predictor layers representing different possible future climates)
# # in this example, changes from a previous model (ensemble.nofactors1)
# # are contrasted with a newly calibrated model (ensemble.nofactors2)
# ensemble.calibrate.step1 <- ensemble.test.splits(x=predictors, p=pres, a=background,
# ext=ext,
# k=4,
# layer.drops=c("biome"),
# SINK=TRUE, species.name="Bradypus",
# MAXENT=1, GBM=1, GBMSTEP=0, RF=1, GLM=1, GLMSTEP=1, GAM=1,
# GAMSTEP=1, MGCV=1, MGCVFIX=1, EARTH=1, RPART=1, NNET=1, FDA=1,
# SVM=1, SVME=1, BIOCLIM=1, DOMAIN=1, MAHAL=0,
# ENSEMBLE.tune=TRUE, PROBIT=TRUE,
# ENSEMBLE.best=0, ENSEMBLE.exponent=c(1, 2, 4, 6, 8),
# ENSEMBLE.min=0.7,
# Yweights="BIOMOD",
# PLOTS=FALSE, formulae.defaults=TRUE,
# GBMSTEP.learning.rate=0.002)
#
# output.weights <- ensemble.calibrate.step1$output.weights.AUC
# output.weights[output.weights < 0.05] <- 0
# output.weights
#
# ensemble.calibrate.step2 <- ensemble.test(x=predictors, p=pres, a=background,
# ext=ext,
# layer.drops=c("biome"),
# SINK=TRUE, species.name="Bradypus",
# models.keep=TRUE,
# input.weights=output.weights,
# AUC.weights=FALSE, ENSEMBLE.tune=FALSE, PROBIT=TRUE,
# Yweights="BIOMOD",
# PLOTS=FALSE, formulae.defaults=TRUE,
# GBMSTEP.learning.rate=0.002)
#
# ensemble.nofactors2 <- ensemble.raster(xn=predictors, ext=ext,
# models.list=ensemble.calibrate.step2$models,
# input.weights=output.weights,
# thresholds=ensemble.calibrate.step2$models$thresholds,
# SINK=TRUE,
# RASTER.species.name="Bradypus", RASTER.stack.name="recalibrated")
#
# base.file <- paste(getwd(), "/ensembles/presence/Bradypus_base.grd", sep="")
# other.file <- paste(getwd(), "/ensembles/presence/Bradypus_recalibrated.grd", sep="")
#
# changed.habitat <- ensemble.habitat.change(base.map=base.file,
# other.maps=c(other.file),
# change.folder="ensembles/change")
#
# change.file <- paste(getwd(), "/ensembles/change/Bradypus_recalibrated_presence.grd", sep="")
# areas <- ensemble.area(raster(change.file))
# areas
# ## End(Not run)
Run the code above in your browser using DataLab