prepare_data_density.Rd
Converts MCMC samples from a hierarchical Bayesian model into a tidy (long-format) `data.frame` enriched with taxonomic information. The function identifies the level (species, complex, or genus), assigns names based on taxonomy, and maps groupings for later plotting.
prepare_data_density(stan_results)
An object of class `mcmc.list` (e.g., output from `coda::mcmc.list`), containing posterior samples for species- and complex-level parameters (named `p1[...]` and `p2[...]`).
Integer. Total number of MCMC iterations per chain.
Integer. Number of initial iterations to discard as burn-in.
Integer. Thinning interval used during sampling (e.g., every 10th draw).
A `data.frame` mapping species and complexes. Must include:
`species`: species names,
`complex`: complex names,
`speciesNb`: integer codes for species (matching Stan),
`complexNb`: integer codes for complexes (matching Stan).
A `data.frame` in long format with the following columns:
Posterior sample value.
Index number of the parameter (species or complex ID).
Level of the parameter: `"species"`, `"complex"`, or `"genus"`.
Name of the species, complex, or `"GENUS"` (for group-level effect).
Grouping label: complex name for species, self-name for complex/genus.
Species-level parameters are assumed to be named `p2[...]` and complex-level parameters `p1[...]`. A special case is handled when `p1[1]` represents the genus-level intercept. The function processes indices and variable names using regular expressions, and joins taxonomic metadata using `species_complex`.