Skip to contents

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.

Usage

prepare_data_density(stan_results)

Arguments

stan_results

A list containing all MCMC results and metadata, expected to include:

samples

An object of class `mcmc.list` (posterior samples for species- and complex-level parameters).

chain_length

Integer. Total number of MCMC iterations per chain.

burnin

Integer. Number of initial iterations to discard.

thinning

Integer. Thinning interval used during sampling.

species_complex

A `data.frame` mapping species and complexes with columns `species`, `complex`, `speciesNb`, `complexNb`.

Value

A `data.frame` in long format with the following columns:

`value`

Posterior sample value.

`Nb`

Index number of the parameter (species or complex ID).

`level`

Level of the parameter: `"species"`, `"complex"`, or `"genus"`.

`name`

Name of the species, complex, or `"GENUS"` (for group-level effect).

`higher_level`

Grouping label: complex name for species, self-name for complex/genus.

Details

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`.