3 ## Ancestral Character Estimation
5 ## Copyright 2005-2013 Emmanuel Paradis and Ben Bolker
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 .getSEs <- function(out)
13 if (any(diag(h) == 0)) {
14 warning("The likelihood gradient seems flat in at least one dimension (gradient null):\ncannot compute the standard-errors of the transition rates.\n")
15 se <- rep(NaN, nrow(h))
17 se <- sqrt(diag(solve(h)))
23 function(x, phy, type = "continuous",
24 method = if (type == "continuous") "REML" else "ML",
25 CI = TRUE, model = if (type == "continuous") "BM" else "ER",
26 scaled = TRUE, kappa = 1, corStruct = NULL, ip = 0.1,
29 if (!inherits(phy, "phylo"))
30 stop('object "phy" is not of class "phylo"')
31 if (is.null(phy$edge.length))
32 stop("tree has no branch lengths")
33 type <- match.arg(type, c("continuous", "discrete"))
34 nb.tip <- length(phy$tip.label)
36 if (nb.node != nb.tip - 1)
37 stop('"phy" is not rooted AND fully dichotomous.')
38 if (length(x) != nb.tip)
39 stop("length of phenotypic and of phylogenetic data do not match.")
40 if (!is.null(names(x))) {
41 if(all(names(x) %in% phy$tip.label))
43 else warning("the names of 'x' and the tip labels of the tree do not match: the former were ignored in the analysis.")
46 if (kappa != 1) phy$edge.length <- phy$edge.length^kappa
47 if (type == "continuous") {
48 switch(method, "REML" = {
49 minusLogLik <- function(sig2) {
50 if (sig2 < 0) return(1e100)
52 ## next three lines borrowed from dmvnorm() in 'mvtnorm'
53 distval <- stats::mahalanobis(x, center = mu, cov = V)
54 logdet <- sum(log(eigen(V, symmetric = TRUE, only.values = TRUE)$values))
55 (nb.tip * log(2 * pi) + logdet + distval)/2
57 mu <- rep(ace(x, phy, method="pic")$ace[1], nb.tip)
58 out <- nlm(minusLogLik, 1, hessian = TRUE)
59 sigma2 <- out$estimate
60 se_sgi2 <- sqrt(1/out$hessian)
61 tip <- phy$edge[, 2] <= nb.tip
62 minus.REML.BM <- function(p) {
63 x1 <- p[phy$edge[, 1] - nb.tip]
64 x2 <- numeric(length(x1))
65 x2[tip] <- x[phy$edge[tip, 2]]
66 x2[!tip] <- p[phy$edge[!tip, 2] - nb.tip]
67 -(-sum((x1 - x2)^2/phy$edge.length)/(2 * sigma2) -
68 nb.node * log(sigma2))
70 out <- nlm(function(p) minus.REML.BM(p),
71 p = rep(mu[1], nb.node), hessian = TRUE)
72 obj$resloglik <- -out$minimum
73 obj$ace <- out$estimate
74 names(obj$ace) <- nb.tip + 1:nb.node
75 obj$sigma2 <- c(sigma2, se_sgi2)
78 tmp <- se * qt(0.025, nb.node)
79 obj$CI95 <- cbind(obj$ace + tmp, obj$ace - tmp)
83 stop('the "pic" method can be used only with model = "BM".')
84 ## See pic.R for some annotations.
85 phy <- reorder(phy, "pruningwise")
86 phenotype <- numeric(nb.tip + nb.node)
87 phenotype[1:nb.tip] <- if (is.null(names(x))) x else x[phy$tip.label]
88 contr <- var.con <- numeric(nb.node)
89 ans <- .C("pic", as.integer(nb.tip), as.integer(nb.node),
90 as.integer(phy$edge[, 1]), as.integer(phy$edge[, 2]),
91 as.double(phy$edge.length), as.double(phenotype),
92 as.double(contr), as.double(var.con),
93 as.integer(CI), as.integer(scaled),
95 obj$ace <- ans[[6]][-(1:nb.tip)]
96 names(obj$ace) <- nb.tip + 1:nb.node
99 tmp <- se * qnorm(0.025)
100 obj$CI95 <- cbind(obj$ace + tmp, obj$ace - tmp)
104 tip <- phy$edge[, 2] <= nb.tip
105 dev.BM <- function(p) {
106 if (p[1] < 0) return(1e100) # in case sigma² is negative
107 x1 <- p[-1][phy$edge[, 1] - nb.tip]
108 x2 <- numeric(length(x1))
109 x2[tip] <- x[phy$edge[tip, 2]]
110 x2[!tip] <- p[-1][phy$edge[!tip, 2] - nb.tip]
111 -2 * (-sum((x1 - x2)^2/phy$edge.length)/(2*p[1]) -
114 out <- nlm(function(p) dev.BM(p),
115 p = c(1, rep(mean(x), nb.node)), hessian = TRUE)
116 obj$loglik <- -out$minimum / 2
117 obj$ace <- out$estimate[-1]
118 names(obj$ace) <- (nb.tip + 1):(nb.tip + nb.node)
120 obj$sigma2 <- c(out$estimate[1], se[1])
122 tmp <- se[-1] * qt(0.025, nb.node)
123 obj$CI95 <- cbind(obj$ace + tmp, obj$ace - tmp)
127 if (is.null(corStruct))
128 stop('you must give a correlation structure if method = "GLS".')
129 if (class(corStruct)[1] == "corMartins")
130 M <- corStruct[1] * dist.nodes(phy)
131 if (class(corStruct)[1] == "corGrafen")
132 phy <- compute.brlen(attr(corStruct, "tree"),
134 power = exp(corStruct[1]))
135 if (class(corStruct)[1] %in% c("corBrownian", "corGrafen")) {
136 dis <- dist.nodes(attr(corStruct, "tree"))
137 MRCA <- mrca(attr(corStruct, "tree"), full = TRUE)
138 M <- dis[as.character(nb.tip + 1), MRCA]
139 dim(M) <- rep(sqrt(length(M)), 2)
141 varAY <- M[-(1:nb.tip), 1:nb.tip]
142 varA <- M[-(1:nb.tip), -(1:nb.tip)]
143 V <- corMatrix(Initialize(corStruct, data.frame(x)),
146 o <- gls(x ~ 1, data.frame(x), correlation = corStruct)
148 obj$ace <- drop(varAY %*% invV %*% (x - GM) + GM)
149 names(obj$ace) <- (nb.tip + 1):(nb.tip + nb.node)
151 se <- sqrt((varA - varAY %*% invV %*% t(varAY))[cbind(1:nb.node, 1:nb.node)])
152 tmp <- se * qnorm(0.025)
153 obj$CI95 <- cbind(obj$ace + tmp, obj$ace - tmp)
156 } else { # type == "discrete"
158 stop("only ML estimation is possible for discrete characters.")
159 if (!is.factor(x)) x <- factor(x)
163 if (is.character(model)) {
164 rate <- matrix(NA, nl, nl)
165 if (model == "ER") np <- rate[] <- 1
166 if (model == "ARD") {
168 rate[col(rate) != row(rate)] <- 1:np
170 if (model == "SYM") {
171 np <- nl * (nl - 1)/2
172 sel <- col(rate) < row(rate)
178 if (ncol(model) != nrow(model))
179 stop("the matrix given as 'model' is not square")
180 if (ncol(model) != nl)
181 stop("the matrix 'model' must have as many rows as the number of categories in 'x'")
186 tmp <- cbind(1:nl, 1:nl)
187 index.matrix[tmp] <- NA
189 rate[rate == 0] <- np + 1 # to avoid 0's since we will use this as numeric indexing
191 liks <- matrix(0, nb.tip + nb.node, nl)
193 liks[cbind(TIPS, x)] <- 1
194 phy <- reorder(phy, "pruningwise")
196 ## E <- if (use.expm) expm::expm else ape::matexpo
199 get("expm", "package:expm")
202 Q <- matrix(0, nl, nl)
203 dev <- function(p, output.liks = FALSE) {
204 if (any(is.nan(p)) || any(is.infinite(p))) return(1e50)
205 ## from Rich FitzJohn:
206 comp <- numeric(nb.tip + nb.node) # Storage...
208 diag(Q) <- -rowSums(Q)
209 for (i in seq(from = 1, by = 2, length.out = nb.node)) {
211 anc <- phy$edge[i, 1]
212 des1 <- phy$edge[i, 2]
213 des2 <- phy$edge[j, 2]
214 v.l <- E(Q * phy$edge.length[i]) %*% liks[des1, ]
215 v.r <- E(Q * phy$edge.length[j]) %*% liks[des2, ]
218 liks[anc, ] <- v/comp[anc]
220 if (output.liks) return(liks[-TIPS, ])
221 dev <- -2 * sum(log(comp[-TIPS]))
222 if (is.na(dev)) Inf else dev
224 out <- nlminb(rep(ip, length.out = np), function(p) dev(p),
225 lower = rep(0, np), upper = rep(1e50, np))
226 obj$loglik <- -out$objective/2
228 oldwarn <- options("warn")
230 out.nlm <- nlm(function(p) dev(p), p = obj$rates, iterlim = 1,
231 stepmax = 0, hessian = TRUE)
233 obj$se <- .getSEs(out.nlm)
234 obj$index.matrix <- index.matrix
236 obj$lik.anc <- dev(obj$rates, TRUE)
237 colnames(obj$lik.anc) <- lvls
240 obj$call <- match.call()
245 logLik.ace <- function(object, ...) object$loglik
247 deviance.ace <- function(object, ...) -2*object$loglik
249 AIC.ace <- function(object, ..., k = 2)
251 if (is.null(object$loglik)) return(NULL)
252 ## Trivial test of "type"; may need to be improved
253 ## if other models are included in ace(type = "c")
254 np <- if (!is.null(object$sigma2)) 1 else length(object$rates)
255 -2*object$loglik + np*k
259 anova.ace <- function(object, ...)
261 X <- c(list(object), list(...))
262 df <- sapply(lapply(X, "[[", "rates"), length)
263 ll <- sapply(X, "[[", "loglik")
264 ## check if models are in correct order?
265 dev <- c(NA, 2*diff(ll))
266 ddf <- c(NA, diff(df))
267 table <- data.frame(ll, df, ddf, dev,
268 pchisq(dev, ddf, lower.tail = FALSE))
269 dimnames(table) <- list(1:length(X), c("Log lik.", "Df",
270 "Df change", "Resid. Dev",
272 structure(table, heading = "Likelihood Ratio Test Table",
273 class = c("anova", "data.frame"))
276 print.ace <- function(x, digits = 4, ...)
278 cat("\n Ancestral Character Estimation\n\n")
282 if (!is.null(x$loglik))
283 cat(" Log-likelihood:", x$loglik, "\n\n")
284 if (!is.null(x$resloglik))
285 cat(" Residual log-likelihood:", x$resloglik, "\n\n")
286 ratemat <- x$index.matrix
287 if (is.null(ratemat)) { # to be improved
289 x$resloglik <- x$loglik <- x$call <- NULL
292 dimnames(ratemat)[1:2] <- dimnames(x$lik.anc)[2]
293 cat("Rate index matrix:\n")
294 print(ratemat, na.print = ".")
296 npar <- length(x$rates)
297 estim <- data.frame(1:npar, round(x$rates, digits), round(x$se, digits))
298 cat("Parameter estimates:\n")
299 names(estim) <- c("rate index", "estimate", "std-err")
300 print(estim, row.names = FALSE)
301 cat("\nScaled likelihoods at the root (type '...$lik.anc' to get them for all nodes):\n")
302 print(x$lik.anc[1, ])