## Draw hit or false alarm rates
rsdt <- function(n, mu_d = 0, mu_c = 0, sd_d = sqrt(2), sd_c = sqrt(2)/2,
what = "HR") {
d <- rnorm(n, mu_d, sd_d)
c <- rnorm(n, mu_c, sd_c)
if(what == "HR") {
1 - pnorm(d/2 + c, d, 1)
} else {
1 - pnorm(d/2 + c, 0, 1)
}
}
par(mfrow = c(2, 3), mai = c(.5, .5, .1, .1), mgp = c(2, .7, 0))
rsdt(5000) |> hist(main = "", xlim = 0:1) # beta(1, 1)
rsdt(5000, 0, 0, 2, 1) |> hist(main = "", xlim = 0:1) # beta(m, m), m < 1
rsdt(5000, 0, 0, 1, 1/2) |> hist(main = "", xlim = 0:1) # beta(m, m), m > 1
rsdt(5000, 0, 0, 1/2, 1/4) |> hist(main = "", xlim = 0:1)
rsdt(5000, 0.5, 0, 0.8, 0.7, "HR") |> hist(main = "", xlim = 0:1)
rsdt(5000, 0.5, 0, 0.8, 0.7, "FR") |> hist(main = "", xlim = 0:1)(see Lee & Wagenmakers, 2014, chap. 11)
dat <- read.table(header = TRUE, text = "
set trial yes no
1 sig 70 30
1 noi 50 50
2 sig 7 3
2 noi 5 5
3 sig 10 0
3 noi 0 10
")
dat2 <- cbind(dat, ntrials = dat$yes + dat$no) |>
reshape(direction = "wide", timevar = "trial", idvar = "set",
drop = "no") |>
setNames(c("set", "hi", "nsig", "fa", "nnoi"))s <- "
data {
int<lower=0> hi; // hits
int<lower=0> fa; // false alarms
int<lower=1> nsig; // signal trials
int<lower=1> nnoi; // noise trials
}
parameters {
real d; // discriminability/sensitivity
real c; // decision bias c = k - d/2 (higher is more conserv.)
}
model {
d ~ normal(0.5, 0.8);
c ~ normal(0, 0.7);
//hi ~ binomial(nsig, 1 - normal_cdf(d/2 + c, d, 1));
//fa ~ binomial(nnoi, 1 - normal_cdf(d/2 + c, 0, 1));
hi ~ binomial(nsig, Phi( d/2 - c)); // center and mirror: -(d/2 + c - d)
fa ~ binomial(nnoi, Phi(-d/2 - c));
}
"
m <- stan(model_code = s, data = dat2[dat2$set == 1, ])
print(m, pars = c("d", "c"), 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
## d 0.52 0 0.18 0.16 0.87 3624 1
## c -0.26 0 0.09 -0.44 -0.10 3256 1
##
## Samples were drawn using NUTS(diag_e) at Fri Nov 6 16:33:01 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).
s <- "
data {
int<lower=1> N;
array[N] int<lower=0> hi;
array[N] int<lower=0> fa;
array[N] int<lower=1> nsig;
array[N] int<lower=1> nnoi;
}
parameters {
vector[N] d;
vector[N] c;
}
model {
d ~ normal(0.5, 0.8);
c ~ normal(0, 0.7);
hi ~ binomial(nsig, Phi( d/2 - c));
fa ~ binomial(nnoi, Phi(-d/2 - c));
}
"
m <- stan(model_code = s, data = c(dat2, N = nrow(dat2)))
print(m, pars = c("d", "c"), 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
## d[1] 0.53 0.00 0.17 0.20 0.87 5792 1
## d[2] 0.53 0.01 0.46 -0.36 1.43 6916 1
## d[3] 2.24 0.01 0.58 1.14 3.40 5584 1
## c[1] -0.26 0.00 0.09 -0.45 -0.08 5585 1
## c[2] -0.23 0.00 0.27 -0.76 0.28 6650 1
## c[3] 0.00 0.00 0.34 -0.68 0.69 5487 1
##
## Samples were drawn using NUTS(diag_e) at Fri Nov 6 16:34:02 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).
## ci_level: 0.8 (80% intervals)
## outer_level: 0.95 (95% intervals)
hr <- ( extract(m)$d/2 - extract(m)$c) |> pnorm()
fr <- (-extract(m)$d/2 - extract(m)$c) |> pnorm()
par(mfrow = c(1, 2), mai = c(.6, .6, .1, .1), mgp = c(2, .7, 0))
density(hr[, 1]) |>
plot(xlim = 0:1, main = "", xlab = "HR", type = "n")
for(i in dat2$set) {
density(hr[, i]) |> lines(lty = i, col = "darkblue")
}
density(fr[, 1]) |>
plot(xlim = 0:1, main = "", xlab = "FAR", type = "n")
for(i in dat2$set) {
density(fr[, i]) |> lines(lty = i, col = "darkblue")
}