[Ecopd-commits] r75 - in branches/single-tree: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Nov 17 07:04:49 CET 2009


Author: regetz
Date: 2009-11-17 07:04:48 +0100 (Tue, 17 Nov 2009)
New Revision: 75

Added:
   branches/single-tree/R/aed.R
   branches/single-tree/man/aed.Rd
   branches/single-tree/man/iac.Rd
   branches/single-tree/man/pae.Rd
Removed:
   branches/single-tree/R/ed.R
Modified:
   branches/single-tree/DESCRIPTION
Log:
added skeleton documentation for pae, iac, and aed (and related)
methods; renamed ed.R file to match, and updated COLLATE section


Modified: branches/single-tree/DESCRIPTION
===================================================================
--- branches/single-tree/DESCRIPTION	2009-11-17 05:14:23 UTC (rev 74)
+++ branches/single-tree/DESCRIPTION	2009-11-17 06:04:48 UTC (rev 75)
@@ -8,6 +8,6 @@
 Depends: methods, phylobase
 Description: A set of metrics and functions for combining ecological and phylogenetic information
 License: GPL
-Collate: phylo4com.R utilities.R community.R pd.R ed.R iac.R pae.R simpson.R indices.R plots.R
+Collate: phylo4com.R utilities.R community.R pd.R aed.R iac.R pae.R simpson.R indices.R plots.R
 LazyLoad: yes
 LazyData: yes

Copied: branches/single-tree/R/aed.R (from rev 63, branches/single-tree/R/ed.R)
===================================================================
--- branches/single-tree/R/aed.R	                        (rev 0)
+++ branches/single-tree/R/aed.R	2009-11-17 06:04:48 UTC (rev 75)
@@ -0,0 +1,221 @@
+##
+## ED and related methods
+##
+
+setGeneric("ed", function(x, na.rm=TRUE) {
+    standardGeneric("ed")
+})
+
+setMethod("ed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
+    phyc <- phylo4com(x)
+    ed(phyc, na.rm=na.rm)
+})
+
+# TODO: This function includes its own code for not counting root edge
+# length. Maybe this should maybe be done at a higher level?
+setMethod("ed", signature(x="phylo4com"), function(x, na.rm=TRUE) {
+
+    comms <- x at metadata$comms
+    if (is.null(comms)) {
+        stop("no community data specified in phylo4com object")
+    }
+    subtrees <- x at subtrees[unique(as.character(comms))]
+    .edi <- function(tree) {
+        # set length of root edge to zero
+        edgeLength(tree)[edgeId(tree, "root")] <- 0
+
+        all.nodes <- nodeId(tree, type = "all")
+        des <- descendants(tree, all.nodes, type="tips")
+        nv <- edgeLength(tree, all.nodes) / sapply(des, length)
+        names(nv) <- all.nodes
+
+        tip.nodes <- nodeId(tree, "tip")
+        anc <- ancestors(tree, tip.nodes, "ALL")
+
+        res <- sapply(anc, function(n) sum(nv[as.character(n)], na.rm=TRUE))
+        names(res) <- tipLabels(tree)
+        res
+    }
+    res <- lapply(subtrees, .edi)[as.character(comms)]
+    names(res) <- names(comms)
+    return(res)
+
+})
+
+setGeneric("hed", function(x, na.rm=TRUE) {
+    standardGeneric("hed")
+})
+
+setMethod("hed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
+    phyc <- phylo4com(x)
+    hed(phyc, na.rm=na.rm)
+})
+
+setMethod("hed", signature(x="phylo4com"), function(x, na.rm=TRUE) {
+    ED <- ed(x)
+    PD <- pd(x)
+    res <- sapply(seq_len(length(PD)), function(i) {
+        scaledED <- ED[[i]] / PD[[i]]
+        -sum(scaledED * log(scaledED))
+    })
+    names(res) <- names(PD)
+    return(res)
+})
+
+setGeneric("eed", function(x, na.rm=TRUE) {
+    standardGeneric("eed")
+})
+
+setMethod("eed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
+    phyc <- phylo4com(x)
+    eed(phyc, na.rm=na.rm)
+})
+
+setMethod("eed", signature(x="phylo4com"), function(x, na.rm=TRUE) {
+    comms <- x at metadata$comms
+    if (is.null(comms)) {
+        stop("no community data specified in phylo4com object")
+    }
+    subtrees <- x at subtrees[unique(as.character(comms))]
+    hed(x) / sapply(subtrees, nTips)[as.character(comms)]
+})
+
+setGeneric("aed", function(x, na.rm=TRUE) {
+    standardGeneric("aed")
+})
+
+setMethod("aed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
+    phyc <- phylo4com(x)
+    aed(phyc, na.rm=na.rm)
+})
+
+# TODO: This function includes its own code for not counting root edge
+# length. Maybe this should maybe be done at a higher level?
+setMethod("aed", signature(x="phylo4com"),
+  function(x, na.rm=TRUE) {
+
+    comms <- x at metadata$comms
+    if (is.null(comms)) {
+        stop("no community data specified in phylo4com object")
+    }
+    subtrees <- x at subtrees[unique(as.character(comms))]
+    .isD <- function(tree) {
+        # get all node IDs, but excluding root node
+        nonroot.nodes <- setdiff(nodeId(tree), rootNode(tree))
+        # Create logical matrix indicating which tips (in columns) are
+        # descendants of each node (in rows), self-inclusive
+        t(sapply(ancestors(tree, tipLabels(tree), "ALL"),
+            function(n) nonroot.nodes %in% n))
+    }
+
+    .elen <- function(tree) {
+        nonroot.nodes <- setdiff(nodeId(tree), rootNode(tree))
+        edgeLength(tree, nonroot.nodes)
+    }
+    isDescendant <- lapply(subtrees, .isD)[as.character(comms)]
+    edge.length <- lapply(subtrees, .elen)[as.character(comms)]
+    N <- abundance(x)
+
+    res <- lapply(seq_along(N), function(i) {
+        spp <- row.names(isDescendant[[i]])        
+        dAbund <- N[spp, i] * isDescendant[[i]]
+
+        # Calculate individual-based AED of each species
+        AED <- colSums(edge.length[[i]] * t(prop.table(dAbund, margin=2)))
+        AED/N[spp, i]
+    })
+    names(res) <- names(comms)
+    return(res)
+
+})
+
+#    .isD <- function(tree, template) {
+#        # get all node IDs, but excluding root node
+#        nonroot.nodes <- setdiff(nodeId(tree), rootNode(tree))
+#        # Create logical matrix indicating which tips (in columns) are
+#        # descendants of each node (in rows), self-inclusive
+#        res <- t(sapply(ancestors(tree, tipLabels(tree), "ALL"),
+#          function(n) nonroot.nodes %in% n))
+#        template[match(rownames(res), rownames(template)), ] <- res
+#        template
+#    }
+#
+#    .el <- function(tree) {
+#        nonroot.nodes <- setdiff(nodeId(tree), rootNode(tree))
+#        edgeLength(tree, nonroot.nodes)
+#    }
+#    tmp <- setNames(rep(NA, nTips(x)), tipLabels(x))
+#    tmp <- matrix(NA, nrow=nTips(x), ncol=nNodes(x)+nTips(x)-1)
+#    rownames(tmp) <- tipLabels(x)
+#    isDescendant <- lapply(subtrees, .isD, tmp)
+#    isDescendant <- array(unlist(isDescendant), dim=c(nTips(x),
+#        nTips(x)+nNodes(x)-1, length(subtrees)))
+#
+#    # Create vector of ancestral edge lengths
+#    edge.length <- sapply(subtrees, .elen)
+#
+#    # Create matrix containing number of individuals of each species
+#    # descending from each interior node
+#    N <- as.matrix(abundance(x))
+#    dAbund <- sweep(isDescendant, c(1,3), N, "*")
+#
+#    # Calculate individual-based AED of each species
+#    pt <- prop.table(dAbund, margin=c(2,3))
+#    AED <- sweep(pt, c(2,3), edge.length, "*")
+#    AED <- apply(AED, 3, rowSums) / N
+#
+#    return(AED)
+
+setGeneric("haed", function(x, na.rm=TRUE) {
+    standardGeneric("haed")
+})
+
+setMethod("haed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
+    phyc <- phylo4com(x)
+    haed(phyc, na.rm=na.rm)
+})
+
+setMethod("haed", signature(x="phylo4com"), function(x, na.rm=TRUE) {
+    # Recast AED in terms of individuals
+    AED <- aed(x)
+    PD <- pd(x)
+    N <- abundance(x)
+    scaled.AED <- lapply(seq_along(N), function(i) {
+        spp <- names(AED[[i]])        
+        rep(unname(AED[[i]]), N[spp, i]) / PD[[i]]
+    })
+    res <- sapply(scaled.AED, function(x) -sum(x * log(x)))
+    names(res) <- names(AED)
+    return(res)
+})
+
+setGeneric("eaed", function(x, na.rm=TRUE) {
+    standardGeneric("eaed")
+})
+
+setMethod("eaed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
+    phyc <- phylo4com(x)
+    eaed(phyc, na.rm=na.rm)
+})
+
+setMethod("eaed", signature(x="phylo4com"), function(x, na.rm=TRUE) {
+  haed(x) / log(colSums(abundance(x)))
+})
+
+setGeneric("value",
+  function(x, na.rm=TRUE) {
+    standardGeneric("value")
+})
+
+setMethod("value", signature(x="phylo4d"),
+  function(x, na.rm=TRUE) {
+    phyc <- phylo4com(x)
+    value(phyc, na.rm=na.rm)
+})
+
+setMethod("value", signature(x="phylo4com"),
+  function(x, na.rm=TRUE) {
+    AED <- aed(x)
+    lapply(AED, function(x) x/sum(x))
+})
+

Deleted: branches/single-tree/R/ed.R
===================================================================
--- branches/single-tree/R/ed.R	2009-11-17 05:14:23 UTC (rev 74)
+++ branches/single-tree/R/ed.R	2009-11-17 06:04:48 UTC (rev 75)
@@ -1,221 +0,0 @@
-##
-## ED and related methods
-##
-
-setGeneric("ed", function(x, na.rm=TRUE) {
-    standardGeneric("ed")
-})
-
-setMethod("ed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
-    phyc <- phylo4com(x)
-    ed(phyc, na.rm=na.rm)
-})
-
-# TODO: This function includes its own code for not counting root edge
-# length. Maybe this should maybe be done at a higher level?
-setMethod("ed", signature(x="phylo4com"), function(x, na.rm=TRUE) {
-
-    comms <- x at metadata$comms
-    if (is.null(comms)) {
-        stop("no community data specified in phylo4com object")
-    }
-    subtrees <- x at subtrees[unique(as.character(comms))]
-    .edi <- function(tree) {
-        # set length of root edge to zero
-        edgeLength(tree)[edgeId(tree, "root")] <- 0
-
-        all.nodes <- nodeId(tree, type = "all")
-        des <- descendants(tree, all.nodes, type="tips")
-        nv <- edgeLength(tree, all.nodes) / sapply(des, length)
-        names(nv) <- all.nodes
-
-        tip.nodes <- nodeId(tree, "tip")
-        anc <- ancestors(tree, tip.nodes, "ALL")
-
-        res <- sapply(anc, function(n) sum(nv[as.character(n)], na.rm=TRUE))
-        names(res) <- tipLabels(tree)
-        res
-    }
-    res <- lapply(subtrees, .edi)[as.character(comms)]
-    names(res) <- names(comms)
-    return(res)
-
-})
-
-setGeneric("hed", function(x, na.rm=TRUE) {
-    standardGeneric("hed")
-})
-
-setMethod("hed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
-    phyc <- phylo4com(x)
-    hed(phyc, na.rm=na.rm)
-})
-
-setMethod("hed", signature(x="phylo4com"), function(x, na.rm=TRUE) {
-    ED <- ed(x)
-    PD <- pd(x)
-    res <- sapply(seq_len(length(PD)), function(i) {
-        scaledED <- ED[[i]] / PD[[i]]
-        -sum(scaledED * log(scaledED))
-    })
-    names(res) <- names(PD)
-    return(res)
-})
-
-setGeneric("eed", function(x, na.rm=TRUE) {
-    standardGeneric("eed")
-})
-
-setMethod("eed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
-    phyc <- phylo4com(x)
-    eed(phyc, na.rm=na.rm)
-})
-
-setMethod("eed", signature(x="phylo4com"), function(x, na.rm=TRUE) {
-    comms <- x at metadata$comms
-    if (is.null(comms)) {
-        stop("no community data specified in phylo4com object")
-    }
-    subtrees <- x at subtrees[unique(as.character(comms))]
-    hed(x) / sapply(subtrees, nTips)[as.character(comms)]
-})
-
-setGeneric("aed", function(x, na.rm=TRUE) {
-    standardGeneric("aed")
-})
-
-setMethod("aed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
-    phyc <- phylo4com(x)
-    aed(phyc, na.rm=na.rm)
-})
-
-# TODO: This function includes its own code for not counting root edge
-# length. Maybe this should maybe be done at a higher level?
-setMethod("aed", signature(x="phylo4com"),
-  function(x, na.rm=TRUE) {
-
-    comms <- x at metadata$comms
-    if (is.null(comms)) {
-        stop("no community data specified in phylo4com object")
-    }
-    subtrees <- x at subtrees[unique(as.character(comms))]
-    .isD <- function(tree) {
-        # get all node IDs, but excluding root node
-        nonroot.nodes <- setdiff(nodeId(tree), rootNode(tree))
-        # Create logical matrix indicating which tips (in columns) are
-        # descendants of each node (in rows), self-inclusive
-        t(sapply(ancestors(tree, tipLabels(tree), "ALL"),
-            function(n) nonroot.nodes %in% n))
-    }
-
-    .elen <- function(tree) {
-        nonroot.nodes <- setdiff(nodeId(tree), rootNode(tree))
-        edgeLength(tree, nonroot.nodes)
-    }
-    isDescendant <- lapply(subtrees, .isD)[as.character(comms)]
-    edge.length <- lapply(subtrees, .elen)[as.character(comms)]
-    N <- abundance(x)
-
-    res <- lapply(seq_along(N), function(i) {
-        spp <- row.names(isDescendant[[i]])        
-        dAbund <- N[spp, i] * isDescendant[[i]]
-
-        # Calculate individual-based AED of each species
-        AED <- colSums(edge.length[[i]] * t(prop.table(dAbund, margin=2)))
-        AED/N[spp, i]
-    })
-    names(res) <- names(comms)
-    return(res)
-
-})
-
-#    .isD <- function(tree, template) {
-#        # get all node IDs, but excluding root node
-#        nonroot.nodes <- setdiff(nodeId(tree), rootNode(tree))
-#        # Create logical matrix indicating which tips (in columns) are
-#        # descendants of each node (in rows), self-inclusive
-#        res <- t(sapply(ancestors(tree, tipLabels(tree), "ALL"),
-#          function(n) nonroot.nodes %in% n))
-#        template[match(rownames(res), rownames(template)), ] <- res
-#        template
-#    }
-#
-#    .el <- function(tree) {
-#        nonroot.nodes <- setdiff(nodeId(tree), rootNode(tree))
-#        edgeLength(tree, nonroot.nodes)
-#    }
-#    tmp <- setNames(rep(NA, nTips(x)), tipLabels(x))
-#    tmp <- matrix(NA, nrow=nTips(x), ncol=nNodes(x)+nTips(x)-1)
-#    rownames(tmp) <- tipLabels(x)
-#    isDescendant <- lapply(subtrees, .isD, tmp)
-#    isDescendant <- array(unlist(isDescendant), dim=c(nTips(x),
-#        nTips(x)+nNodes(x)-1, length(subtrees)))
-#
-#    # Create vector of ancestral edge lengths
-#    edge.length <- sapply(subtrees, .elen)
-#
-#    # Create matrix containing number of individuals of each species
-#    # descending from each interior node
-#    N <- as.matrix(abundance(x))
-#    dAbund <- sweep(isDescendant, c(1,3), N, "*")
-#
-#    # Calculate individual-based AED of each species
-#    pt <- prop.table(dAbund, margin=c(2,3))
-#    AED <- sweep(pt, c(2,3), edge.length, "*")
-#    AED <- apply(AED, 3, rowSums) / N
-#
-#    return(AED)
-
-setGeneric("haed", function(x, na.rm=TRUE) {
-    standardGeneric("haed")
-})
-
-setMethod("haed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
-    phyc <- phylo4com(x)
-    haed(phyc, na.rm=na.rm)
-})
-
-setMethod("haed", signature(x="phylo4com"), function(x, na.rm=TRUE) {
-    # Recast AED in terms of individuals
-    AED <- aed(x)
-    PD <- pd(x)
-    N <- abundance(x)
-    scaled.AED <- lapply(seq_along(N), function(i) {
-        spp <- names(AED[[i]])        
-        rep(unname(AED[[i]]), N[spp, i]) / PD[[i]]
-    })
-    res <- sapply(scaled.AED, function(x) -sum(x * log(x)))
-    names(res) <- names(AED)
-    return(res)
-})
-
-setGeneric("eaed", function(x, na.rm=TRUE) {
-    standardGeneric("eaed")
-})
-
-setMethod("eaed", signature(x="phylo4d"), function(x, na.rm=TRUE) {
-    phyc <- phylo4com(x)
-    eaed(phyc, na.rm=na.rm)
-})
-
-setMethod("eaed", signature(x="phylo4com"), function(x, na.rm=TRUE) {
-  haed(x) / log(colSums(abundance(x)))
-})
-
-setGeneric("value",
-  function(x, na.rm=TRUE) {
-    standardGeneric("value")
-})
-
-setMethod("value", signature(x="phylo4d"),
-  function(x, na.rm=TRUE) {
-    phyc <- phylo4com(x)
-    value(phyc, na.rm=na.rm)
-})
-
-setMethod("value", signature(x="phylo4com"),
-  function(x, na.rm=TRUE) {
-    AED <- aed(x)
-    lapply(AED, function(x) x/sum(x))
-})
-

Added: branches/single-tree/man/aed.Rd
===================================================================
--- branches/single-tree/man/aed.Rd	                        (rev 0)
+++ branches/single-tree/man/aed.Rd	2009-11-17 06:04:48 UTC (rev 75)
@@ -0,0 +1,59 @@
+\name{ed}
+\alias{ed}
+\alias{ed-methods}
+\alias{ed,phylo4d-method}
+\alias{ed,phylo4com-method}
+\alias{aed}
+\alias{aed-methods}
+\alias{aed,phylo4d-method}
+\alias{aed,phylo4com-method}
+\alias{eed}
+\alias{eed-methods}
+\alias{eed,phylo4d-method}
+\alias{eed,phylo4com-method}
+\alias{hed}
+\alias{hed-methods}
+\alias{hed,phylo4d-method}
+\alias{hed,phylo4com-method}
+\alias{eaed}
+\alias{eaed-methods}
+\alias{eaed,phylo4d-method}
+\alias{eaed,phylo4com-method}
+\alias{haed}
+\alias{haed-methods}
+\alias{haed,phylo4d-method}
+\alias{haed,phylo4com-method}
+\alias{value}
+\alias{value-methods}
+\alias{value,phylo4d-method}
+\alias{value,phylo4com-method}
+\title{Abundance-weighted evolutionary distinctiveness}
+\description{
+  Calculates AED (and related metrics) for a set of communities with
+  phylogenetic data.
+}
+\usage{
+  \S4method{aed}{phylo4com}(x, na.rm = TRUE)
+  \S4method{aed}{phylo4d}(x, na.rm = TRUE)
+}
+\arguments{
+  \item{x}{A \code{phylo4d} object.}
+  \item{na.rm}{logical. Should NA abundances be treated as zero?}
+}
+\details{
+  TODO
+}
+\value{
+  For \code{aed}, \code{ed}, and \code{value}, a list of numeric
+  vectors, one per community, with values for each species in the
+  community. For \code{eed}, \code{hed}, \code{eaed}, and \code{haed}, a
+  numeric vector of length equal to the number of communities.
+}
+\references{
+  TODO
+}
+\author{Jim Regetz (regetz at nceas.ucsb.edu)}
+\examples{
+  aed(weeds)
+  haed(weeds)
+}


Property changes on: branches/single-tree/man/aed.Rd
___________________________________________________________________
Name: svn:eol-style
   + native

Added: branches/single-tree/man/iac.Rd
===================================================================
--- branches/single-tree/man/iac.Rd	                        (rev 0)
+++ branches/single-tree/man/iac.Rd	2009-11-17 06:04:48 UTC (rev 75)
@@ -0,0 +1,30 @@
+\name{iac}
+\alias{iac}
+\alias{iac-methods}
+\alias{iac,phylo4com-method}
+\alias{iac,phylo4d-method}
+\title{Imbalance of Abundances at the Clade level}
+\description{
+  Calculates IAC for a set of communities with phylogenetic data.
+}
+\usage{
+  \S4method{iac}{phylo4com}(x, na.rm = TRUE)
+  \S4method{iac}{phylo4d}(x, na.rm = TRUE)
+}
+\arguments{
+  \item{x}{A \code{phylo4d} object.}
+  \item{na.rm}{logical. Should NA abundances be treated as zero?}
+}
+\details{
+  TODO
+}
+\value{
+  A numeric vector of length equal to the number of communities.
+}
+\references{
+  TODO
+}
+\author{Jim Regetz (regetz at nceas.ucsb.edu)}
+\examples{
+  iac(weeds)
+}


Property changes on: branches/single-tree/man/iac.Rd
___________________________________________________________________
Name: svn:eol-style
   + native

Added: branches/single-tree/man/pae.Rd
===================================================================
--- branches/single-tree/man/pae.Rd	                        (rev 0)
+++ branches/single-tree/man/pae.Rd	2009-11-17 06:04:48 UTC (rev 75)
@@ -0,0 +1,30 @@
+\name{pae}
+\alias{pae}
+\alias{pae-methods}
+\alias{pae,phylo4com-method}
+\alias{pae,phylo4d-method}
+\title{Phylogenetic-Abundance Evenness}
+\description{
+  Calculates PAE for a set of communities with phylogenetic data.
+}
+\usage{
+  \S4method{pae}{phylo4com}(x, na.rm = TRUE)
+  \S4method{pae}{phylo4d}(x, na.rm = TRUE)
+}
+\arguments{
+  \item{x}{A \code{phylo4d} object.}
+  \item{na.rm}{logical. Should NA abundances be treated as zero?}
+}
+\details{
+  TODO
+}
+\value{
+  A numeric vector of length equal to the number of communities.
+}
+\references{
+  TODO
+}
+\author{Jim Regetz (regetz at nceas.ucsb.edu)}
+\examples{
+  pae(weeds)
+}


Property changes on: branches/single-tree/man/pae.Rd
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Ecopd-commits mailing list