From ed9aff201e4de729758a8dcf46ce90948ecc2c9d Mon Sep 17 00:00:00 2001 From: Francesco Sabatini <francesco.sabatini@idiv.de> Date: Fri, 28 Aug 2020 13:33:56 +0200 Subject: [PATCH] Fixing problem with matrix.x when species have all NAs --- 99_HIDDEN_functions.R | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/99_HIDDEN_functions.R b/99_HIDDEN_functions.R index c5c919a..0c4da10 100644 --- a/99_HIDDEN_functions.R +++ b/99_HIDDEN_functions.R @@ -37,7 +37,7 @@ get.corXY.bootstrap <- function(comm, traits, trait.sel="all", bootstrap=199){ ## CAUTION - function below doesn't work as expected # binary.traits <- which(apply(traits[,ii,drop=F], MARGIN=2, function(x)( all(na.omit(x) %in% 0:1) ))==T) - syn.out.tmp <- matrix.x(comm=comm, traits=traits[,ii,drop=F], scale=T)$matrix.X #, asym.bin=binary.traits + syn.out.tmp <- SYNCSA::matrix.x(comm=comm, traits=traits[,ii,drop=F], scale=T)$matrix.X #, asym.bin=binary.traits W.beals <- as.data.frame(beals(comm, include=T, type=2)) # permute traits ### Create vector of species to resample from, which exclude species with NAs @@ -107,6 +107,7 @@ matrix.x <- function (comm, traits, scale = TRUE, ranks = TRUE, ord, notificatio warning("Warning: NA in community data", call. = FALSE) } } + #x.all.NA <- apply(traits, 1, function(x){!sum(is.na(x))==0}) x.NA <- apply(traits, 2, is.na) if (notification) { if (any(x.NA)) { @@ -134,6 +135,16 @@ matrix.x <- function (comm, traits, scale = TRUE, ranks = TRUE, ord, notificatio warning("Warning: NA in matrix U", call. = FALSE) } } + ## re-standardize matrix.w, replace abundance of species which have no trait information with 0 + u.all.NA <- which(rowSums(is.na(matrix.u))==ncol(matrix.u)) + if (notification) { + if (length(u.all.NA)>0) { + warning("Warning: For species without trait information, replace the abundance with 0 in matrix.w, and re-standardize by plot totals", call. = FALSE) + } + } + matrix.w[,u.all.NA] <- 0 + matrix.w <- sweep(matrix.w, 1, rowSums(matrix.w), "/") + matrix.u[u.NA] <- 0 matrix.X <- matrix.w %*% matrix.u matrix.X <- sweep(matrix.X, 1, rowSums(matrix.X), "/") ## RE-SCALE to avoid problems propagating from NAs -- GitLab