Package {bGMYC4}


Type: Package
Title: Bayesian General Mixed Yule-Coalescent Model for Species Delimitation
Version: 4.3.1
Description: Implements a Bayesian version of the general mixed Yule-coalescent (GMYC) model for species delimitation from single-locus data (Reid & Carstens 2012, BMC Evolutionary Biology 12:196). Markov Chain Monte Carlo sampling evaluates the posterior distribution of species boundaries under flexible uniform priors, with branching rates profiled at their maximum likelihood estimates. Supports pooling over multiple posterior trees to propagate phylogenetic uncertainty into marginal probabilities of conspecificity, convergence diagnostics (split-Rhat, ESS, prior-vs-posterior contraction), and interactive heatmap visualization of the resulting matrices.
License: GPL (≥ 3)
Encoding: UTF-8
LazyData: true
Depends: R (≥ 4.0.0)
Imports: ape (≥ 5.0), parallel
Suggests: mcmcse, plotly, base64enc, testthat (≥ 3.0.0), knitr, rmarkdown, htmlwidgets, treeio, ggtree, dplyr
VignetteBuilder: knitr, rmarkdown
URL: https://github.com/DmitryKarabanov/bGMYC4
BugReports: https://github.com/DmitryKarabanov/bGMYC4/issues
Config/roxygen2/version: 8.1.1
NeedsCompilation: no
Packaged: 2026-11-23 13:28:35 UTC; Professional
Author: Noah Reid [ctb, cph] (Original author of bGMYC), Dmitry Karabanov ORCID iD [aut, cre]
Maintainer: Dmitry Karabanov <dk@ibiw.ru>
Repository: CRAN
Date/Publication: 2026-11-23 15:40:02 UTC

bGMYC4: Bayesian General Mixed Yule-Coalescent Model for Species Delimitation

Description

Bayesian implementation of the GMYC model (Pons et al. 2006; Reid & Carstens 2012). MCMC sampling of the threshold between Yule (speciation) and coalescent (within-species) branching on ultrametric trees. Pooling samples over a posterior sample of trees propagates phylogenetic uncertainty into marginal posterior probabilities of species limits and pairwise conspecificity.

Workflow

  1. bgmyc.singlephy() (one tree) or bgmyc.multiphylo() (many trees);

  2. diagnostics: plot(), summary(), checkrates(), bgmyc.chains() + bgmyc.convergence();

  3. spec.probmat() -> plot() / plot.interactive.probmat();

  4. point estimate bgmyc.point(); cluster posterior bgmyc.spec().

Author(s)

Maintainer: Dmitry Karabanov dk@ibiw.ru (ORCID)

Authors:

Other contributors:

References

Reid NM, Carstens BC (2012) Phylogenetic estimation error can decrease the accuracy of species delimitation: a Bayesian implementation of the general mixed Yule-coalescent model. BMC Evolutionary Biology 12:196. doi:10.1186/1471-2148-12-196

Pons J, Barraclough TG, Gomez-Zurita J, et al. (2006) Sequence-based species delimitation for the DNA taxonomy of undescribed insects. Systematic Biology 55(4):595–609. doi:10.1080/10635150600852011

See Also

Useful links:


Multiple independent MCMC chains for ONE tree

Description

Runs nchains independent chains on the same tree (optionally from overdispersed starts drawn from the priors) for Gelman-Rubin diagnostics.

Usage

bgmyc.chains(
  phylo,
  mcmc,
  burnin,
  thinning,
  py1 = 0,
  py2 = 2,
  pc1 = 0,
  pc2 = 2,
  t1 = 2,
  t2 = NULL,
  scale = c(20, 10, 5),
  start = NULL,
  sampler = bgmyc.gibbs,
  likelihood = bgmyc.lik,
  prior = bgmyc.prior,
  nchains = 4L,
  seed = NULL,
  cores = 1L,
  dispersed = TRUE
)

Arguments

phylo

An ape "phylo" object (see bgmyc.singlephy()).

mcmc

Total number of MCMC iterations (not the number of retained samples).

burnin

Number of initial iterations discarded as burn-in.

thinning

Interval at which samples are retained; the number of retained samples is floor((mcmc - burnin) / thinning).

py1, py2

Bounds of the uniform prior on the Yule rate change parameter py. The speciation rate with n lineages is proportional to n^py (Pons et al. 2006). With 50 species, py = 2 allows a 50-fold rate increase from root to tips, blurring the Yule/coalescent transition; py2 <= 1.5 (about 7-fold) is more realistic, and Reid & Carstens (2012) also used U(0, 1.2). This is the most influential of the three priors.

pc1, pc2

Bounds of the uniform prior on the coalescent rate change parameter pc; the within-species rate is proportional to (n(n-1))^pc. In principle pc < 1 mimics population decline and pc > 1 growth, but because identical haplotypes must be excluded, a strict demographic interpretation is not possible.

t1, t2

Lower and upper bounds of the threshold prior. t2 = NULL (the default) means the full range ⁠[max(2, t1), Nnode]⁠; the effective prior is always clamped to ⁠[2, Nnode]⁠.

scale

Numeric(3): proposal widths for py, pc (mean-preserving gamma proposals; larger = more conservative) and t (rounded normal random walk; larger = more liberal). Tune via acceptance rates (target roughly 0.2–0.4).

start

Starting values for ⁠(py, pc, t)⁠. NULL (the default) uses the midpoint of each prior interval, which always lies inside the prior.

sampler

MCMC sampler function; default bgmyc.gibbs().

likelihood

Log-likelihood function; default bgmyc.lik() (profile ML estimates of branching rates, Nee 2001).

prior

Log-prior function; default bgmyc.prior() (uniform). Replace dunif with dgamma/dlnorm for other prior families.

nchains

Number of chains (>= 2).

seed

Optional integer; per-chain L'Ecuyer-CMRG streams, caller RNG restored.

cores

Number of parallel PSOCK workers (default 1).

dispersed

Logical; draw overdispersed starting values from the priors.

Value

Object of class "bgmycchains": chains (list of "singlebgmyc"), nchains, dispersed.

See Also

bgmyc.convergence(), bgmyc.pool()

Examples

data(east10)
ch <- bgmyc.chains(east10[[1]], mcmc = 100, burnin = 10, thinning = 10,
                   nchains = 2, seed = 123)
bgmyc.convergence(ch)

Convergence diagnostics for replicate chains of one tree

Description

Split-Rhat (target < 1.05) and per-chain ESS (target > 200; requires mcmcse). Rhat is computed ONLY across chains of the same tree, never across trees.

Usage

bgmyc.convergence(chains)

Arguments

chains

A "bgmycchains" object from bgmyc.chains().

Value

List: summary (data frame with Rhat, ESS, acceptance, flags), per_chain_accept, per_chain_ess, rhat, ess_total.


Prepare an ultrametric tree for GMYC analysis

Description

Decomposes the tree into waiting times between branching events and, for every possible threshold, precomputes lineage-count (list.i.mat) and event-count (list.s.nod) matrices for the Yule and coalescent processes.

Usage

bgmyc.dataprep(tr)

Arguments

tr

An ape "phylo" object: ultrametric, fully bifurcating, no zero-length terminal branches.

Value

A list:

mrca.nodes

MRCA node indices (node number minus number of tips) per threshold.

nod.types

Node types per threshold: 0 = Yule, 1 = coalescent, 2 = species MRCA.

n

Number of coalescent processes (multi-tip species) per threshold.

list.s.nod

Event-count matrices per threshold.

list.i.mat

Lineage-count matrices per threshold (coalescent rows: n(n-1)).

internod

Waiting times between successive branching events (last: to present).

tree

The input tree.


MCMC sampler for the Bayesian GMYC model

Description

Metropolis-within-Gibbs sampler used by bgmyc.singlephy(), bgmyc.multiphylo() and bgmyc.chains(); rarely needed directly.

Usage

bgmyc.gibbs(
  data,
  m,
  burnin = 1,
  thinning = 1,
  py1,
  py2,
  pc1,
  pc2,
  t1,
  t2,
  scale = c(20, 10, 5),
  start = c(1, 0.5, 50),
  likelihood,
  prior
)

Arguments

data

Output of bgmyc.dataprep().

m

Total number of MCMC iterations.

burnin

Number of initial iterations discarded as burn-in.

thinning

Interval at which samples are retained; the number of retained samples is floor((mcmc - burnin) / thinning).

py1, py2

Bounds of the uniform prior on the Yule rate change parameter py. The speciation rate with n lineages is proportional to n^py (Pons et al. 2006). With 50 species, py = 2 allows a 50-fold rate increase from root to tips, blurring the Yule/coalescent transition; py2 <= 1.5 (about 7-fold) is more realistic, and Reid & Carstens (2012) also used U(0, 1.2). This is the most influential of the three priors.

pc1, pc2

Bounds of the uniform prior on the coalescent rate change parameter pc; the within-species rate is proportional to (n(n-1))^pc. In principle pc < 1 mimics population decline and pc > 1 growth, but because identical haplotypes must be excluded, a strict demographic interpretation is not possible.

t1, t2

Lower and upper bounds of the threshold prior. t2 = NULL (the default) means the full range ⁠[max(2, t1), Nnode]⁠; the effective prior is always clamped to ⁠[2, Nnode]⁠.

scale

Numeric(3): proposal widths for py, pc (mean-preserving gamma proposals; larger = more conservative) and t (rounded normal random walk; larger = more liberal). Tune via acceptance rates (target roughly 0.2–0.4).

start

Starting values for ⁠(py, pc, t)⁠. NULL (the default) uses the midpoint of each prior interval, which always lies inside the prior.

likelihood

Log-likelihood function; default bgmyc.lik() (profile ML estimates of branching rates, Nee 2001).

prior

Log-prior function; default bgmyc.prior() (uniform). Replace dunif with dgamma/dlnorm for other prior families.

Details

py and pc use mean-preserving gamma proposals with the correct Hastings correction; t uses a symmetric rounded-normal random walk (Hastings ratio 1), rejected outside ⁠[2, Nnode]⁠. Retained samples are seq(burnin + thinning, m, by = thinning).

Value

An object of class "singlebgmyc" (see bgmyc.singlephy()).

Examples

set.seed(123)
data(east10)
dat <- bgmyc.dataprep(east10[[1]])
out <- bgmyc.gibbs(dat, m = 100, burnin = 10, thinning = 10,
                   py1 = 0, py2 = 2, pc1 = 0, pc2 = 2, t1 = 2, t2 = 100,
                   start = c(1, 0.5, 50),
                   likelihood = bgmyc.lik, prior = bgmyc.prior)

GMYC log-likelihood with profiled branching rates

Description

GMYC log-likelihood with profiled branching rates

Usage

bgmyc.lik(params, data)

Arguments

params

Numeric vector c(py, pc, t).

data

Output of bgmyc.dataprep().

Details

Implements ⁠L(x_i) = b_i exp(-b_i x_i)⁠ with ⁠b_i = lambda_y n_i^py + sum_j lambda_c (n_ij (n_ij - 1))^pc⁠. lambda_y and lambda_c are profile maximum-likelihood estimates (Nee 2001), shared across coalescent processes, as in Reid & Carstens (2012). Empty intervals contribute zero rate even when p = 0 (the 0^0 guard is mathematically required).

Value

Log-likelihood (scalar); -Inf for invalid states.

References

Reid & Carstens (2012) doi:10.1186/1471-2148-12-196.


Bayesian GMYC analysis of multiple trees (phylogenetic uncertainty)

Description

Runs an independent MCMC chain for each tree of a "multiPhylo" object (e.g. a post-burn-in sample from BEAST). Pooling samples across trees yields marginal posterior probabilities of species limits that integrate over phylogenetic uncertainty (Reid & Carstens 2012).

Usage

bgmyc.multiphylo(
  multiphylo,
  mcmc,
  burnin,
  thinning,
  py1 = 0,
  py2 = 2,
  pc1 = 0,
  pc2 = 2,
  t1 = 2,
  t2 = NULL,
  scale = c(20, 10, 5),
  start = NULL,
  sampler = bgmyc.gibbs,
  likelihood = bgmyc.lik,
  prior = bgmyc.prior,
  cores = 1L,
  seed = NULL
)

Arguments

multiphylo

An ape "multiPhylo" object; all trees must share the same tip labels and meet the requirements of bgmyc.singlephy().

mcmc

Total number of MCMC iterations (not the number of retained samples).

burnin

Number of initial iterations discarded as burn-in.

thinning

Interval at which samples are retained; the number of retained samples is floor((mcmc - burnin) / thinning).

py1, py2

Bounds of the uniform prior on the Yule rate change parameter py. The speciation rate with n lineages is proportional to n^py (Pons et al. 2006). With 50 species, py = 2 allows a 50-fold rate increase from root to tips, blurring the Yule/coalescent transition; py2 <= 1.5 (about 7-fold) is more realistic, and Reid & Carstens (2012) also used U(0, 1.2). This is the most influential of the three priors.

pc1, pc2

Bounds of the uniform prior on the coalescent rate change parameter pc; the within-species rate is proportional to (n(n-1))^pc. In principle pc < 1 mimics population decline and pc > 1 growth, but because identical haplotypes must be excluded, a strict demographic interpretation is not possible.

t1, t2

Lower and upper bounds of the threshold prior. t2 = NULL (the default) means the full range ⁠[max(2, t1), Nnode]⁠; the effective prior is always clamped to ⁠[2, Nnode]⁠.

scale

Numeric(3): proposal widths for py, pc (mean-preserving gamma proposals; larger = more conservative) and t (rounded normal random walk; larger = more liberal). Tune via acceptance rates (target roughly 0.2–0.4).

start

Starting values for ⁠(py, pc, t)⁠. NULL (the default) uses the midpoint of each prior interval, which always lies inside the prior.

sampler

MCMC sampler function; default bgmyc.gibbs().

likelihood

Log-likelihood function; default bgmyc.lik() (profile ML estimates of branching rates, Nee 2001).

prior

Log-prior function; default bgmyc.prior() (uniform). Replace dunif with dgamma/dlnorm for other prior families.

cores

Number of parallel PSOCK workers. Default 1 (sequential, CRAN-safe). Workers require the package to be installed (not just load_all()-ed).

seed

Optional integer seed. If given, reproducible per-tree L'Ecuyer-CMRG streams are used and the caller's RNG state is restored on exit; if NULL, results are not reproducible.

Details

Recommended protocol of Reid & Carstens (2012): about 10,000 iterations per tree, burn-in 1,000, thinning 100 (about 90 samples per tree; 100 trees -> 9,000 pooled samples). Precision comes from pooling across trees; Gelman-Rubin Rhat is NOT computed across trees (each tree defines a distinct conditional posterior). For replicate chains of ONE tree use bgmyc.chains() + bgmyc.convergence().

Value

Object of class "multibgmyc": a list of "singlebgmyc" objects.

References

Reid & Carstens (2012) doi:10.1186/1471-2148-12-196.

Examples

data(east10)
res <- bgmyc.multiphylo(east10[1:2], mcmc = 100, burnin = 10, thinning = 10, seed = 123)
pm <- spec.probmat(res)

Point estimate of species limits from a conspecificity matrix

Description

Greedy single-linkage clustering: a sequence joins a cluster if its probability of conspecificity with ANY member exceeds ppcutoff; clusters with overlapping memberships are merged.

Usage

bgmyc.point(probmat, ppcutoff)

Arguments

probmat

A "bgmycprobmat" matrix from spec.probmat().

ppcutoff

Posterior probability threshold for lumping tips into species. spec.probmat() returns a marginal probability pooled over thresholds and trees, so it is not comparable to a clade posterior probability. Because clustering is single linkage, a species is held together by its weakest link: raising the cutoff removes bridges between intraspecific lineages and fragments real species, whereas pairs from different species usually have probabilities near zero. Empirical data typically show a stability plateau at low values (0.001 to 0.05); 0.05 is the value used in the original bGMYC documentation. Choose the cutoff from a sweep over several values and report the plateau in your methods.

Value

A list of character vectors, each giving the members of one species. Because of phylogenetic uncertainty, not every pair within a returned species necessarily exceeds ppcutoff; members are connected through intermediate individuals (single linkage). A warning flags such chains.

Examples

set.seed(123)
data(east10)
res <- bgmyc.singlephy(east10[[1]], mcmc = 100, burnin = 10, thinning = 10)
bgmyc.point(spec.probmat(res), ppcutoff = 0.5)

Pool replicate chains of one tree into a single sample

Description

Concatenates retained samples of chains targeting the SAME conditional posterior into one "singlebgmyc" object.

Usage

bgmyc.pool(chains)

Arguments

chains

A "bgmycchains" object.

Value

A "singlebgmyc" object (see bgmyc.singlephy()).


Log-prior density of the GMYC parameters

Description

Log-prior density of the GMYC parameters

Usage

bgmyc.prior(params, py1, py2, pc1, pc2, t1, t2)

Arguments

params

Numeric vector c(py, pc, t).

py1, py2

Bounds of the uniform prior on the Yule rate change parameter py. The speciation rate with n lineages is proportional to n^py (Pons et al. 2006). With 50 species, py = 2 allows a 50-fold rate increase from root to tips, blurring the Yule/coalescent transition; py2 <= 1.5 (about 7-fold) is more realistic, and Reid & Carstens (2012) also used U(0, 1.2). This is the most influential of the three priors.

pc1, pc2

Bounds of the uniform prior on the coalescent rate change parameter pc; the within-species rate is proportional to (n(n-1))^pc. In principle pc < 1 mimics population decline and pc > 1 growth, but because identical haplotypes must be excluded, a strict demographic interpretation is not possible.

t1, t2

Lower and upper bounds of the threshold prior. t2 = NULL (the default) means the full range ⁠[max(2, t1), Nnode]⁠; the effective prior is always clamped to ⁠[2, Nnode]⁠.

Details

Sum of three uniform log-densities. For the integer threshold this is a discrete uniform up to a constant (cancels in the MH ratio). Modify this function to use gamma or lognormal priors; it plugs directly into bgmyc.gibbs().

Value

Log prior density (scalar).


Bayesian GMYC analysis of a single ultrametric tree

Description

Runs an MCMC sampler for the general mixed Yule-coalescent model on one ultrametric, fully bifurcating tree with unique haplotypes only.

Usage

bgmyc.singlephy(
  phylo,
  mcmc,
  burnin,
  thinning,
  py1 = 0,
  py2 = 2,
  pc1 = 0,
  pc2 = 2,
  t1 = 2,
  t2 = NULL,
  scale = c(20, 10, 5),
  start = NULL,
  sampler = bgmyc.gibbs,
  likelihood = bgmyc.lik,
  prior = bgmyc.prior
)

Arguments

phylo

An ape "phylo" object. Must be ultrametric, fully resolved, and without zero-length terminal branches. Prune identical haplotypes BEFORE tree estimation (Reid & Carstens 2012); see fix_ultrametric() for rounding-level non-ultrametricity.

mcmc

Total number of MCMC iterations (not the number of retained samples).

burnin

Number of initial iterations discarded as burn-in.

thinning

Interval at which samples are retained; the number of retained samples is floor((mcmc - burnin) / thinning).

py1, py2

Bounds of the uniform prior on the Yule rate change parameter py. The speciation rate with n lineages is proportional to n^py (Pons et al. 2006). With 50 species, py = 2 allows a 50-fold rate increase from root to tips, blurring the Yule/coalescent transition; py2 <= 1.5 (about 7-fold) is more realistic, and Reid & Carstens (2012) also used U(0, 1.2). This is the most influential of the three priors.

pc1, pc2

Bounds of the uniform prior on the coalescent rate change parameter pc; the within-species rate is proportional to (n(n-1))^pc. In principle pc < 1 mimics population decline and pc > 1 growth, but because identical haplotypes must be excluded, a strict demographic interpretation is not possible.

t1, t2

Lower and upper bounds of the threshold prior. t2 = NULL (the default) means the full range ⁠[max(2, t1), Nnode]⁠; the effective prior is always clamped to ⁠[2, Nnode]⁠.

scale

Numeric(3): proposal widths for py, pc (mean-preserving gamma proposals; larger = more conservative) and t (rounded normal random walk; larger = more liberal). Tune via acceptance rates (target roughly 0.2–0.4).

start

Starting values for ⁠(py, pc, t)⁠. NULL (the default) uses the midpoint of each prior interval, which always lies inside the prior.

sampler

MCMC sampler function; default bgmyc.gibbs().

likelihood

Log-likelihood function; default bgmyc.lik() (profile ML estimates of branching rates, Nee 2001).

prior

Log-prior function; default bgmyc.prior() (uniform). Replace dunif with dgamma/dlnorm for other prior families.

Details

Branching rates lambda are NOT sampled: at every step they are fixed at their maximum-likelihood estimates conditional on the current threshold and rate-change parameters, exactly as in Reid & Carstens (2012); rates and rate-change parameters are shared across all coalescent processes. The resulting posterior is therefore an empirical-Bayes (profile) posterior. Check stationarity with plot(output) and summary(output); acceptance rates are computed over all iterations, including burn-in.

Value

An object of class "singlebgmyc":

par

Matrix of retained samples: py, pc, t, log-posterior.

accept

Acceptance rates for the three parameters.

tree

The input tree.

mrca

MRCA node indices per threshold (from bgmyc.dataprep()).

priors

Named vector ⁠py1, py2, pc1, pc2, t1, t2⁠.

References

Reid & Carstens (2012) doi:10.1186/1471-2148-12-196; Pons et al. (2006) doi:10.1080/10635150600852011.

See Also

bgmyc.multiphylo(), bgmyc.chains(), spec.probmat(), checkrates()

Examples

set.seed(123)
data(east10)
res <- bgmyc.singlephy(east10[[1]], mcmc = 100, burnin = 10, thinning = 10)
plot(res)

Posterior distribution of species clusters

Description

Processes the output of bgmyc.singlephy() or bgmyc.multiphylo() and tabulates every distinct species partition with its posterior probability. Optionally returns the posterior collection of species-by-locality matrices for downstream community analyses (Picante, Vegan). Cluster strings are generated once per unique threshold and weighted by its frequency.

Usage

bgmyc.spec(res, filename = NULL, cmatrix = NULL)

Arguments

res

A "singlebgmyc" or "multibgmyc" object.

filename

Optional path; the cluster table is also written to this file.

cmatrix

Optional tips-by-localities matrix (rownames = tip labels).

Value

A list with specprobs and, optionally, matrices.


Posterior summaries for bGMYC analyses

Description

Threshold mean/median, 95% shortest credible interval and equal-tailed interval; parameter summaries; ESS (via mcmcse); prior-vs-posterior contraction. For "multibgmyc", thresholds are pooled across trees (marginal posterior); per-tree diagnostics are reported separately and Rhat is NOT computed across trees.

Usage

## S3 method for class 'singlebgmyc'
summary(object, prob = 0.95, priors = NULL, ...)

## S3 method for class 'bgmycchains'
summary(object, prob = 0.95, priors = NULL, ...)

## S3 method for class 'multibgmyc'
summary(object, prob = 0.95, ...)

## S3 method for class 'bgmycsummary'
print(x, ...)

Arguments

object

A "singlebgmyc", "bgmycchains" or "multibgmyc" object.

prob

Coverage probability (default 0.95).

priors

Optional named vector ⁠py1, py2, pc1, pc2, t1, t2⁠ overriding the priors stored in the object.

...

Unused.

x

A "bgmycsummary" object returned by summary().

Value

Object of class "bgmycsummary" (print method provided).


Branching rates and model-fit diagnostics

Description

Recomputes the Yule and coalescent branching rates for every retained MCMC sample. GMYC assumes the two processes act on very different time scales (roughly an order of magnitude apart); if the rates are similar, the threshold is indistinct and results unreliable.

Usage

checkrates(result)

Arguments

result

A "singlebgmyc" or "multibgmyc" object.

Value

Object of class "bgmycrates": matrix with columns p.div, p.coal, threshold, lambda.div, lambda.coal, lambda.div.mod. The last is lambda.div * t^p.div / t, i.e. the fold change in speciation rate from the root to the last speciation event (Reid & Carstens 2012).

Examples

set.seed(123)
data(east10)
res <- bgmyc.singlephy(east10[[1]], mcmc = 100, burnin = 10, thinning = 10)
rates <- checkrates(res)
plot(rates)

Ten posterior trees from a simulated dataset

Description

A "multiPhylo" object of 10 ultrametric trees (215 tips, 214 internal nodes each) sampled from the BEAST posterior of a simulated single-locus dataset; used in examples and the vignette.

Usage

east10

Format

A "multiPhylo" list of 10 "phylo" objects with shared tip labels.

Source

Simulated example data inherited from the original bGMYC package (Reid & Carstens 2012); tree estimation as in that study.

Examples

data(east10)
length(east10)

Fix rounding-level non-ultrametricity

Description

Rounds branch lengths to digits decimals; stops if the tree remains non-ultrametric (re-export from BEAST or check the clock model instead). Preprocessing only — the GMYC likelihood is unaffected.

Usage

fix_ultrametric(tr, digits = 8)

Arguments

tr

A "phylo" object.

digits

Number of decimals for rounding (default 8).

Value

The (possibly rounded) tree.


GMYC likelihood with all five parameters free (experimental)

Description

GMYC likelihood with all five parameters free (experimental)

Usage

gmyc.5parameter.lik(params, data)

Arguments

params

Numeric vector c(py, pc, lambda_y, lambda_c, t) — rate-change parameters first, then branching rates, then the threshold index.

data

Output of bgmyc.dataprep().

Details

Not used by the default sampler. The Pons et al. implementation and bgmyc.lik() profile the branching rates at their ML estimates (3 free parameters); letting all 5 vary can produce a flat likelihood surface with a peak far from the true value. Reid & Carstens (2012) found the profile version empirically better; this function is kept for experimentation.

Value

Log-likelihood (scalar).


Shortest discrete credible interval (HPD analogue)

Description

Shortest contiguous interval of integer values carrying at least prob posterior mass. For multimodal posteriors a true highest-density set may be disjoint; this is the standard shortest-interval analogue used for the threshold (number of species).

Usage

hpd_discrete(x, prob = 0.95)

Arguments

x

Numeric/integer vector of posterior samples (e.g. threshold column).

prob

Coverage probability (default 0.95).

Value

Integer vector c(lo, hi).


Conspecificity heatmap with the phylogenetic tree

Description

Plots a "bgmycprobmat" matrix ordered by tree topology, alongside the tree (cf. Reid & Carstens 2012, Fig. 5).

Usage

## S3 method for class 'bgmycprobmat'
plot(x, tree, palette = "classic", save_pdf = NULL, legend_cex = 0.5, ...)

Arguments

x

A "bgmycprobmat" matrix from spec.probmat().

tree

A "phylo" object with the same tip labels.

palette

"classic" (heat colours), "viridis" or "RdYlBu".

save_pdf

Optional PDF path (10 x 8 in).

legend_cex

Legend text size.

...

Unused.

Value

No return value; called for side effects.

Examples

set.seed(123)
data(east10)
res <- bgmyc.singlephy(east10[[1]], mcmc = 100, burnin = 10, thinning = 10)
plot(spec.probmat(res), east10[[1]])

Diagnostic plots of coalescent vs Yule rates

Description

2x2 panel of lambda.coal / lambda.div.mod (trace, log10 trace, histograms). Values near 1 indicate an indistinct threshold and unreliable delimitation.

Usage

## S3 method for class 'bgmycrates'
plot(x, ...)

Arguments

x

A "bgmycrates" object from checkrates().

...

Unused.

Value

No return value; called for side effects.


Interactive conspecificity heatmap (plotly)

Description

HTML heatmap of pairwise conspecificity probabilities with hover details, optionally aligned with a PNG of the tree; exportable via save_html. Requires suggested packages plotly, base64enc, htmlwidgets.

Usage

## S3 method for class 'interactive.probmat'
plot(
  x,
  tree,
  palette = "green",
  show_tree = TRUE,
  tree_width = 0.3,
  width = 1400,
  height = 1000,
  save_html = NULL,
  ...
)

Arguments

x

A "bgmycprobmat" matrix.

tree

A "phylo" object with the same tip labels.

palette

"green" (default), "viridis", "RdYlBu" or "warm".

show_tree

Logical; render the tree to the left of the matrix.

tree_width

Fraction of the figure width used by the tree (0–1).

width, height

Figure size in pixels.

save_html

Optional HTML output path (self-contained).

...

Passed to ape::plot.phylo() when drawing the tree.

Value

Invisibly, the plotly object.

Examples


set.seed(123)
data(east10)
res <- bgmyc.singlephy(east10[[1]], mcmc = 100, burnin = 10, thinning = 10)
plot.interactive.probmat(spec.probmat(res), east10[[1]])


Trace plots of pooled multi-tree bGMYC MCMC

Description

Trace plots of pooled multi-tree bGMYC MCMC

Usage

## S3 method for class 'multibgmyc'
plot(x, plot = TRUE, ...)

Arguments

x

A "multibgmyc" object.

plot

Logical; if FALSE, returns the pooled parameter matrix instead of plotting.

...

Passed to graphics::plot().

Value

If plot = TRUE: side effects only; else a pooled matrix (py, pc, t, log-posterior).


Trace plots of a single-tree bGMYC MCMC

Description

2x2 panel: py, pc, threshold, log-posterior.

Usage

## S3 method for class 'singlebgmyc'
plot(x, burnin = 0, thinning = 1, ...)

Arguments

x

A "singlebgmyc" object.

burnin, thinning

Applied to the ALREADY thinned retained samples; leave at defaults (0, 1). A warning is issued otherwise.

...

Passed to graphics::plot().

Value

No return value; called for side effects.


Matrix of pairwise posterior probabilities of conspecificity

Description

Computes the marginal posterior probability that each pair of tips belongs to the same species, pooling over all retained MCMC samples (and, for "multibgmyc", over all trees). Samples are aggregated by unique threshold instead of looping over every retained sample, which is much faster on large outputs.

Usage

spec.probmat(res)

Arguments

res

Output of bgmyc.singlephy() or bgmyc.multiphylo().

Value

A square matrix of class "bgmycprobmat" with tip-label dimnames; the diagonal is always 1.