]> git.donarmstrong.com Git - ape.git/blobdiff - R/ace.R
change nlm to nlminb in ace() + new makeNodeLabel + fixed drop.tip
[ape.git] / R / ace.R
diff --git a/R/ace.R b/R/ace.R
index 3e5d16ff5e036614d7a3cf4321f70f9306e17351..0439600832a0f1018af4b75974ea11703f2a6e02 100644 (file)
--- a/R/ace.R
+++ b/R/ace.R
@@ -1,8 +1,8 @@
-## ace.R (2007-12-14)
+## ace.R (2009-03-22)
 
 ##     Ancestral Character Estimation
 
-## Copyright 2005-2007 Emmanuel Paradis and Ben Bolker
+## Copyright 2005-2009 Emmanuel Paradis and Ben Bolker
 
 ## This file is part of the R-package `ape'.
 ## See the file ../COPYING for licensing issues.
@@ -102,7 +102,10 @@ did not match: the former were ignored in the analysis.')
             V <- corMatrix(Initialize(corStruct, data.frame(x)),
                            corr = FALSE)
             invV <- solve(V)
-            obj$ace <- varAY %*% invV %*% x
+            o <- gls(x ~ 1, data.frame(x), correlation = corStruct)
+            GM <- o$coefficients
+            obj$ace <- drop(varAY %*% invV %*% (x - GM) + GM)
+            names(obj$ace) <- (nb.tip + 1):(nb.tip + nb.node)
             if (CI) {
                 CI95 <- matrix(NA, nb.node, 2)
                 se <- sqrt((varA - varAY %*% invV %*% t(varAY))[cbind(1:nb.node, 1:nb.node)])
@@ -167,13 +170,18 @@ as the number of categories in `x'")
             if (output.liks) return(liks[-(1:nb.tip), ])
             - 2 * log(sum(liks[nb.tip + 1, ]))
         }
-        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(Inf, 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)