# /pi -- "longer" phi: lapse rate, mixing proportion
# phi pi: guess rate
# / \1 - pi -- "shorter"
# /
# \ F(x) -- "longer"
# \ /
# 1 - phi
# \1 - F(x) -- "shorter"\[\begin{align*} k &\sim \text{bernoulli}(\phi) \\ y &\sim \begin{cases} p(y | \theta_1), \text{ if } k = 1 \\ p(y | \theta_2), \text{ if } k = 0 \end{cases} \\ p(y | \theta) &= p(y, k = 1 | \theta) + p(y, k = 0 | \theta) \\ &= p(k = 1) p(y | \theta, k = 1) + p(k = 0) p(y | \theta, k = 0) \\ &= \phi \cdot \text{bernoulli}(\pi) + (1 - \phi) \cdot \text{bernoulli}(F(x)) \end{align*}\]
s <- "
data {
int<lower=1> N;
vector[N] cx;
array[N] int<lower=0, upper=1> y;
}
parameters {
real phi_l; // logit mixing proportion (lapse rate)
real alpha;
real beta;
real pi_l; // logit 'longer' rate under contamination (guess rate)
}
model {
phi_l ~ normal(-1, 1.5);
alpha ~ normal(0, 2);
beta ~ normal(0.1, 0.1);
pi_l ~ normal(0, 1);
for (j in 1:N) {
target += log_mix(
inv_logit(phi_l),
bernoulli_logit_lpmf(y[j] | pi_l),
bernoulli_logit_lpmf(y[j] | alpha + beta * cx[j])
);
}
}
generated quantities {
real phi = inv_logit(phi_l);
real pi = inv_logit(pi_l);
real pse = -alpha/beta;
real jnd = logit(.84) * 1/beta;
real gamma = phi * pi;
real lambda = phi * (1 - pi);
}
"
m1 <- stan(model_code = s, data = datlist)
print(m1, pars = c("alpha", "beta", "phi", "pi", "pse", "jnd", "gamma",
"lambda"), probs = c(.025, .975))## Inference for Stan model: anon_model.
## 4 chains, each with iter=2000; warmup=1000; thin=1;
## post-warmup draws per chain=1000, total post-warmup draws=4000.
##
## mean se_mean sd 2.5% 97.5% n_eff Rhat
## alpha -0.41 0.02 0.64 -2.07 0.49 748 1.00
## beta 0.10 0.00 0.04 0.06 0.20 675 1.01
## phi 0.18 0.00 0.08 0.05 0.36 991 1.00
## pi 0.44 0.00 0.17 0.12 0.75 1332 1.00
## pse 3.14 0.14 4.59 -5.76 12.11 1150 1.00
## jnd 18.24 0.18 5.41 8.24 28.84 881 1.01
## gamma 0.08 0.00 0.05 0.01 0.20 880 1.00
## lambda 0.10 0.00 0.05 0.03 0.20 1763 1.00
##
## Samples were drawn using NUTS(diag_e) at Wed Nov 11 16:13:05 2026.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at
## convergence, Rhat=1).
datlist$stimlevel <- factor(dat3$x) |> as.numeric()
datlist$nstim <- factor(dat3$x) |> unique() |> length()
s <- "
data {
int<lower=1> N;
int<lower=1> nstim; // number of stimulus levels
array[N] int<lower=1, upper=nstim> stimlevel; // stimulus level id
vector[N] cx;
array[N] int<lower=0, upper=1> y;
}
parameters {
real phi_l;
real alpha;
real beta;
vector[nstim] pi_l; // by-stimulus-level logit guess rate
}
model {
phi_l ~ normal(-1, 1.5);
alpha ~ normal(0, 2);
beta ~ normal(0.1, 0.1);
pi_l ~ normal(0, 1);
for (j in 1:N) {
target += log_mix(
inv_logit(phi_l),
bernoulli_logit_lpmf(y[j] | pi_l[stimlevel[j]]),
bernoulli_logit_lpmf(y[j] | alpha + beta * cx[j])
);
}
}
generated quantities {
real phi = inv_logit(phi_l);
real pse = -alpha/beta;
real jnd = logit(.84) * 1/beta;
vector[nstim] pi = inv_logit(pi_l);
}
"
m2 <- stan(model_code = s, data = datlist)
print(m2, pars = c("alpha", "beta", "pi[7]", "pi[22]", "pse", "jnd", "phi"),
probs = c(.025, .975))## Inference for Stan model: anon_model.
## 4 chains, each with iter=2000; warmup=1000; thin=1;
## post-warmup draws per chain=1000, total post-warmup draws=4000.
##
## mean se_mean sd 2.5% 97.5% n_eff Rhat
## alpha -0.56 0.01 0.56 -1.98 0.23 2009 1
## beta 0.11 0.00 0.04 0.06 0.22 1640 1
## pi[7] 0.64 0.00 0.17 0.28 0.91 5625 1
## pi[22] 0.35 0.00 0.17 0.08 0.70 7562 1
## pse 4.44 0.06 3.50 -2.43 11.11 3289 1
## jnd 16.11 0.10 5.04 7.57 26.26 2401 1
## phi 0.22 0.00 0.08 0.07 0.39 2756 1
##
## Samples were drawn using NUTS(diag_e) at Wed Nov 11 16:14:17 2026.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at
## convergence, Rhat=1).
datlist <- c(dat2,
cx = list(dat2$x - ave(dat2$x, dat2$subj)),
N = nrow(dat2),
nsubj = unique(dat2$subj) |> length(),
stimlevel = list(interaction(dat2$x, dat2$subj) |> factor() |> as.numeric()),
nstim = interaction(dat2$x, dat2$subj) |> unique() |> length()
)s <- "
data {
int<lower=1> N;
int<lower=1> nsubj;
int<lower=1> nstim;
array[N] int<lower=1, upper=nsubj> subj;
array[N] int<lower=1, upper=nstim> stimlevel;
vector[N] cx;
array[N] int<lower=0, upper=1> y;
}
parameters {
vector[nsubj] phi_l;
vector[nsubj] alpha;
vector[nsubj] beta;
vector[nstim] pi_l;
}
model {
phi_l ~ normal(-1, 1.5);
alpha ~ normal(0, 2);
beta ~ normal(0.1, 0.1);
pi_l ~ normal(0, 1);
for (j in 1:N) {
target += log_mix(
inv_logit(phi_l[subj[j]]),
bernoulli_logit_lpmf(y[j] | pi_l[stimlevel[j]]),
bernoulli_logit_lpmf(y[j] | alpha[subj[j]] + beta[subj[j]] * cx[j])
);
}
}
generated quantities {
vector[nsubj] phi = inv_logit(phi_l);
vector[nsubj] pse = -alpha ./ beta;
vector[nsubj] jnd = logit(.84) * 1/beta;
vector[nstim] pi = inv_logit(pi_l);
vector[N] pmp; // posterior mixing proportion
array[N] int<lower=0, upper=1> yrep;
for (j in 1:N) {
pmp[j] = softmax([
log(phi[subj[j]]) +
bernoulli_logit_lpmf(y[j] | pi_l[stimlevel[j]]),
log1m(phi[subj[j]]) +
bernoulli_logit_lpmf(y[j] | alpha[subj[j]] + beta[subj[j]] * cx[j])
]')[1];
yrep[j] = pmp[j] > 0.5 ?
bernoulli_logit_rng(pi_l[stimlevel[j]]) :
bernoulli_logit_rng(alpha[subj[j]] + beta[subj[j]] * cx[j]);
}
}
"
m <- stan(model_code = s, data = datlist)
print(m, pars = c("alpha[1]", "alpha[4]", "beta[1]", "beta[4]", "pse[1]",
"pse[4]", "jnd[1]", "jnd[4]", "phi[1]", "phi[4]"),
probs = c(.025, .975))## Inference for Stan model: anon_model.
## 4 chains, each with iter=2000; warmup=1000; thin=1;
## post-warmup draws per chain=1000, total post-warmup draws=4000.
##
## mean se_mean sd 2.5% 97.5% n_eff Rhat
## alpha[1] -0.70 0.03 0.61 -2.62 -0.11 473 1.00
## alpha[4] -0.54 0.01 0.55 -1.97 0.25 1717 1.00
## beta[1] 0.06 0.00 0.03 0.03 0.16 435 1.01
## beta[4] 0.11 0.00 0.04 0.06 0.22 1375 1.00
## pse[1] 11.49 0.08 4.60 2.37 20.39 3213 1.00
## pse[4] 4.35 0.06 3.55 -2.81 11.10 2984 1.00
## jnd[1] 33.18 0.34 9.52 10.13 49.61 798 1.00
## jnd[4] 16.31 0.12 5.07 7.62 26.93 1795 1.00
## phi[1] 0.12 0.00 0.09 0.01 0.35 872 1.00
## phi[4] 0.22 0.00 0.08 0.07 0.39 2146 1.00
##
## Samples were drawn using NUTS(diag_e) at Wed Nov 11 16:20:39 2026.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at
## convergence, Rhat=1).
lattice::xyplot(
extract(m)$pi |> colMeans() ~ x | factor(subj), dat,
type = c("g", "smooth", "p"), layout = c(4, 2),
ylab = "'Longer' rate under contamination"
)\[ p(k = 1 | y, \theta) = \frac{p(k = 1) p(y | \theta, k = 1)} {p(k = 1) p(y | \theta, k = 1) + p(k = 0) p(y | \theta, k = 0)} \]
Background:
lattice::xyplot(
extract(m)$pmp |>
apply(1, function(x) tapply(x, datlist$stimlevel, mean)) |>
rowMeans() ~ x | factor(subj), dat,
type = c("g", "smooth", "p"), layout = c(4, 2),
ylab = "Posterior contamination rate"
)extract(m)$yrep |>
apply(1, function(x) tapply(x, datlist$stimlevel, mean)) |>
_[dat$subj == 4, ] |>
t() |>
boxplot(border = "darkgray", at = dat[dat$subj == 4, "x"], boxwex = 4,
xaxt = "n", xlab = "Test interval (ms)", ylab = "P('longer')")
axis(1, at = dat[dat$subj == 4, "x"])
lines(y/n ~ x, dat[dat$subj == 4, ], type = "b", pch = 4, col = "darkblue")