]> git.donarmstrong.com Git - ape.git/blobdiff - R/ace.R
some updates (including new CADM.global.Rd file)
[ape.git] / R / ace.R
diff --git a/R/ace.R b/R/ace.R
index f728ee7c6045f9478e21fa0edd1e12fafa5aa4fa..b67549f9dccf4cd55b3d68b0fe5bd7ffb3f9ef8b 100644 (file)
--- a/R/ace.R
+++ b/R/ace.R
@@ -1,4 +1,4 @@
-## ace.R (2010-02-03)
+## ace.R (2010-12-08)
 
 ##   Ancestral Character Estimation
 
@@ -12,7 +12,7 @@ ace <- function(x, phy, type = "continuous", method = "ML", CI = TRUE,
                 scaled = TRUE, kappa = 1, corStruct = NULL, ip = 0.1)
 {
     if (!inherits(phy, "phylo"))
-      stop('object "phy" is not of class "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
@@ -157,13 +156,14 @@ as the number of categories in `x'")
         phy <- reorder(phy, "pruningwise")
 
         Q <- matrix(0, nl, nl)
-        ## from Rich FitzJohn:
-        comp <- numeric(nb.tip + nb.node) # Storage...
         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]
@@ -174,10 +174,11 @@ as the number of categories in `x'")
                 liks[anc, ] <- v/comp[anc]
             }
             if (output.liks) return(liks[-TIPS, ])
-            -2 * sum(log(comp[-TIPS]))
+            dev <- -2 * sum(log(comp[-TIPS]))
+            if (is.na(dev)) Inf else dev
         }
         out <- nlminb(rep(ip, length.out = np), function(p) dev(p),
-                      lower = rep(0, np), upper = rep(Inf, np))
+                      lower = rep(0, np), upper = rep(1e50, np))
         obj$loglik <- -out$objective/2
         obj$rates <- out$par
         oldwarn <- options("warn")
@@ -229,3 +230,31 @@ anova.ace <- function(object, ...)
     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, ])
+    }
+}