[Vegan-commits] r1068 - in pkg/vegan: R inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Nov 14 07:53:05 CET 2009


Author: jarioksa
Date: 2009-11-14 07:52:57 +0100 (Sat, 14 Nov 2009)
New Revision: 1068

Added:
   pkg/vegan/R/spandepth.R
Modified:
   pkg/vegan/inst/ChangeLog
   pkg/vegan/man/spantree.Rd
Log:
added spandepth(): depth of nodes in a 'spantree' object

Added: pkg/vegan/R/spandepth.R
===================================================================
--- pkg/vegan/R/spandepth.R	                        (rev 0)
+++ pkg/vegan/R/spandepth.R	2009-11-14 06:52:57 UTC (rev 1068)
@@ -0,0 +1,28 @@
+### The depths of nodes in a 'spantree' object: The nodes are either
+### leaves with one link, or internal nodes with >1 links. The leaves
+### are removed recursively from the tree and at each step the depth
+### is increased with one.
+`spandepth` <-
+    function (x) 
+{
+    kid <- c(NA, x$kid)
+    par <- p <- seq_along(kid)
+    par[1] <- NA
+    ## Isolated nodes in disconnected tree have depth 0, other nodes
+    ## start from depth 1
+    intree <- p %in% kid | !is.na(kid) 
+    depth <- numeric(length(par))
+    depth[intree] <- 1
+    if (!is.null(tree$labels))
+        names(depth) <- tree$labels
+    while(any(intree)) {
+        ## Node is internal (intree) if it is both a parent and a kid
+        ## and kid is in the tree or it is kid to two or more parents
+        intree <- (p %in% intersect(kid[intree], par[intree]) &
+                   p %in% p[intree][kid[intree] %in% p[intree]] |
+                   p %in% kid[intree][duplicated(kid[intree])]) 
+        depth[intree] <- depth[intree] + 1
+    }
+    depth
+}
+

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2009-11-13 16:22:01 UTC (rev 1067)
+++ pkg/vegan/inst/ChangeLog	2009-11-14 06:52:57 UTC (rev 1068)
@@ -5,8 +5,12 @@
 Version 1.16-33 (opened November 13, 2009)
 
 	* DESCRIPTION: Guillaume Blanchet added to the Authors. 
+
+	* spandepth: New function to find the depths of nodes in a
+	spanning tree from spantree() function. Per request of
+	W. E. Sharp. 
 	
-Version 1.16-32 (closeded November 13, 2009)
+Version 1.16-32 (closed November 13, 2009)
 
 	* alias.cca: gained argument 'names.only' (defaults FALSE) to only
 	return the names of the aliased variables insted of their defining

Modified: pkg/vegan/man/spantree.Rd
===================================================================
--- pkg/vegan/man/spantree.Rd	2009-11-13 16:22:01 UTC (rev 1067)
+++ pkg/vegan/man/spantree.Rd	2009-11-14 06:52:57 UTC (rev 1068)
@@ -3,6 +3,7 @@
 \alias{cophenetic.spantree}
 \alias{plot.spantree}
 \alias{lines.spantree}
+\alias{spandepth}
 
 \title{Minimum Spanning Tree}
 \description{
@@ -13,6 +14,7 @@
 \usage{
 spantree(d, toolong = 0)
 \method{cophenetic}{spantree}(x)
+spandepth(x)
 \method{plot}{spantree}(x, ord, cex = 0.7, type = "p", labels, dlim,
      FUN = sammon,  ...)
 \method{lines}{spantree}(x, ord, display="sites", ...)
@@ -57,7 +59,15 @@
   using \code{\link{distconnected}}.
 
   Function \code{cophenetic} finds distances between all points along
-  the tree segments. Function \code{plot} displays the tree over a
+  the tree segments. Function \code{spandepth} returns the depth of
+  each node. The nodes of a tree are either leaves (with one link) or
+  internal nodes (more than one link). The leaves are recursively
+  removed from the tree, and the depth is the layer at with the leaf
+  was removed. In disconnected \code{spantree} object (in a forest)
+  each tree is analysed separately and disconnected nodes not in any
+  tree have depth zero.
+  
+  Function \code{plot} displays the tree over a
   supplied ordination configuration, and \code{lines} adds a spanning
   tree to an ordination graph. If configuration is not supplied for \code{plot},
   the function ordinates the cophenetic dissimilarities of the
@@ -119,6 +129,9 @@
 plot(tr, cmdscale(dis), type = "t")
 ## Find a configuration to display the tree neatly
 plot(tr, type = "t")
+## Depths of nodes
+depths <- spandepth(tr)
+plot(tr, type = "t", label = depths)
 }
 \keyword{ multivariate}
 



More information about the Vegan-commits mailing list