library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
(Riefer et al., 2002)
data(MemoryDeficits, package = "psychotools")
dat <-
subset(MemoryDeficits, trial %in% c(1, 2, 6) & group == "AlcCtl") |>
droplevels()
datlist <- list(
y = dat[, c("E1", "E2", "E3", "E4")],
N = nrow(dat)
)
s <- "
#include ../pcat.stan
data {
int<lower=1> N;
array[N, 4] int<lower=0> y;
}
parameters {
vector[3] mu; // probit group parameters [c, r, u]'
vector<lower=0>[3] sigma;
matrix[3, N] z; // probit by-subject offsets
}
transformed parameters {
matrix<lower=0, upper=1>[3, N] theta =
Phi(rep_matrix(mu, N) + diag_pre_multiply(sigma, z));
}
model {
mu ~ normal(0, 1.5);
sigma ~ normal(0, 2);
to_vector(z) ~ std_normal();
for (i in 1:N) {
y[i] ~ multinomial(pcat(theta[, i]));
}
}
"
m1 <- stan(model_code = s, data = datlist)
print(m1, pars = c("mu", "sigma"), 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.00 0.00 0.13 -0.28 0.23 1181 1.00
## mu[2] 0.55 0.01 0.30 0.07 1.19 1236 1.00
## mu[3] -0.15 0.00 0.10 -0.36 0.04 1770 1.00
## sigma[1] 0.57 0.00 0.10 0.39 0.78 1054 1.01
## sigma[2] 1.26 0.01 0.23 0.90 1.79 1983 1.00
## sigma[3] 0.50 0.00 0.09 0.34 0.68 983 1.00
##
## Samples were drawn using NUTS(diag_e) at Mon Nov 10 12:26:17 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).
extract(m1)$mu |>
pnorm() |>
boxplot(ylim = 0:1, horizontal = TRUE, yaxt = "n")
axis(2, 1:3, expression(mu[c], mu[r], mu[u]), las = 1)
s <- "
#include ../pcat.stan
data {
int<lower=1> N;
array[N, 4] int<lower=0> y;
}
parameters {
vector[3] mu;
vector<lower=0>[3] sigma;
matrix[3, N] z;
cholesky_factor_corr[3] L;
}
transformed parameters {
matrix<lower=0, upper=1>[3, N] theta =
Phi(rep_matrix(mu, N) + diag_pre_multiply(sigma, L) * z);
}
model {
mu ~ normal(0, 1.5);
sigma ~ normal(0, 2);
to_vector(z) ~ std_normal();
L ~ lkj_corr_cholesky(2);
for (i in 1:N) {
y[i] ~ multinomial(pcat(theta[, i]));
}
}
generated quantities {
matrix[3, 3] Rho = L * L'; // correlation matrix
}
"
m2 <- stan(model_code = s, data = datlist)
## Warning: There were 2 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
## 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(m2, pars = c("mu", "sigma", "Rho[1, 2]", "Rho[1, 3]", "Rho[2, 3]"),
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.11 0.01 0.16 -0.45 0.15 535 1.00
## mu[2] 0.53 0.02 0.36 0.07 1.29 257 1.01
## mu[3] -0.14 0.00 0.11 -0.37 0.08 813 1.00
## sigma[1] 0.66 0.00 0.11 0.46 0.89 1254 1.00
## sigma[2] 0.90 0.02 0.27 0.44 1.41 305 1.02
## sigma[3] 0.61 0.00 0.10 0.43 0.81 1398 1.00
## Rho[1,2] 0.29 0.01 0.26 -0.25 0.76 1119 1.00
## Rho[1,3] 0.64 0.00 0.14 0.32 0.86 970 1.00
## Rho[2,3] 0.53 0.01 0.26 -0.10 0.91 440 1.01
##
## Samples were drawn using NUTS(diag_e) at Mon Nov 10 12:27:13 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).
extract(m2)$Rho |>
apply(2:3, mean) |>
round(2)
##
## [,1] [,2] [,3]
## [1,] 1.00 0.29 0.64
## [2,] 0.29 1.00 0.53
## [3,] 0.64 0.53 1.00
extract(m2)$mu |>
pnorm() |>
boxplot(ylim = 0:1, horizontal = TRUE, yaxt = "n")
axis(2, 1:3, expression(mu[c], mu[r], mu[u]), las = 1)