Bayesian estimation: https://apps.mathpsy.uni-tuebingen.de/fw/bayesestim/
# - Explicit constraints on data and parameters
# - Explicit prior
s <- "
data {
int<lower=0> y; // avoid nonsensical values
}
parameters {
real<lower=0, upper=1> theta; // satisfy initialization requirements
}
model {
theta ~ beta(1, 1); // uninformative prior (avoid if possible)
y ~ binomial(30, theta);
}
"# The stan function
# - transpiles the model code to C++
# - compiles the C++ to an executable
# - calls the executable: draw MCMC samples for all parameters
m <- stan(model_code = s, data = list(y = 20))
## Working with the stanfit object
print(m) # estimates## 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% 25% 50% 75% 97.5% n_eff Rhat
## theta 0.65 0.00 0.08 0.48 0.6 0.66 0.71 0.81 1419 1
## lp__ -21.11 0.02 0.74 -23.15 -21.3 -20.83 -20.64 -20.59 1701 1
##
## Samples were drawn using NUTS(diag_e) at Wed Oct 14 13:40:48 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).
## 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% 50% 97.5% n_eff Rhat
## theta 0.65 0 0.08 0.48 0.66 0.81 1419 1
##
## Samples were drawn using NUTS(diag_e) at Wed Oct 14 13:40:48 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)
## [1] 0.3997579 0.6418424 0.7798567 0.7390159 0.6874286 0.6966794
# data {
# int<lower=0> y;
# }
# parameters {
# real logittheta; // unconstrain
# }
# transformed parameters {
# real<lower=0, upper=1> theta;
# theta = inv_logit(logittheta); // back-transform
# }
# model {
# logittheta ~ normal(0, 1);
# y ~ binomial_logit(30, logittheta); // expect log(theta/(1 - theta))
# }s <- "
data {
int<lower=0> y;
}
parameters {
real<lower=0, upper=1> theta;
}
model {
theta ~ beta(1, 1);
//Uncomment the following line to draw from posterior predictive
// y ~ binomial(30, theta);
}
generated quantities {
int yrep = binomial_rng(30, theta);
}
"
m <- stan(model_code = s, data = list(y = 20))
extract(m)$yrep |> table() |> plot()## Prior predictive distribution in R
rbeta(5000, 1, 1) |> # draw theta from prior
rbinom(5000, 30, prob = _) |> # draw from data distribution
table() |>
plot()## Posterior predictive distribution in R
rbeta(5000, 20 + 1, 10 + 1) |> # draw theta from posterior
rbinom(5000, 30, prob = _) |> # draw from data distribution
table() |>
plot()## One-dimensional containers
# array[n] int x; // {1, 2, 3}
# array[n] real x;
# vector[n] x; // for reals only, [1, 2, 3]'
# row_vector[n] x; // for reals only, [1, 2, 3]
## Two-dimensional containers
# array[n, m] int x;
# array[n, m] real x;
# array[n] vector[m] x;
# array[n] row_vector[m] x;
# matrix[n, m] x;s <- "
data {
int N; // sample size
array[N] real y; // response
}
parameters {
real mu;
real<lower=0> sigma;
}
model {
mu ~ normal(50, 20); // prior for mu
sigma ~ lognormal(log(10), 1); // prior for sigma
y ~ normal(mu, sigma); // data distribution
}
"
m <- stan(model_code = s,
data = list(y = attitude$rating, # satisfaction with departments
N = nrow(attitude)) # of a company
)
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 64.43 0.04 2.29 59.75 68.94 3176 1
## sigma 12.44 0.03 1.64 9.74 15.99 3102 1
##
## Samples were drawn using NUTS(diag_e) at Wed Oct 14 13:42:44 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).
s <- "
data {
int<lower=1> N;
vector[N] x;
vector[N] y;
}
parameters {
real alpha;
real beta;
real<lower=0> sigma;
}
model {
alpha ~ normal(0, 20);
beta ~ normal(5, 10);
sigma ~ lognormal(log(20), 1);
// y ~ normal(alpha + beta * x, sigma); // uncomment for post. pred.
}
generated quantities {
array[N] real yrep = normal_rng(alpha + beta * x, sigma);
}
"
m <- stan(model_code = s,
data = list(y = cars$dist, x = cars$speed, N = nrow(cars))
)
print(m, pars = c("alpha", "beta", "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
## alpha 0.11 0.31 19.92 -38.55 38.70 4186 1
## beta 5.12 0.14 10.09 -14.85 24.96 4916 1
## sigma 33.37 0.76 42.08 2.90 143.03 3091 1
##
## Samples were drawn using NUTS(diag_e) at Wed Oct 14 13:43:43 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).
par(mfrow = c(3, 3), mai = c(.4, .4, .1, .1), mgp = c(2, .7, 0))
plot(dist ~ speed, cars, ylim = c(0, 120), ylab = "Original dist")
for(i in 1:8)
plot(extract(m)$yrep[sample(4000, 1), ] ~ speed, cars,
ylab = "Prior pred. dist")