dat <- read.table("../IvryTimeDiscrim.txt", header = TRUE)
lattice::xyplot(y/n ~ x | factor(subj), dat, type = c("g", "p", "smooth"),
xlab = "Test interval (ms)", ylab = "P('longer')")Logistic psychometric function \[\begin{align*} \text{Pr('longer')} &= F(x) = \frac{1}{1 + \exp(-(\alpha + \beta \cdot x))}\\ \text{logit}(F(x)) &= \log\frac{F(x)}{1 - F(x)} = \alpha + \beta \cdot x, \qquad x_p = \frac{\text{logit}(p) - \alpha}{\beta} \end{align*}\]
## Prior sd's
plotPsyMetFun <- function(mu = c(0, 0.1), sigma = c(2, 0.1), nrep = 30) {
xval <- seq(-120, 120, length.out = 25)
replicate(nrep, {
alpha <- rnorm(1, mu[1], sigma[1])
beta <- rnorm(1, mu[2], sigma[2])
plogis(alpha + beta * xval)
}) |>
matplot(xval, y = _, type = "l", xlab = "Centered test interval (ms)",
ylab = "P('longer')")
}
plotPsyMetFun()s <- "
data {
int<lower=1> N;
int<lower=1> nsubj;
vector[N] cx; // centered test duration (ms)
array[N] int<lower=1, upper=nsubj> subj;
array[N] int<lower=1> n; // number of trials
array[N] int<lower=0> y; // 'longer' responses
}
parameters {
vector[2] mu; // group pars on logit scale
vector<lower=0>[2] sigma;
vector<offset=mu[1], multiplier=sigma[1]>[nsubj] alpha; // by-subj intercept
vector<offset=mu[2], multiplier=sigma[2]>[nsubj] beta; // by-subj slope
}
model {
mu ~ normal([0, 0.1], [2, 0.1]);
sigma ~ normal(0, [5, 3]);
alpha ~ normal(mu[1], sigma[1]);
beta ~ normal(mu[2], sigma[2]);
y ~ binomial_logit(n, alpha[subj] + beta[subj] .* cx);
}
generated quantities {
vector[nsubj] pse = -alpha ./ beta;
vector[nsubj] jnd = logit(.84) * 1/beta; // x.84 - x.50
}
"
m1 <- stan(model_code = s, data = datlist)## Warning: There were 1 divergent transitions after warmup. See
## https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
## to find out why this is a problem and how to eliminate them.
## Warning: Examine the pairs() plot to diagnose sampling problems
print(m1, pars = c("mu", "sigma", "alpha[1]", "alpha[4]", "beta[1]",
"beta[4]", "pse[1]", "pse[4]", "jnd[1]", "jnd[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
## mu[1] 0.19 0.01 0.36 -0.54 0.94 1195 1.01
## mu[2] 0.04 0.00 0.01 0.03 0.06 1482 1.00
## sigma[1] 0.98 0.01 0.37 0.50 1.91 1163 1.00
## sigma[2] 0.01 0.00 0.01 0.01 0.03 1090 1.01
## alpha[1] -0.38 0.00 0.17 -0.71 -0.05 4839 1.00
## alpha[4] -0.29 0.00 0.16 -0.61 0.02 5111 1.00
## beta[1] 0.04 0.00 0.00 0.03 0.05 5078 1.00
## beta[4] 0.05 0.00 0.01 0.04 0.06 5448 1.00
## pse[1] 9.33 0.06 4.16 1.30 17.41 4827 1.00
## pse[4] 6.11 0.05 3.41 -0.51 12.84 4774 1.00
## jnd[1] 41.65 0.06 4.86 33.50 52.60 5588 1.00
## jnd[4] 35.65 0.06 4.63 27.85 45.99 5611 1.00
##
## Samples were drawn using NUTS(diag_e) at Wed Nov 11 13:42:27 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).
Lower asymptote \(\gamma\), upper asymptote \(\lambda\) \[ \text{Pr('longer')} = \gamma + (1 - \gamma - \lambda) \cdot F(x) \]
s <- "
data {
int<lower=1> N;
int<lower=1> nsubj;
vector[N] cx;
array[N] int<lower=1, upper=nsubj> subj;
array[N] int<lower=1> n;
array[N] int<lower=0> y;
}
parameters {
vector[2] mu; // group alpha, beta
vector<upper=0>[2] nu; // group gamma, lambda; bounded < 0.5
vector<lower=0>[4] sigma;
vector<offset=mu[1], multiplier=sigma[1]>[nsubj] alpha;
vector<offset=mu[2], multiplier=sigma[2]>[nsubj] beta;
matrix<upper=0>[nsubj, 2] z; // bounded < 0.5
}
transformed parameters { // by-subject lower and upper asymptotes
vector[nsubj] gamma = inv_logit(nu[1] + sigma[3] * z[, 1]);
vector[nsubj] lambda = inv_logit(nu[2] + sigma[4] * z[, 2]);
}
model {
mu ~ normal([0, 0.1], [2, 0.1]);
nu ~ normal(-1, 1);
sigma ~ normal(0, [5, 3, 2, 2]);
alpha ~ normal(mu[1], sigma[1]);
beta ~ normal(mu[2], sigma[2]);
to_vector(z) ~ std_normal();
y ~ binomial(n, gamma[subj] + (1 - gamma[subj] - lambda[subj]) .*
inv_logit(alpha[subj] + beta[subj] .* cx));
}
generated quantities {
vector[nsubj] pse = -alpha ./ beta;
vector[nsubj] jnd = logit(.84) * 1/beta;
}
"
m2 <- stan(model_code = s, data = datlist)
print(m2, pars = c("mu", "nu", "sigma", "alpha[1]", "alpha[4]", "beta[1]",
"beta[4]", "gamma[1]", "gamma[4]", "lambda[1]", "lambda[4]", "pse[1]",
"pse[4]", "jnd[1]", "jnd[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
## mu[1] 0.29 0.01 0.39 -0.50 1.05 843 1
## mu[2] 0.05 0.00 0.01 0.04 0.07 1418 1
## nu[1] -2.40 0.02 0.84 -4.01 -0.78 1435 1
## nu[2] -1.64 0.02 0.66 -3.02 -0.37 875 1
## sigma[1] 1.01 0.01 0.39 0.51 2.00 1285 1
## sigma[2] 0.01 0.00 0.01 0.00 0.03 947 1
## sigma[3] 2.97 0.03 1.33 0.60 5.84 2752 1
## sigma[4] 2.74 0.03 1.15 0.81 5.31 1945 1
## alpha[1] -0.19 0.01 0.31 -0.73 0.51 2420 1
## alpha[4] -0.13 0.00 0.23 -0.57 0.32 4086 1
## beta[1] 0.05 0.00 0.01 0.03 0.06 2018 1
## beta[4] 0.06 0.00 0.01 0.05 0.08 3252 1
## gamma[1] 0.01 0.00 0.02 0.00 0.06 3025 1
## gamma[4] 0.01 0.00 0.02 0.00 0.06 2564 1
## lambda[1] 0.07 0.00 0.06 0.00 0.20 1689 1
## lambda[4] 0.07 0.00 0.04 0.01 0.17 3355 1
## pse[1] 4.50 0.13 6.51 -9.09 16.20 2331 1
## pse[4] 2.13 0.06 3.61 -5.08 9.12 4274 1
## jnd[1] 35.55 0.12 5.60 25.73 47.56 2101 1
## jnd[4] 27.24 0.07 4.04 19.95 35.39 3080 1
##
## Samples were drawn using NUTS(diag_e) at Wed Nov 11 13:42:50 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).
pars1 <- extract(m1)[c("alpha", "beta", "pse", "jnd")] |>
lapply(colMeans)
pars1$pse <- pars1$pse + tapply(dat$x, dat$subj, mean) # uncenter
pars2 <- extract(m2)[c("alpha", "beta", "gamma", "lambda", "pse", "jnd")] |>
lapply(colMeans)
pars2$pse <- pars2$pse + tapply(dat$x, dat$subj, mean)
par(mfrow = c(2, 4), mai = c(.4, .4, .1, .1), mgp = c(2, .7, 0))
for(i in 1:8) {
plot(y/n ~ x, dat[dat$subj == i, ], xlim = range(dat$x), ylim = 0:1,
xlab = "Test interval (ms)", ylab = "P('longer')")
with(pars1, {
abline(h = c(.5, .84), v = c(pse[i], pse[i] + jnd[i]), col = "gray")
curve(plogis(x, pse[i], 1/beta[i]), add = TRUE, col = "darkgreen")
})
with(pars2, {
abline(v = c(pse[i], pse[i] + jnd[i]), lty = 2, col = "darkblue")
curve(gamma[i] + (1 - gamma[i] - lambda[i]) *
plogis(x, pse[i], 1/beta[i]),
add = TRUE, col = "darkblue")
})
}