Run Hierarchical Bayesian Model with rstan
run_stan.RdLoads, filters, and formats data, then compiles and runs a hierarchical Bayesian binomial model using Stan. The model estimates species-level proportions within species complexes, accounting for hierarchical structure and covariates such as region and year.
Value
A named list containing:
- `fit`
An `rstan` `stanfit` object with the fitted model and posterior samples.
- `species_complex`
Data frame or vector linking species to complexes used in the model.
- `nice_varname`
User-friendly variable name for labeling and plotting.
- `varname`
The original variable name input.
- `iter`
Number of iterations used in sampling.
- `warmup`
Number of warmup (burn-in) iterations.
- `thinning`
Thinning interval applied during sampling.
Details
The function calls `creation_df()` to filter and preprocess the data, then prepares Stan-compatible data using `prepare_stan_data()`. The Stan model file `"Hierarchical_Model.stan"` is loaded from the package directory, compiled, and sampled with specified MCMC parameters. Recommended control parameters `adapt_delta = 0.99` and `max_treedepth = 15` improve convergence. Warmup is set to 20