View source: R/simulate_mk_model.R
simulate_mk_model | R Documentation |
Given a rooted phylogenetic tree, a fixed-rates continuous-time Markov model for the evolution of a discrete trait ("Mk model", described by a transition matrix) and a probability vector for the root, simulate random outcomes of the model on all nodes and/or tips of the tree. The function traverses nodes from root to tips and randomly assigns a state to each node or tip based on its parent's previously assigned state and the specified transition rates between states. The generated states have joint distributions consistent with the Markov model. Optionally, multiple independent simulations can be performed using the same model.
simulate_mk_model(tree, Q, root_probabilities="stationary",
include_tips=TRUE, include_nodes=TRUE,
Nsimulations=1, drop_dims=TRUE)
tree |
A rooted tree of class "phylo". The root is assumed to be the unique node with no incoming edge. |
Q |
A numeric matrix of size Nstates x Nstates, storing the transition rates between states. In particular, every row must sum up to zero. |
root_probabilities |
Probabilities of the different states at the root.
Either a character vector with value "stationary" or "flat", or a numeric vector of length Nstates, where Nstates is the number of possible states of the trait. In the later case, |
include_tips |
Include random states for the tips. If |
include_nodes |
Include random states for the nodes. If |
Nsimulations |
Number of random independent simulations to perform. For each node and/or tip, there will be |
drop_dims |
Logical, specifying whether the returned |
For this function, the trait's states must be represented by integers within 1,..,Nstates, where Nstates is the total number of possible states. If the states are originally in some other format (e.g. characters or factors), you should map them to a set of integers 1,..,Nstates. These integers should correspond to row & column indices in the transition matrix Q
. You can easily map any set of discrete states to integers using the function map_to_state_space
.
If tree$edge.length
is missing, each edge in the tree is assumed to have length 1. The tree may include multi-furcations (i.e. nodes with more than 2 children) as well as mono-furcations (i.e. nodes with only one child). The time required per simulation decreases with the total number of requested simulations.
A list with the following elements:
tip_states |
Either |
node_states |
Either |
Stilianos Louca
exponentiate_matrix
, get_stationary_distribution
,
simulate_bm_model
, simulate_ou_model
, simulate_rou_model
## Not run:
# generate a random tree
tree = generate_random_tree(list(birth_rate_intercept=1),max_tips=1000)$tree
# simulate discrete trait evolution on the tree (5 states)
Nstates = 5
Q = get_random_mk_transition_matrix(Nstates, rate_model="ARD", max_rate=0.1)
tip_states = simulate_mk_model(tree, Q)$tip_states
# plot histogram of simulated tip states
barplot(table(tip_states)/length(tip_states), xlab="state")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.