library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
(van Ravenzwaaij et al., 2011)
dat <- read.table("../VanRavenzwaaijBart.txt", header = TRUE)
dat$cond <- factor(dat$cond, levels = c("sober", "tipsy", "drunk"))
## Pump-or-cash decisions within each trial
dlist <- mapply(\(x, y) c(rep(0, x), if(y > 0) 1), # 0: pump, 1: cash
dat$pumps, dat$cash)
dat2 <- data.frame(
subj = rep(dat$subj, lengths(dlist)),
cond = rep(dat$cond, lengths(dlist)),
trial = sprintf("%i-%.2i", dat$block, dat$trial) |>
rep(lengths(dlist)),
p = rep(dat$prob., lengths(dlist))/100,
k = lapply(dlist, seq_along) |> unlist(),
d = unlist(dlist)
)
## Sober Mo aka George
dat3 <- aggregate(d ~ k + p, dat2, sum, subset = subj == "Mo" &
cond == "sober")
dat3$n <- aggregate(d ~ k + p, dat2, length, subset = subj == "Mo" &
cond == "sober")$d
datlist <- c(dat3, N = nrow(dat3))
s <- "
data {
int<lower=1> N;
array[N] int<lower=1> n; // number of decision opportunities
array[N] int<lower=0> d; // number of cash decisions
vector<lower=1>[N] k; // decision opportunity
vector<lower=0, upper=1>[N] p; // burst probability
}
parameters {
real<lower=0> gamma; // risk propensity
real<lower=0> beta; // consistency
}
model {
gamma ~ normal(1, 2);
beta ~ normal(1, 2);
d ~ binomial_logit(n, beta * (k + gamma / log1m(p)));
}
generated quantities {
array[N] int d_rep =
binomial_rng(n, inv_logit(beta * (k + gamma / log1m(p))));
}
"
m <- stan(model_code = s, data = datlist)
## recompiling to avoid crashing R session
print(m, pars = c("gamma", "beta"), 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
## gamma 0.64 0 0.05 0.56 0.76 1092 1
## beta 0.65 0 0.11 0.45 0.88 1126 1
##
## Samples were drawn using NUTS(diag_e) at Mon Dec 29 15:58:37 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(m)$gamma |> density() |> plot(main = "", xlim = c(0, 1.2))
extract(m)$beta |> density() |> lines(lty = 2)
legend("topright", c("gamma", "beta"), lty = 1:2, bty = "n")
par(mfrow = c(1, 3), mai = c(.1, .2, .1, .1), mgp = c(2, .7, 0),
omi = c(.5, .5, 0, 0))
for (p in unique(dat3$p)) {
extract(m)$d_rep[, dat3$p == p] |>
sweep(2, dat3[dat3$p == p, "n"], "/") |>
boxplot(border = "darkgray", xlim = c(0.5, 8.5))
with(dat3[dat3$p == p, ],
points(d/n, pch = 4, col = "darkblue", type = "o"))
legend("topleft", legend = p, bty = "n")
}
mtext("Decision opportunity", 1, 2, outer = TRUE)
mtext("Proportion of cash responses", 2, 2, outer = TRUE)
## Sober, all subjects
dat4 <- aggregate(d ~ k + p + subj, dat2, sum, subset = cond == "sober")
dat4$n <- aggregate(d ~ k + p + subj, dat2, length, subset = cond == "sober")$d
datlist <- c(subset(dat4, select = -subj), list(
subj = dat4$subj |> factor(levels = unique(dat$subj)) |> as.numeric(),
nsubj = dat4$subj |> unique() |> length(),
N = nrow(dat4)
))
s <- "
data {
int<lower=1> N;
int<lower=1> nsubj;
array[N] int<lower=1, upper=nsubj> subj;
array[N] int<lower=1> n;
array[N] int<lower=0> d;
vector<lower=1>[N] k;
vector<lower=0, upper=1>[N] p;
}
parameters {
vector<lower=0>[2] mu; // group risk propensity and consistency
vector<lower=0>[2] sigma;
vector<lower=0>[nsubj] gamma; // by-subj pars
vector<lower=0>[nsubj] beta;
}
model {
mu ~ normal(1, 2);
sigma ~ normal(1, 2);
gamma ~ normal(mu[1], sigma[1]);
beta ~ normal(mu[2], sigma[2]);
d ~ binomial_logit(n, beta[subj] .* (k + gamma[subj] ./ log1m(p)));
}
"
m <- stan(model_code = s, data = datlist)
## recompiling to avoid crashing R session
print(m, 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] 1.46 0 0.15 1.18 1.78 4570 1
## mu[2] 0.45 0 0.07 0.32 0.59 3662 1
## sigma[1] 0.59 0 0.14 0.38 0.95 2691 1
## sigma[2] 0.26 0 0.06 0.17 0.40 3454 1
##
## Samples were drawn using NUTS(diag_e) at Mon Dec 29 16:00:50 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).
plot(m, pars = "gamma")
## ci_level: 0.8 (80% intervals)
## outer_level: 0.95 (95% intervals)
plot(m, pars = "beta")
## ci_level: 0.8 (80% intervals)
## outer_level: 0.95 (95% intervals)