1 ## yule.time.R (2009-02-20)
3 ## Fits the Time-Dependent Yule Model
5 ## Copyright 2009 Emmanuel Paradis
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 yule.time <- function(phy, birth, BIRTH = NULL, root.time = 0,
11 opti = "nlm", start = 0.01)
13 opti <- pmatch(opti, c("nlm", "nlminb", "optim"))
14 if (is.na(opti)) stop("ambiguous argument 'opti'")
15 LAMBDA <- function() x
16 body(LAMBDA) <- body(birth)
17 formals(LAMBDA) <- alist(t=)
18 BT <- branching.times(phy)
20 x <- BT[1] - BT + root.time
23 paranam <- c(names(formals(birth)))
25 start <- rep(start, length.out = np)
27 ## Foo is always vectorized
33 res[i] <- integrate(LAMBDA, x[i], T)$value
37 environment(BIRTH) <- environment()
38 Foo <- function(x) BIRTH(T) - BIRTH(x)
41 half.dev <- function(p) {
43 assign(paranam[i], p[i], pos = sys.frame(1))
45 if (is.null(BIRTH)) integrate(LAMBDA, x[1], T)$value
46 else BIRTH(T) - BIRTH(x[1])
47 sum(Foo(x)) + root.term - sum(log(LAMBDA(x[2:m])))
52 out <- nlm(half.dev, start, hessian = TRUE)
54 se <- sqrt(diag(solve(out$hessian)))
55 loglik <- lfactorial(m) - out$minimum
57 out <- nlminb(start, half.dev)
60 loglik <- lfactorial(m) - out$objective
62 out <- optim(start, half.dev, hessian = TRUE,
63 control = list(maxit = 1000), method = "BFGS")
65 se <- sqrt(diag(solve(out$hessian)))
66 loglik <- lfactorial(m) - out$value
69 if (!is.null(se)) names(se) <- paranam
70 structure(list(estimate = est, se = se, loglik = loglik), class = "yule")