R/stress.R

Defines functions stress_spline stress_logistic stress_convex stress_linear

Documented in stress_convex stress_linear stress_logistic stress_spline

#' Fits a linear function to describe a stress response
stress_linear <- function() {
  function(srel) {
    1 - srel
  }
}

#' Fits a convex function to describe a stress response
stress_convex <- function(fshape) {
  function(srel) {
    1 - (exp(fshape * srel) - 1) / (exp(fshape) - 1)
  }
}

#' Fits a logistic function to describe a stress response
stress_logistic <- function(sn, sx) {
  r <- -log(2 * sn * (sx - 0.5) / (sx - sn)) * 2
  kmax <- (sn * sx) / (sn + (sx - sn) * exp(-r))
  kmin <- sn
  function(srel) {
    ks <- (sn * sx) / (sn + (sx - sn) * exp(-r * (1 - srel)))
    (ks - kmin) / (kmax - kmin)
  }
}

#' Stress response based on a spline function
stress_spline <- function(known_srel, known_ks, method = "hyman", ties = mean) {
  fun <- splinefun(x = known_srel, y = known_ks, method = method, ties = ties)
  function(srel) {
    fun(x = srel)
  }
}
jvitorpinto/agmet documentation built on Jan. 30, 2023, 6:33 a.m.