Background: https://mc-stan.org/learn-stan/case-studies/pool-binary-trials.html
Data: EfronMorrisBB.txt
dat <- read.table("EfronMorrisBB.txt", header = TRUE)
dat <- cbind(
dat[, c("FirstName", "LastName", "At.Bats", "Hits", "RemainingAt.Bats")],
"RemainingHits" = dat$SeasonHits - dat$Hits
)
datlist <- list(
N = nrow(dat),
n = dat$At.Bats,
y = dat$Hits,
nnew = dat$RemainingAt.Bats
)
dotchart(dat$Hits/dat$At.Bats, dat$LastName, xlab = "Hit rate")
points(dat$RemainingHits/dat$RemainingAt.Bats, 1:18,
col = "lightblue4", pch = 4)
legend(.2, 21, c("first 45", "remaining season"), pch = c(1, 4), xpd = NA,
col = c("black", "lightblue4"), horiz = TRUE, bty = "n")s <- "
data {
int<lower=1> N; // number of players (total N)
array[N] int<lower=1> n; // trials per player
array[N] int<lower=0> y; // hits per player
}
parameters {
real<lower=0, upper=1> theta; // common hit rate
}
model {
theta ~ beta(1, 3);
y ~ binomial(n, theta);
}
"
m1 <- stan(model_code = s, data = datlist)
print(m1, pars = "theta", 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
## theta 0.27 0 0.02 0.23 0.3 1343 1
##
## Samples were drawn using NUTS(diag_e) at Tue Nov 3 14:35:40 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).
## ci_level: 0.8 (80% intervals)
## outer_level: 0.95 (95% intervals)
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.
s <- "
data {
int<lower=1> N;
array[N] int<lower=1> n;
array[N] int<lower=0> y;
}
parameters {
vector<lower=0, upper=1>[N] theta; // by-player hit rate
}
model {
theta ~ beta(1, 3);
y ~ binomial(n, theta);
}
"
m2 <- stan(model_code = s, data = datlist)
print(m2, pars = c("theta[1]", "theta[18]"), 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
## theta[1] 0.39 0 0.07 0.26 0.53 9769 1
## theta[18] 0.16 0 0.05 0.08 0.28 10861 1
##
## Samples were drawn using NUTS(diag_e) at Tue Nov 3 14:36:49 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).
## ci_level: 0.8 (80% intervals)
## outer_level: 0.95 (95% intervals)
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.
s <- "
data {
int<lower=1> N;
array[N] int<lower=1> n;
array[N] int<lower=0> y;
}
parameters {
real<lower=0, upper=1> phi; // group hit rate, phi = a/(a + b)
real<lower=1> kappa; // hyperparameter, kappa = a + b
vector<lower=0, upper=1>[N] theta; // by-player hit rate
}
model {
phi ~ beta(1, 3);
//kappa ~ uniform(1, 50);
kappa ~ pareto(1, 1.5); // PIT samples: x_min / runif(5000)^(1/a)
theta ~ beta(phi * kappa, kappa - phi * kappa); // back-transform
y ~ binomial(n, theta);
}
"
m3 <- stan(model_code = s, data = datlist)
print(m3, pars = c("phi", "kappa", "theta[1]", "theta[18]"),
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 0.27 0.00 0.02 0.23 0.31 2333 1
## kappa 93.37 3.83 99.39 15.96 387.07 674 1
## theta[1] 0.32 0.00 0.05 0.23 0.44 2628 1
## theta[18] 0.22 0.00 0.04 0.13 0.30 2537 1
##
## Samples were drawn using NUTS(diag_e) at Tue Nov 3 14:37:53 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).
## ci_level: 0.8 (80% intervals)
## outer_level: 0.95 (95% intervals)
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.
## Help with convergence: switch to logit scale (but wait, there's more)
# data {
# int<lower=1> N;
# array[N] int<lower=1> n;
# array[N] int<lower=0> y;
# }
# parameters {
# real mu; // group logit hit rate
# real<lower=0> sigma; // group logit sd
# vector[N] logittheta; // by-player logit hit rate
# }
# model {
# mu ~ normal(-1, 1);
# sigma ~ normal(0, 1); // half normal
# logittheta ~ normal(mu, sigma);
# y ~ binomial_logit(n, logittheta);
# }
# generated quantities {
# real phi = inv_logit(mu); // back-transform
# vector[N] theta = inv_logit(logittheta);
# }## Help with convergence: noncentered parameterization
s <- "
data {
int N;
array[N] int n;
array[N] int<lower=1> nnew; // by-player remaining at bats
array[N] int y;
}
parameters {
real mu;
real<lower=0> sigma;
vector[N] z; // by-player standardized logit hit rate
}
model {
mu ~ normal(-1, 1);
sigma ~ normal(0, 1);
z ~ std_normal();
y ~ binomial_logit(n, mu + sigma * z);
}
generated quantities {
real phi = inv_logit(mu);
vector[N] theta = inv_logit(mu + sigma * z);
array[N] int yrep = binomial_rng(n, theta); // replication data
array[N] int ynew = binomial_rng(nnew, theta); // held-out data
}
"
m4 <- stan(model_code = s, data = datlist)
print(m4, pars = c("phi", "sigma", "theta[1]", "theta[18]"),
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 0.26 0 0.02 0.23 0.30 3809 1
## sigma 0.17 0 0.11 0.01 0.42 1635 1
## theta[1] 0.29 0 0.04 0.23 0.39 3248 1
## theta[18] 0.24 0 0.03 0.16 0.30 3130 1
##
## Samples were drawn using NUTS(diag_e) at Tue Nov 3 14:50:47 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).
## ci_level: 0.8 (80% intervals)
## outer_level: 0.95 (95% intervals)
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.
## Neal funnel
par(mfrow = 1:2)
extract(m4) |>
plot(log(sigma) ~ z[, 1], data = _)
extract(m4) |>
plot(log(sigma) ~ theta[, 1], data = _)# data {
# int<lower=1> N;
# array[N] int<lower=1> n;
# array[N] int<lower=0> y;
# }
# parameters {
# real mu;
# real<lower=0> sigma;
# vector<offset=mu, // implicitly creates
# multiplier=sigma>[N] logittheta; // x = mu + s*z
# }
# model {
# mu ~ normal(-1, 1);
# sigma ~ normal(0, 1);
# logittheta ~ normal(mu, sigma);
# y ~ binomial_logit(n, logittheta);
# }