Loads, 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.

run_stan(data = data, iter = 2000, chains = 4, thinning = 1)

Arguments

data

clean generated by creation_df()

iter

Integer. Total number of MCMC iterations per chain (including warmup). Default is 2000.

chains

Integer. Number of MCMC chains. Default is 4.

thinning

Integer. Thinning interval for posterior samples. Default is 1.

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