R/maxent_selection.R

Defines functions maxent_selection

Documented in maxent_selection

#' Select sets of uncorrelated variables for maxent modeling
#'
#' @param x A data frame of predictor variables.
#' @param p A binary vector of presence/background values.
#' @param cor_thresh Maximum level of Pearson's correlation for candidate variables.
#' @param con_thresh Minimum percent contribution of candidate variables.
#' @param return_model Boolean. Whether to return the maxent model object. Otherwise a vector with selected variables is returned.
#' @param ... arguments passed on to dismo::maxent()
#'
#' @return Maxent model or vector with selected variables.
#' @export


maxent_selection <- function(x, p, cor_thresh = .7, con_thresh = 5,  return_model = TRUE, ...) {

  selected <- character(0)

  n_candidates <- ncol(x)
  candidates <- x

  while(n_candidates > 0) {

    model <- dismo::maxent(candidates, p)

    contribution <- model@results[grep("contribution", rownames(model@results)), ]
    names(contribution) <- gsub(".contribution", "", names(contribution))

    # remove variables that have contribution < con_thresh
    above_con_thresh <- names(contribution)[contribution >= con_thresh]
    candidates <- dplyr::select(candidates, above_con_thresh)

    # pick top contributing variable and add to selected variables
    candidates_contribution <- contribution[names(contribution) %in% colnames(candidates)]
    top <- names(candidates_contribution)[which.max(candidates_contribution)]
    selected <- c(selected, top)

    # remove all variables correlated at > cor_thresh with selected variable
    below_cor_thresh <- names(candidates)[abs(cor(candidates))[top,] <= cor_thresh]
    candidates <- dplyr::select(candidates, below_cor_thresh)
    n_candidates <- ncol(candidates)
  }

  if(return_model) {
    dismo::maxent(dplyr::select(x, selected), p, ...)
  } else {
    selected
  }
}
juoe/sdmflow documentation built on Feb. 23, 2020, 7:38 p.m.