# based on examples in the dismo package
# simplified example of ensemble modelling with 4 modeling algorithms
# get predictor variables
library(dismo)
predictors <- stack(list.files(path=paste(system.file(package="dismo"), '/ex', sep=''), pattern='grd', full.names=TRUE ))
# predictors
# presence points
presence <- paste(system.file(package="dismo"), '/ex/bradypus.csv', sep='')
pres <- read.table(presence, header=TRUE, sep=',')[,-1]
# the kfold function randomly assigns data to groups; subdivide in calibration and training data
groupp <- kfold(pres, 5)
pres_train <- pres[groupp != 1, ]
pres_test <- pres[groupp == 1, ]
# choose background points
ext = extent(-90, -32, -33, 23)
background <- randomPoints(predictors, n=1000, ext=ext, extf = 1.00)
colnames(background) = c('lon', 'lat')
groupa <- kfold(background, 5)
backg_train <- background[groupa != 1, ]
backg_test <- background[groupa == 1, ]
# formulae for random forest and generalized linear model
# compare with: ensemble.formulae(predictors, factors=c("biome"))
rfformula <- as.formula(pb ~ bio1+bio5+bio6+bio7+bio8+bio12+bio16+bio17)
glmformula <- as.formula(pb ~ bio1 + I(bio1^2) + I(bio1^3) +
bio5 + I(bio5^2) + I(bio5^3) + bio6 + I(bio6^2) + I(bio6^3) +
bio7 + I(bio7^2) + I(bio7^3) + bio8 + I(bio8^2) + I(bio8^3) +
bio12 + I(bio12^2) + I(bio12^3) + bio16 + I(bio16^2) + I(bio16^3) +
bio17 + I(bio17^2) + I(bio17^3) )
# fit four ensemble models (RF, GLM, BIOCLIM, DOMAIN)
ensemble.nofactors <- ensemble.test(x=predictors, p=pres_train, a=backg_train, pt=pres_test, at=backg_test,
MAXENT=0, GBM=0, GBMSTEP=0, RF=1, GLM=1, GLMSTEP=0, GAM=0, GAMSTEP=0, MGCV=0,
EARTH=0, RPART=0, NNET=0, FDA=0, SVM=0, BIOCLIM=1, DOMAIN=1, MAHAL=0,
Yweights="BIOMOD", factors=NULL,
PLOTS=FALSE, evaluations.keep=TRUE,
RF.formula=rfformula,
GLM.formula=glmformula)
# test performance of different suitability models; data are split in 4 subsets, each used once for evaluation
# GAMSTEP option is not available at the moment (problems with assignments)
ensemble.nofactors2 <- ensemble.test.splits(x=predictors, p=pres, a=background, k=4,
MAXENT=1, GBM=1, GBMSTEP=1, RF=1, GLM=1, GLMSTEP=1, GAM=1, GAMSTEP=0, MGCV=1,
EARTH=1, RPART=1, NNET=1, FDA=1, SVM=1, BIOCLIM=1, DOMAIN=1, MAHAL=1,
Yweights="BIOMOD", factors=NULL,
PLOTS=FALSE, formulae.defaults=TRUE,
GBMSTEP.learning.rate=0.002)
ensemble.nofactors2Run the code above in your browser using DataLab