R/raster.overlap.R

Defines functions raster.overlap

Documented in raster.overlap

#' raster.overlap, measures overlap between two ENMs
#'
#' This function measures similarity in the geographic distribution of suitability scores
#' from two ENMs.  It returns two metrics, I and D.  These metrics are described in
#' Warren et al. 2008.
#'
#' @param x A raster or RasterLayer object, or ENMTools model object containing a suitability raster.
#' @param y Another raster or RasterLayer object, or ENMTools model object containing a suitability raster.
#' @param verbose Controls printing of diagnostic messages
#'
#' @return results A vector containing the three metrics (I, D, and Spearman rank correlation)
#'
#' @keywords keywords
#'
#' @examples
#' aurelioi.glm <- enmtools.glm(iberolacerta.clade$species$aurelioi,
#' euro.worldclim, f = pres ~ bio1 + bio12)
#' aranica.glm <- enmtools.glm(iberolacerta.clade$species$aranica,
#' euro.worldclim, f = pres ~ bio1 + bio12)
#' raster.overlap(aurelioi.glm, aranica.glm)

raster.overlap <- function(x, y, verbose=FALSE){

  if(any(grepl("enmtools", class(x)))){
    x <- x$suitability
  }

  if(any(grepl("enmtools", class(y)))){
    y <- y$suitability
  }

  if(verbose){
    print(paste("Starting overlap at", Sys.time()))
  }

  x <- raster.standardize(x)
  y <- raster.standardize(y)

  D <- 1 - as.numeric(terra::global(abs(x - y), "sum", na.rm = TRUE)/2)
  I <- 1 - as.numeric(terra::global((sqrt(x) - sqrt(y))^2, "sum", na.rm = TRUE)/2)
  rank.cor <- raster.cor(x, y)

  results <- list(D = D, I = I, rank.cor = rank.cor)
  return(results)

}
danlwarren/ENMTools documentation built on Feb. 2, 2024, 1:05 p.m.