R/detection.R

Defines functions detect_sweep detect_random_search detect_normal detect_random detect_constant detect

Documented in detect_random_search detect_sweep

# DETECTION FUNCTIONS ----------------------------------------------------------

# Sample a fraction of points in a unit
detect <- function(unit, points, p = 1) {
  unit <- sf::st_sfc(unit, crs = sf::st_crs(points))
  points %>%
    sf::st_intersection(unit) %>%
    # Workaround for https://github.com/tidyverse/dplyr/issues/2457
    dplyr::as_tibble() %>%
    dplyr::sample_frac(rate) %>%
    return()
}

# Detect points by applying a constant detection rate
#' @export
detect_constant <- function(rate = 1) {
  return(rate)
}

# Detect points by applying a uniform stochastic detection rate
#' @export
detect_random <- function(unit, points, min = 0, max = 1) {
  return(detect_rate(unit, points, rate = runif(1,min,max)))
}

# Detect points by applying a (truncated) normal stochastic detection rate
#' @export
detect_normal <- function(unit, points, mean = 0.5, sd = 0.2) {
  repeat {
    rate <- rnorm(1, mean, sd)
    if(rate > 0 && rate < 1) break
  }

  return(detect_rate(unit, points, rate = rate))
}

#' Detection Function: Law of Random Search
#'
#' @param unit
#' @param points
#' @param time
#' @param g
#'
#' @return
#' @export
#'
#' @examples
#'
#' @references
#'
#' * \insertRef{Banning2002-uv}{fieldwalkr}
#' * \insertRef{Banning2011-wo}{fieldwalkr}
detect_random_search <- function(unit, points, time, g) {
  rate <- 1 - exp(-g * time)
  return(detect_rate(unit, points, rate))
}

#' Detection Function: Sweep Width
#'
#' @param unit
#' @param points
#'
#' @return
#' @export
#'
#' @examples
#'
#' @references
#'
#' * \insertRef{Banning2011-wo}{fieldwalkr}
detect_sweep <- function(unit, points, b, k, r) {
  rate <- b - exp(-k * r^2)
  return(detect_rate(unit, points, rate))
}

# TODO:
# Definite or 'clean sweep' detection (Koopman 1980:83; Banning 2002: pp. 57-58)
#  Perfect detection within an area defined by the visual sweep of n observers
#  (assumed to be walking equally spaced transects).
joeroe/fieldwalkr documentation built on Feb. 17, 2024, 12:15 a.m.