Skip to content

Commit

Permalink
Merge branch 'vegdist-na'
Browse files Browse the repository at this point in the history
  • Loading branch information
jarioksa committed Jun 25, 2024
2 parents c234296 + 21bb8d9 commit f998fa8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
11 changes: 10 additions & 1 deletion R/decostand.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,26 @@
x <- sweep(x, MARGIN, ran, "/")
attr <- list("min" = tmp, "range" = ran, "margin" = MARGIN)
}, rank = {
wasNA <- is.na(x)
if (any(wasNA) && !na.rm)
stop("missing values are not allowed with 'na.rm = FALSE'")
if (missing(MARGIN)) MARGIN <- 1
x[x==0] <- NA
x <- apply(x, MARGIN, rank, na.last = "keep")
if (MARGIN == 1) # gives transposed x
x <- t(x)
x[is.na(x)] <- 0
if(any(wasNA))
x[wasNA] <- NA
attr <- list("margin" = MARGIN)
}, rrank = {
if (missing(MARGIN)) MARGIN <- 1
x <- decostand(x, "rank", MARGIN = MARGIN)
x <- decostand(x, "rank", MARGIN = MARGIN, na.rm = na.rm)
if (na.rm && any(wasNA <- is.na(x)))
x[wasNA] <- 0
x <- sweep(x, MARGIN, specnumber(x, MARGIN = MARGIN), "/")
if (any(wasNA))
x[wasNA] <- NA
attr <- list("margin" = MARGIN)
}, standardize = {
if (!missing(MARGIN) && MARGIN == 1)
Expand Down
4 changes: 2 additions & 2 deletions R/veganMahatrans.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
### input must be correct: 'x' must be a centred matrix (not a
### data.frame, not raw data).
`veganMahatrans` <-
function (x, s2, tol = sqrt(.Machine$double.eps))
function (x, s2, tol = sqrt(.Machine$double.eps), na.rm = FALSE)
{
if (missing(s2))
s2 <- cov(x)
s2 <- cov(x, use = if(na.rm) "pairwise.complete.obs" else "all.obs")
e <- eigen(s2, symmetric = TRUE)
k <- e$values > max(tol, tol * e$values[1L])
sisqr <- e$vectors[,k, drop=FALSE] %*%
Expand Down
6 changes: 3 additions & 3 deletions R/vegdist.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@
warning("results may be meaningless because data have negative entries
in method ",
dQuote(inm))
if (method %in% c(11,18) && any(colSums(x) == 0))
if (method %in% c(11,18) && any(colSums(x) == 0, na.rm = TRUE))
warning("data have empty species which influence the results in
method ",
dQuote(inm))
if (method == 6) # gower, but no altGower
x <- decostand(x, "range", 2, na.rm = TRUE, ...)
if (method == 16) # mahalanobis
x <- veganMahatrans(scale(x, scale = FALSE))
x <- veganMahatrans(scale(x, scale = FALSE), na.rm = na.rm)
if (method == 18) # chisq
x <- decostand(x, "chi.square")
x <- decostand(x, "chi.square", na.rm = na.rm)
if (method == 21) # aitchison
x <- decostand(x, "clr", ...) # dots to pass possible pseudocount
if (method == 22) # robust.aitchison
Expand Down
4 changes: 3 additions & 1 deletion man/decostand.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ decobackstand(x, zap = TRUE)
subsets of data. The dimensions of \code{MARGIN} must match with
\code{x}. }
\item{logbase}{The logarithm base used in \code{method = "log"}.}
\item{na.rm}{Ignore missing values in row or column standardizations.}
\item{na.rm}{Ignore missing values in row or column
standardizations. The \code{NA} values remain as \code{NA}, but they
are ignored in standardization of other values.}
\item{zap}{Make near-zero values exact zeros to avoid negative
values and exaggerated estimates of species richness.}
\item{\dots}{Other arguments to the function (ignored).}
Expand Down
5 changes: 3 additions & 2 deletions man/vegan-internal.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ getPermuteMatrix(perm, N, strata = NULL)
howHead(x, ...)
pasteCall(call, prefix = "Call:")
veganCovEllipse(cov, center = c(0, 0), scale = 1, npoints = 100)
veganMahatrans(x, s2, tol = sqrt(.Machine$double.eps))
veganMahatrans(x, s2, tol = sqrt(.Machine$double.eps), na.rm = FALSE)
hierParseFormula(formula, data)
GowerDblcen(x, na.rm = TRUE)
addLingoes(d)
Expand Down Expand Up @@ -95,7 +95,8 @@ addCailliez(d)
distances are Mahalanobis distances. The input data \code{x} must be
a matrix centred by columns, and \code{s2} its covariance matrix. If
\code{s2} is not given, covariance matrix is found from \code{x}
within the function.
within the function. If \code{na.rm = TRUE}, \code{\link{cov}} is
called with \code{use = "pairwise.complete.obs"}.

\code{hierParseFormula} returns a list of one matrix (left hand side)
and a model frame with factors representing hierarchy levels
Expand Down
3 changes: 2 additions & 1 deletion man/vegdist.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
\item{diag}{Compute diagonals. }
\item{upper}{Return only the upper diagonal. }
\item{na.rm}{Pairwise deletion of missing observations when
computing dissimilarities.}
computing dissimilarities (but some dissimilarities may still be
\code{NA}, although calculation is handled).}
\item{\dots}{Other parameters. These are ignored, except in
\code{method ="gower"} which accepts \code{range.global} parameter of
\code{\link{decostand}}, and in \code{method="aitchison"}, which
Expand Down

0 comments on commit f998fa8

Please sign in to comment.