The “stan_hist” Function in R

  • Package: rstan

  • Purpose: Histogram plotting for Stan model diagnostics.

  • General class: Visualization

  • Required argument(s):

    • object: Stanfit object from which to extract samples for plotting.

  • Notable optional arguments:

    • pars: A character vector specifying the parameters to plot. If not specified, all parameters are plotted.

    • ...: Additional arguments passed to the plotting function.

  • Example:

  • # Load the rstan package
    library(rstan)

    # Fit a simple linear regression model
    model_code <- "
    data {
    int<lower=0> N;
    vector[N] x;
    vector[N] y;
    }
    parameters {
    real alpha;
    real beta;
    real<lower=0> sigma;
    }
    model {
    y ~ normal(alpha + beta * x, sigma);
    }
    "
    model_data <- list(N = 100, x = rnorm(100), y = rnorm(100))
    fit <- stan(model_code = model_code, data = model_data, chains = 4)

    # Plot the histogram of the alpha parameter
    stan_hist(fit, pars = "alpha")

  • This example demonstrates how to use the stan_hist function from the rstan package to plot a histogram of a parameter (alpha in this case) extracted from a Stan model (fit). The function takes the Stan object fit as the x argument and allows optional arguments (...) to be passed to customize the plot.

Previous
Previous

The “stan_rhat” Function in R

Next
Next

The “traceplot” Function in R