[Vegan-commits] r451 - in pkg: . R inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 30 18:28:03 CEST 2008


Author: jarioksa
Date: 2008-07-30 18:28:03 +0200 (Wed, 30 Jul 2008)
New Revision: 451

Added:
   pkg/R/wcmdscale.R
   pkg/man/wcmdscale.Rd
Modified:
   pkg/DESCRIPTION
   pkg/inst/ChangeLog
Log:
added weighted metric scaling a.k.a. weighted principal coordinates analysis

Modified: pkg/DESCRIPTION
===================================================================
--- pkg/DESCRIPTION	2008-07-29 06:12:20 UTC (rev 450)
+++ pkg/DESCRIPTION	2008-07-30 16:28:03 UTC (rev 451)
@@ -1,7 +1,7 @@
 Package: vegan
 Title: Community Ecology Package
-Version: 1.14-7
-Date: July 5, 2008
+Version: 1.14-8
+Date: July 30, 2008
 Author: Jari Oksanen, Roeland Kindt, Pierre Legendre, Bob O'Hara, Gavin L. Simpson, 
    Peter Solymos, M. Henry H. Stevens, Helene Wagner  
 Maintainer: Jari Oksanen <jari.oksanen at oulu.fi>

Added: pkg/R/wcmdscale.R
===================================================================
--- pkg/R/wcmdscale.R	                        (rev 0)
+++ pkg/R/wcmdscale.R	2008-07-30 16:28:03 UTC (rev 451)
@@ -0,0 +1,40 @@
+`wcmdscale` <-
+function(d, k, eig = FALSE, add = FALSE, x.ret = FALSE, w)
+{
+    weight.centre <- function(x, w) {
+        w.c <- apply(x, 2, weighted.mean, w = w)
+        x <- sweep(x, 2, w.c, "-")
+        x
+    }
+    if (add)
+        .NotYetUsed("add")
+    ZERO <- sqrt(.Machine$double.eps)
+    if (!inherits(d, "dist")) {
+        op <- options(warn = 2)
+        on.exit(options(op))
+        d <- as.dist(d)
+        options(op)
+    }
+    m <- as.matrix(d^2)
+    n <- nrow(m)
+    if (missing(w))
+        w <- rep(1, n)
+    m <- weight.centre(m, w)
+    m <- t(weight.centre(t(m), w))
+    m <- m * sqrt(w) %o% sqrt(w)
+    e <- eigen(-m/2, symmetric = TRUE)
+    if (missing(k))
+        k <- sum(e$values > ZERO)
+    ev <- e$values[1:k]
+    points <- sweep(e$vectors[, 1:k, drop=FALSE], 1, sqrt(w), "/")
+    points <- sweep(points, 2, sqrt(ev), "*")
+    rownames(points) <- rownames(m)
+    if (eig || x.ret || add) {
+        out <- list(points = points, eig = if (eig) e$values[-n],
+                    x = if (x.ret) m, ac = NA, GOF = NA, weigths = w)
+        class(out) <- "wcmdscale"
+    }
+    else out <- points
+    out
+}
+

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2008-07-29 06:12:20 UTC (rev 450)
+++ pkg/inst/ChangeLog	2008-07-30 16:28:03 UTC (rev 451)
@@ -2,8 +2,13 @@
 
 VEGAN DEVEL VERSIONS at http://r-forge.r-project.org/
 
-Version 1.14-7 (opened July 5, 2008)
+Versin 1.14-8 (opened July 30, 2008)
 
+	* wcmdscale: new function to perform metric scaling
+	(a.k.a. principal coordinates analysis) with weights for points. 
+	
+Version 1.14-7 (closed July 30, 2008)
+
 	* orditkplot: imitates now plotting character (argument
 	pch). Zooming maintains graphical parameters (such as mar). Label
 	selection shown by a rectangle, since label may already be

Added: pkg/man/wcmdscale.Rd
===================================================================
--- pkg/man/wcmdscale.Rd	                        (rev 0)
+++ pkg/man/wcmdscale.Rd	2008-07-30 16:28:03 UTC (rev 451)
@@ -0,0 +1,83 @@
+% Based on:
+% File src/library/stats/man/cmdscale.Rd
+% Part of the R package, http://www.R-project.org
+% Copyright 1995-2007 R Core Development Team
+% Distributed under GPL 2 or later
+
+\name{cmdscale}
+\alias{cmdscale}
+\title{Weighted Classical (Metric) Multidimensional Scaling}
+\usage{
+cmdscale(d, k, eig = FALSE, add = FALSE, x.ret = FALSE, w)
+}
+\description{
+  Weighted classical multidimensional scaling, 
+  also known as weighted \emph{principal coordinates analysis}.
+}
+\arguments{
+  \item{d}{a distance structure such as that returned by \code{dist}
+    or a full symmetric matrix containing the dissimilarities.}
+  \item{k}{the dimension of the space which the data are to be
+    represented in; must be in \eqn{\{1,2,\ldots,n-1\}}. If missing,
+    dimensions with above zero eigenvalue.}
+  \item{eig}{indicates whether eigenvalues should be returned.}
+  \item{add}{logical indicating if an additive constant \eqn{c*} should
+    be computed, and added to the non-diagonal dissimilarities such that
+    all \eqn{n-1} eigenvalues are non-negative. \strong{Not implemented}. }
+  \item{x.ret}{indicates whether the doubly centred symmetric distance
+    matrix should be returned.}
+  \item{w}{Weights of points.}
+}
+\details{
+  Function \code{wcmdscale} is based on function
+  \code{\link{cmdscale}} (package \pkg{stats} of base \R), but it uses
+  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.
+}
+\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 object of class \code{wcmdscale} list containing the
+  following components. 
+  \item{points}{a matrix with \code{k} columns whose rows give the
+    coordinates of the points chosen to represent the dissimilarities.}
+  \item{eig}{the \eqn{n-1} eigenvalues computed during the scaling process if
+    \code{eig} is true.}
+  \item{x}{the doubly centred and weighted distance matrix if \code{x.ret} is true.}
+  \item{weights}{Weights.}
+     }
+}
+\references{
+  Gower, J. C. (1966)  
+  Some distance properties of latent root and vector 
+  methods used in multivariate analysis.  
+  \emph{Biometrika} \bold{53}, 325--328.
+
+  Mardia, K. V., Kent, J. T. and Bibby, J. M. (1979).  Chapter 14 of
+  \emph{Multivariate Analysis}, London: Academic Press.
+}
+\seealso{
+  \code{\link{cmdscale}}.
+  Also \code{\link[MASS]{isoMDS}} and \code{\link[MASS]{sammon}}
+  in package \pkg{MASS}.
+}
+\examples{
+## Correspondence analysis as a weighted principal coordinates
+## analysis of Euclidean distances of Chi-square transformed data
+data(dune)
+rs <- rowSums(dune)/sum(dune)
+d <- dist(decostand(dune, "chi"))
+ord <- wcmdscale(d, w = rs, eig = TRUE)
+## Ordinary CA
+ca <- cca(dune)
+## Eigevalues are numerically similar
+ca$CA$eig - ord$eig
+## Configurations are similar when site scores are scaled by
+## eigenvalues in CA
+procrustes(ord, ca, choices=1:19, scaling = 1)
+plot(procrustes(ord, ca, choices=1:2, scaling=1))
+}
+\keyword{multivariate}



More information about the Vegan-commits mailing list