library(bmm)
library(brms)
library(tidyverse)
library(tidybayes)
library(here)
source(here("_common.R")) # shared workshop colors + ggplot themeThe EZ-Drift Diffusion Model with bmm
Take-home notebook — Psychonomics 2026 bmm workshop, Part 3
Setup
Data
Brightness judgments from Experiment 1 of Ratcliff & Rouder (1998), participant N.H., as reanalyzed in Chávez De la Peña & Vandekerckhove (2025). On each trial the participant decided whether a pixel array was “high” or “low” in brightness (i.e., a binary choice task). Brightness was controlled by the proportion of black-to-white pixels, varied in 32 steps, under accuracy or speed instructions ( design cells).
All columns are documented in the codebook: data/README.md. Each row is a different trial.
dat <- read.csv(here("data", "ezddm_perceptual.csv"))
glimpse(dat)Rows: 7,369
Columns: 4
$ index <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ cond <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ response <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ rt_ms <int> 459, 457, 569, 390, 534, 501, 488, 850, 509, 443, 505, 636, 4…
Before any model: We inspect accuracy in each of the 64 design cells.
We start by coding the response as correct or incorrect. Response 1 (“Brightness is Low”) is correct on the “more-black” conditions and response 2 (“Brightness is High”) is correct on the “more-white” conditions. We then compute the accuracy rate for each cell and plot it against the black-to-white proportion, using different lines for accuracy and speed instructions.
The black-to-white proportion is coded as Xs = ((cond - 1) %% 33 - 16) / 5, so that negative values indicate more black pixels (low brightness), and positive values indicate more white pixels (high brightness). The lines break around zero, which corresponds to the 50/50 black-to-white cells not included in the analysis.
cell_summary <- dat |>
mutate(instruction = if_else(cond <= 33, "accuracy", "speed"),
xs = ((cond - 1) %% 33 - 16) / 5,
majority = if_else(xs < 0, "more black", "more white"),
correct = as.integer((response == 1 & majority == "more black") |
(response == 2 & majority == "more white"))) |>
summarise(accuracy = mean(correct), .by = c(instruction, majority, xs))
ggplot(cell_summary, aes(xs, accuracy, color = instruction,
group = interaction(instruction, majority))) + geom_line() +
geom_point(size = 2) +
scale_color_manual(values = c(accuracy = wk_colors[["blue"]],
speed = wk_colors[["orange"]])) +
labs(x = "Black-to-white pixel configuration (Xs)", y = "Accuracy", color = NULL) +
theme_workshop()
Specify: the model and its formula
We will use the three-parameter EZ-DDM to estimate a drift rate (how quickly evidence accumulates), a boundary separation (how much evidence is required before a choice is made), and a non-decision time (time elapsed outside the accumulation process) for each design cell. The starting point is fixed halfway between the boundaries for each cell.
ezdm() expects one row per design cell containing the required summary statistics from the choice and response time (RT) data. Mean RT and RT variance are in seconds and use every trial in the cell. n_upper is the number of correct responses, and n_trials is the number of trials behind those summaries. This data shape is a contract. Check it against head() before fitting:
ez_summary <- dat |>
mutate(instruction = factor(if_else(cond <= 33, "accuracy", "speed"),
levels = c("accuracy", "speed")),
xs = ((cond - 1) %% 33 - 16) / 5,
abs_xs = abs(xs),
majority = if_else(xs < 0, "more black", "more white"),
correct = as.integer((response == 1 & majority == "more black") |
(response == 2 & majority == "more white")),
rt_s = rt_ms / 1000) |> summarise(mean_rt = mean(rt_s),
var_rt = var(rt_s),
n_upper = sum(correct),
n_trials = n(),
.by = c(cond, instruction, abs_xs))
head(ez_summary, 4) cond instruction abs_xs mean_rt var_rt n_upper n_trials
1 1 accuracy 3.2 0.4906774 0.03015249 31 31
2 2 accuracy 3.0 0.4888750 0.03118168 24 24
3 3 accuracy 2.8 0.4994722 0.02872146 36 36
4 4 accuracy 2.6 0.5037959 0.02274342 49 49
ez_model <- ezdm(mean_rt = "mean_rt",
var_rt = "var_rt",
n_upper = "n_upper",
n_trials = "n_trials",
version = "3par")
ez_modelezdm(mean_rt = "mean_rt", var_rt = "var_rt", n_upper = "n_upper", n_trials = "n_trials", version = "3par")
Response: mean_rt = mean_rt (seconds)
var_rt = var_rt (seconds^2)
n_upper = n_upper (count of upper-boundary responses)
Parameters: drift, bound, ndt, s
Links: drift = identity; bound = log; ndt = log; s = log
Fixed: s = 0, mu = 0
Use parameter_info() for parameter descriptions, links and fixed values.
Printing the model object shows the parameters while identifying the link function applied to each one, and which are fixed. In this example, drift uses an identity link, boundary separation and non-decision time use a log link.
fit_ez <- bmm(formula = bmf(drift ~ 1 + instruction + abs_xs,
bound ~ 1 + instruction,
ndt ~ 1 + instruction),
data = ez_summary,
model = ez_model,
sample_prior = "yes")Drift depends on the instruction condition and on how far the black-to-white pixel configuration sits from 50/50 (abs_xs). Boundary separation and non-decision time depend on the instruction condition alone. The accuracy instruction condition serves as the reference level:
| Coefficient | Meaning |
|---|---|
drift_Intercept |
drift for accuracy instructions when the pixel configuration is 50/50 (abs_xs = 0) |
drift_instructionspeed |
difference in drift between speed and accuracy instructions |
drift_abs_xs |
change in drift as the pixel configuration moves away from 50/50 |
bound_Intercept |
log boundary for accuracy instructions when the pixel configuration is 50/50 (abs_xs = 0) |
bound_instructionspeed |
difference in log boundary between speed and accuracy instructions |
ndt_Intercept |
log non-decision time for accuracy instructions when the pixel configuration is 50/50 (abs_xs = 0) |
ndt_instructionspeed |
difference in log non-decision time between speed and accuracy instructions |
As such, exp(bound_Intercept) and exp(ndt_Intercept) return the boundary and the non-decision time for a 50/50 pixel configuration under accuracy instructions, back on their original scale.
Fit: load the cached model
All workshop models ship pre-fitted in models/. Nothing to compile, nothing to wait for:
fit_ez <- readRDS(here("models", "ezddm_summary.rds"))
summary(fit_ez) Model: ezdm(mean_rt = "mean_rt",
var_rt = "var_rt",
n_upper = "n_upper",
n_trials = "n_trials",
version = "3par")
Links: drift = identity; bound = log; ndt = log; s = log
Formula: drift ~ 1 + instruction + abs_xs
bound ~ 1 + instruction
ndt ~ 1 + instruction
s = 0
Data: ez_summary (Number of observations: 64)
Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup draws = 4000
Regression Coefficients:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS
drift_Intercept 0.54 0.04 0.46 0.62 1.00 3548
bound_Intercept 0.54 0.01 0.52 0.57 1.00 2382
ndt_Intercept -1.63 0.03 -1.70 -1.58 1.00 1821
drift_instructionspeed 1.18 0.06 1.06 1.30 1.00 2942
drift_abs_xs 1.13 0.04 1.06 1.21 1.00 2047
bound_instructionspeed -0.77 0.02 -0.81 -0.74 1.00 2693
ndt_instructionspeed 0.27 0.03 0.22 0.33 1.00 1849
Tail_ESS
drift_Intercept 2832
bound_Intercept 2460
ndt_Intercept 2064
drift_instructionspeed 2111
drift_abs_xs 2470
bound_instructionspeed 2512
ndt_instructionspeed 1964
Constant Parameters:
Value
s_Intercept 0.00
Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
Three things to read in that output before trusting the estimates:
Rhatshould be 1.00. Anything above about 1.01 means the chains have not converged, and the estimates are not ready to be used.Bulk_ESS/Tail_ESSare effective sample sizes. A few hundred is enough for a point estimate. Aim for more than 1000 before quoting the tails of an interval.s_Interceptis exactly 0 with no uncertainty becauseezdm()fixes the diffusion coefficient there. That choice sets the scale of the drift rate and the boundary.
Which priors were used?
prior_info() lists the priors on the scale the model samples on:
prior_info(fit_ez)Model: EZ-Diffusion Model
parameter link class prior source
bound log b normal(0,0.5) bmm default
drift identity b normal(0,0.5) bmm default
ndt log b normal(0,0.3) bmm default
bound log Intercept normal(0,0.5) bmm default
drift identity Intercept cauchy(0,1) bmm default
ndt log Intercept normal(-1.5,0.5) bmm default
s log Intercept constant(0) [native: 1] bmm default
Priors and constants apply on the sampling scale set by each parameter's link
function.
Optional: re-fit the model yourself
You do not need this to follow the workshop. Re-fitting requires the Stan toolchain (Install_Packages.R, Part 2) and takes a few minutes. The fitting script is models/fit_ezddm.R.
Evaluate: posteriors and hypotheses
Boundary separation and non-decision time were sampled on the log scale. Exponentiating the draws puts them back in seconds of evidence and seconds of time. Drift was already on its original scale.
ez_pars <- fit_ez |>
spread_draws(b_drift_Intercept, b_drift_instructionspeed, b_drift_abs_xs,
b_bound_Intercept, b_bound_instructionspeed,
b_ndt_Intercept, b_ndt_instructionspeed) |>
mutate(bound_accuracy = exp(b_bound_Intercept),
bound_speed = exp(b_bound_Intercept + b_bound_instructionspeed),
ndt_accuracy = exp(b_ndt_Intercept),
ndt_speed = exp(b_ndt_Intercept + b_ndt_instructionspeed))
ez_pars |>
pivot_longer(c(bound_accuracy, bound_speed, ndt_accuracy, ndt_speed,
b_drift_abs_xs, b_drift_instructionspeed),
names_to = "parameter") |>
mutate(parameter = recode(parameter,
bound_accuracy = "boundary, accuracy",
bound_speed = "boundary, speed",
ndt_accuracy = "non-decision time, accuracy",
ndt_speed = "non-decision time, speed",
b_drift_abs_xs = "drift, per unit |Xs|",
b_drift_instructionspeed = "drift, speed - accuracy")) |>
group_by(parameter) |> median_qi(value)# A tibble: 6 × 7
parameter value .lower .upper .width .point .interval
<chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 boundary, accuracy 1.72 1.68 1.77 0.95 median qi
2 boundary, speed 0.795 0.775 0.816 0.95 median qi
3 drift, per unit |Xs| 1.13 1.06 1.21 0.95 median qi
4 drift, speed - accuracy 1.18 1.06 1.30 0.95 median qi
5 non-decision time, accuracy 0.196 0.183 0.207 0.95 median qi
6 non-decision time, speed 0.256 0.253 0.260 0.95 median qi
The boundary is in arbitrary units of evidence. The non-decision time is in seconds. Drift is in evidence per second. In the table above, drift, per unit |Xs| is the change in drift for a one-unit step away from a 50/50 pixel configuration, and drift, speed - accuracy is the difference between speed and accuracy instructions.
ez_pars |>
pivot_longer(c(bound_accuracy, bound_speed, ndt_accuracy, ndt_speed),
names_to = c("parameter", "instruction"),
names_sep = "_") |>
mutate(parameter = recode(parameter, bound = "boundary separation",
ndt = "non-decision time")) |>
ggplot(aes(x = value, y = instruction, fill = instruction)) +
stat_halfeye(alpha = 0.8) +
facet_wrap(~ parameter, scales = "free_x") +
scale_fill_manual(values = c(accuracy = wk_colors[["blue"]],
speed = wk_colors[["orange"]]),
guide = "none") +
labs(x = "Posterior value", y = NULL) +
theme_workshop()
We can test any coefficient with one line of code using the hypothesis() function.
# Does drift increase as the pixel configuration moves away from 50/50?
hypothesis(fit_ez, "drift_abs_xs > 0")Hypothesis Tests for class b:
Hypothesis Estimate Est.Error CI.Lower CI.Upper Evid.Ratio Post.Prob
1 (drift_abs_xs) > 0 1.13 0.04 1.07 1.19 Inf 1
Star
1 *
---
'CI': 90%-CI for one-sided and 95%-CI for two-sided hypotheses.
'*': For one-sided hypotheses, the posterior probability exceeds 95%;
for two-sided hypotheses, the value tested against lies outside the 95%-CI.
Posterior probabilities of point hypotheses assume equal prior probabilities.
# Is the boundary lower under speed instructions?
hypothesis(fit_ez, "bound_instructionspeed < 0")Hypothesis Tests for class b:
Hypothesis Estimate Est.Error CI.Lower CI.Upper Evid.Ratio
1 (bound_instructio... < 0 -0.77 0.02 -0.8 -0.74 Inf
Post.Prob Star
1 1 *
---
'CI': 90%-CI for one-sided and 95%-CI for two-sided hypotheses.
'*': For one-sided hypotheses, the posterior probability exceeds 95%;
for two-sided hypotheses, the value tested against lies outside the 95%-CI.
Posterior probabilities of point hypotheses assume equal prior probabilities.
How to read the two outputs:
Estimate,CI.Lower,CI.Upper: the posterior mean of the tested coefficient and its credible interval. These tests are directional, so the interval is one-sided and differs slightly from the 95% interval insummary().Evid.Ratiois the posterior odds in favour of the inequality.Post.Probis the share of draws that satisfy it.Starmarks a hypothesis whose interval excludes zero.
In this fit, drift increases as the black-to-white pixel configuration moves away from 50/50, and the boundary is lower under speed instructions. Both intervals lie entirely on that side of zero. Speed instructions also come with a higher drift and a longer non-decision time.
Scaling up: trial-level DDM
The same three parameters can be estimated from the trials themselves, with a Wiener likelihood, when the study needs the whole RT distribution rather than the cell summaries. ddm() expects one row per trial: rt in seconds, and response coded 1 for a correct judgment and 0 for an error. The starting point stays at its default, halfway between the boundaries.
trial_data <- dat |>
mutate(
instruction = factor(if_else(cond <= 33, "accuracy", "speed"),
levels = c("accuracy", "speed")),
xs = ((cond - 1) %% 33 - 16) / 5,
abs_xs = abs(xs),
majority = if_else(xs < 0, "more black", "more white"),
response = as.integer(
(response == 1 & majority == "more black") |
(response == 2 & majority == "more white")
),
rt = rt_ms / 1000
) |>
select(rt, response, instruction, abs_xs)
head(trial_data, 4) rt response instruction abs_xs
1 0.459 1 accuracy 3.2
2 0.457 1 accuracy 3.2
3 0.569 1 accuracy 3.2
4 0.390 1 accuracy 3.2
trial_model <- ddm(rt = "rt", response = "response")
trial_modelddm(rt = "rt", response = "response")
Response: rt = rt (seconds)
response = response (0/1 or logical; 1 = upper boundary)
Parameters: drift, bound, ndt, zr
Links: drift = identity; bound = log; ndt = log; zr = logit
Fixed: zr = 0, mu = 0
Use parameter_info() for parameter descriptions, links and fixed values.
The formulas are the ones used for the EZ model. The fitted object ships in models/:
fit_trial <- readRDS(here("models", "ddm_trial.rds"))
summary(fit_trial) Model: ddm(rt = "rt",
response = "response")
Links: drift = identity; bound = log; ndt = log; zr = logit
Formula: drift ~ 1 + instruction + abs_xs
bound ~ 1 + instruction
ndt ~ 1 + instruction
zr = 0
Data: trial_data (Number of observations: 7369)
Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup draws = 4000
Regression Coefficients:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS
drift_Intercept 0.67 0.04 0.59 0.75 1.00 4074
bound_Intercept 0.54 0.01 0.52 0.57 1.00 2660
ndt_Intercept -1.52 0.01 -1.54 -1.50 1.00 2521
drift_instructionspeed 0.79 0.06 0.69 0.91 1.00 2698
drift_abs_xs 1.17 0.03 1.10 1.23 1.00 2879
bound_instructionspeed -0.40 0.02 -0.43 -0.37 1.00 2725
ndt_instructionspeed -0.11 0.01 -0.13 -0.09 1.00 2564
Tail_ESS
drift_Intercept 3022
bound_Intercept 2817
ndt_Intercept 2481
drift_instructionspeed 2529
drift_abs_xs 2920
bound_instructionspeed 2964
ndt_instructionspeed 2601
Constant Parameters:
Value
zr_Intercept 0.00
Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
# Safety lock: a saved fit is loaded. Delete models/ddm_trial.rds to compile again.
if(file.exists(here("models", "ddm_trial.rds"))) {
fit_trial <- readRDS(here("models", "ddm_trial.rds"))
}else{
fit_trial <- bmm(formula = bmf(drift ~ 1 + instruction + abs_xs,
bound ~ 1 + instruction,
ndt ~ 1 + instruction),
data = trial_data,
model = trial_model,
sample_prior = "yes",
file = here("models", "ddm_trial"),
file_refit = "on_change",
cores = 4)
}# Posterior mean of drift per unit |Xs|, from each saved fit.
drift_ez <- sprintf("%.2f", fixef(fit_ez)["drift_abs_xs", "Estimate"])
drift_trial <- sprintf("%.2f", fixef(fit_trial)["drift_abs_xs", "Estimate"])The two fits agree that drift increases as the pixel configuration moves away from 50/50 (drift_abs_xs is 1.13 in the EZ fit and 1.17 in the trial-level fit) and that the boundary is lower under speed instructions. That boundary difference is smaller in the trial-level fit. Non-decision time does not agree: under speed instructions it is longer in the EZ fit and shorter in the trial-level fit. The trial-level model uses each response time. The EZ model uses the mean, the variance, and the number correct.
Further reading
Wagenmakers, van der Maas, and Grasman (2007) derive the EZ summary equations. Chávez De la Peña and Vandekerckhove (2025) place that model in a Bayesian hierarchical regression. DOIs and PDFs are listed in resources/README.md.