View source: R/OUT_GET_get.bg.rate.R
get.bg.rate | R Documentation |
This function extracts background rate parameters from an
evorates_fit
object. It even works when the object has no branchwise
rate parameters (i.e., a Brownian Motion model).
get.bg.rate(
fit,
node.groups = list(),
edge.groups = list(),
type = c("chains", "quantiles", "means", "diagnostics"),
extra.select = NULL,
simplify = TRUE,
remove.trend = FALSE,
geometric = FALSE,
log = TRUE,
partial.match = TRUE,
keep.R = FALSE
)
fit |
An object of class " |
node.groups |
A list of numeric or character vectors specifying clades for
which to calculate background rates. Basically finds monophyletic clades including
all nodes in a given vector by passing the vector to |
edge.groups |
A list of numeric vectors specifying edge indices in |
type |
A string specifying whether to extract posterior samples (" |
extra.select |
A numeric, integer, or character vector specifying the specific samples/
quantiles/diagnostics to extract, depending on |
simplify |
|
remove.trend |
|
geometric |
|
log |
|
partial.match |
|
keep.R |
|
The tip and node indices of fit$call$tree
can be viewed by running:
plot(fit$call$tree); tiplabels(); nodelabels()
. The edge indices of
fit$call$tree
can be similarly viewed by running: plot(fit$call$tree);
edgelabels()
.
In the case of a fit
with constrained rate variance (R_sig2
) and no trend
(R_mu
) parameter, the background rate for any clade will simply be an
appropriately named/formatted (see Value for details) copy of the root rate (R_0
).
Even if there is a trend parameter but remove.trend
is TRUE
, this will
still result detrended background rates equivalent to the root rate (R_0
).
Typically an array of class "param_block
" with a param_type
set to whatever
type
is. The dimension of these arrays will generally go in the order of
iterations/quantiles/diagnostics, then parameters, then chains. Any dimensions of length 1 are
collapsed and stored as attributes if simplify
is TRUE
. Here, the parameters
will be background rates for edge groups defined by node.groups
, then background rates for
edge groups defined by edge.groups
.
The parameter names will differ according to function arguments: in general, each node/edge
group is assigned a name (either taken from original list input or an abbreviated code indicating
which edges are included in the group). The resulting parameter names will then generally be of
the form "bg_rate(<group_name>)
", denoting the weighted average of branchwise
rates for a particular group, with weights given by proportions of branch lengths for each edge
within the group. Additionally, names may contain "exp()
" or "log()
" per user
specifications of geometric
and log
. Lastly, if remove.trend
is TRUE
,
names may refer to "uncent_Rdev_i
" (i.e., uncentered rate deviations) rather than
"R_i
", where i denotes edge indices.
If keep.R
is TRUE
, the output will instead be a list containing param_block
arrays. If only one node/edge group is specified, this will be a list of two param_blocks: one
containing branchwise rate parameters named "R
" and one containing the background rate
named "bg_rate
". If multiple node/edge groups are specified, this will be a list with a
named element for each group. Each element will, in turn, contain a corresponding "R
"
and "bg_rate
" param_block.
more information on both detrending and averaging branchwise rates is available in the documentation for fit.evorates, input.evorates, run.evorates, and output.evorates, particularly in the section on parameter definitions
Other parameter extraction functions:
get.R()
,
get.post.traits()
,
remove.trend()
#get whale/dolphin evorates fit
data("cet_fit")
#get posterior samples of global background rate
bg.rate <- get.bg.rate(cet_fit)
#let's say we want to get the background rate for the genus Mesoplodon
bg.rate <- get.bg.rate(cet_fit, node.groups = "Mesoplodon")
#now let's say we want to get the background rate for some edges towards the root
plot(cet_fit$call$tree); edgelabels()
bg.rate <- get.bg.rate(cet_fit, edge.groups = c(1, 2, 9, 28, 29, 30))
#in the case of edge groups specifically, we can also do this to exclude edges
#does NOT work for node groups!
bg.rate <- get.bg.rate(cet_fit, edge.groups = -c(1, 2, 9, 28, 29, 30))
#we can also select specific samples
bg.rate <- get.bg.rate(cet_fit, edge.groups = c(1, 2, 9, 28, 29, 30), extra.select = c(1, 23, 47))
#here's a way get better names for the resulting parameters
#note that we can also combine multiple node/edge groups
bg.rate <- get.bg.rate(cet_fit,
node.groups = list("Mesoplodon" = "Mesoplodon"),
edge.groups = list("root_edges" = c(1, 2, 9, 28, 29, 30)))
#now let's look at a more practical example
#maybe we want to some rate differences among key clades?
#define clades containing Mesoplodon, orca, globicephalinae, and blue whale
node.groups <- list("Mesoplodon" = "Mesoplodon",
"Orcinus_orca" = "Orcinus",
"globicephalniae" = c("Pseudorca", "Feresa"),
"Balaenoptera_musculus" = "musculus")
#define edge group that includes all delphinidae EXCEPT for orca and globicephalinae
edge.groups <- exclude.clade(cet_fit$call$tree,
node1 = c("Feresa", "Orcinus"),
node2 = "Orcinus")
edge.groups <- exclude.clade(cet_fit$call$tree,
edge.group1 = edge.groups,
node2 = c("Pseudorca", "Feresa"))
edge.groups <- list("other_delphinidae" = edge.groups)
#now we get our nicely formatted parameters!
bg.rate <- get.bg.rate(cet_fit,
node.groups = node.groups,
edge.groups = edge.groups)
#could also look at quantiles, means, or diagnostics
med.bg.rate <- get.bg.rate(cet_fit,
node.groups = node.groups,
edge.groups = edge.groups,
type = "quantiles",
extra.select = 0.5)
mean.bg.rate <- get.bg.rate(cet_fit,
node.groups = node.groups,
edge.groups = edge.groups,
type = "means")
init.bg.rate <- get.bg.rate(cet_fit,
node.groups = node.groups,
edge.groups = edge.groups,
type = "diagnostics",
type = c("inits", "ess"))
#here's an example of what happens when you don't simplify the result
med.bg.rate <- get.bg.rate(cet_fit,
node.groups = node.groups,
edge.groups = edge.groups,
type = "quantiles",
extra.select = 0.5,
simplify = FALSE)
#note that, depending on your goals, it may be more efficient to do things like this instead
#just to avoid some redundant calculations
bg.rate <- get.bg.rate(cet_fit,
node.groups = node.groups,
edge.groups = edge.groups)
med.bg.rate <- bg.rate %quantiles% list(".", 0.5)
mean.bg.rate <- bg.rate %means% "."
#in some cases, it may be interesting to compare averaged rate deviations among groups
bg.rate <- get.bg.rate(cet_fit,
node.groups = node.groups,
edge.groups = edge.groups,
remove.trend = TRUE,
geometric = TRUE)
whole.bg <- get.bg.rate(cet_fit,
remove.trend = TRUE,
geometric = TRUE)
bg.rdev <- bg.rate - whole.bg
#we can even use this to get a posterior probability that, as a whole, a given clade is evolving anomalously fast
#given the trend in rates over time, at least, though we could shift this by changing the remove.trend and geometric arguments
post.probs <- (bg.rdev > 0) %means% "."
#or equivalently
post.probs <- (bg.rate > whole.bg) %means% "."
#as a demonstration, we can easily use these functions to recalculate branchwise rate deviations in general
tmp <- get.bg.rate(cet_fit,
remove.trend = TRUE,
geometric = TRUE,
keep.R = TRUE)
#note the change in format
rdevs <- tmp$R - tmp$bg_rate
c(rdevs %select% 1, cet_fit %chains% "^Rdev_1$") #exactly the same
#I don't necessarily recommend this, but you could even calculate rate deviations for subclades
#just a good demonstration of what happens when keep.R is TRUE
#though it doesn't make much sense for single lineages...
tmp <- get.bg.rate(cet_fit,
node.groups = node.groups,
edge.groups = edge.groups,
remove.trend = TRUE,
geometric = TRUE,
keep.R = TRUE)
#note the further change in format
sub.rdevs <- do.call(c, lapply(tmp, function(ii) ii$R - ii$bg_rate))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.