[Vegan-commits] r2358 - in pkg/vegan: . R inst man tests/Examples

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jan 10 10:49:02 CET 2013


Author: jarioksa
Date: 2013-01-10 10:49:01 +0100 (Thu, 10 Jan 2013)
New Revision: 2358

Added:
   pkg/vegan/R/print.wcmdscale.R
Modified:
   pkg/vegan/NAMESPACE
   pkg/vegan/R/stressplot.wcmdscale.R
   pkg/vegan/R/wcmdscale.R
   pkg/vegan/inst/ChangeLog
   pkg/vegan/man/wcmdscale.Rd
   pkg/vegan/tests/Examples/vegan-Ex.Rout.save
Log:
add print, scores and plot methods for wcmdscale

Modified: pkg/vegan/NAMESPACE
===================================================================
--- pkg/vegan/NAMESPACE	2013-01-10 07:39:26 UTC (rev 2357)
+++ pkg/vegan/NAMESPACE	2013-01-10 09:49:01 UTC (rev 2358)
@@ -275,6 +275,7 @@
 S3method(plot, varpart)
 S3method(plot, varpart234)
 S3method(plot, vegandensity)
+S3method(plot, wcmdscale)
 # points: graphics
 S3method(points, cca)
 S3method(points, decorana)
@@ -356,6 +357,7 @@
 S3method(print, varpart)
 S3method(print, varpart234)
 S3method(print, vectorfit)
+S3method(print, wcmdscale)
 # profile: stats
 # see note on 'confint'
 S3method(profile, MOStest)
@@ -386,6 +388,7 @@
 S3method(scores, orditkplot)
 S3method(scores, pcnm)
 S3method(scores, rda)
+S3method(scores, wcmdscale)
 # screeplot: stats
 S3method(screeplot, cca)
 S3method(screeplot, decorana)

Added: pkg/vegan/R/print.wcmdscale.R
===================================================================
--- pkg/vegan/R/print.wcmdscale.R	                        (rev 0)
+++ pkg/vegan/R/print.wcmdscale.R	2013-01-10 09:49:01 UTC (rev 2358)
@@ -0,0 +1,61 @@
+### support functions for wcmdscale results: print, scores and plot.
+
+`print.wcmdscale` <-
+    function(x, digits = max(3, getOption("digits") - 3), ...)
+{
+    writeLines(strwrap(pasteCall(x$call)))
+    cat("\n")
+    ## tabulate total inertia and ranks
+    totev <- sum(x$eig)
+    negax <- x$eig < 0
+    if (any(negax)) {
+        ranks <- c(NA, sum(!negax), sum(negax))
+        negax <- x$eig < 0
+        realev <- sum(x$eig[!negax])
+        imev <- sum(x$eig[negax])
+        evs <- c("Total" = totev, "Real" = realev, "Imaginary" = imev)
+    } else {
+        ranks <- length(x$eig)
+        evs <- c("Total" = totev)
+    }
+    tbl <- cbind("Inertia" = evs, "Rank" = ranks)
+    printCoefmat(tbl, digits = digits, na.print = "")
+    cat("\nResults have", NROW(x$points), "points,", NCOL(x$points), "axes\n")
+    ## print eigenvalues, but truncate very long lists
+    PRINLIM <- 120
+    neig <- length(x$eig)
+    cat("\nEigenvalues:\n")
+    print(zapsmall(x$eig[1 : min(neig, PRINLIM)], digits = digits, ...))
+    if (neig > PRINLIM)
+        cat("(Showed only", PRINLIM, "of all", neig, "eigenvalues)\n")
+    wvar <- var(x$weights)
+    wlen <- length(x$weights)
+    cat("\nWeights:")
+    if (wvar < 1e-6)
+        cat(" Constant\n")
+    else {
+        cat("\n")
+        print(zapsmall(x$weights[1 : min(wlen, PRINLIM)], digits = digits, ...))
+        if (wlen > PRINLIM)
+            cat("(Showed only", PRINLIM, "of all", wlen, "weights)\n")
+    }
+    cat("\n")
+    invisible(x)
+}
+
+`scores.wcmdscale` <-
+    function(x, choices = NA, ...)
+{
+    if (any(is.na(choices)))
+        x$points
+    else {
+        choices <- choices[choices <= NCOL(x$points)]
+        x$points[, choices, drop = FALSE]
+    }
+}
+
+`plot.wcmdscale` <-
+    function(x, choices = c(1,2), type = "t", ...)
+{
+    ordiplot(x, display = "sites", choices = choices, type = type, ...)
+}

Modified: pkg/vegan/R/stressplot.wcmdscale.R
===================================================================
--- pkg/vegan/R/stressplot.wcmdscale.R	2013-01-10 07:39:26 UTC (rev 2357)
+++ pkg/vegan/R/stressplot.wcmdscale.R	2013-01-10 09:49:01 UTC (rev 2358)
@@ -5,7 +5,14 @@
     function(object, k = 2, pch,  p.col = "blue", l.col = "red", lwd = 2, ...)
 {
     ## NB: Ignores weights
-    
+
+    ## Check that original distances can be reconstructed: this
+    ## requires that all axes were calculated instead of 'k' first.
+    hasdims <- NCOL(object$points)
+    if (!is.null(object$negaxes))
+        hasdims <- hasdims + NCOL(object$negaxes)
+    if (hasdims < length(object$eig))
+        stop("observed distances cannot be reconstructed: all axes were not calculated")
     ## Get the ordination distances in k dimensions
     if (k > NCOL(object$points))
         stop("'k' cannot exceed the number of real dimensions")

Modified: pkg/vegan/R/wcmdscale.R
===================================================================
--- pkg/vegan/R/wcmdscale.R	2013-01-10 07:39:26 UTC (rev 2357)
+++ pkg/vegan/R/wcmdscale.R	2013-01-10 09:49:01 UTC (rev 2358)
@@ -48,9 +48,10 @@
     GOF <- c(sum(ev)/sum(abs(e$values)),
              sum(ev)/sum(e$values[e$values > 0]))
     if (eig || x.ret || add) {
+        colnames(points) <- paste("Dim", seq_len(NCOL(points)), sep="") 
         out <- list(points = points, eig = if (eig) e$values,
                     x = if (x.ret) m, ac = NA, GOF = GOF, weights = w,
-                    negaxes = negaxes)
+                    negaxes = negaxes, call = match.call())
         class(out) <- "wcmdscale"
     }
     else out <- points

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2013-01-10 07:39:26 UTC (rev 2357)
+++ pkg/vegan/inst/ChangeLog	2013-01-10 09:49:01 UTC (rev 2358)
@@ -4,12 +4,13 @@
 
 Version 2.1-23 (opened January 8, 2013)
 
-	* capscale: It was wrongly assumed that eigenvalues could
-	be used in normalization of species scores, but this worked only
-	with Euclidean distances. Now normalization is done explicitly
-	with decostand() function. This change means that scaling of
-	species scores will change, and graphs can look different than
-	previously. All analyses should be redone. 
+	* capscale: It was wrongly assumed that eigenvalues could be used
+	in normalization of species scores, but this worked only with
+	Euclidean distances. Now normalization is done explicitly with
+	decostand() function. This change means that scaling of species
+	scores will change, and graphs can look different than
+	previously. All analyses should be redone. Function now displays
+	the value of the additive constant with 'add = TRUE'.
 
 	* stressplot: added stressplot() methods for wcmdscale(),
 	capscale(), cca() and rda() results.  These also work with
@@ -21,6 +22,10 @@
 	correct for the imaginary axes. The weights are not used in
 	wcmdscale() and cca(): I must first figure out if they should be
 	used or ignored.
+
+	* wcmdscale: added method functions print(), plot() and
+	scores(). Now class "wcmdscale" results also retun the function
+	call and dimensions have names.
 	
 Version 2.1-22 (closed January 8, 2013)
 

Modified: pkg/vegan/man/wcmdscale.Rd
===================================================================
--- pkg/vegan/man/wcmdscale.Rd	2013-01-10 07:39:26 UTC (rev 2357)
+++ pkg/vegan/man/wcmdscale.Rd	2013-01-10 09:49:01 UTC (rev 2358)
@@ -6,9 +6,15 @@
 
 \name{wcmdscale}
 \alias{wcmdscale}
+\alias{print.wcmdscale}
+\alias{scores.wcmdscale}
+\alias{plot.wcmdscale}
+
 \title{Weighted Classical (Metric) Multidimensional Scaling}
 \usage{
 wcmdscale(d, k, eig = FALSE, add = FALSE, x.ret = FALSE, w)
+\method{plot}{wcmdscale}(x, choices = c(1, 2), type = "t", ...)
+\method{scores}{wcmdscale}(x, choices = NA, ...) 
 }
 \description{
   Weighted classical multidimensional scaling, 
@@ -27,6 +33,13 @@
   \item{x.ret}{indicates whether the doubly centred symmetric distance
     matrix should be returned.}
   \item{w}{Weights of points.}
+  \item{x}{The \code{wcmdscale} result object when the function was
+    called with options \code{eig = TRUE} or \code{x.ret = TRUE} (See
+    Details).}
+  \item{choices}{Axes to be returned; \code{NA} returns all real axes.}
+  \item{type}{Type of graph which may be \code{"t"}ext, \code{"p"}oints
+    or \code{"n"}one.}
+  \item{\dots}{Other arguments passed to graphical functions.}
 }
 \details{
   Function \code{wcmdscale} is based on function
@@ -34,8 +47,14 @@
   point weights. Points with high weights will have a stronger
   influence on the result than those with low weights. Setting equal
   weights \code{w = 1} will give ordinary multidimensional scaling.
-}
 
+  With default options, the function returns only a matrix of scores
+  scaled by eigenvalues for all real axes. If the function is called
+  with \code{eig = TRUE} or \code{x.ret = TRUE}, the function returns an
+  object of class \code{"wcmdscale"} with \code{print}, \code{plot},
+  \code{scores}, \code{\link{eigenvals}} and \code{\link{stressplot}}
+  methods, and described in section Value.  }
+
 \value{ If \code{eig = FALSE} and \code{x.ret = FALSE} (default), a
   matrix with \code{k} columns whose rows give the coordinates of the
   points chosen to represent the dissimilarities.  Otherwise, an
@@ -56,7 +75,8 @@
      scaled by the absolute eigenvalues similarly as
      \code{points}. This is \code{NULL} if there are no negative
      eigenvalues or \code{k} was specified, and would not include
-     negative eigenvalues.}  
+     negative eigenvalues.}
+  \item{call}{Function call.}
 }
 
 \references{
@@ -69,10 +89,13 @@
   \emph{Multivariate Analysis}, London: Academic Press.
 }
 
-\seealso{ \code{\link{cmdscale}}.  Also \code{\link{monoMDS}}, and
+\seealso{The function is modelled after \code{\link{cmdscale}}, but adds
+  weights (hence name) and handles negative eigenvalues differently.
+  \code{\link{eigenvals.wcmdscale}} and
+  \code{\link{stressplot.wcmdscale}} are some specific methods. Other
+  multidimensional scaling methods are \code{\link{monoMDS}}, and
   \code{\link[MASS]{isoMDS}} and \code{\link[MASS]{sammon}} in package
-  \pkg{MASS}.  
-}
+  \pkg{MASS}.  }
 
 \examples{
 ## Correspondence analysis as a weighted principal coordinates

Modified: pkg/vegan/tests/Examples/vegan-Ex.Rout.save
===================================================================
--- pkg/vegan/tests/Examples/vegan-Ex.Rout.save	2013-01-10 07:39:26 UTC (rev 2357)
+++ pkg/vegan/tests/Examples/vegan-Ex.Rout.save	2013-01-10 09:49:01 UTC (rev 2358)
@@ -1,5 +1,5 @@
 
-R Under development (unstable) (2013-01-08 r61589) -- "Unsuffered Consequences"
+R Under development (unstable) (2013-01-09 r61595) -- "Unsuffered Consequences"
 Copyright (C) 2013 The R Foundation for Statistical Computing
 ISBN 3-900051-07-0
 Platform: x86_64-unknown-linux-gnu (64-bit)
@@ -161,7 +161,7 @@
 
 Formula:
 y ~ poly(x1, 1) + poly(x2, 1)
-<environment: 0x3160ba8>
+<environment: 0x27d9338>
 Total model degrees of freedom 3 
 
 GCV score: 0.04278782
@@ -1586,6 +1586,8 @@
 1.4408 0.8523 0.6015 0.4888 0.4187 0.3538 0.2877 0.2160 
 (Showed only 8 of all 19 unconstrained eigenvalues)
 
+Constant added to distances: 0.2614286 
+
 > ## Avoid negative eigenvalues by taking square roots of dissimilarities
 > capscale(varespec ~ N + P + K + Condition(Al), varechem,
 +                      dist = "bray", sqrt.dist= TRUE)
@@ -4910,7 +4912,7 @@
 
 Formula:
 y ~ s(x1, x2, k = knots)
-<environment: 0xa20afb0>
+<environment: 0x9181780>
 
 Estimated degrees of freedom:
 6.45  total = 7.45 
@@ -4926,7 +4928,7 @@
 
 Formula:
 y ~ s(x1, x2, k = knots)
-<environment: 0xa251f70>
+<environment: 0x9cff1b8>
 
 Estimated degrees of freedom:
 6.12  total = 7.12 
@@ -5086,7 +5088,7 @@
 
 Formula:
 y ~ s(x1, x2, k = knots)
-<environment: 0xa336010>
+<environment: 0x782f2c8>
 
 Estimated degrees of freedom:
 8.93  total = 9.93 
@@ -5099,7 +5101,7 @@
 
 Formula:
 y ~ s(x1, x2, k = knots)
-<environment: 0xab88d18>
+<environment: 0x9797dc8>
 
 Estimated degrees of freedom:
 7.75  total = 8.75 
@@ -5112,7 +5114,7 @@
 
 Formula:
 y ~ s(x1, x2, k = knots)
-<environment: 0xab15780>
+<environment: 0x9786660>
 
 Estimated degrees of freedom:
 8.9  total = 9.9 
@@ -7689,7 +7691,7 @@
 
 Formula:
 y ~ s(x1, x2, k = knots)
-<environment: 0x950e978>
+<environment: 0x876fd18>
 
 Estimated degrees of freedom:
 2  total = 3 
@@ -8120,7 +8122,7 @@
 > 
 > ### Name: wcmdscale
 > ### Title: Weighted Classical (Metric) Multidimensional Scaling
-> ### Aliases: wcmdscale
+> ### Aliases: wcmdscale print.wcmdscale scores.wcmdscale plot.wcmdscale
 > ### Keywords: multivariate
 > 
 > ### ** Examples
@@ -8169,7 +8171,7 @@
 > ### * <FOOTER>
 > ###
 > cat("Time elapsed: ", proc.time() - get("ptime", pos = 'CheckExEnv'),"\n")
-Time elapsed:  26.121 0.156 26.372 0 0 
+Time elapsed:  27.421 0.132 27.652 0 0 
 > grDevices::dev.off()
 null device 
           1 



More information about the Vegan-commits mailing list