The “stan_rhat” Function in R
Package: rstan
Purpose: Computes the potential scale reduction factor (R-hat) for Stan model parameters.
General class: Diagnostic
Required argument(s):
object: Stanfit object containing the samples.
Notable optional arguments:
...: Additional arguments passed to the function.
Example:
# Load the rstan package
library(rstan)
# Fit a simple linear regression model
model_code <- "
data {
int<lower=0> N;
vector[N] y;
}
parameters {
real mu;
real<lower=0> sigma;
}
model {
y ~ normal(mu, sigma);
}
"
model_data <- list(N = 100, y = rnorm(100))
fit <- stan(model_code = model_code, data = model_data, chains = 4)
# Compute R-hat for all parameters
rhat <- stan_rhat(fit)
print(rhat)This example shows how to use the stan_rhat function from the rstan package to compute the potential scale reduction factor (R-hat) for parameters in a Stan model (fit). The function takes the Stan object fit as the x argument and allows optional arguments (...) to be passed for customization.