R/model_lba1.R

Defines functions posterior_epred_cogmod_lba1 posterior_predict_cogmod_lba1 log_lik_cogmod_lba1 cogmod_lba1_stanvars cogmod_lba1_lpdf_expose .cogmod_lba1_lpdf cogmod_lba1 dcogmod_lba1 rcogmod_lba1

Documented in cogmod_lba1 cogmod_lba1_lpdf_expose cogmod_lba1_stanvars dcogmod_lba1 log_lik_cogmod_lba1 posterior_epred_cogmod_lba1 posterior_predict_cogmod_lba1 rcogmod_lba1

#' @title Shifted Single-Accumulator LBA Model
#'
#' @aliases recinormal LATER
#'
#' @description
#' Density, random generation, and `brms` custom family for a single-accumulator
#' Linear Ballistic Accumulator. Evidence rises linearly and ballistically - no
#' within-trial noise - from a start point drawn uniformly on `[0, sigmabias]`
#' at a rate drawn from a normal truncated at zero, until it reaches the
#' threshold `b = sigmabias + boundary`. That finishing time is shifted by a
#' non-decision time `ndt`, and a fixed proportion `poutlier` of responses is
#' generated by an outlier process instead.
#'
#' Fixing `sigmabias = 0` gives the **recinormal**, or LATER, model, in which
#' `1 / (RT - ndt)` is normally distributed; see the section below.
#'
#' Functions:
#' - `rcogmod_lba1()`: Simulates random draws.
#' - `dcogmod_lba1()`: Computes the density (likelihood).
#' - `cogmod_lba1()`: Creates a `brms::custom_family()`.
#' - `cogmod_lba1_stanvars()`: Generates the `stanvars` to pass to `brm()`.
#'
#' @details
#' The full LBA is a race between one accumulator per response option, with the
#' winner determining both the choice and the RT. With no choice to model there
#' is nothing to race, so this is the single-accumulator version and the RT is
#' just that one accumulator's finishing time. All the RT variability comes from
#' across-trial variability in the start point and the drift rate, rather than
#' from moment-to-moment noise within the trial.
#'
#' The threshold is written as an *offset*, `b = sigmabias + boundary`, which is
#' the `B` parameterization of `DMC` and `EMC2` (`b = B + A`) rather than the
#' absolute threshold `rtdists` estimates. The threshold has to sit above the
#' highest possible starting point, and the offset makes `b > sigmabias` hold
#' automatically for any positive value instead of needing an order constraint
#' between two estimated parameters. See [cogmod_lba2()] for the full note. The
#' cost is that `boundary` alone is not the quantity to read off a fitted model;
#' `boundary + sigmabias` is.
#'
#' `sigma` is conventionally **fixed to 1** rather than estimated, because the
#' evidence scale is arbitrary: multiplying `mu`, `sigma`, `sigmabias` and `boundary`
#' by a common constant leaves the decision time `(b - start) / drift` unchanged,
#' so only ratios are identified and one parameter must be pinned at a non-zero
#' value to fix the scale. Fix it in the formula with `sigma = 1`.
#'
#' @section The recinormal (LATER) special case:
#' Setting `sigmabias = 0` removes the start-point variability altogether: the
#' accumulator starts at zero on every trial, the decision time is `b / drift`,
#' and `1 / (RT - ndt)` is therefore normally distributed. That is the
#' *recinormal*, better known in the oculomotor literature as the **LATER**
#' model of Carpenter and Williams (1995), whose `mu` and `sigma` are the mean
#' and SD of *promptness* - the quantity a reciprobit plot puts on its axis.
#'
#' This is not an approximation reached in the limit. At `sigmabias = 0` the
#' density evaluates to `dnorm(b / t, drift, sigma) * b / t^2 / pnorm(drift /
#' sigma)` exactly, to machine precision, in both the R and the Stan
#' implementation. Two pins are needed rather than one, because zero is the one
#' value the arbitrary evidence scale leaves alone and so `sigmabias` drops off
#' the scale ray rather than pinning it:
#'
#' ```r
#' # free: mu and sigma, the mean and SD of promptness
#' bf(rt ~ 1, sigmabias = 0, boundary = 1)
#' ```
#'
#' Because `sigmabias` is then a constant rather than a parameter, none of the
#' trouble described next applies to it, and [cogmod_priors()] emits no row for
#' it.
#'
#' @section The same accumulator with a LogNormal rate:
#' The other single-accumulator LBA in the package is [cogmod_lognormal()]:
#' the same start point `Uniform(0, sigmabias)` and the same threshold offset,
#' with a LogNormal rather than a truncated-Normal rate, so its `sigmabias = 0`
#' limit is the shifted LogNormal where this family's is the recinormal. It
#' lives under that name rather than here because a LogNormal rate's `sigma` is
#' untouched by rescaling the evidence axis, so the threshold offset has to be
#' the pin (it is fixed at 1 there) where here `sigma = 1` does the job. Two
#' such accumulators raced against each other are [cogmod_lnr()].
#'
#' @section Estimating the start-point range:
#' Left free, `sigmabias` is estimable but treacherous, precisely because the
#' recinormal limit above is reached *smoothly*: once the start-point range is
#' small enough, making it smaller stops changing the density, so the likelihood
#' goes **flat**. On a `softplus` link zero is at minus infinity, so a flat prior
#' there leaves the posterior improper, and the symptom is a chain that wanders
#' off rather than one that fails. Fitted without priors on the 4285-trial data
#' in the [RT models article](https://dominiquemakowski.com/cogmod/articles/rt_models.html), `sigmabias` for one condition ran to
#' `softplus(-10.4) = 3e-05` with `Rhat` 1.69 and an effective sample size of 6.
#'
#' There are two ways out, and the choice is a modelling decision rather than a
#' technical one. Pin `sigmabias = 0` and fit the recinormal, which is the honest
#' option when the design cannot identify a start-point range. Or keep it free
#' and fence the flat direction off with a prior: [cogmod_priors()] does this for
#' both `sigmabias` and `boundary` - the threshold is `b = sigmabias + boundary`,
#' so the two share the ridge - in the same way and for the same reason it fences
#' off `ndt` and `poutlier`. Pass `prior = cogmod_priors(f, df)`; the defaults are
#' weak (`normal(0, 1)` on the softplus scale, so a start-point range of roughly
#' 0.3 to 1.3) and are meant to be replaced rather than relied on if you know
#' more. The two are nested, so `loo_compare()` on the two fits is a like-for-like
#' comparison through the same likelihood.
#'
#' `ndt` and `poutlier` mean exactly what they do in [cogmod_lognormal()],
#' and [with_outliers()], [without_outliers()] and [cogmod_priors()] work here
#' too. See `?rcogmod_lognormal` for the full account.
#'
#' Note that `posterior_epred()` is not available: the decision time has no
#' finite mean, because `E[1 / drift]` diverges for a normal truncated at zero.
#'
#' @references
#' Carpenter, R. H. S., & Williams, M. L. L. (1995). Neural computation of log
#' likelihood in control of saccadic eye movements. *Nature, 377*(6544),
#' 59-62.
#'
#' @inheritParams rcogmod_lognormal
#' @param drift Mean drift rate.
#' @param sigma Standard deviation of the drift rate. Conventionally fixed to 1.
#' @param sigmabias The starting-point range (`A`); must be non-negative. Zero is
#'   the recinormal (LATER) model rather than an invalid value - see the section
#'   above.
#' @param boundary The threshold offset, such that `b = sigmabias + boundary`; must be
#'   positive.
#'
#' @return `rcogmod_lba1()` returns a numeric vector of `n` simulated reaction
#'   times, in seconds. `dcogmod_lba1()` returns the density at each element
#'   of `x` - the log density if `log = TRUE` - recycled to the length of the
#'   longest argument. `cogmod_lba1()` returns a `brms::custom_family` object,
#'   to put on a `brms::bf()` formula. `cogmod_lba1_stanvars()` returns a
#'   `brms::stanvars` object holding the family's Stan `functions` block, to
#'   pass to `brms::brm()`, and `cogmod_lba1_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_lba1()` returns a
#'   numeric vector holding one log-likelihood value per posterior draw for
#'   observation `i`, and `posterior_predict_cogmod_lba1()` a draws x 1 matrix
#'   of reaction times simulated for observation `i`.
#'   `posterior_epred_cogmod_lba1()` returns nothing: the decision time has no
#'   finite mean, so it errors rather than report one - summarise
#'   `posterior_predict()` draws instead.
#'
#' @examples
#' # Simulate 1000 trials with 2% outliers
#' rts <- rcogmod_lba1(1000, drift = 3, sigma = 1, sigmabias = 0.5, boundary = 0.5,
#'                ndt = 0.3, poutlier = 0.02)
#' hist(rts, breaks = 100, xlab = "RT (s)")
#'
#' # sigmabias = 0 is the recinormal (LATER): 1 / (RT - ndt) is normal
#' dcogmod_lba1(0.5, drift = 3, sigma = 1, sigmabias = 0, boundary = 0.5, ndt = 0.2)
#' dnorm(0.5 / 0.3, 3, 1) * 0.5 / 0.3^2 / pnorm(3)
#'
#' # Responses faster than ndt keep positive density, unlike the unmixed model
#' dcogmod_lba1(0.1, ndt = 0.3, poutlier = 0.02)
#' dcogmod_lba1(0.1, ndt = 0.3, poutlier = 0)
#'
#' @export
rcogmod_lba1 <- function(n, drift = 3, sigma = 1, sigmabias = 0.5, boundary = 0.5,
                    ndt = 0.3, poutlier = 0) {
  .rshifted("cogmod_lba1", n = n, ndt = ndt, poutlier = poutlier, mu = drift, sigma = sigma, sigmabias = sigmabias, boundary = boundary)
}


#' @rdname rcogmod_lba1
#' @param x Vector of quantiles (observed reaction times).
#' @param log Logical; if TRUE, probabilities p are given as log(p).
#' @export
dcogmod_lba1 <- function(x, drift = 3, sigma = 1, sigmabias = 0.5, boundary = 0.5,
                    ndt = 0.3, poutlier = 0, log = FALSE) {
  .dshifted("cogmod_lba1", x = x, ndt = ndt, poutlier = poutlier, log = log, mu = drift, sigma = sigma, sigmabias = sigmabias,
               boundary = boundary)
}


#' @rdname rcogmod_lba1
#' @param link_mu,link_sigma,link_sigmabias,link_boundary,link_ndt,link_poutlier Link
#'   functions for the parameters.
#' @param predict_outliers Logical; whether `posterior_predict()` 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()].
#' @inheritParams rcogmod_lognormal
#' @export
cogmod_lba1 <- function(
  link_mu = "softplus",
  link_sigma = "softplus",
  link_sigmabias = "softplus",
  link_boundary = "softplus",
  link_ndt = "log",
  link_poutlier = "logit",
  predict_outliers = FALSE
) {
  .shifted_family(
    "cogmod_lba1",
    links = c(link_mu, link_sigma, link_sigmabias, link_boundary),
    predict_outliers = predict_outliers
  )
}


#' @keywords internal
.cogmod_lba1_lpdf <- function() {
  .shifted_lpdf("cogmod_lba1")
}


#' @rdname rcogmod_lba1
#' @export
cogmod_lba1_lpdf_expose <- function() {
  .shifted_expose("cogmod_lba1")
}


#' @rdname rcogmod_lba1
#' @export
cogmod_lba1_stanvars <- function() {
  brms::stanvar(scode = .cogmod_lba1_lpdf(), block = "functions")
}


# brms Post-processing Functions ------------------------------------------

#' @rdname rcogmod_lba1
#' @inheritParams cogmod_lnr
#' @export
log_lik_cogmod_lba1 <- function(i, prep) {
  .log_lik_shifted("cogmod_lba1", i, prep)
}


#' @rdname rcogmod_lba1
#' @export
posterior_predict_cogmod_lba1 <- function(i, prep, predict_outliers = NULL, ...) {
  .posterior_predict_shifted("cogmod_lba1", i, prep, predict_outliers)
}


#' @rdname rcogmod_lba1
#' @export
posterior_epred_cogmod_lba1 <- function(prep, predict_outliers = NULL) {
  # Not merely expensive: the expectation does not exist. The decision time is
  # (b - U(0, A)) / v with v a normal truncated at zero, and since that density
  # is positive at v = 0, E[1/v] diverges. Simulating and summarising is the
  # only option, and even the sample mean will not settle down.
  stop(
    "The single-accumulator LBA decision time has no finite mean - E[1/drift] ",
    "diverges because the truncated-normal drift has positive density at 0 - ",
    "so there is no expectation to return.\n",
    "Use `posterior_predict()` and summarise the draws with a median or ",
    "another quantile instead.",
    call. = FALSE
  )
}

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.