Nothing
#' @title Log-Normal Race (LNR) Model
#'
#' @description
#' The Log-Normal Race (LNR) model is useful for modeling reaction times and
#' choices in decision-making tasks. Each choice option (accumulator) draws a
#' processing time from a LogNormal distribution; the winning accumulator (the
#' minimum draw) determines both the observed reaction time and the choice. The
#' observed RT is that decision time shifted by a non-decision time `ndt`, and a
#' fixed proportion `poutlier` of responses is generated by an outlier process
#' instead of by the race.
#'
#' Functions:
#' - `rcogmod_lnr()`: Simulates random draws from the LNR model.
#' - `dcogmod_lnr()`: Computes the density (likelihood).
#' - `cogmod_lnr()`: Creates a `brms::custom_family()` for use in `brms` models.
#' - `cogmod_lnr_stanvars()`: Generates the `stanvars` to pass to `brm()`.
#' - `p_outlier()`: Per-trial posterior probability of being an outlier.
#'
#' @details
#' # Parameterization
#'
#' Each accumulator `k` runs from a start point `z_k ~ Uniform(0, sigmabias)` to
#' the threshold `1 + sigmabias` at a rate `v_k ~ LogNormal(nu_k, sigma_k)`, so
#' its finishing time is `(1 + sigmabias - z_k) / v_k`. With `sigmabias = 0` -
#' the default of `rcogmod_lnr()` and `dcogmod_lnr()`, and the value to fix in
#' the formula unless the design can identify a start-point range - the
#' distance is 1, the finishing time is `1 / v_k`, and it is LogNormal with
#' `meanlog = -nu_k` and `sdlog = sigma_k`: the LNR proper, in which **larger
#' `nu` means faster**. The observed reaction time is `ndt + min(T_0, T_1)` and
#' the observed choice is whichever accumulator got there first.
#'
#' # The start-point range
#'
#' The LNR is the LBA ([rcogmod_lba2()]) with LogNormal drift rates and the
#' start point removed. In an LBA the finishing time is the distance to the
#' threshold divided by the rate; if both are LogNormal the ratio is LogNormal
#' too, with the two log-variances summed into one `sigma`, which is why the
#' LNR of Heathcote and Love (2012) has no start-point parameter of its own -
#' start-point and rate variability cannot be told apart. A *Uniform* start
#' point is a different matter: the distance is then `Uniform(1, 1 + sigmabias)`
#' and the ratio is no longer LogNormal, so `sigmabias` is identified, at least
#' in principle, through the shape it gives the RT distribution.
#'
#' The threshold *offset* above the highest start point is pinned at 1, the
#' LBA's `boundary` convention with `boundary = 1`. It has to be the threshold
#' rather than `sigma` that pins the evidence scale: rescaling the evidence axis
#' shifts `nu` and scales `sigmabias` and the threshold but leaves a LogNormal
#' rate's `sigma` untouched, so `sigma = 1` would fix nothing. `sigmabias` is
#' therefore read in units of that offset: `sigmabias = 1` says the start point
#' varies over as wide a band as the one above it.
#'
#' Estimating `sigmabias` is treacherous in the same way as in [cogmod_lba1()].
#' As it approaches zero the likelihood goes flat - the model is converging to
#' the LNR and once the range is small enough making it smaller changes nothing
#' - while the softplus link reaches zero only at minus infinity. Left flat
#' that is an improper posterior; [cogmod_priors()] fences it. A start-point
#' range is also hard to identify from RT shape alone, and the general density
#' costs about four normal CDFs per accumulator where the LNR costs one density
#' and one survival. Fix `sigmabias = 0` in the formula unless the design
#' speaks to start-point variability; at zero the family computes exactly what
#' it computed before the parameter existed, at the same cost.
#'
#' `ndt` is expressed **directly, in seconds** (through a log link in the `brms`
#' family). Nothing about it is taken from the data: it is not bounded by the
#' fastest observed response, so a non-decision time that varies by condition or
#' by participant can exceed the sample minimum wherever the data support it.
#'
#' Tying `ndt` to the fastest observed response would cap it at an order
#' statistic of the sample, so any condition or participant whose true `ndt`
#' exceeded that response would be inexpressible, and the misfit would surface
#' as spurious effects on the race parameters. Expressing it directly is what
#' avoids that.
#'
#' # The outlier component
#'
#' A shifted distribution assigns exactly zero density to any response faster
#' than `ndt`, which puts a hard boundary in the likelihood at the fastest
#' observed RT. Mixing in a component with support over the whole positive line
#' removes it: every response keeps positive density whatever `ndt` is, so the
#' boundary becomes a finite cost rather than a wall and the log-density stays
#' smooth and differentiable. That is what makes the direct parameterization of
#' `ndt` workable without taking a bound from the data.
#'
#' Because this model produces a **choice as well as a time**, the contaminant
#' has to produce both. It is a guess: the choice is uniform over the two
#' options, and the RT is a **half Normal** with scale `0.2` seconds.
#'
#' \deqn{f(t, k) = p \frac{1}{K} g(t) + (1 - p) f_k(t - ndt)}
#'
#' The `1 / K` is what keeps the total summing to one over the response options;
#' without it it would come to `1 + poutlier`. A half Normal is used for the
#' timing because it is **flat at the origin** (zero derivative), so the very
#' fastest responses - the ones least plausibly decisions - are not starved of
#' density, and because it dies away fast enough above `ndt` to leave the slow
#' tail to the decision process. Plot it with `curve(2 * dnorm(x, 0, 0.2), 0, 3)`.
#'
#' `poutlier` is a *rate*, not a classification: the model never labels
#' individual trials, it estimates what share of them came from elsewhere. Use
#' [p_outlier()] for per-trial posterior probabilities.
#'
#' # Reaction times must be in seconds
#'
#' The outlier component's scale is a constant in seconds, and so are the
#' priors [cogmod_priors()] supplies. There is no argument for changing the
#' unit. Millisecond data fails silently rather than loudly - the outlier
#' component contributes nothing and the min-RT boundary comes back. See the
#' corresponding section of [cogmod_lognormal()] for the full account, which
#' applies unchanged here.
#'
#' # Fitting
#'
#' ```r
#' f <- brms::bf(RT | dec(Error) ~ Condition, nuone ~ Condition,
#' sigmazero ~ 1, sigmaone ~ 1, sigmabias = 0,
#' ndt ~ 1, poutlier ~ 1,
#' family = cogmod_lnr())
#' brms::brm(f, data = df,
#' prior = cogmod_priors(f, df),
#' init = cogmod_inits(f, df),
#' stanvars = cogmod_stanvars(f))
#' ```
#'
#' Use [cogmod_inits()] rather than `init = 0`. `brms` initialises on the
#' unconstrained scale, so `init = 0` puts `ndt` at `exp(0) = 1` second - above
#' nearly every sub-second RT, which leaves every response attributed to the
#' outlier component and the race parameters with no gradient at all.
#'
#' [cogmod_priors()] is not a convenience here either. Beyond `ndt` and
#' `poutlier`, a race has a flat direction of its own: push an accumulator's
#' rate far enough down and it stops finishing first *ever*, so the density
#' depends on it only through the loser's survival term, which has already
#' saturated at 1. Past about `nuone = -6` the log-likelihood is **exactly**
#' constant, and that accumulator's `sigma` is unidentified along with it -
#' nothing is left for it to act on. On the identity link `nuone` uses, that is
#' an unbounded flat region under a flat prior: an improper posterior, the same
#' failure as `poutlier` running to 1.
#'
#' The outlier component makes this reachable rather than hypothetical. Without
#' it, an accumulator that never wins would still have to explain the trials on
#' which the *other* one lost, and the likelihood would object. With it, those
#' trials are floored by the contaminant instead, so the plateau is there even
#' when both responses are well represented. [cogmod_priors()] fences off
#' `nuone`, `sigmazero` and `sigmaone` for this reason.
#'
#' `mu` is `nuzero` and has the mirror-image plateau, but it is the response's
#' own intercept, so `brms` already gives it a proper `student_t` default and
#' [cogmod_priors()] leaves it alone. If one option is chosen only rarely, the
#' accumulator that loses is the one at risk, and it is worth putting the same
#' prior on both by hand:
#'
#' ```r
#' priors <- c(cogmod_priors(f, df),
#' brms::prior(normal(0.7, 1.5), class = "Intercept"),
#' replace = TRUE)
#' ```
#'
#' # Predictions exclude the outlier component
#'
#' `posterior_predict()` describes the **race alone** by default, as if
#' `poutlier` were zero, because the outlier component is a fixed regularizer
#' rather than a claim about how guesses are distributed. Use [with_outliers()]
#' for the fitted mixture - chiefly for `brms::pp_check()` - and
#' [without_outliers()] to go back. `log_lik()` is always the full mixture.
#'
#' `posterior_epred()` is not provided: for a race model the expectation needs
#' numerical integration per draw and per observation, and users are better off
#' summarising `posterior_predict()` draws.
#'
#' @param n Number of simulated trials. If `length(n) > 1`, the length is taken
#' to be the number required.
#' @param nuzero,nuone The (inverse of the) log-space mean parameter for both
#' accumulators (choice 0 and 1). Controls the central tendency of the
#' reaction time. Can take any real value (-Inf, Inf), with larger values
#' leading to *faster* RTs. Named 'nu' (=-meanlog) for consistency with other
#' race models.
#' @param sigmazero,sigmaone The log-space standard deviation for both
#' accumulators (choice 0 and 1). Controls the variability of reaction times.
#' Must be positive (0, Inf). Larger values increase variability.
#' @param sigmabias Start-point range, in units of the threshold offset: each
#' accumulator starts at `Uniform(0, sigmabias)` and runs to the threshold
#' `1 + sigmabias`. Must be non-negative. At `0` (the default) both start at
#' zero on every trial and the model is the plain LNR; see the section on
#' the start-point range.
#' @param ndt Non-decision time (shift parameter), in seconds. Represents the
#' time taken for processes unrelated to the decision (e.g., encoding, motor
#' response). Must be non-negative. Range: [0, Inf).
#' @param poutlier Proportion of responses generated by the outlier process
#' rather than by the race. Range: `[0, 1]`. At `poutlier = 0` the
#' distribution reduces to the plain shifted LNR.
#'
#' @references
#' - Rouder, J. N., Province, J. M., Morey, R. D., Gomez, P., & Heathcote, A. (2015).
#' The lognormal race: A cognitive-process model of choice and latency with desirable
#' psychometric properties. Psychometrika, 80(2), 491-513.
#' \doi{10.1007/s11336-013-9396-3}
#' - Heathcote, A., & Love, J. (2012). Linear deterministic accumulator models of
#' simple choice. Frontiers in Psychology, 3, 292.
#' \doi{10.3389/fpsyg.2012.00292}
#'
#' @return `rcogmod_lnr()` returns a data frame with `n` rows and two columns,
#' `rt` (the simulated reaction time, in seconds) and `response` (the
#' boundary reached, `0` or `1`, matching the `dec()` coding used by the
#' `brms` family). `dcogmod_lnr()` returns the defective density at each
#' element of `x` - the log density if `log = TRUE` - recycled to the length
#' of the longest argument. `cogmod_lnr()` returns a `brms::custom_family`
#' object, to put on a `brms::bf()` formula. `cogmod_lnr_stanvars()` returns
#' a `brms::stanvars` object holding the family's Stan `functions` block, to
#' pass to `brms::brm()`, and `cogmod_lnr_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_lnr()` returns a
#' numeric vector holding one log-likelihood value per posterior draw for
#' observation `i`, and `posterior_predict_cogmod_lnr()` a draws x 2 matrix
#' of reaction times and choices simulated for observation `i` - or, given a
#' vector of observation indices, a `(draws * length(i))` x 2 matrix with the
#' draws for `i[1]` first, which predicts many observations in one vectorised
#' call; see [posterior_predict_cogmod_ddm()] for the recipe.
#' `posterior_epred_cogmod_lnr()` returns nothing: the expected reaction
#' time of a race has no closed form, so it errors rather than report one -
#' summarise `posterior_predict()` draws instead.
#'
#' @examples
#' # Simulate data, with 2% of trials from the outlier process
#' data <- rcogmod_lnr(1000,
#' nuzero = 1, nuone = 0.5, sigmazero = 1, sigmaone = 0.8,
#' ndt = 0.2, poutlier = 0.02
#' )
#' head(data)
#'
#' # Responses faster than ndt keep positive density, unlike the unmixed model
#' dcogmod_lnr(0.1, ndt = 0.2, response = 0, poutlier = 0.02)
#' dcogmod_lnr(0.1, ndt = 0.2, response = 0, poutlier = 0)
#'
#' @export
rcogmod_lnr <- function(n, nuzero = 0, nuone = 0, sigmazero = 1, sigmaone = 1,
ndt = 0.2, sigmabias = 0, poutlier = 0) {
.rchoice("cogmod_lnr", n = n, ndt = ndt, poutlier = poutlier, mu = nuzero,
nuone = nuone, sigmazero = sigmazero, sigmaone = sigmaone,
sigmabias = sigmabias)
}
#' @rdname rcogmod_lnr
#' @param x The observed reaction time (RT).
#' @param response The decision indicator (0 or 1). 0 for choice 0, 1 for
#' choice 1.
#' @param log Logical; if TRUE, returns the log-density. Default: FALSE.
#' @export
dcogmod_lnr <- function(x, nuzero = 0, nuone = 0, sigmazero = 1, sigmaone = 1,
ndt = 0.2, response, sigmabias = 0, poutlier = 0,
log = FALSE) {
.dchoice("cogmod_lnr", x = x, response = response, ndt = ndt,
poutlier = poutlier, log = log,
mu = nuzero, nuone = nuone, sigmazero = sigmazero,
sigmaone = sigmaone, sigmabias = sigmabias)
}
# Decision component ------------------------------------------------------
# The LNR is a race between two of the accumulators cogmod_lognormal() is: each
# runs from z ~ Uniform(0, sigmabias) to the threshold 1 + sigmabias at a rate
# v ~ LogNormal(nu, sigma), so its finishing time is a LogNormal(-nu, sigma)
# time multiplied by a Uniform(1, 1 + sigmabias) distance. The kernels - the
# density, both tails, the threshold-offset pin and the numerics - live in
# core_shifted.R with the LogNormal family (.lognormal_acc_ldens() and
# friends); they take the finishing time's meanlog, which is -nu here. At
# sigmabias = 0 they call the plain lognormal functions, so the LNR proper
# costs what it did before the parameter existed. See ?rcogmod_lnr.
# Defective log-density of one race outcome: the winner finishing at t while
# the loser has not. `.ldec_choice()` masks out `t <= 0` and restores the shape
# afterwards, so this only has to work elementwise; everything is flattened
# because `p_outlier()` passes draws x observations matrices.
#' @keywords internal
.lnr_ldens <- function(t, k, p) {
nu_win <- as.vector(ifelse(k == 0, p$mu, p$nuone))
s_win <- as.vector(ifelse(k == 0, p$sigmazero, p$sigmaone))
nu_los <- as.vector(ifelse(k == 0, p$nuone, p$mu))
s_los <- as.vector(ifelse(k == 0, p$sigmaone, p$sigmazero))
tv <- as.vector(t)
aa <- as.vector(p$sigmabias)
.lognormal_acc_ldens(tv, -nu_win, s_win, aa) +
.lognormal_acc_lccdf(tv, -nu_los, s_los, aa)
}
# Decision-component sampler. 1 / v ~ LogNormal(-nu, sigma) is drawn directly
# and first, so at sigmabias = 0 the stream is exactly what rcogmod_lnr() drew
# before the parameter existed; the distances multiply it afterwards.
#' @keywords internal
.lnr_rng <- function(n, p) {
r0 <- stats::rlnorm(n, meanlog = -p$mu, sdlog = p$sigmazero)
r1 <- stats::rlnorm(n, meanlog = -p$nuone, sdlog = p$sigmaone)
d0 <- 1 + stats::runif(n, min = 0, max = p$sigmabias)
d1 <- 1 + stats::runif(n, min = 0, max = p$sigmabias)
t0 <- d0 * r0
t1 <- d1 * r1
list(rt = pmin(t0, t1), response = as.numeric(t0 >= t1))
}
# The kernels are shared with cogmod_lognormal() (see .LOGNORMAL_STAN_PRELUDE
# in core_shifted.R); only the race on top of them is specific to this family.
#' @keywords internal
.LNR_STAN_PRELUDE <- paste0(
.LOGNORMAL_STAN_PRELUDE,
"
// Defective log-density of one LNR race outcome at decision time t: the winner
// finishing just then while the loser has not finished at all. Each accumulator
// is cogmod_lognormal()'s, with meanlog = -nu.
real cogmod_lnr_decision_lpdf(real t, real nu_win, real s_win,
real nu_los, real s_los, real sigmabias) {
return cogmod_lognormal_acc_ldens(t, -nu_win, s_win, sigmabias)
+ cogmod_lognormal_acc_logsurv(t, -nu_los, s_los, sigmabias);
}
"
)
# Family ------------------------------------------------------------------
#' @rdname rcogmod_lnr
#' @param link_mu,link_nuone Link function for the nu parameters. `mu` is
#' `nuzero`: `brms` requires the first distributional parameter of a custom
#' family to be called `mu`, so that is the name the formula and this
#' argument use, and `nuzero` is what it means.
#' @param link_sigmazero,link_sigmaone Link function for the sigma parameters.
#' @param link_sigmabias Link function for the start-point range. Softplus, so
#' that the natural-scale value reaches zero only at minus infinity - see
#' the start-point range section for why the range is best fixed at zero in
#' the formula rather than estimated.
#' @param link_ndt,link_poutlier Link functions for the non-decision time and
#' the outlier rate.
#' @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 race alone; the likelihood is
#' always the full mixture either way. On the prediction method itself the
#' default is `NULL`, which defers to the flag carried on the model - see
#' [with_outliers()] to change it after fitting. See Details.
#' @export
cogmod_lnr <- function(
link_mu = "identity",
link_nuone = "identity",
link_sigmazero = "softplus",
link_sigmaone = "softplus",
link_sigmabias = "softplus",
link_ndt = "log",
link_poutlier = "logit",
predict_outliers = FALSE
) {
.choice_family(
"cogmod_lnr",
links = c(link_mu, link_nuone, link_sigmazero, link_sigmaone,
link_sigmabias),
predict_outliers = predict_outliers
)
}
# Stanvars ----------------------------------------------------------------
#' @keywords internal
.cogmod_lnr_lpdf <- function() {
.choice_lpdf("cogmod_lnr")
}
#' @rdname rcogmod_lnr
#' @examples
#' \dontrun{
#' # Needs cmdstanr and a CmdStan toolchain, which live outside CRAN - see the
#' # package website to install them. Not run under R CMD check, which executes
#' # every example in one R session: once brms has fitted a model there (the
#' # cogmod_inits() and p_outlier() examples do), rstan is live in the process
#' # and loading an exposed Stan function next to it segfaults on Linux.
#' lpdf <- cogmod_lnr_lpdf_expose()
#' lpdf(
#' Y = 0.5, mu = 0.5, nuone = 0.2, sigmazero = 1.0, sigmaone = 0.8,
#' ndt = 0.2, poutlier = 0.02, dec = 0
#' )
#' }
#'
#' @export
cogmod_lnr_lpdf_expose <- function() {
.choice_expose("cogmod_lnr")
}
#' @rdname rcogmod_lnr
#' @export
cogmod_lnr_stanvars <- function() {
brms::stanvar(
scode = .cogmod_lnr_lpdf(),
block = "functions"
)
}
# brms methods ------------------------------------------------------------
#' @rdname rcogmod_lnr
#' @inheritParams rcogmod_betagate
#' @export
log_lik_cogmod_lnr <- function(i, prep) {
.log_lik_choice("cogmod_lnr", i, prep)
}
#' @rdname rcogmod_lnr
#' @inheritParams rcogmod_betagate
#' @importFrom brms get_dpar
#' @export
posterior_predict_cogmod_lnr <- function(i, prep, predict_outliers = NULL,
...) {
.posterior_predict_choice("cogmod_lnr", i, prep, predict_outliers)
}
#' @rdname rcogmod_lnr
#' @export
posterior_epred_cogmod_lnr <- function(prep) {
stop(
"Computing posterior_epred for a race model needs numerical integration ",
"per draw and per observation, which is prohibitive here. Use ",
"posterior_predict() and summarise the draws instead.",
call. = FALSE
)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.