n <- 10
x <- 7
c <- 2
par(mai = c(.7, .7, .1, .55), mgp = c(2, .7, 0))
curve(dbeta(p, 22, 12), xname = "p", type = "n", axes = FALSE,
ylim = c(0, sqrt(2*n)), xlab = "Parameter (p)", ylab = "Density")
abline(h = 0, col = "gray")
curve(dbeta(p, c, c), xname = "p", col = "gray", lwd = 2, add = TRUE)
curve(dbeta(p, x + 1, n - x + 1), xname = "p", lty = 2, add = TRUE)
curve(dbeta(p, x + c, n - x + c), xname = "p", col = "darkblue", add = TRUE)
legend("topright", c("Posterior", "Likelihood", "Prior"), xpd = NA,
bty = "n", col = c("darkblue", "black", "gray"), lty = c(1, 2, 1),
lwd = c(1, 1, 2))
axis(1)
axis(2)
box(bty = "L")Interactive graph: https://apps.mathpsy.uni-tuebingen.de/fw/bayesestim/
## ML EAP MAP
## 0.7000000 0.6428571 0.6666667
Note for Windows users
install.packages("rstan")coinflip <- "
data {
int<lower=1> n;
int<lower=0> x;
}
parameters {
real<lower=0, upper=1> theta;
}
model {
theta ~ beta(2, 2); // prior
x ~ binomial(n, theta); // likelihood
}
"
m <- stan(model_code = coinflip, data = list(n = 10, x = 7))## recompiling to avoid crashing R session
## 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
## theta 0.65 0 0.12 0.39 0.87 1434 1
##
## Samples were drawn using NUTS(diag_e) at Fri Jun 26 10:28:42 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).
## 2.5% 97.5%
## 0.3926736 0.8659249
## Prior and posterior density
par(mai = c(.6, .6, .1, .1), mgp = c(2, .7, 0), bty = "L")
curve(dbeta(x, 2, 2), ylim = c(0, 3), lwd = 2, col = "gray", # prior
xlab = expression(theta), ylab = "Density")
density(theta) |> lines(col = "darkblue") # posterior
rug(theta, col = "darkblue") # samplesSignal detection theory: https://apps.mathpsy.uni-tuebingen.de/fw/recogapp/
## Heit and Rotello (2005) data on deductive and inductive reasoning
dat <- read.table("HeitRotello.txt", header = TRUE)
head(dat)## reason f1 f2 f3 f4 t1 t2 t3 t4 hi mi fa cr
## 1 dedu nv/ns nv/ns nv/ns v/s v/s v/s v/s v/s 4 0 1 3
## 2 dedu nv/ns nv/ns nv/ns nv/ns v/s v/s v/s v/s 4 0 0 4
## 3 dedu nv/ns nv/ns nv/ns nv/ns v/s v/s v/s v/s 4 0 0 4
## 4 dedu nv/ns nv/ns nv/ns nv/ns v/s v/s v/s v/s 4 0 0 4
## 5 indu nv/ns v/s nv/ns nv/ns nv/ns v/s v/s v/s 3 1 1 3
## 6 dedu nv/ns nv/ns nv/ns nv/ns v/s v/s v/s v/s 4 0 0 4
datlist = c(dat[dat$reason == "dedu", c("hi", "fa")],
nsubj = sum(dat$reason == "dedu")
)
sdt <- "
data {
int<lower=1> nsubj;
array[nsubj] int<lower=0, upper=4> hi;
array[nsubj] int<lower=0, upper=4> fa;
}
parameters {
vector[2] mu;
vector<lower=0>[2] sigma;
matrix[nsubj, 2] z;
}
transformed parameters {
vector[nsubj] d = mu[1] + sigma[1] * z[, 1];
vector[nsubj] c = mu[2] + sigma[2] * z[, 2];
}
model {
mu ~ normal([0.5, 0], [0.8, 0.7]);
sigma ~ normal(0, 5);
to_vector(z) ~ std_normal();
hi ~ binomial(4, Phi( d/2 - c));
fa ~ binomial(4, Phi(-d/2 - c));
}
"
m <- stan(model_code = sdt, data = datlist)## recompiling to avoid crashing R session
## 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] 2.52 0.01 0.38 1.81 3.28 2176 1
## mu[2] -0.25 0.00 0.15 -0.55 0.05 3114 1
## sigma[1] 2.06 0.01 0.51 1.22 3.19 1209 1
## sigma[2] 0.64 0.01 0.20 0.27 1.05 993 1
##
## Samples were drawn using NUTS(diag_e) at Fri Jun 26 10:23:58 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).