## (see Issa Mattos & Martins Silva Ramos, 2022)
library(rstan)
options(mc.cores = 4)
rstan_options(auto_write = TRUE)https://cran.r-project.org/web/packages/eba/refman/eba.html#winetaste
data(winetaste, package = "eba"); rm(ambilight)
table(redwines$best) |> as.matrix() |> drop() |> dotchart(xlab = "Best choice") pc <- unclass(redwines$preference)
wine <- attr(pc, "labels") |> factor(1:5, labels = _)
idx0 <- colnames(pc) |> gsub("(.):(.)", "\\2", x = _) |> as.numeric()
idx1 <- colnames(pc) |> gsub("(.):(.)", "\\1", x = _) |> as.numeric()
dat <- data.frame(
subj = rep(1:nrow(pc), each = ncol(pc)), # subj indicator
wine0 = rep(wine[idx0], nrow(pc)), # wine indicators
wine1 = rep(wine[idx1], nrow(pc)),
y = (as.vector(t(pc)) + 1) / 2 # choice 0/1
)
## Required for judge effects: subj x wine indicator
dat$sw0 <- interaction(dat$wine0, dat$subj)
dat$sw1 <- interaction(dat$wine1, dat$subj)
datlist <- c(lapply(dat, as.numeric),
nitem = unique(c(dat$wine0, dat$wine1)) |> length(),
nsubj = unique(dat$subj) |> length(),
N = nrow(dat)
)\[\begin{align} \text{logit Pr}(i \succ j) &= \log \frac{\dfrac{\exp(\alpha_i + U_{i,s})} {\exp(\alpha_i + U_{i,s}) + \exp(\alpha_j + U_{j,s})}} {\dfrac{\exp(\alpha_j + U_{j,s})} {\exp(\alpha_i + U_{i,s}) + \exp(\alpha_j + U_{j,s})}}\\ &= \log \frac{\exp(\alpha_i + U_{i,s})} {\exp(\alpha_j + U_{j,s})}\\ &= (\alpha_i + U_{i,s}) - (\alpha_j + U_{j,s})\\ &= \alpha_i - \alpha_j + U_{i,s} - U_{j,s} \end{align}\]
s <- "
data {
int<lower=1> nitem;
int<lower=1> nsubj;
int<lower=1> N;
array[N] int<lower=1> sw0; // subj x wine indicator
array[N] int<lower=1> sw1;
array[N] int<lower=1, upper=nitem> wine0;
array[N] int<lower=1, upper=nitem> wine1;
array[N] int<lower=0, upper=1> y;
}
parameters {
//vector[nitem] alpha;
sum_to_zero_vector[nitem] alpha;
real<lower=0> sigma; // soft constraint
real<lower=0> tau; // judge sd
vector<multiplier=tau>[nitem * nsubj] U; // by-judge adjustment
}
model {
sigma ~ lognormal(0, 1);
alpha ~ normal(0, sigma);
tau ~ normal(0, 5);
U ~ normal(0, tau);
y ~ bernoulli_logit(alpha[wine1] - alpha[wine0] + U[sw1] - U[sw0]);
}
"
m <- stan(model_code = s, data = datlist)## Warning: There were 39 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: 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
## 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[1] -0.01 0.01 0.32 -0.70 0.67 2733 1
## alpha[2] -0.09 0.01 0.33 -0.83 0.54 2724 1
## alpha[3] -0.41 0.01 0.42 -1.42 0.19 1204 1
## alpha[4] 0.41 0.01 0.44 -0.21 1.46 1307 1
## alpha[5] 0.09 0.01 0.32 -0.51 0.81 2827 1
## sigma 0.47 0.01 0.34 0.08 1.33 959 1
## tau 1.61 0.02 0.55 0.72 2.87 830 1
##
## Samples were drawn using NUTS(diag_e) at Wed Nov 25 18:33:33 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).