1 ## compar.ou.R (2006-10-05)
3 ## Ornstein--Uhlenbeck Model for Continuous Characters
5 ## Copyright 2005-2006 Emmanuel Paradis
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 compar.ou <- function(x, phy, node = NULL, alpha = NULL)
12 if (class(phy) != "phylo")
13 stop('object "phy" is not of class "phylo".')
14 if (!is.numeric(x)) stop("'x' must be numeric.")
15 if (!is.null(names(x))) {
16 if (all(names(x) %in% phy$tip.label)) x <- x[phy$tip.label]
17 else warning('the names of argument "x" and the tip labels of the tree did not match: the former were ignored in the analysis.')
19 nb.tip <- length(phy$tip.label)
21 if (is.null(node)) node <- numeric(0)
22 if (root %in% node) node <- node[node != root]
23 bt <- branching.times(phy)
25 Wend <- matrix(0, nb.tip, length(node) + 1)
26 colnames(Wend) <- c(names(sort(bt[node])), as.character(root))
28 Wstart[, ncol(Wstart)] <- Tmax
29 root2tip <- .Call("seq_root2tip", phy$edge, nb.tip,
30 phy$Nnode, PACKAGE = "ape")
32 last.change <- names(Tmax)
33 for (j in root2tip[[i]]) {#[-1]) {# don't need to look at the root
36 Wend[i, last.change] <- Wstart[i, jb] <- bt[jb]
41 W <- cophenetic.phylo(phy)
43 M <- rowSums(exp(-p[1] * Wstart) - exp(-p[1] * Wend) * p[-(1:2)])
44 V <- exp(-p[1]*W) * (1 - exp(-2*p[1]*(Tmax - W/2)))
45 nb.tip*log(2*pi*p[2]) + log(det(V)) +
46 (t(x - M) %*% chol2inv(V) %*% (x - M)) / p[2]
49 out <- nlm(function(p) dev(p),
50 p = c(0.1, 1, rep(mean(x), ncol(Wstart))),
53 out <- nlm(function(p) dev(c(alpha, p)),
54 p = c(1, rep(mean(x), ncol(Wstart))),
56 para <- cbind(out$estimate, sqrt(diag(solve(out$hessian))))
57 nms <- c("sigma2", paste("theta", 1:ncol(Wstart), sep = ""))
58 if (is.null(alpha)) nms <- c("alpha", nms)
59 dimnames(para) <- list(nms, c("estimate", "stderr"))
60 obj <- list(deviance = out$minimum, para = para, call = match.call())
61 class(obj) <- "compar.ou"