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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun May 31 16:44:50 CEST 2009


Author: jarioksa
Date: 2009-05-31 16:44:50 +0200 (Sun, 31 May 2009)
New Revision: 843

Modified:
   pkg/vegan/R/eigenvals.R
   pkg/vegan/inst/ChangeLog
   pkg/vegan/man/eigenvals.Rd
Log:
eigenvals got class and a summary method

Modified: pkg/vegan/R/eigenvals.R
===================================================================
--- pkg/vegan/R/eigenvals.R	2009-05-31 07:58:41 UTC (rev 842)
+++ pkg/vegan/R/eigenvals.R	2009-05-31 14:44:50 UTC (rev 843)
@@ -11,44 +11,71 @@
 {
     ## svd and eigen return unspecified 'list', see if this could be
     ## either of them
+    out <- NA
     if (is.list(x)) {
         ## eigen
         if (length(x) == 2 && all(names(x) %in% c("values", "vectors")))
-            return(x$values)
+            out <- x$values
         ## svd: return squares of singular values
         if (length(x) == 3 && all(names(x) %in% c("d", "u", "v")))
-            return(x$d^2)
-        else
-            stop("No eigenvalues found")
+            out <- x$d^2
     }
-    else
-        stop("No eigenvalues found")
+    class(out) <- "eigenvals"
+    out
 }
 
 ## squares of sdev 
 `eigenvals.prcomp` <-
     function(x)
 {
-    x$sdev^2
+    out <- x$sdev^2
+    names(out) <- colnames(x$rotation)
+    class(out) <- "eigenvals"
+    out
 }
 
 ## squares of sdev
 `eigenvals.princomp` <-
     function(x)
 {
-    x$sdev^2
+    out <- x$sdev^2
+    class(out) <- "eigenvals"
+    out
 }
 
 ## concatenate constrained and unconstrained eigenvalues in cca, rda
 ## and capscale (vegan) -- ignore pCCA component
 `eigenvals.cca` <- function(x)
 {
-   c(x$CCA$eig, x$CA$eig) 
+   out <- c(x$CCA$eig, x$CA$eig)
+   class(out) <- c("eigenvals")
+   out
 }
 
 ## wcmdscale (in vegan)
 `eigenvals.wcmdscale` <-
     function(x)
 {
-    x$eig
+    out <- x$eig
+    class(out) <-"eigenvals"
+    out
 }
+
+`print.eigenvals` <-
+    function(x, ...)
+{
+    print(unclass(x), ...)
+    invisible(x)
+}
+
+`summary.eigenvals` <-
+    function(object, ...)
+{
+    vars <- object/sum(object)
+    importance <- rbind(`Eigenvalue` = object,
+                        `Proportion Explained` = round(vars, 5),
+                        `Cumulative Proportion`= round(cumsum(vars), 5))
+    out <- list(importance = importance)
+    class(out) <- c("summary.eigenvals", "summary.prcomp")
+    out
+}

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2009-05-31 07:58:41 UTC (rev 842)
+++ pkg/vegan/inst/ChangeLog	2009-05-31 14:44:50 UTC (rev 843)
@@ -17,7 +17,9 @@
 	* eigenvals: new function to extract eigenvalues of cca, rda,
 	capscale (constrained & unconstrained), wcmdscale, prcomp,
 	princomp, eigen and svd. For svd returns squares of singular
-	values, and for prcomp and princomp squares of 'sdev'.
+	values, and for prcomp and princomp squares of 'sdev'. The
+	'summary' of eigenvals() retrurn eigenvalues with the proportion
+	explained and cumulatitive proportion explained.
 
 	* protest: observed value of test statistic is now considered as
 	one of the permutations. Default N.perm decreased to 999 to account

Modified: pkg/vegan/man/eigenvals.Rd
===================================================================
--- pkg/vegan/man/eigenvals.Rd	2009-05-31 07:58:41 UTC (rev 842)
+++ pkg/vegan/man/eigenvals.Rd	2009-05-31 14:44:50 UTC (rev 843)
@@ -6,6 +6,8 @@
 \alias{eigenvals.princomp}
 \alias{eigenvals.cca}
 \alias{eigenvals.wcmdscale}
+\alias{print.eigenvals}
+\alias{summary.eigenvals}
 
 \title{
   Extract Eigenvalues from an Ordination Object
@@ -16,12 +18,16 @@
 }
 \usage{
 eigenvals(x)
+\method{summary}{eigenvals}(object, ...)
 }
 
 \arguments{
   \item{x}{
     An object from which to extract eigenvalues.
 }
+  \item{object}{
+    An \code{eigenvals} result object.
+}	
 }
 
 \details{
@@ -39,10 +45,13 @@
   \code{\link{capscale}} the function returns the both constrained and
   unconstrained eigenvalues concatenated in one vector, but the partial
   component will be ignored.
+
+  The \code{summary} of \code{eigenvals} result returns eigenvalues,
+  proportion explained and cumulative proportion explained.
 }
 
 \value{
-  Vector of eigenvalues.
+  An object of class \code{"eigenvals"} which is a vector of eigenvalues.
 }
 
 \author{
@@ -61,7 +70,7 @@
 mod <- cca(varespec ~ Al + P + K, varechem)
 ev <- eigenvals(mod)
 ev
-ev/sum(ev)
+summary(ev)
 }
 \keyword{ multivariate }
 



More information about the Vegan-commits mailing list