Complete pooling
datlist <- c(dat,
nsubj = unique(dat$subj) |> length(),
N = nrow(dat),
tnew = list(datnew$tnew[1:10]),
npred = 10
)
s <- "
data {
int<lower=1> N;
int<lower=1> npred;
vector<lower=0>[N] t;
vector<lower=0>[npred] tnew;
array[N] int<lower=0> y;
}
parameters {
real<lower=0, upper=1> alpha; // offset
real<lower=0, upper=1> beta; // decay
real<lower=0, upper=1> gamma; // retention rate
}
model {
alpha ~ beta(2, 1);
beta ~ beta(2, 1);
gamma ~ beta(2, 5);
y ~ binomial(18, alpha * exp(-beta * t) + gamma);
}
generated quantities {
vector[npred] prob = alpha * exp(-beta * tnew) + gamma;
array[npred] int ynew = binomial_rng(18, prob);
}
"
m1 <- stan(model_code = s, data = datlist)
## Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
## Running the chains for more iterations may help. See
## https://mc-stan.org/misc/warnings.html#tail-ess
print(m1, pars = c("alpha", "beta", "gamma"), 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.42 0 0.04 0.35 0.49 743 1.00
## beta 0.07 0 0.05 0.03 0.14 547 1.01
## gamma 0.12 0 0.03 0.06 0.18 979 1.00
##
## Samples were drawn using NUTS(diag_e) at Wed Nov 18 06:06:52 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).
## Observed vs. predicted
xval <- datlist$tnew
par(mfrow = 1:2, mai = c(.6, .6, .1, .1), mgp = c(2, .7, 0))
plot.default(factor(xval), colMeans(extract(m1)$prob),
type = "l", axes = FALSE, ylim = 0:1, col = "darkblue",
xlab = "Lag", ylab = "Proportion recalled")
(tapply(dat$y, dat$t, sum) / (18 * 8)) |>
points()
axis(1, factor(xval), xval)
axis(2)
box(bty = "L")
## Posterior predictive distribution
extract(m1)$ynew |>
boxplot(axes = FALSE, ylim = c(0, 18), xlab = "Lag", ylab = "Recalled")
axis(1, factor(xval), xval)
axis(2)
box(bty = "L")
lines(dat[dat$subj == 1, "y"], col = "darkblue")
lines(dat[dat$subj == 8, "y"], col = "darkblue")

No pooling
datlist <- c(dat,
nsubj = unique(dat$subj) |> length(),
N = nrow(dat),
datnew,
npred = nrow(datnew)
)
## Starting distribution
init <- function() {
list(alpha = rbeta(9, 2, 1),
beta = rbeta(9, 2, 1),
gamma = rbeta(9, 2, 5))
}
s <- "
data {
int<lower=1> N;
int<lower=1> nsubj;
int<lower=1> npred;
vector<lower=0>[N] t;
vector<lower=0>[npred] tnew;
array[N] int<lower=0> y;
array[N] int<lower=1, upper=nsubj> subj;
array[npred] int<lower=1> subjnew;
}
parameters {
vector<lower=0, upper=1>[nsubj + 1] alpha; // plus virtual subject
vector<lower=0, upper=1>[nsubj + 1] beta;
vector<lower=0, upper=1>[nsubj + 1] gamma;
}
model {
alpha ~ beta(2, 1);
beta ~ beta(2, 1);
gamma ~ beta(2, 5);
y ~ binomial(18, alpha[subj] .* exp(-beta[subj] .* t) + gamma[subj]);
}
generated quantities {
vector[npred] prob =
fmin(1, alpha[subjnew] .* exp(-beta[subjnew] .* tnew) + gamma[subjnew]);
array[npred] int ynew = binomial_rng(18, prob);
}
"
m2 <- stan(model_code = s, data = datlist,
init = init, control = list(adapt_delta = 0.95))
## recompiling to avoid crashing R session
## Warning in .local(object, ...): some chains had errors; consider specifying
## chains = 1 to debug
## here are whatever error messages were returned
## [[1]]
## Stan model 'anon_model' does not contain samples.
##
## [[2]]
## Stan model 'anon_model' does not contain samples.
## Warning: There were 107 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(m2, pars = c("alpha[1]", "alpha[8]", "alpha[9]",
"beta[1]", "beta[8]", "beta[9]",
"gamma[1]", "gamma[8]", "gamma[9]"), probs = c(.025, .975))
## Inference for Stan model: anon_model.
## 2 chains, each with iter=2000; warmup=1000; thin=1;
## post-warmup draws per chain=1000, total post-warmup draws=2000.
##
## mean se_mean sd 2.5% 97.5% n_eff Rhat
## alpha[1] 0.73 0.00 0.18 0.33 0.99 1674 1
## alpha[8] 0.74 0.01 0.15 0.47 0.99 678 1
## alpha[9] 0.67 0.00 0.23 0.19 0.99 2432 1
## beta[1] 0.76 0.00 0.18 0.31 0.99 1280 1
## beta[8] 0.37 0.01 0.25 0.05 0.90 548 1
## beta[9] 0.66 0.01 0.24 0.14 0.99 2243 1
## gamma[1] 0.11 0.00 0.03 0.06 0.16 1991 1
## gamma[8] 0.33 0.00 0.08 0.18 0.46 860 1
## gamma[9] 0.28 0.00 0.16 0.04 0.62 2171 1
##
## Samples were drawn using NUTS(diag_e) at Wed Nov 18 06:08: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).
## Observed vs. predicted
par(mfrow = c(3, 3), mai = c(.5, .5, .1, .1), mgp = c(2, .7, 0))
for(s in 1:9) {
plot.default(factor(xval), colMeans(extract(m2)$prob)[datnew$subj == s],
type = "l", axes = FALSE, ylim = 0:1, col = "darkblue",
xlab = "Lag", ylab = "Proportion recalled")
points(dat$y[dat$subj == s]/18)
axis(1, factor(xval), xval)
axis(2)
box(bty = "L")
}

## Posterior predictive distribution
for(s in 1:9) {
extract(m2)$ynew[, datnew$subj == s] |>
structure(dimnames = list(NULL, datnew$tnew[1:10])) |>
boxplot(xlab = "Lag", ylab = "Recalled", border = "darkgray",
ylim = c(0, 18))
points(dat[dat$subj == s, "y"], type = "o", pch = 16, col = "darkblue")
}

Partial pooling (centered)
s <- "
data {
int<lower=1> N;
int<lower=1> nsubj;
int<lower=1> npred;
vector<lower=0>[N] t;
vector<lower=0>[npred] tnew;
array[N] int<lower=0> y;
array[N] int<lower=1, upper=nsubj> subj;
array[npred] int<lower=1> subjnew;
}
parameters {
vector<lower=0, upper=1>[3] phi;
vector<lower=1>[3] kappa;
vector<lower=0, upper=1>[nsubj + 1] alpha;
vector<lower=0, upper=1>[nsubj + 1] beta;
vector<lower=0, upper=1>[nsubj + 1] gamma;
}
model {
phi[1] ~ beta(2, 1);
phi[2] ~ beta(2, 1);
phi[3] ~ beta(2, 5);
kappa ~ pareto(1, 1.5);
alpha ~ beta(phi[1] * kappa[1], (1 - phi[1]) * kappa[1]);
beta ~ beta(phi[2] * kappa[2], (1 - phi[2]) * kappa[2]);
gamma ~ beta(phi[3] * kappa[3], (1 - phi[3]) * kappa[3]);
y ~ binomial(18, alpha[subj] .* exp(-beta[subj] .* t) + gamma[subj]);
}
generated quantities {
vector[npred] prob =
fmin(1, alpha[subjnew] .* exp(-beta[subjnew] .* tnew) + gamma[subjnew]);
array[npred] int ynew = binomial_rng(18, prob);
}
"
m3 <- stan(model_code = s, data = datlist)
## Warning: There were 157 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: There were 4 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. See
## https://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded
## Warning: Examine the pairs() plot to diagnose sampling problems
## Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
## Running the chains for more iterations may help. See
## https://mc-stan.org/misc/warnings.html#bulk-ess
## Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
## Running the chains for more iterations may help. See
## https://mc-stan.org/misc/warnings.html#tail-ess
print(m3, pars = c("phi", "kappa", "alpha[1]", "alpha[8]", "alpha[9]",
"beta[1]", "beta[8]", "beta[9]",
"gamma[1]", "gamma[8]", "gamma[9]"), 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
## phi[1] 0.56 0.01 0.10 0.38 0.79 244 1.01
## phi[2] 0.37 0.01 0.14 0.11 0.67 329 1.01
## phi[3] 0.19 0.00 0.06 0.09 0.34 1707 1.01
## kappa[1] 15.54 1.90 42.57 1.21 86.06 501 1.01
## kappa[2] 2.58 0.16 6.62 1.02 8.94 1658 1.00
## kappa[3] 9.63 0.50 16.55 1.12 47.55 1093 1.00
## alpha[1] 0.57 0.01 0.19 0.23 0.98 448 1.01
## alpha[8] 0.61 0.01 0.11 0.42 0.95 233 1.01
## alpha[9] 0.57 0.01 0.22 0.10 0.99 607 1.00
## beta[1] 0.52 0.01 0.28 0.05 0.99 587 1.00
## beta[8] 0.11 0.01 0.13 0.02 0.53 129 1.02
## beta[9] 0.37 0.01 0.32 0.00 0.99 1146 1.00
## gamma[1] 0.09 0.00 0.03 0.01 0.16 1073 1.00
## gamma[8] 0.22 0.00 0.09 0.05 0.40 640 1.01
## gamma[9] 0.18 0.00 0.19 0.00 0.72 3523 1.00
##
## Samples were drawn using NUTS(diag_e) at Wed Nov 18 06:08:31 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).
## Observed vs. predicted
par(mfrow = c(3, 3), mai = c(.5, .5, .1, .1), mgp = c(2, .7, 0))
for(s in 1:9) {
plot.default(factor(xval), colMeans(extract(m3)$prob)[datnew$subj == s],
type = "l", axes = FALSE, ylim = 0:1, col = "darkblue",
xlab = "Lag", ylab = "Proportion recalled")
points(dat$y[dat$subj == s]/18)
axis(1, factor(xval), xval)
axis(2)
box(bty = "L")
}

## Posterior predictive distribution
for(s in 1:9) {
extract(m3)$ynew[, datnew$subj == s] |>
structure(dimnames = list(NULL, datnew$tnew[1:10])) |>
boxplot(xlab = "Lag", ylab = "Recalled", border = "darkgray",
ylim = c(0, 18))
points(dat[dat$subj == s, "y"], type = "o", pch = 16, col = "darkblue")
}

Partial pooling (logit, noncentered)
s <- "
data {
int<lower=1> N;
int<lower=1> nsubj;
int<lower=1> npred;
vector<lower=0>[N] t;
vector<lower=0>[npred] tnew;
array[N] int<lower=0> y;
array[N] int<lower=1, upper=nsubj> subj;
array[npred] int<lower=1> subjnew;
}
parameters {
vector[3] mu; // group logit mean [offset, decay, retention]'
vector<lower=0>[3] sigma; // group logit sd
matrix[nsubj + 1, 3] z; // by-subject logit offset
}
transformed parameters {
vector[nsubj + 1] alpha = inv_logit(mu[1] + sigma[1] * z[, 1]);
vector[nsubj + 1] beta = inv_logit(mu[2] + sigma[2] * z[, 2]);
vector[nsubj + 1] gamma = inv_logit(mu[3] + sigma[3] * z[, 3]);
}
model {
mu ~ normal([1, 1, -1], 1);
sigma ~ normal(0, 1);
to_vector(z) ~ std_normal();
y ~ binomial(18, alpha[subj] .* exp(-beta[subj] .* t) +
gamma[subj]);
}
generated quantities {
vector[3] phi = inv_logit(mu); // back-transform mean
vector[npred] prob =
fmin(1, alpha[subjnew] .* exp(-beta[subjnew] .* tnew) + gamma[subjnew]);
array[npred] int ynew = binomial_rng(18, prob);
}
"
m4 <- stan(model_code = s, data = datlist)
## recompiling to avoid crashing R session
## Warning: There were 23 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(m4, pars = c("mu", "sigma", "phi", "alpha[1]", "alpha[8]", "alpha[9]",
"beta[1]", "beta[8]", "beta[9]",
"gamma[1]", "gamma[8]", "gamma[9]"), 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.34 0.01 0.31 -0.24 1.04 1731 1
## mu[2] -0.36 0.02 0.78 -1.92 1.27 1337 1
## mu[3] -1.74 0.01 0.26 -2.31 -1.22 1553 1
## sigma[1] 0.35 0.01 0.30 0.01 1.12 1430 1
## sigma[2] 1.80 0.01 0.50 0.93 2.87 1519 1
## sigma[3] 0.51 0.01 0.31 0.06 1.25 1196 1
## phi[1] 0.58 0.00 0.07 0.44 0.74 1762 1
## phi[2] 0.42 0.00 0.17 0.13 0.78 1406 1
## phi[3] 0.15 0.00 0.03 0.09 0.23 1648 1
## alpha[1] 0.58 0.00 0.11 0.34 0.84 2021 1
## alpha[8] 0.60 0.00 0.07 0.45 0.75 3319 1
## alpha[9] 0.58 0.00 0.12 0.32 0.82 3039 1
## beta[1] 0.63 0.01 0.22 0.17 0.98 1963 1
## beta[8] 0.07 0.00 0.06 0.02 0.20 1857 1
## beta[9] 0.44 0.00 0.30 0.02 0.98 3864 1
## gamma[1] 0.11 0.00 0.03 0.05 0.17 2244 1
## gamma[8] 0.20 0.00 0.06 0.09 0.34 2694 1
## gamma[9] 0.17 0.00 0.09 0.04 0.41 2821 1
##
## Samples were drawn using NUTS(diag_e) at Wed Nov 18 06:09:48 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).
## Observed vs. predicted
par(mfrow = c(3, 3), mai = c(.5, .5, .1, .1), mgp = c(2, .7, 0))
for(s in 1:9) {
plot.default(factor(xval), colMeans(extract(m4)$prob)[datnew$subj == s],
type = "l", axes = FALSE, ylim = 0:1, col = "darkblue",
xlab = "Lag", ylab = "Proportion recalled")
points(dat$y[dat$subj == s]/18)
axis(1, factor(xval), xval)
axis(2)
box(bty = "L")
}

## Posterior predictive distribution
for(s in 1:9) {
extract(m4)$ynew[, datnew$subj == s] |>
structure(dimnames = list(NULL, datnew$tnew[1:10])) |>
boxplot(xlab = "Lag", ylab = "Recalled", border = "darkgray",
ylim = c(0, 18))
points(dat[dat$subj == s, "y"], type = "o", pch = 16, col = "darkblue")
}
