Wednesday, March 10, 2021

Doing replicates of ENMTools models

This is coming from a question on the maxent google group.  Someone wanted to know how to do multiple replicates of an ENMTools model.  At some point we need to create some code for that, but for now it's not too hard to do it using R's existing "replicate" function.


# First we'll load in ENMTools

library(ENMTools)


# Now we're just grabbing a species object out of the built-in data 

mont <- iberolacerta.clade$species$monticola


# Here we're using the "replicate" function to repeat the modeling command 10 times 

# and stuff the results into a list

my.models <- replicate(10, 

          enmtools.gam(mont, euro.worldclim[[c(1,12)]], test.prop = 0.3), 

          simplify=FALSE)


# And now we want to give each rep a name

names(my.models) <- paste0("rep", 1:10)


# This is a slightly tricky bit - basically I'm grabbing the suitability raster out of each

# replicate model, and then turning them into a raster stack

rep.rasters <- stack(sapply(my.models, function(x) x$suitability))


# Now that we've got that, we can calculate any summary stats we want on that stack

# Here's how you'd plot the mean suitability

mean.raster <- calc(rep.rasters, fun = mean)

plot(mean.raster)


# Here's how you'd do variance

var.raster <- calc(rep.rasters, fun = var)

plot(var.raster)


# And here's minimum suitability

min.raster <- calc(rep.rasters, fun = min)

plot(min.raster)


No comments:

Post a Comment