Skip to content

Commit

Permalink
Merge branch 'sppscores'
Browse files Browse the repository at this point in the history
  • Loading branch information
jarioksa committed Aug 13, 2024
2 parents 791e981 + 68fbfe0 commit c88b680
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 49 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ S3method(specslope, fitspecaccum)
S3method("sppscores<-", capscale)
S3method("sppscores<-", dbrda)
S3method("sppscores<-", metaMDS)
S3method("sppscores<-", monoMDS)
S3method("sppscores<-", wcmdscale)
# SSD: stats
S3method(SSD, cca)
# str: utils
Expand Down
14 changes: 5 additions & 9 deletions R/print.monoMDS.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,16 @@
}

`scores.monoMDS` <-
function(x, choices = NA, ...)
function(x, display = "sites", shrink = FALSE, choices, tidy = FALSE, ...)
{
if (any(is.na(choices)))
x$points
else {
choices <- choices[choices <= x$ndim]
x$points[, choices, drop = FALSE]
}
scores.metaMDS(x, display = display, shrink = shrink, choices, tidy = tidy,
...)
}

`plot.monoMDS` <-
function(x, choices = c(1,2), type = "t", ...)
function(x, display = "sites", choices = c(1,2), type = "t", ...)
{
ordiplot(x, display = "sites", choices = choices, type = type, ...)
ordiplot(x, display = display, choices = choices, type = type, ...)
}

`points.monoMDS` <-
Expand Down
47 changes: 32 additions & 15 deletions R/print.wcmdscale.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,43 @@
}

`scores.wcmdscale` <-
function(x, choices = NA, tidy = FALSE, ...)
function(x, choices = NA, display = "sites", tidy = FALSE, ...)
{
p <-
if (any(is.na(choices))) {
x$points
} else {
choices <- choices[choices <= NCOL(x$points)]
x$points[, choices, drop = FALSE]
if ("species" %in% names(x))
display <- match.arg(display, c("sites", "species"), several.ok = TRUE)
else
display <- match.arg(display, "sites")
p <- list()
for (sco in display) {
if (sco == "sites")
p[[sco]] <- x$points
else
p[[sco]] <- x[[sco]]
if (!anyNA(choices)) {
choices <- choices[choices <= NCOL(p[[sco]])]
p[[sco]] <- p[[sco]][, choices, drop = FALSE]
}
if (is.null(rownames(p)))
rownames(p) <- as.character(seq_len(nrow(p)))
if (tidy) {
p <- data.frame(p, "scores" = "sites", "label" = rownames(p),
"weight" = weights(x))
if (is.null(rownames(p[[sco]])))
rownames(sco) <- as.character(seq_len(nrow(p[[sco]])))
if (tidy) {
wts <- if(sco == "sites") weights(x) else NA
p[[sco]] <- data.frame(p[[sco]], "score" = sco,
"label" = rownames(p[[sco]]),
weights = wts)
}
}
if (tidy && length(display) > 1) {
p <- do.call("rbind", p)
rownames(p) <- p$label
}
p
if (length(p) == 1)
p[[1]]
else
p
}

`plot.wcmdscale` <-
function(x, choices = c(1,2), type = "t", ...)
function(x, choices = c(1,2), display = "sites", type = "t", ...)
{
ordiplot(x, display = "sites", choices = choices, type = type, ...)
ordiplot(x, display = display, choices = choices, type = type, ...)
}
4 changes: 3 additions & 1 deletion R/scores.metaMDS.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
colnames(sites) <- paste0("NMDS", choices)
out$sites <- sites
}
if ("species" %in% display && !is.null(x$species) && !all(is.na(x$species))) {
if ("species" %in% display) {
if (is.null(x$species) || all(is.na(x$species)))
stop("ordination object has no 'species' scores")
species <- x$species[, choices, drop=FALSE]
colnames(species) <- paste0("NMDS", choices)
if (shrink) {
Expand Down
45 changes: 45 additions & 0 deletions R/sppscores.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,51 @@
object
}

## monoMDS

`sppscores<-.monoMDS` <-
function(object, value)
{
wa <- wascores(object$points, value, expand = TRUE)
attr(wa, "data") <- deparse(substitute(value))
object$species <- wa
object
}


## sppscores<-.wcmdscale works with the simple case of row
## weights. Verified to give the same result as RDA with duplicated
## rows when using the number of duplicates as row weights in
## wcmdscale of Eucliden distances (per sqrt(n-1)). However, does not
## give the same results as CA in wcmdscale(vegdist(x, "chisq"),
## w=rowSums(x)/sum(x)).

`sppscores<-.wcmdscale` <-
function(object, value)
{
value <- as.matrix(value)
w <- weights(object)
spp <- crossprod(value, w * object$points)
spp <- decostand(spp, "normalize", MARGIN = 2)
attr(spp, "vdata") <- deparse(substitute(value))
object$species <- spp
object
}

## This code would give CA species scores for
## wcmdscale(vegdist(x, "chisq"), w = rowSums(x)/sum(x), eig=TRUE)
## or CA as weighted PCoA of Chi-square distances

## `sppscores<-.wcmdscale` <-
## function(object, value)
## {
## value <- as.matrix(value)
## value <- decostand(value, "total", MARGIN=2) # for sum(w) = 1
## spp <- crossprod(value, object$points) # wa: sum(w*x) with sum(w)=1
## spp <- sweep(spp, 2, object$eig, "/") # for wascores(..., expand=T)
## object$species <- spp
## object
## }
## the main purpose of accessor function is to provide nicer command
## autocompletion and cross-references in help, and of course, to tell
## that it is not implemented (and may never be)
Expand Down
22 changes: 19 additions & 3 deletions man/monoMDS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ monoMDS(dist, y, k = 2, model = c("global", "local", "linear", "hybrid"),
threshold = 0.8, maxit = 200, weakties = TRUE, stress = 1,
scaling = TRUE, pc = TRUE, smin = 1e-4, sfgrmin = 1e-7,
sratmax=0.999999, ...)
\method{scores}{monoMDS}(x, choices = NA, ...)
\method{plot}{monoMDS}(x, choices = c(1,2), type = "t", ...)
\method{scores}{monoMDS}(x, display = "sites", shrink = FALSE, choices,
tidy = FALSE, ...)
\method{plot}{monoMDS}(x, display = "sites", choices = c(1,2), type = "t", ...)
\method{points}{monoMDS}(x, choices = c(1,2), select, ...)
\method{text}{monoMDS}(x, labels, choices = c(1,2), select, ...)
}
Expand Down Expand Up @@ -65,6 +66,20 @@ monoMDS(dist, y, k = 2, model = c("global", "local", "linear", "hybrid"),
\item{x}{A \code{monoMDS} result.}
\item{display}{Kind of scores. Normally there are only scores for
\code{"sites"}, but \code{"species"} scores can be added with
\code{\link{sppscores}.}}
\item{shrink}{Shrink back species scores if they were expanded in
\code{\link{wascores}}.}
\item{tidy}{Return scores that are compatible with \CRANpkg{ggplot2}:
all scores are in a single \code{data.frame}, score type is
identified by factor variable \code{code} (\code{"sites"} or
\code{"species"}), the names by variable \code{label}. These scores
are incompatible with conventional \code{plot} functions, but they can
be used in \pkg{ggplot2}.}
\item{choices}{Dimensions returned or plotted. The default \code{NA}
returns all dimensions. }
Expand Down Expand Up @@ -191,7 +206,8 @@ monoMDS(dist, y, k = 2, model = c("global", "local", "linear", "hybrid"),
are returned in item \code{points} (function \code{scores} extracts
these results), and the stress in item \code{stress}. In addition,
there is a large number of other items (but these may change without
notice in the future releases). }
notice in the future releases). There are no species scores, but these
can be added with \code{\link{sppscores}} function.}

\references{

Expand Down
41 changes: 23 additions & 18 deletions man/sppscores.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
\alias{sppscores<-.dbrda}
\alias{sppscores<-.capscale}
\alias{sppscores<-.metaMDS}
\alias{sppscores<-.monoMDS}
\alias{sppscores<-.wcmdscale}

\title{
Add or Replace Species Scores in Distance-Based Ordination
Expand All @@ -12,12 +14,13 @@ Add or Replace Species Scores in Distance-Based Ordination
\description{

Distance-based ordination (\code{\link{dbrda}},
\code{\link{capscale}}, \code{\link{metaMDS}}) have no information
on species, but some methods may add species scores if community
data were available. However, the species scores may be missing (and
they always are in \code{\link{dbrda}}), or they may not have a
close relation to used dissimilarity index. This function will add
the species scores or replace the existing species scores in
\code{\link{capscale}}, \code{\link{metaMDS}}, \code{\link{monoMDS}},
\code{\link{wcmdscale}}) has no information on species, but some
methods may add species scores if community data were
available. However, the species scores may be missing (and they always
are in \code{\link{dbrda}} and \code{\link{wcmdscale}}), or they may
not have a close relation to used dissimilarity index. This function
will add the species scores or replace the existing species scores in
distance-based methods.

}
Expand All @@ -27,7 +30,9 @@ sppscores(object) <- value
}

\arguments{
\item{object}{Ordination result.}
\item{object}{Ordination result from \code{\link{capscale}},
\code{\link{dbrda}}, \code{\link{metaMDS}}, \code{\link{monoMDS}},
\code{\link{pco}} or \code{\link{wcmdscale}}.}
\item{value}{Community data to find the species scores.}
}

Expand All @@ -39,7 +44,7 @@ sppscores(object) <- value
information after the analysis to help the interpretation of
results. Some ordination methods (\code{\link{capscale}},
\code{\link{metaMDS}}) can supplement the species scores during the
analysis if community data was available in the analysis.
analysis if community data were available in the analysis.

In \code{\link{capscale}} the species scores are found by projecting
the community data to site ordination (linear combination scores),
Expand All @@ -58,16 +63,17 @@ sppscores(object) <- value
squareroot transformation to damp down the effect of squared
differences (see Examples).

Function \code{\link{dbrda}} never finds the species scores, but it
is mathematically similar to \code{\link{capscale}}, and similar
rules should be followed when supplementing the species scores.
Functions \code{\link{dbrda}} and \code{\link{wcmdscale}} never find
the species scores, but they mathematically similar to
\code{\link{capscale}}, and similar rules should be followed when
supplementing the species scores.

Function \code{\link{metaMDS}} uses weighted averages
(\code{\link{wascores}}) to find the species scores. These have a
better relationship with most dissimilarities than the projection
scores used in metric ordination, but similar transformation of the
community data should be used both in dissimilarities and in species
scores.
Functions for species scores in \code{\link{metaMDS}} and
\code{\link{monoMDS}} use weighted averages (\code{\link{wascores}})
to find the species scores. These have better relationship with most
dissimilarities than the projection scores used in metric ordination,
but similar transformation of the community data should be used both
in dissimilarities and in species scores.

}

Expand All @@ -78,7 +84,6 @@ sppscores(object) <- value

}


\author{
Jari Oksanen
}
Expand Down
10 changes: 7 additions & 3 deletions man/wcmdscale.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
\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, tidy = FALSE, ...)
\method{plot}{wcmdscale}(x, choices = c(1, 2), display = "sites", type = "t", ...)
\method{scores}{wcmdscale}(x, choices = NA, display = "sites", tidy = FALSE, ...)
}
\description{
Weighted classical multidimensional scaling,
Expand All @@ -39,6 +39,9 @@ wcmdscale(d, k, eig = FALSE, add = FALSE, x.ret = FALSE, w)
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{display}{Kind of scores. Normally only \code{"sites"} are
available, but \code{"species"} can be supplemented with
\code{\link{sppscores}}.}
\item{type}{Type of graph which may be \code{"t"}ext, \code{"p"}oints
or \code{"n"}one.}
\item{tidy}{Return scores that are compatible with \CRANpkg{ggplot2}:
Expand Down Expand Up @@ -119,7 +122,8 @@ wcmdscale(d, k, eig = FALSE, add = FALSE, x.ret = FALSE, w)
\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
\code{\link{stressplot.wcmdscale}} are some specific methods. Species
scores can be added to the result with \code{\link{sppscores}}. Other
multidimensional scaling methods are \code{\link{monoMDS}}, and
\code{\link[MASS]{isoMDS}} and \code{\link[MASS]{sammon}} in package
\pkg{MASS}. }
Expand Down

0 comments on commit c88b680

Please sign in to comment.