library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
(Murdock, 1962)
dat <- read.table("../MurdockSerialPosition.txt", header = TRUE)
mtr <- c(
"10-2" = 10*2 + 15, # mean time of recall =
"15-2" = 15*2 + 20, # presentation duration + retention interval
"20-2" = 20*2 + 25,
"20-1" = 20*1 + 10,
"30-1" = 30*1 + 15,
"40-1" = 40*1 + 20
)
dat$t <- # temporal distance (age) = mtr - rate * position
mtr[dat$cond] - as.numeric(substr(dat$cond, 4, 4)) * dat$pos
dat$m <- log(dat$t)
stanc("predSIMPLE.stan") |>
expose_stan_functions()
predSIMPLE(c = 10, t = 0.5, s = 10,
dist = dat$m[dat$cond == "10-2"],
nitem = 10)
## [1] 0.5337080 0.4001761 0.3471102 0.3379708 0.3535940 0.3893292 0.4467361
## [8] 0.5326442 0.6665959 0.8858079
dat1 <- dat[dat$cond == "10-2", ]
datlist <- c(dat1, nitem = nrow(dat1))
init <- function() {
list(c = rnorm(1, mean = 10, sd = 3),
t = rbeta(1, 2, 2),
s = rnorm(1, mean = 10, sd = 3))
}
s <- "
#include predSIMPLE.stan
data {
int<lower=1> nitem;
array[nitem] int<lower=0> y;
array[nitem] int<lower=1> n;
array[nitem] real<lower=0> m; // log temporal distance
}
parameters {
real<lower=1> c; // distinctiveness
real<lower=0, upper=1> t; // threshold
real<lower=1> s; // slope
}
model {
c ~ normal(10, 3);
t ~ beta(2, 2);
s ~ normal(10, 3);
y ~ binomial(n, predSIMPLE(c, t, s, m, nitem));
}
generated quantities {
array[nitem] int yrep = binomial_rng(n, predSIMPLE(c, t, s, m, nitem));
}
"
m <- stan(model_code = s, data = datlist, init = init)
print(m, pars = c("c", "t", "s"), 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
## c 11.44 0.02 0.50 10.52 12.45 637 1.01
## t 0.47 0.00 0.01 0.45 0.50 633 1.01
## s 10.22 0.01 0.34 9.58 10.95 761 1.01
##
## Samples were drawn using NUTS(diag_e) at Wed Dec 17 09:54:43 2025.
## 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).
## Posterior predictive distribution
plot(y/n ~ pos, dat1, type = "n", xlim = c(0, 40), ylim = 0:1,
xlab = "Serial position", ylab = "Recall probability")
for(i in 1:20) {
yrep <- extract(m)$yrep[sample(4000, 1), ]
lines(yrep/n ~ pos, dat1, col = "gray")
}
points(y/n ~ pos, dat1, col = "darkblue")
legend("topright", unique(dat1$cond), bty = "n")
datlist <- c(dat, list(
nitem = table(dat$cond)[names(mtr)],
ncond = unique(dat$cond) |> length(),
N = nrow(dat)
))
init <- function() {
list(c = rnorm(datlist$ncond, mean = 10, sd = 3),
t = rbeta(datlist$ncond, 2, 2),
s = rnorm(datlist$ncond, mean = 10, sd = 3))
}
## https://mc-stan.org/docs/stan-users-guide/sparse-ragged.html
s <- "
#include predSIMPLE.stan
data {
int<lower=1> N;
int<lower=1> ncond; // number of list conditions
array[ncond] int<lower=1> nitem; // number of items per cond
array[N] int<lower=0> y;
array[N] int<lower=1> n;
array[N] real<lower=0> m;
}
parameters {
vector<lower=1>[ncond] c;
vector<lower=0, upper=1>[ncond] t;
vector<lower=1>[ncond] s;
}
model {
c ~ normal(10, 3);
t ~ beta(2, 2);
s ~ normal(10, 3);
int pos = 1;
for (k in 1:ncond) {
segment(y, pos, nitem[k]) ~
binomial(segment(n, pos, nitem[k]),
predSIMPLE(c[k], t[k], s[k],
segment(m, pos, nitem[k]), nitem[k]));
pos += nitem[k];
}
}
generated quantities {
array[N] int yrep;
int pos = 1;
for (k in 1:ncond) {
yrep[pos:(pos + nitem[k] - 1)] =
binomial_rng(segment(n, pos, nitem[k]),
predSIMPLE(c[k], t[k], s[k],
segment(m, pos, nitem[k]), nitem[k]));
pos += nitem[k];
}
}
"
m1 <- stan(model_code = s, data = datlist, init = init)
print(m1, pars = c("c", "t", "s"), 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
## c[1] 11.45 0.01 0.49 10.51 12.47 2962 1
## c[2] 9.78 0.01 0.36 9.10 10.50 2919 1
## c[3] 7.82 0.01 0.37 7.11 8.56 3080 1
## c[4] 9.56 0.01 0.34 8.91 10.24 2460 1
## c[5] 6.49 0.00 0.25 6.02 6.99 3131 1
## c[6] 7.97 0.01 0.33 7.33 8.64 2729 1
## t[1] 0.47 0.00 0.01 0.45 0.50 2875 1
## t[2] 0.35 0.00 0.01 0.33 0.37 2944 1
## t[3] 0.27 0.00 0.01 0.25 0.29 3072 1
## t[4] 0.36 0.00 0.01 0.34 0.38 2492 1
## t[5] 0.21 0.00 0.01 0.20 0.22 3116 1
## t[6] 0.21 0.00 0.01 0.20 0.22 2706 1
## s[1] 10.23 0.01 0.34 9.60 10.92 2950 1
## s[2] 15.20 0.01 0.52 14.20 16.21 3021 1
## s[3] 21.48 0.02 0.95 19.66 23.46 3128 1
## s[4] 18.21 0.01 0.63 17.02 19.46 2626 1
## s[5] 32.47 0.02 1.19 30.23 34.84 3133 1
## s[6] 34.62 0.03 1.29 32.12 37.37 2651 1
##
## Samples were drawn using NUTS(diag_e) at Wed Dec 17 09:58:16 2025.
## 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).