X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=R%2FrTrait.R;h=b87dd6f8a60ff023271acfb1339c36fdc53d190e;hb=3f91879755fdbbe39cfd936495b2985fa4621615;hp=3359df6f71da40c3b8a5d7a254844bafb50b79e3;hpb=24fc6c03893f85a3f9ab3d088201b3731f3035b4;p=ape.git diff --git a/R/rTrait.R b/R/rTrait.R index 3359df6..b87dd6f 100644 --- a/R/rTrait.R +++ b/R/rTrait.R @@ -1,4 +1,4 @@ -## rTrait.R (2010-01-11) +## rTrait.R (2010-12-24) ## Trait Evolution @@ -10,7 +10,7 @@ rTraitDisc <- function(phy, model = "ER", k = if (is.matrix(model)) ncol(model) else 2, rate = 0.1, states = LETTERS[1:k], freq = rep(1/k, k), - ancestor = FALSE, root.value = 1) + ancestor = FALSE, root.value = 1, ...) { if (is.null(phy$edge.length)) stop("tree has no branch length") @@ -18,7 +18,7 @@ rTraitDisc <- stop("at least one branch length negative") if (is.character(model)) { - switch(model, "ER" = { + switch(toupper(model), "ER" = { if (length(rate) != 1) stop("`rate' must have one element") Q <- matrix(rate, k, k) @@ -56,11 +56,14 @@ rTraitDisc <- if (is.function(model)) { environment(model) <- environment() # to find 'k' - for (i in N:1) x[des[i]] <- model(x[anc[i]], el[i]) + for (i in N:1) x[des[i]] <- model(x[anc[i]], el[i], ...) } else { + freq <- rep(freq, each = k) + Q <- Q * freq + diag(Q) <- 0 diag(Q) <- -rowSums(Q) for (i in N:1) { - p <- matexpo(Q * freq * el[i])[x[anc[i]], ] + p <- matexpo(Q * el[i])[x[anc[i]], ] x[des[i]] <- .Internal(sample(k, size = 1, FALSE, prob = p)) } } @@ -78,8 +81,8 @@ rTraitDisc <- } rTraitCont <- - function(phy, model = "BM", sigma = 0.1, alpha = 1, - theta = 0, ancestor = FALSE, root.value = 0) + function(phy, model = "BM", sigma = 0.1, alpha = 1, theta = 0, + ancestor = FALSE, root.value = 0, linear = TRUE, ...) { if (is.null(phy$edge.length)) stop("tree has no branch length") @@ -99,9 +102,9 @@ rTraitCont <- if (is.function(model)) { environment(model) <- environment() - for (i in N:1) x[des[i]] <- model(x[anc[i]], el[i]) + for (i in N:1) x[des[i]] <- model(x[anc[i]], el[i], ...) } else { - model <- pmatch(model, c("BM", "OU")) + model <- pmatch(toupper(model), c("BM", "OU")) if (length(sigma) == 1) sigma <- rep(sigma, N) else if (length(sigma) != N) stop("'sigma' must have one or Nedge(phy) elements") @@ -112,6 +115,7 @@ rTraitCont <- if (length(theta) == 1) theta <- rep(theta, N) else if (length(theta) != N) stop("'theta' must have one or Nedge(phy) elements") + if (!linear) model <- model + 1L } .C("rTraitCont", as.integer(model), as.integer(N), as.integer(anc - 1L), as.integer(des - 1L), el, sigma, alpha, theta, x, DUP = FALSE, NAOK = TRUE, PACKAGE = "ape") }