R/remove_singleVariable.R

Defines functions remove_singleVariable

Documented in remove_singleVariable

#' Remove single correlated variable based on VIF values and permutation
#' importance to a Maxent model
#'
#' @description Removes the variable that contributed the least to a default
#' Maxent model and had a VIF greater than a user-defined value.
#'
#' @param pred_vars raster stack of variables to be used as potential
#' predictor variables
#'
#' @param th numeric value that denotes the maximum allowed VIF value among
#' predictor variables.
#'
#' @return A raster stack of predictor variables that has had one variable
#' removed based on its VIF and permutation importance to maxent model.
#'
#' @import dplyr
#'
#' @importFrom dismo maxent
#' @importFrom raster dropLayer
#' @importFrom magrittr %>%
#' @importFrom usdm vif
#' @importFrom sp coordinates
#' @importFrom stringr str_detect word
#'
#' @keywords internal

# First we have the function to be nested.
remove_singleVariable <- function(pred_vars, th){
  # fit a maxent model with default parameters
  max_model <- dismo::maxent(x = pred_vars, p = sp::coordinates(spp_df),
                      progress = "text")
  #calculate vifs of predictor variables
  pVIF <- usdm::vif(pred_vars)
  #calculate variable importance of maxent models
  m_results <- as.data.frame(as.table(maxent_mod@results)) %>%
    dplyr::rename(variables = 1, rem = 2, permutation.importance = 3) %>%
    dplyr::select(variables, permutation.importance)
  vIMP <- m_results %>%
    dplyr::filter(stringr::str_detect(variables, '.permutation.importance')) %>%
    dplyr::mutate(Variables = stringr::word(variables,  sep = fixed(".")))
  #join var importance with vifs
  jdf <- dplyr::left_join(pVIF, vIMP)
  # select the variables with the highest VIFs and select least important variable
  # based on permutation importance
  lowVar <- jdf %>%
    dplyr::filter(VIF > th) %>%
    dplyr::filter(VIF == sort(VIF, decreasing = TRUE)[1] |
             VIF == sort(VIF, decreasing = TRUE)[2]) %>%
    dplyr::filter(permutation.importance == min(permutation.importance))

  # make raster stack of variable without biggest VIF and lowest permutation imp
  predictors_notInfl <- raster::dropLayer(pred_vars,
                                          as.character(lowVar$Variables))

  #p2_vifs <- usdm::vif(predictors_notInfl)

  return(predictors_notInfl)
}
mbelitz/autoSDM documentation built on Dec. 21, 2021, 3:55 p.m.