]> git.donarmstrong.com Git - ape.git/blobdiff - R/ace.R
some big fixes for ape 2.7-1
[ape.git] / R / ace.R
diff --git a/R/ace.R b/R/ace.R
index c38faeb6bdb2ef2b8b489139a841b3b5a440adaf..b67549f9dccf4cd55b3d68b0fe5bd7ffb3f9ef8b 100644 (file)
--- a/R/ace.R
+++ b/R/ace.R
@@ -1,8 +1,8 @@
-## ace.R (2009-01-19)
+## ace.R (2010-12-08)
 
-##     Ancestral Character Estimation
+##   Ancestral Character Estimation
 
-## Copyright 2005-2009 Emmanuel Paradis and Ben Bolker
+## Copyright 2005-2010 Emmanuel Paradis and Ben Bolker
 
 ## This file is part of the R-package `ape'.
 ## See the file ../COPYING for licensing issues.
@@ -11,8 +11,8 @@ ace <- function(x, phy, type = "continuous", method = "ML", CI = TRUE,
                 model = if (type == "continuous") "BM" else "ER",
                 scaled = TRUE, kappa = 1, corStruct = NULL, ip = 0.1)
 {
-    if (class(phy) != "phylo")
-      stop('object "phy" is not of class "phylo".')
+    if (!inherits(phy, "phylo"))
+        stop('object "phy" is not of class "phylo".')
     if (is.null(phy$edge.length))
         stop("tree has no branch lengths")
     type <- match.arg(type, c("continuous", "discrete"))
@@ -25,8 +25,7 @@ ace <- function(x, phy, type = "continuous", method = "ML", CI = TRUE,
     if (!is.null(names(x))) {
         if(all(names(x) %in% phy$tip.label))
           x <- x[phy$tip.label]
-        else warning('the names of argument "x" and the tip labels of the tree
-did not match: the former were ignored in the analysis.')
+        else warning("the names of 'x' and the tip labels of the tree do not match: the former were ignored in the analysis.")
     }
     obj <- list()
     if (kappa != 1) phy$edge.length <- phy$edge.length^kappa
@@ -59,6 +58,7 @@ did not match: the former were ignored in the analysis.')
             if (model == "BM") {
                 tip <- phy$edge[, 2] <= nb.tip
                 dev.BM <- function(p) {
+                    if (p[1] < 0) return(1e100) # in case sigma² is negative
                     x1 <- p[-1][phy$edge[, 1] - nb.tip]
                     x2 <- numeric(length(x1))
                     x2[tip] <- x[phy$edge[tip, 2]]
@@ -130,9 +130,10 @@ did not match: the former were ignored in the analysis.')
             }
             if (model == "SYM") {
                 np <- nl * (nl - 1)/2
-                rate[col(rate) < row(rate)] <- 1:np
+                sel <- col(rate) < row(rate)
+                rate[sel] <- 1:np
                 rate <- t(rate)
-                rate[col(rate) < row(rate)] <- 1:np
+                rate[sel] <- 1:np
             }
         } else {
             if (ncol(model) != nrow(model))
@@ -144,45 +145,54 @@ as the number of categories in `x'")
             np <- max(rate)
         }
         index.matrix <- rate
-        index.matrix[cbind(1:nl, 1:nl)] <- NA
-        rate[cbind(1:nl, 1:nl)] <- 0
-        rate[rate == 0] <- np + 1 # to avoid 0's since we will use this an numeric indexing
+        tmp <- cbind(1:nl, 1:nl)
+        index.matrix[tmp] <- NA
+        rate[tmp] <- 0
+        rate[rate == 0] <- np + 1 # to avoid 0's since we will use this as numeric indexing
 
         liks <- matrix(0, nb.tip + nb.node, nl)
-        for (i in 1:nb.tip) liks[i, x[i]] <- 1
+        TIPS <- 1:nb.tip
+        liks[cbind(TIPS, x)] <- 1
         phy <- reorder(phy, "pruningwise")
 
         Q <- matrix(0, nl, nl)
         dev <- function(p, output.liks = FALSE) {
+            if (any(is.nan(p)) || any(is.infinite(p))) return(1e50)
+            ## from Rich FitzJohn:
+            comp <- numeric(nb.tip + nb.node) # Storage...
             Q[] <- c(p, 0)[rate]
             diag(Q) <- -rowSums(Q)
             for (i  in seq(from = 1, by = 2, length.out = nb.node)) {
-                j <- i + 1
+                j <- i + 1L
                 anc <- phy$edge[i, 1]
                 des1 <- phy$edge[i, 2]
                 des2 <- phy$edge[j, 2]
-                tmp <- eigen(Q * phy$edge.length[i], symmetric = FALSE)
-                P1 <- tmp$vectors %*% diag(exp(tmp$values)) %*% solve(tmp$vectors)
-                tmp <- eigen(Q * phy$edge.length[j], symmetric = FALSE)
-                P2 <- tmp$vectors %*% diag(exp(tmp$values)) %*% solve(tmp$vectors)
-                liks[anc, ] <- P1 %*% liks[des1, ] * P2 %*% liks[des2, ]
+                v.l <- matexpo(Q * phy$edge.length[i]) %*% liks[des1, ]
+                v.r <- matexpo(Q * phy$edge.length[j]) %*% liks[des2, ]
+                v <- v.l * v.r
+                comp[anc] <- sum(v)
+                liks[anc, ] <- v/comp[anc]
             }
-            if (output.liks) return(liks[-(1:nb.tip), ])
-            - 2 * log(sum(liks[nb.tip + 1, ]))
+            if (output.liks) return(liks[-TIPS, ])
+            dev <- -2 * sum(log(comp[-TIPS]))
+            if (is.na(dev)) Inf else dev
         }
-        out <- nlm(function(p) dev(p), p = rep(ip, length.out = np),
-                   hessian = TRUE)
-        obj$loglik <- -out$minimum / 2
-        obj$rates <- out$estimate
-        if (any(out$gradient == 0))
+        out <- nlminb(rep(ip, length.out = np), function(p) dev(p),
+                      lower = rep(0, np), upper = rep(1e50, np))
+        obj$loglik <- -out$objective/2
+        obj$rates <- out$par
+        oldwarn <- options("warn")
+        options(warn = -1)
+        h <- nlm(function(p) dev(p), p = obj$rates, iterlim = 1,
+                 stepmax = 0, hessian = TRUE)$hessian
+        options(oldwarn)
+        if (any(h == 0))
           warning("The likelihood gradient seems flat in at least one dimension (gradient null):\ncannot compute the standard-errors of the transition rates.\n")
-        else obj$se <- sqrt(diag(solve(out$hessian)))
+        else obj$se <- sqrt(diag(solve(h)))
         obj$index.matrix <- index.matrix
         if (CI) {
-            lik.anc <- dev(obj$rates, TRUE)
-            lik.anc <- lik.anc / rowSums(lik.anc)
-            colnames(lik.anc) <- lvls
-            obj$lik.anc <- lik.anc
+            obj$lik.anc <- dev(obj$rates, TRUE)
+            colnames(obj$lik.anc) <- lvls
         }
     }
     obj$call <- match.call()
@@ -215,8 +225,36 @@ anova.ace <- function(object, ...)
     table <- data.frame(ll, df, ddf, dev,
                         pchisq(dev, ddf, lower.tail = FALSE))
     dimnames(table) <- list(1:length(X), c("Log lik.", "Df",
-                                           "Df change", "Deviance",
+                                           "Df change", "Resid. Dev",
                                            "Pr(>|Chi|)"))
     structure(table, heading = "Likelihood Ratio Test Table",
               class = c("anova", "data.frame"))
 }
+
+print.ace <- function(x, digits = 4, ...)
+{
+    cat("\n    Ancestral Character Estimation\n\n")
+    cat("Call: ")
+    print(x$call)
+    cat("\n")
+    if (!is.null(x$loglik))
+        cat("    Log-likelihood:", x$loglik, "\n\n")
+    ratemat <- x$index.matrix
+    if (is.null(ratemat)) { # to be improved
+        class(x) <- NULL
+        x$loglik <- x$call <- NULL
+        print(x)
+    } else {
+        dimnames(ratemat)[1:2] <- dimnames(x$lik.anc)[2]
+        cat("Rate index matrix:\n")
+        print(ratemat, na.print = ".")
+        cat("\n")
+        npar <- length(x$rates)
+        estim <- data.frame(1:npar, round(x$rates, digits), round(x$se, digits))
+        cat("Parameter estimates:\n")
+        names(estim) <- c("rate index", "estimate", "std-err")
+        print(estim, row.names = FALSE)
+        cat("\nScaled likelihoods at the root (type '...$lik.anc' to get them for all nodes):\n")
+        print(x$lik.anc[1, ])
+    }
+}