[Vegan-commits] r1575 - pkg/vegan/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Apr 6 18:21:47 CEST 2011


Author: jarioksa
Date: 2011-04-06 18:21:47 +0200 (Wed, 06 Apr 2011)
New Revision: 1575

Modified:
   pkg/vegan/R/metaMDSiter.R
Log:
fix 'previous.best' in metaMDS

Modified: pkg/vegan/R/metaMDSiter.R
===================================================================
--- pkg/vegan/R/metaMDSiter.R	2011-04-06 15:32:27 UTC (rev 1574)
+++ pkg/vegan/R/metaMDSiter.R	2011-04-06 16:21:47 UTC (rev 1575)
@@ -19,45 +19,34 @@
         if (inherits(previous.best, "metaMDS") ||
             is.list(previous.best) &&
             all(c("points", "stress") %in% names(previous.best))) {
-            ## real "previous best"
-            if (NCOL(previous.best$points) == k) {
-                s0 <- previous.best
-                if (trace) 
-                    cat("Starting from a previous solution\n")
-            } else {
-                init <- previous.best$points
-                nc <- NCOL(init)
-                if (nc > k)
-                    init <- init[, 1:k, drop = FALSE]
-                else   # nc < k
-                    for (i in 1:(k-nc))
-                        init <- cbind(init, runif(NROW(init), -0.1, 0.1))
-                # evaluate isoMDS with stress
-                s0 <- switch(engine,
-                             "monoMDS" = monoMDS(dist, init, k = k, maxit = 0,
-                             ...),
-                             "isoMDS" = isoMDS(dist, init, k = k, maxit = 0))
-                # zero 'tries': this was a new start
-                if (inherits(previous.best, "metaMDS"))
-                    previous.best$tries <- 0
-                if (trace)
-                    cat(gettextf("Starting from %d-dimensional solution\n", nc))
-            }
-        } else if (is.matrix(previous.best) || is.data.frame(previous.best)) {
-            s0 <- switch(engine,
-                         "monoMDS" = monoMDS(dist, previous.best, k=k, maxit = 0),
-                         "isoMDS" = isoMDS(dist, previous.best, k = k, maxit = 0)
-                         )
+            ## Previous best may come from another 'engine' or
+            ## 'model': extract its 'points' and use as an initial
+            ## configuration with 'maxit = 0' to evaluate the stress
+            ## in current case, or take a matrix as configuration.
+            init <- previous.best$points
+            nc <- NCOL(init)
+            if (nc > k)
+                init <- init[, 1:k, drop = FALSE]
+            else if (nc < k)
+                for (i in 1:(k-nc))
+                    init <- cbind(init, runif(NROW(init), -0.1, 0.1))
+            ## zero 'tries' if started from different no. of dims
+            if (inherits(previous.best, "metaMDS") && nc != k)
+                previous.best$tries <- 0
             if (trace)
-                cat("Starting from supplied configuration\n")
-        } else { # an error!
-            stop("'previous.best' of unknown kind")
+                cat(gettextf("Starting from %d-dimensional configuration\n", nc))
+        } else {
+            init <- as.matrix(previous.best)
         }
-    } # No previous best:
-    else
+        ## evaluate stress
         s0 <- switch(engine,
-                     "monoMDS" = monoMDS(dist, k = k, ...),
-                     "isoMDS" = isoMDS(dist, k = k, trace = isotrace))
+                     "monoMDS" = monoMDS(dist, y = init, k = k, maxit = 0, ...),
+                     "isoMDS" = isoMDS(dist, y = init, k = k, maxit = 0))
+    }
+    ## no previous.best: start with cmdscale
+    s0 <- switch(engine,
+                 "monoMDS" = monoMDS(dist, y = cmdscale(dist), k = k, ...),
+                 "isoMDS" = isoMDS(dist, k = k, trace = isotrace))
     if (trace) 
         cat("Run 0 stress", s0$stress, "\n")
     tries <- 0



More information about the Vegan-commits mailing list