| rcogmod_lnr | R Documentation |
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.
rcogmod_lnr(
n,
nuzero = 0,
nuone = 0,
sigmazero = 1,
sigmaone = 1,
ndt = 0.2,
sigmabias = 0,
poutlier = 0
)
dcogmod_lnr(
x,
nuzero = 0,
nuone = 0,
sigmazero = 1,
sigmaone = 1,
ndt = 0.2,
response,
sigmabias = 0,
poutlier = 0,
log = FALSE
)
cogmod_lnr(
link_mu = "identity",
link_nuone = "identity",
link_sigmazero = "softplus",
link_sigmaone = "softplus",
link_sigmabias = "softplus",
link_ndt = "log",
link_poutlier = "logit",
predict_outliers = FALSE
)
cogmod_lnr_lpdf_expose()
cogmod_lnr_stanvars()
log_lik_cogmod_lnr(i, prep)
posterior_predict_cogmod_lnr(i, prep, predict_outliers = NULL, ...)
posterior_epred_cogmod_lnr(prep)
n |
Number of simulated trials. If |
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. |
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. |
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). |
sigmabias |
Start-point range, in units of the threshold offset: each
accumulator starts at |
poutlier |
Proportion of responses generated by the outlier process
rather than by the race. Range: |
x |
The observed reaction time (RT). |
response |
The decision indicator (0 or 1). 0 for choice 0, 1 for choice 1. |
log |
Logical; if TRUE, returns the log-density. Default: FALSE. |
link_mu, link_nuone |
Link function for the nu parameters. |
link_sigmazero, link_sigmaone |
Link function for the sigma parameters. |
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. |
link_ndt, link_poutlier |
Link functions for the non-decision time and the outlier rate. |
predict_outliers |
Logical; whether |
i, prep |
For brms' functions to run: index of the observation and a |
... |
Additional arguments. |
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.
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 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.
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.
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.
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.
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:
priors <- c(cogmod_priors(f, df),
brms::prior(normal(0.7, 1.5), class = "Intercept"),
replace = TRUE)
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.
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. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1007/s11336-013-9396-3")}
Heathcote, A., & Love, J. (2012). Linear deterministic accumulator models of simple choice. Frontiers in Psychology, 3, 292. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.3389/fpsyg.2012.00292")}
# 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)
## Not run:
# 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
)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.