Created
August 18, 2016 15:16
-
-
Save sdwfrost/45c1d7f3a97b5cd2bca49b082da68064 to your computer and use it in GitHub Desktop.
MKn model in diversitree with deSolve and custom initial conditions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "mkn test" | |
author: "Simon D.W. Frost" | |
date: "18 August 2016" | |
output: html_document | |
--- | |
```{r} | |
library(ape) | |
library(diversitree) | |
``` | |
```{r} | |
set.seed(4) | |
tr <- rtree(2,br=1) | |
``` | |
```{r} | |
k <- 2 | |
states <- c(2,1) | |
names(states) <- tr$tip.label | |
``` | |
```{r} | |
derivs <- function(t, y, pars) { | |
k <- length(y) | |
Q <- matrix(pars, k, k) | |
ret <- Q %*% y | |
dim(ret) <- NULL | |
list(ret) | |
} | |
control <- diversitree:::check.control.mkn(list(),k) | |
control$method <- "ode" | |
control$backend <- "deSolve" | |
cache <- diversitree:::make.cache.mkn(tr,states,2,FALSE,control) | |
cache$info$derivs <- derivs | |
initial.conditions <- function (init, pars, t, idx) | |
{ | |
init[, 1] * init[, 2] | |
} | |
all.branches <- diversitree:::make.all.branches.dtlik(cache, control, initial.conditions) | |
rootfunc <- diversitree:::rootfunc.mkn | |
f.pars <- diversitree:::make.pars.mkn(k) | |
ll <- function(pars, root = ROOT.OBS, root.p = NULL, intermediates = FALSE) { | |
qmat <- f.pars(pars) | |
ans <- all.branches(qmat, intermediates) | |
rootfunc(ans, qmat, root, root.p, intermediates) | |
} | |
class(ll) <- c("mkn", "dtlik", "function") | |
ll(c(1,0),root=ROOT.GIVEN,root.p=c(1,0),intermediates=TRUE) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment