R/model_invweibull.R

Defines functions posterior_epred_cogmod_invweibull posterior_predict_cogmod_invweibull log_lik_cogmod_invweibull cogmod_invweibull_stanvars cogmod_invweibull_lpdf_expose .cogmod_invweibull_lpdf cogmod_invweibull pcogmod_invweibull dcogmod_invweibull rcogmod_invweibull

Documented in cogmod_invweibull cogmod_invweibull_lpdf_expose cogmod_invweibull_stanvars dcogmod_invweibull log_lik_cogmod_invweibull pcogmod_invweibull posterior_epred_cogmod_invweibull posterior_predict_cogmod_invweibull rcogmod_invweibull

#' @title Shifted Inverse Weibull Model
#'
#' @description
#' Density, random generation, and `brms` custom family for the shifted
#' Inverse Weibull distribution. A Inverse Weibull-distributed decision time is shifted by a
#' non-decision time `ndt`, and a fixed proportion `poutlier` of responses is
#' generated by an outlier process instead of by the decision process.
#'
#' Functions:
#' - `rcogmod_invweibull()`: Simulates random draws.
#' - `dcogmod_invweibull()`: Computes the density (likelihood).
#' - `pcogmod_invweibull()`: Computes the cumulative distribution function (CDF) or survival.
#' - `cogmod_invweibull()`: Creates a `brms::custom_family()`.
#' - `cogmod_invweibull_stanvars()`: Generates the `stanvars` to pass to `brm()`.
#'
#' @details
#' `mu` is the **shape** and `sigma` the **scale** of the inverse Weibull
#' (Frechet) decision time, whose mean is `sigma * gamma(1 - 1 / mu)` and exists
#' only for `mu > 1`.
#'
#' `ndt` and `poutlier` mean exactly what they do in [cogmod_lognormal()],
#' and [with_outliers()], [without_outliers()], [p_outlier()] and
#' [cogmod_priors()] all work here too. See `?rcogmod_lognormal` for why `ndt` is
#' expressed directly in seconds rather than as a fraction of the fastest
#' observed response, what the half Normal outlier component is for, and why
#' the outlier component's scale is a constant rather than a `dpar`, and why
#' reaction times have to be in seconds.
#'
#' `posterior_epred()` returns `Inf` where `mu <= 1`, because the Frechet has no
#' mean there. [cogmod_loggamma()] nests this family at `shape = -1`.
#'
#' @inheritParams rcogmod_lognormal
#' @inheritParams cogmod_lnr
#' @param mu Shape of the inverse Weibull (Frechet) decision time. Must be positive.
#' @param sigma Scale of the inverse Weibull (Frechet) decision time. Must be positive.
#'
#' @return `rcogmod_invweibull()` returns a numeric vector of `n` simulated
#'   reaction times, in seconds. `dcogmod_invweibull()` returns the density at
#'   each element of `x` - the log density if `log = TRUE` - recycled to the
#'   length of the longest argument. `cogmod_invweibull()` returns a
#'   `brms::custom_family` object, to put on a `brms::bf()` formula.
#'   `cogmod_invweibull_stanvars()` returns a `brms::stanvars` object holding
#'   the family's Stan `functions` block, to pass to `brms::brm()`, and
#'   `cogmod_invweibull_lpdf_expose()` compiles that Stan code and returns it
#'   as an R function, for checking the density outside of a model. The
#'   remaining functions are `brms` post-processing methods, called by `brms`
#'   rather than directly: `log_lik_cogmod_invweibull()` returns a numeric
#'   vector holding one log-likelihood value per posterior draw for
#'   observation `i`, and `posterior_predict_cogmod_invweibull()` a draws x 1
#'   matrix of reaction times simulated for observation `i`.
#'   `posterior_epred_cogmod_invweibull()` returns a draws x observations
#'   matrix of expected reaction times, with `Inf` wherever the mean does not
#'   exist.
#'
#' @examples
#' rts <- rcogmod_invweibull(1000, mu = 3, sigma = 0.4, ndt = 0.3, poutlier = 0.02)
#' hist(rts, breaks = 100, xlab = "RT (s)")
#'
#' # Responses faster than ndt keep positive density, unlike the unmixed model
#' dcogmod_invweibull(0.1, ndt = 0.3, poutlier = 0.02)
#' dcogmod_invweibull(0.1, ndt = 0.3, poutlier = 0)
#'
#' @export
rcogmod_invweibull <- function(n, mu = 3, sigma = 0.4, ndt = 0.2, poutlier = 0) {
  .rshifted("cogmod_invweibull", n = n, ndt = ndt, poutlier = poutlier,
               mu = mu, sigma = sigma)
}


#' @rdname rcogmod_invweibull
#' @param x Vector of quantiles (observed reaction times).
#' @param log Logical; if TRUE, probabilities p are given as log(p).
#' @export
dcogmod_invweibull <- function(x, mu = 3, sigma = 0.4, ndt = 0.2, poutlier = 0,
                      log = FALSE) {
  .dshifted("cogmod_invweibull", x = x, ndt = ndt, poutlier = poutlier,
               log = log, mu = mu, sigma = sigma)
}


#' @rdname rcogmod_invweibull
#' @export
pcogmod_invweibull <- function(q, mu = 3, sigma = 0.4, ndt = 0.2, poutlier = 0,
                               lower.tail = TRUE, log.p = FALSE) {
  .pshifted("cogmod_invweibull", q = q, ndt = ndt, poutlier = poutlier,
            lower.tail = lower.tail, log.p = log.p, mu = mu, sigma = sigma)
}


#' @rdname rcogmod_invweibull
#' @param link_mu,link_sigma,link_ndt,link_poutlier Link functions for the
#'   parameters.
#' @param predict_outliers Logical; whether `posterior_predict()` and
#'   `posterior_epred()` should include the outlier component. `FALSE` (the
#'   default) fixes `poutlier` to zero for prediction, so predictions describe
#'   the decision process alone; the likelihood is always the full mixture
#'   either way. See [with_outliers()].
#' @export
cogmod_invweibull <- function(link_mu = "softplus", link_sigma = "softplus",
                     link_ndt = "log", link_poutlier = "logit",
                     predict_outliers = FALSE) {
  .shifted_family("cogmod_invweibull", links = c(link_mu, link_sigma),
                     predict_outliers = predict_outliers)
}


#' @keywords internal
.cogmod_invweibull_lpdf <- function() {
  .shifted_lpdf("cogmod_invweibull")
}


#' @rdname rcogmod_invweibull
#' @export
cogmod_invweibull_lpdf_expose <- function() {
  .shifted_expose("cogmod_invweibull")
}


#' @rdname rcogmod_invweibull
#' @export
cogmod_invweibull_stanvars <- function() {
  brms::stanvar(scode = .cogmod_invweibull_lpdf(), block = "functions")
}


# brms methods ------------------------------------------------------------

#' @rdname rcogmod_invweibull
#' @export
log_lik_cogmod_invweibull <- function(i, prep) {
  .log_lik_shifted("cogmod_invweibull", i, prep)
}


#' @rdname rcogmod_invweibull
#' @export
posterior_predict_cogmod_invweibull <- function(i, prep, predict_outliers = NULL, ...) {
  .posterior_predict_shifted("cogmod_invweibull", i, prep, predict_outliers)
}


#' @rdname rcogmod_invweibull
#' @export
posterior_epred_cogmod_invweibull <- function(prep, predict_outliers = NULL) {
  .posterior_epred_shifted("cogmod_invweibull", prep, predict_outliers)
}

Try the cogmod package in your browser

Any scripts or data that you put into this service are public.

cogmod documentation built on Nov. 28, 2026, 1:08 a.m.