extract lambda for maxent models using only one of the predictor variables

952 views
Skip to first unread message

Anais Gorel

unread,
Mar 11, 2014, 3:07:57 PM3/11/14
to max...@googlegroups.com
Hy everybody,
 
I'm working on Maxent using the package dismo in R.

Maxent shows response curves and especially curves for models using only one of the predictor variables (for an easier interpretation if there are strong correlations between variables). So i conclude that Maxent calculates  lambda values for each model using a single predictor variable.

Does anyone know how to extract this lambdas using the package dismo in R?

Cheers

Anaïs

John Baumgartner

unread,
Mar 11, 2014, 3:10:53 PM3/11/14
to max...@googlegroups.com
Hi Anaïs,

The lambdas are stored in the "lambdas" slot of your MaxEnt object. If your object is called m1, they can be accessed by:

m1@lambdas

Anais Gorel

unread,
Mar 12, 2014, 5:16:51 AM3/12/14
to max...@googlegroups.com

Hi John,

Thanks for your answer,
I thought the lambdas that were stored in name_model@lambda were those calculated for the complete model (calculated with all predictors). When you write arg =c(“-P”) in R,  maxent returns automatically the probability response curves for models calculated with each predictors individually (after marginal response curves).  It is that lambda I would have.  

Have a nice day,

Anaïs

Jamie Kass

unread,
Mar 12, 2014, 5:57:58 AM3/12/14
to max...@googlegroups.com
You can access the lambdas file and pull out the number of parameters used in R like this, where curpath is the path to the lambdas file:

  # read lambdas file
  rf <- read.table(file.path(curpath, 'species.lambdas'), sep=',', fill=TRUE)
  # record no. of params (restrict df to rows with four values and no 0 in 2nd column)
  p <- nrow(rf[!is.na(rf[3]) & rf[2] != 0,])

John Baumgartner

unread,
Mar 13, 2014, 5:33:03 PM3/13/14
to max...@googlegroups.com
FYI The number of rows in the lambdas file corresponds to the number of features in the model, not the number of environmental variables provided to Maxent.

But to return to Anaïs's original question, you're right, the @lambda slot includes lambdas for all features, rather than just features relating to a single environmental predictor. 

To access lambdas for single predictor models, you'll need to fit each model separately.

Here's an example, using (a subset of) the bradypus data that comes with dismo:

library(dismo)
jar <- paste(system.file(package="dismo"), "/java/maxent.jar", sep='')

# get predictor variables
fnames <- list.files(path=paste(system.file(package="dismo"), '/ex', sep=''), 
                     pattern='grd', full.names=TRUE )
predictors <- stack(fnames[1:3])

# file with presence points
occurence <- paste(system.file(package="dismo"), '/ex/bradypus.csv', sep='')
occ <- read.table(occurence, header=TRUE, sep=',')[,-1]

# witholding a 20% sample for testing 
fold <- kfold(occ, k=5)
occtest <- occ[fold == 1, ]
occtrain <- occ[fold != 1, ]

dummy <- raster(predictors)
dummy[] <- 1
me <- lapply(seq_len(nlayers(predictors)), function(i) {
  maxent(stack(predictors[[i]], dummy), occtrain, arg='-P')
})

# html output
me

# lambdas
lapply(me, function(x) x@lambdas)

Note that the maxent function fails if you try to fit a model with just a single predictor. To overcome this, for each single predictor model I actually give it a stack that combines the predictor of interest with a constant grid (in this case, all cells = 1).

Anais Gorel

unread,
Mar 14, 2014, 4:31:09 AM3/14/14
to max...@googlegroups.com
Thank you John. This is the answer to my question.
I'm going to do it now

Have a nice day.

Anaïs

Jamie Kass

unread,
Mar 15, 2014, 5:55:43 AM3/15/14
to max...@googlegroups.com
One thing to keep in mind is that, according to the PDF on lambdas found here, just because an environmental predictor is listed in your lambdas file doesn't mean it is included in your fitted model. Only rows with the second position not equal to 0 were included as predictors. So if you wanted to calculate the number of parameters used in fitting your model, you should count only rows with a non-zero entry in column 2. This is what the code I wrote above does.
Reply all
Reply to author
Forward
0 new messages