predictMaxEnt: Predict a MaxEnt model object (with optional feature-level...

View source: R/predictMaxEnt.r

predictMaxEntR Documentation

Predict a MaxEnt model object (with optional feature-level permutation)

Description

Takes a MaxEnt lambda object or a MaxEnt object and returns raw or logistic predictions. Its output is the same as the function raster::predict(maxentModelObject, dataFrame) or raster::predict(maxentModelObject, dataFrame, args='outputformat=raw') (to within rounding error), and in fact those functions should be faster. However, this function does allow custom manipulations that those functions do not allow (e.g., permuting product features while leaving other features with the same variables intact). This function does not clamp predictions–beyond the range of the training data, it extends the prediction in the direction it was going (up/down/no change). The function is based on Peter D. Wilson's document "Guidelines for computing MaxEnt model output values from a lambdas file". The function has a special feature in that it allows you to permute single variables or combinations of variables in specific features before making predictions. This is potentially useful, for example, if you wanted to determine the relative importance of a quadratic feature for a particular variable in a Maxent model relative to the other features in the model. You can also permute values of a variable regardless of which features they appear in. For product features, you can implement the permutation before or after the values are multiplied together (before often makes for bigger differences in predictions).

Usage

predictMaxEnt(
  x,
  data,
  type = "cloglog",
  perm = NULL,
  permLinear = NULL,
  permQuad = NULL,
  permHinge = NULL,
  permThresh = NULL,
  permProd = NULL,
  permProdRule = NULL,
  ...
)

Arguments

x

Either a Maxent lambda object or a Maxent model object

data

Data frame. Data to which to make predictions

type

Character. One of:

  • 'raw': Maxent "raw" values

  • 'logistic': Maxent logistic values

  • 'cloglog' complementary log-log output (as per version 3.4.0+ of maxent–called "maxnet()" in the package of the same name)

perm

Character vector. Name(s) of variable to permute before calculating predictions. This permutes the variables for all features in which they occur. If a variable is named here, it overrides permutation settings for each feature featType. Note that for product features the variable is permuted before the product is taken. This permutation is performed before any subsequent permutations (i.e., so if both variables in a product feature are included in perms, then this is equivalent to using the 'before' rule for permProdRule). Ignored if NULL.

permLinear

Character list. Names(s) of variables to permute in linear features before calculating predictions. Ignored if NULL.

permQuad

Names(s) of variables to permute in quadratic features before calculating predictions. Ignored if NULL.

permHinge

Character vector. Names(s) of variables to permute in forward/reverse hinge features before calculating predictions. Ignored if NULL.

permThresh

Character list. Names(s) of variables to permute in threshold features before calculating predictions. Ignored if NULL.

permProd

Character list. A list object of n elements, each of which has two character elements naming the variables to permute if they occur in a product feature. Depending on the value of permProdRule, the function will either permute the individual variables then calculate their product or calculate their product, then permute the product across observations. Any other features containing the variables will produce values as normal. Example: permProd=list(c('precipWinter', 'tempWinter'), c('tempSummer', 'precipFall')). The order of the variables in each element of permProd doesn't matter, so permProd=list(c('temp', 'precip')) is the same as permProd=list(c('precip', 'temp')). Ignored if NULL.

permProdRule

Character. Rule for how permutation of product features is applied: 'before' ==> Permute individual variable values then calculate product; 'after' ==> calculate product then permute across these values. Ignored if permProd is NULL.

...

Extra arguments (not used).

Value

Numeric.

See Also

maxent

Examples

### model red-bellied lemurs
data(mad0)
data(lemurs)

# climate data
bios <- c(1, 5, 12, 15)
clim <- raster::getData('worldclim', var='bio', res=10)
clim <- raster::subset(clim, bios)
clim <- raster::crop(clim, mad0)

# occurrence data
occs <- lemurs[lemurs$species == 'Eulemur rubriventer', ]
occsEnv <- raster::extract(clim, occs[ , c('longitude', 'latitude')])
occsEnv <- as.data.frame(occsEnv) # need to do this for prediction later

# background sites
bg <- 2000 # too few cells to locate 10000 background points
bgSites <- dismo::randomPoints(clim, 2000)
bgEnv <- extract(clim, bgSites)

# collate
presBg <- rep(c(1, 0), c(nrow(occs), nrow(bgSites)))
env <- rbind(occsEnv, bgEnv)
env <- cbind(presBg, env)
env <- as.data.frame(env)

preds <- paste0('bio', bios)

regMult <- 1:3 # default values are probably better, but these will be faster

# calibrate MaxEnt model
ent <- trainMaxEnt(
	data=env,
	resp='presBg',
	preds=preds,
	regMult=regMult,
	classes='lpq',
	verbose=TRUE
)

# calibrate MaxNet model
net <- trainMaxNet(
	data=env,
	resp='presBg',
	preds=preds,
	regMult=regMult,
	classes='lpq',
	verbose=TRUE
)

# prediction rasters
mapEnt <- predict(ent, clim, type='logistic')
mapNet <- predict(clim, net, type='logistic')

par(mfrow=c(1, 2))
plot(mapEnt, main='MaxEnt')
points(occs[ , c('longitude', 'latitude')])
plot(mapNet, main='MaxNet')
points(occs[ , c('longitude', 'latitude')])

# predictions to occurrences
(dismo::predict(ent, occsEnv, args=c('outputformat=logistic')))
(enmSdm::predictMaxEnt(ent, occsEnv, type='logistic'))
(c(predict(net, occsEnv, type='logistic')))

# note the differences between the tuning of the two models...
# this is because maxnet() (used by trainMaxNet())
# uses an approximation:
# (note maxnet() calculates hinges and thresholds differently
# so we will turn them off)

data(bradypus, package='maxnet')
p <- bradypus$presence
data <- bradypus[ , 2:3] # easier to inspect betas
mn <- maxnet::maxnet(p, data,
maxnet::maxnet.formula(p, data, classes='lpq'))
mx <- dismo::maxent(data, p,
args=c('linear=true', 'product=true', 'quadratic=true', 'hinge=false',
'threshold=false'))

predMx <- dismo::predict(mx, data)
predMn <- predict(mn, data, type='logistic')

par(mfrow=c(1, 1))
plot(predMx, predMn)
abline(0, 1)

adamlilith/enmSdm documentation built on Jan. 6, 2023, 11 a.m.