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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jan 11 10:58:40 CET 2011


Author: jarioksa
Date: 2011-01-11 10:58:40 +0100 (Tue, 11 Jan 2011)
New Revision: 1452

Modified:
   pkg/vegan/R/capscale.R
   pkg/vegan/R/simulate.rda.R
   pkg/vegan/inst/ChangeLog
   pkg/vegan/man/cca.object.Rd
   pkg/vegan/man/simulate.rda.Rd
Log:
implement simulate.capscale

Modified: pkg/vegan/R/capscale.R
===================================================================
--- pkg/vegan/R/capscale.R	2011-01-10 16:00:38 UTC (rev 1451)
+++ pkg/vegan/R/capscale.R	2011-01-11 09:58:40 UTC (rev 1452)
@@ -133,6 +133,7 @@
     sol$method <- "capscale"
     if (add)
         sol$ac <- X$ac
+    sol$adjust <- adjust
     sol$inertia <- inertia
     if (metaMDSdist)
         sol$metaMDSdist <- commname

Modified: pkg/vegan/R/simulate.rda.R
===================================================================
--- pkg/vegan/R/simulate.rda.R	2011-01-10 16:00:38 UTC (rev 1451)
+++ pkg/vegan/R/simulate.rda.R	2011-01-11 09:58:40 UTC (rev 1452)
@@ -83,8 +83,63 @@
     ans
 }
 
+
+### capscale method: copies simulate.rda as much as possible. Function
+### works with the internal metric scaling mapping of fit and error,
+### but returns Euclidean distances adjusted to the original scaling
+### of input dissimilarities. Only the real components are used, and
+### capscale() of simulated dissimilarities have no Imaginary
+### component.
+
 `simulate.capscale` <-
-    function(object, nsim = 1, seed = NULL, ...)
+    function(object, nsim = 1, seed = NULL, indx = NULL, rank = "full", ...) 
 {
-    .NotYetImplemented()
+    ## Handle RNG: code directly from stats::simulate.lm
+    if (!exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE)) 
+        runif(1)
+    if (is.null(seed)) 
+        RNGstate <- get(".Random.seed", envir = .GlobalEnv)
+    else {
+        R.seed <- get(".Random.seed", envir = .GlobalEnv)
+        set.seed(seed)
+        RNGstate <- structure(seed, kind = as.list(RNGkind()))
+        on.exit(assign(".Random.seed", R.seed, envir = .GlobalEnv))
+    }
+    if (nsim > 1)
+        .NotYetUsed("nsim")
+    ## predict.capscale cannot be used because it returns either
+    ## dissimilarities ("response") or scores with the rank of the
+    ## constrained solution, and we need rank of the data (not of
+    ## constraints).
+    if (rank > 0) {
+        ftd <- qr.fitted(object$CCA$QR, object$CCA$Xbar)
+        ## redo analysis when rank < full
+        if (rank < object$CCA$rank) {
+            x <- svd(ftd, nu = rank, nv = rank)
+            ftd <- x$u %*% diag(x$d[1:rank], nrow=rank) %*% t(x$v)
+        }
+    } else {
+        ftd <- matrix(0, nrow=nrow(object$CA$Xbar),
+                      ncol = ncol(object$CA$Xbar))
+    }
+    ## add partial Fit to the constrained
+    if (!is.null(object$pCCA))
+        ftd <- ftd + object$pCCA$Fit
+    if (is.null(indx))
+        ans <- as.data.frame(ftd + matrix(rnorm(length(ftd), 
+               sd = outer(rep(1,nrow(ftd)), sd(object$CA$Xbar))), 
+               nrow = nrow(ftd)))
+    else
+        ans <- ftd + object$CA$Xbar[indx,]
+    ## return Euclidean distances
+    ans <- dist(ans)
+    ## remove adjustment done in capscale and put dissimilarities to
+    ## (approximately) original scale
+    ans <- ans/object$adjust
+    if (is.null(indx))
+        attr(ans, "seed") <- RNGstate
+    else
+        attr(ans, "seed") <- indx
+    ans
 }
+

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2011-01-10 16:00:38 UTC (rev 1451)
+++ pkg/vegan/inst/ChangeLog	2011-01-11 09:58:40 UTC (rev 1452)
@@ -14,6 +14,12 @@
 	scores that produce the dissimilarities as Euclidean distances --
 	like was claimed in the help page.
 
+	* simulate.capscale: implemented simulate() for capscale. This
+	uses internal metric scaling mapping to simulate data and add
+	error, but returns Euclidean distances adjusted to the original
+	scale of input dissimilarities. The function uses only the real
+	axes, and imaginary dimensions are ignored.
+
 Version 1.18-20 (closed January 9, 2011)
 
 	* base of vegan release 1.17-6 (released on Jan 10, 2011).

Modified: pkg/vegan/man/cca.object.Rd
===================================================================
--- pkg/vegan/man/cca.object.Rd	2011-01-10 16:00:38 UTC (rev 1451)
+++ pkg/vegan/man/cca.object.Rd	2011-01-11 09:58:40 UTC (rev 1452)
@@ -158,6 +158,8 @@
     \item{\code{metaMDSdist}}{The data set name if 
        \code{metaMDSdist = TRUE}.} 
     \item{\code{ac}}{Additive constant used if \code{add = TRUE}.}
+    \item{\code{adjust}}{Adjustment of dissimilarities: see
+      \code{\link{capscale}}, section \dQuote{Notes}.}
   }
 }
   

Modified: pkg/vegan/man/simulate.rda.Rd
===================================================================
--- pkg/vegan/man/simulate.rda.Rd	2011-01-10 16:00:38 UTC (rev 1451)
+++ pkg/vegan/man/simulate.rda.Rd	2011-01-11 09:58:40 UTC (rev 1452)
@@ -6,10 +6,10 @@
 
 \description{ Function simulates a response data frame so that it adds
  Gaussian error to the fitted responses of Redundancy Analysis
- (\code{\link{rda}}) or Constrained Correspondence Analysis
- (\code{\link{cca}}).  The function is a special case of generic
- \code{\link{simulate}}, and works similarly as \code{simulate.lm}.  
-}
+ (\code{\link{rda}}), Constrained Correspondence Analysis
+ (\code{\link{cca}}) or distance-based RDA (\code{\link{capscale}}).
+ The function is a special case of generic \code{\link{simulate}}, and
+ works similarly as \code{simulate.lm}.  }
 
 \usage{
 \method{simulate}{rda}(object, nsim = 1, seed = NULL, indx = NULL, rank = "full", ...)
@@ -40,9 +40,13 @@
   fitting the constraints. Alternatively, the function can take a
   permutation index that is used to add permuted residuals
   (unconstrained component) to the fitted values. Raw data are used in
-  \code{\link{rda}} and internal Chi-square transformed data in
+  \code{\link{rda}}. Internal Chi-square transformed data in
   \code{\link{cca}} within the function, but the returned data frame is 
-  similar to the original input data. 
+  similar to the original input data. The simulation is performed on
+  internal metric scaling data in \code{\link{capscale}}, but the
+  function returns the Euclidean distances calculated from the simulated
+  data.  The simulation uses only the real components, and the imaginary
+  dimensions are ignored.
 }
 
 \value{ Returns a data frame with similar additional arguments on
@@ -50,7 +54,6 @@
 
 \author{Jari Oksanen}
 
-\note{ The function is not implemented for \code{\link{capscale}} objects.}
 
 \seealso{ \code{\link{simulate}} for the generic case and for
   \code{\link{lm}} objects. Functions \code{\link{fitted.rda}} and



More information about the Vegan-commits mailing list