]> git.donarmstrong.com Git - ape.git/blob - R/ace.R
current 2.1 release
[ape.git] / R / ace.R
1 ## ace.R (2007-12-14)
2
3 ##     Ancestral Character Estimation
4
5 ## Copyright 2005-2007 Emmanuel Paradis and Ben Bolker
6
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
9
10 ace <- function(x, phy, type = "continuous", method = "ML", CI = TRUE,
11                 model = if (type == "continuous") "BM" else "ER",
12                 scaled = TRUE, kappa = 1, corStruct = NULL, ip = 0.1)
13 {
14     if (class(phy) != "phylo")
15       stop('object "phy" is not of class "phylo".')
16     if (is.null(phy$edge.length))
17         stop("tree has no branch lengths")
18     type <- match.arg(type, c("continuous", "discrete"))
19     nb.tip <- length(phy$tip.label)
20     nb.node <- phy$Nnode
21     if (nb.node != nb.tip - 1)
22       stop('"phy" is not rooted AND fully dichotomous.')
23     if (length(x) != nb.tip)
24       stop("length of phenotypic and of phylogenetic data do not match.")
25     if (!is.null(names(x))) {
26         if(all(names(x) %in% phy$tip.label))
27           x <- x[phy$tip.label]
28         else warning('the names of argument "x" and the tip labels of the tree
29 did not match: the former were ignored in the analysis.')
30     }
31     obj <- list()
32     if (kappa != 1) phy$edge.length <- phy$edge.length^kappa
33     if (type == "continuous") {
34         if (method == "pic") {
35             if (model != "BM")
36               stop('the "pic" method can be used only with model = "BM".')
37             ## See pic.R for some annotations.
38             phy <- reorder(phy, "pruningwise")
39             phenotype <- numeric(nb.tip + nb.node)
40             phenotype[1:nb.tip] <- if (is.null(names(x))) x else x[phy$tip.label]
41             contr <- var.con <- numeric(nb.node)
42             ans <- .C("pic", as.integer(nb.tip), as.integer(nb.node),
43                       as.integer(phy$edge[, 1]), as.integer(phy$edge[, 2]),
44                       as.double(phy$edge.length), as.double(phenotype),
45                       as.double(contr), as.double(var.con),
46                       as.integer(CI), as.integer(scaled),
47                       PACKAGE = "ape")
48             obj$ace <- ans[[6]][-(1:nb.tip)]
49             names(obj$ace) <- (nb.tip + 1):(nb.tip + nb.node)
50             if (CI) {
51                 se <- sqrt(ans[[8]])
52                 CI95 <- matrix(NA, nb.node, 2)
53                 CI95[, 1] <- obj$ace + se * qnorm(0.025)
54                 CI95[, 2] <- obj$ace - se * qnorm(0.025)
55                 obj$CI95 <- CI95
56             }
57         }
58         if (method == "ML") {
59             if (model == "BM") {
60                 tip <- phy$edge[, 2] <= nb.tip
61                 dev.BM <- function(p) {
62                     x1 <- p[-1][phy$edge[, 1] - nb.tip]
63                     x2 <- numeric(length(x1))
64                     x2[tip] <- x[phy$edge[tip, 2]]
65                     x2[!tip] <- p[-1][phy$edge[!tip, 2] - nb.tip]
66                     -2 * (-sum((x1 - x2)^2/phy$edge.length)/(2*p[1]) -
67                           nb.node * log(p[1]))
68                 }
69                 out <- nlm(function(p) dev.BM(p),
70                            p = c(1, rep(mean(x), nb.node)), hessian = TRUE)
71                 obj$loglik <- -out$minimum / 2
72                 obj$ace <- out$estimate[-1]
73                 names(obj$ace) <- (nb.tip + 1):(nb.tip + nb.node)
74                 se <- sqrt(diag(solve(out$hessian)))
75                 obj$sigma2 <- c(out$estimate[1], se[1])
76                 se <- se[-1]
77                 if (CI) {
78                     CI95 <- matrix(NA, nb.node, 2)
79                     CI95[, 1] <- obj$ace + se * qt(0.025, nb.node)
80                     CI95[, 2] <- obj$ace - se * qt(0.025, nb.node)
81                     obj$CI95 <- CI95
82                 }
83             }
84         }
85         if (method == "GLS") {
86             if (is.null(corStruct))
87               stop('you must give a correlation structure if method = "GLS".')
88             if (class(corStruct)[1] == "corMartins")
89               M <- corStruct[1] * dist.nodes(phy)
90             if (class(corStruct)[1] == "corGrafen")
91               phy <- compute.brlen(attr(corStruct, "tree"),
92                                    method = "Grafen",
93                                    power = exp(corStruct[1]))
94             if (class(corStruct)[1] %in% c("corBrownian", "corGrafen")) {
95                 dis <- dist.nodes(attr(corStruct, "tree"))
96                 MRCA <- mrca(attr(corStruct, "tree"), full = TRUE)
97                 M <- dis[as.character(nb.tip + 1), MRCA]
98                 dim(M) <- rep(sqrt(length(M)), 2)
99             }
100             varAY <- M[-(1:nb.tip), 1:nb.tip]
101             varA <- M[-(1:nb.tip), -(1:nb.tip)]
102             V <- corMatrix(Initialize(corStruct, data.frame(x)),
103                            corr = FALSE)
104             invV <- solve(V)
105             obj$ace <- varAY %*% invV %*% x
106             if (CI) {
107                 CI95 <- matrix(NA, nb.node, 2)
108                 se <- sqrt((varA - varAY %*% invV %*% t(varAY))[cbind(1:nb.node, 1:nb.node)])
109                 CI95[, 1] <- obj$ace + se * qnorm(0.025)
110                 CI95[, 2] <- obj$ace - se * qnorm(0.025)
111                 obj$CI95 <- CI95
112             }
113         }
114     } else { # type == "discrete"
115         if (method != "ML")
116           stop("only ML estimation is possible for discrete characters.")
117         if (!is.factor(x)) x <- factor(x)
118         nl <- nlevels(x)
119         lvls <- levels(x)
120         x <- as.integer(x)
121         if (is.character(model)) {
122             rate <- matrix(NA, nl, nl)
123             if (model == "ER") np <- rate[] <- 1
124             if (model == "ARD") {
125                 np <- nl*(nl - 1)
126                 rate[col(rate) != row(rate)] <- 1:np
127             }
128             if (model == "SYM") {
129                 np <- nl * (nl - 1)/2
130                 rate[col(rate) < row(rate)] <- 1:np
131                 rate <- t(rate)
132                 rate[col(rate) < row(rate)] <- 1:np
133             }
134         } else {
135             if (ncol(model) != nrow(model))
136               stop("the matrix given as `model' is not square")
137             if (ncol(model) != nl)
138               stop("the matrix `model' must have as many rows
139 as the number of categories in `x'")
140             rate <- model
141             np <- max(rate)
142         }
143         index.matrix <- rate
144         index.matrix[cbind(1:nl, 1:nl)] <- NA
145         rate[cbind(1:nl, 1:nl)] <- 0
146         rate[rate == 0] <- np + 1 # to avoid 0's since we will use this an numeric indexing
147
148         liks <- matrix(0, nb.tip + nb.node, nl)
149         for (i in 1:nb.tip) liks[i, x[i]] <- 1
150         phy <- reorder(phy, "pruningwise")
151
152         Q <- matrix(0, nl, nl)
153         dev <- function(p, output.liks = FALSE) {
154             Q[] <- c(p, 0)[rate]
155             diag(Q) <- -rowSums(Q)
156             for (i  in seq(from = 1, by = 2, length.out = nb.node)) {
157                 j <- i + 1
158                 anc <- phy$edge[i, 1]
159                 des1 <- phy$edge[i, 2]
160                 des2 <- phy$edge[j, 2]
161                 tmp <- eigen(Q * phy$edge.length[i], symmetric = FALSE)
162                 P1 <- tmp$vectors %*% diag(exp(tmp$values)) %*% solve(tmp$vectors)
163                 tmp <- eigen(Q * phy$edge.length[j], symmetric = FALSE)
164                 P2 <- tmp$vectors %*% diag(exp(tmp$values)) %*% solve(tmp$vectors)
165                 liks[anc, ] <- P1 %*% liks[des1, ] * P2 %*% liks[des2, ]
166             }
167             if (output.liks) return(liks[-(1:nb.tip), ])
168             - 2 * log(sum(liks[nb.tip + 1, ]))
169         }
170         out <- nlm(function(p) dev(p), p = rep(ip, length.out = np),
171                    hessian = TRUE)
172         obj$loglik <- -out$minimum / 2
173         obj$rates <- out$estimate
174         if (any(out$gradient == 0))
175           warning("The likelihood gradient seems flat in at least one dimension (gradient null):\ncannot compute the standard-errors of the transition rates.\n")
176         else obj$se <- sqrt(diag(solve(out$hessian)))
177         obj$index.matrix <- index.matrix
178         if (CI) {
179             lik.anc <- dev(obj$rates, TRUE)
180             lik.anc <- lik.anc / rowSums(lik.anc)
181             colnames(lik.anc) <- lvls
182             obj$lik.anc <- lik.anc
183         }
184     }
185     obj$call <- match.call()
186     class(obj) <- "ace"
187     obj
188 }
189
190 logLik.ace <- function(object, ...) object$loglik
191
192 deviance.ace <- function(object, ...) -2*object$loglik
193
194 AIC.ace <- function(object, ..., k = 2)
195 {
196     if (is.null(object$loglik)) return(NULL)
197     ## Trivial test of "type"; may need to be improved
198     ## if other models are included in ace(type = "c")
199     np <- if (!is.null(object$sigma2)) 1 else length(object$rates)
200     -2*object$loglik + np*k
201 }
202
203 ### by BB:
204 anova.ace <- function(object, ...)
205 {
206     X <- c(list(object), list(...))
207     df <- sapply(lapply(X, "[[", "rates"), length)
208     ll <- sapply(X, "[[", "loglik")
209     ## check if models are in correct order?
210     dev <- c(NA, 2*diff(ll))
211     ddf <- c(NA, diff(df))
212     table <- data.frame(ll, df, ddf, dev,
213                         pchisq(dev, ddf, lower.tail = FALSE))
214     dimnames(table) <- list(1:length(X), c("Log lik.", "Df",
215                                            "Df change", "Deviance",
216                                            "Pr(>|Chi|)"))
217     structure(table, heading = "Likelihood Ratio Test Table",
218               class = c("anova", "data.frame"))
219 }