| 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 |
| 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
-
bgmyc.singlephy()(one tree) orbgmyc.multiphylo()(many trees); diagnostics:
plot(),summary(),checkrates(),bgmyc.chains()+bgmyc.convergence();-
spec.probmat()->plot()/plot.interactive.probmat(); point estimate
bgmyc.point(); cluster posteriorbgmyc.spec().
Author(s)
Maintainer: Dmitry Karabanov dk@ibiw.ru (ORCID)
Authors:
Dmitry Karabanov dk@ibiw.ru (ORCID)
Other contributors:
Noah Reid noah.reid@gmail.com (Original author of bGMYC) [contributor, copyright holder]
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:
Report bugs at https://github.com/DmitryKarabanov/bGMYC4/issues
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 |
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 |
py1, py2 |
Bounds of the uniform prior on the Yule rate change parameter
|
pc1, pc2 |
Bounds of the uniform prior on the coalescent rate change
parameter |
t1, t2 |
Lower and upper bounds of the threshold prior. |
scale |
Numeric(3): proposal widths for |
start |
Starting values for |
sampler |
MCMC sampler function; default |
likelihood |
Log-likelihood function; default |
prior |
Log-prior function; default |
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 |
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 |
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 |
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 |
py1, py2 |
Bounds of the uniform prior on the Yule rate change parameter
|
pc1, pc2 |
Bounds of the uniform prior on the coalescent rate change
parameter |
t1, t2 |
Lower and upper bounds of the threshold prior. |
scale |
Numeric(3): proposal widths for |
start |
Starting values for |
likelihood |
Log-likelihood function; default |
prior |
Log-prior function; default |
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 |
data |
Output of |
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 |
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 |
py1, py2 |
Bounds of the uniform prior on the Yule rate change parameter
|
pc1, pc2 |
Bounds of the uniform prior on the coalescent rate change
parameter |
t1, t2 |
Lower and upper bounds of the threshold prior. |
scale |
Numeric(3): proposal widths for |
start |
Starting values for |
sampler |
MCMC sampler function; default |
likelihood |
Log-likelihood function; default |
prior |
Log-prior function; default |
cores |
Number of parallel PSOCK workers. Default 1 (sequential,
CRAN-safe). Workers require the package to be installed (not just
|
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 |
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 |
ppcutoff |
Posterior probability threshold for lumping tips into
species. |
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 |
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 |
py1, py2 |
Bounds of the uniform prior on the Yule rate change parameter
|
pc1, pc2 |
Bounds of the uniform prior on the coalescent rate change
parameter |
t1, t2 |
Lower and upper bounds of the threshold prior. |
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 |
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 |
py1, py2 |
Bounds of the uniform prior on the Yule rate change parameter
|
pc1, pc2 |
Bounds of the uniform prior on the coalescent rate change
parameter |
t1, t2 |
Lower and upper bounds of the threshold prior. |
scale |
Numeric(3): proposal widths for |
start |
Starting values for |
sampler |
MCMC sampler function; default |
likelihood |
Log-likelihood function; default |
prior |
Log-prior function; default |
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: |
accept |
Acceptance rates for the three parameters. |
tree |
The input tree. |
mrca |
MRCA node indices per threshold (from |
priors |
Named vector |
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 |
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 |
prob |
Coverage probability (default 0.95). |
priors |
Optional named vector |
... |
Unused. |
x |
A |
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 |
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 |
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 |
data |
Output of |
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 |
tree |
A |
palette |
|
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 |
... |
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 |
tree |
A |
palette |
|
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 |
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 |
plot |
Logical; if |
... |
Passed to |
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 |
burnin, thinning |
Applied to the ALREADY thinned retained samples; leave at defaults (0, 1). A warning is issued otherwise. |
... |
Passed to |
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 |
Value
A square matrix of class "bgmycprobmat" with tip-label dimnames;
the diagonal is always 1.