Skip to content
Snippets Groups Projects
Commit ed9aff20 authored by Francesco Sabatini's avatar Francesco Sabatini
Browse files

Fixing problem with matrix.x when species have all NAs

parent 0bc2be36
No related branches found
No related tags found
No related merge requests found
...@@ -37,7 +37,7 @@ get.corXY.bootstrap <- function(comm, traits, trait.sel="all", bootstrap=199){ ...@@ -37,7 +37,7 @@ get.corXY.bootstrap <- function(comm, traits, trait.sel="all", bootstrap=199){
## CAUTION - function below doesn't work as expected ## 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) # 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)) W.beals <- as.data.frame(beals(comm, include=T, type=2))
# permute traits # permute traits
### Create vector of species to resample from, which exclude species with NAs ### 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 ...@@ -107,6 +107,7 @@ matrix.x <- function (comm, traits, scale = TRUE, ranks = TRUE, ord, notificatio
warning("Warning: NA in community data", call. = FALSE) 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) x.NA <- apply(traits, 2, is.na)
if (notification) { if (notification) {
if (any(x.NA)) { if (any(x.NA)) {
...@@ -134,6 +135,16 @@ matrix.x <- function (comm, traits, scale = TRUE, ranks = TRUE, ord, notificatio ...@@ -134,6 +135,16 @@ matrix.x <- function (comm, traits, scale = TRUE, ranks = TRUE, ord, notificatio
warning("Warning: NA in matrix U", call. = FALSE) 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.u[u.NA] <- 0
matrix.X <- matrix.w %*% matrix.u matrix.X <- matrix.w %*% matrix.u
matrix.X <- sweep(matrix.X, 1, rowSums(matrix.X), "/") ## RE-SCALE to avoid problems propagating from NAs matrix.X <- sweep(matrix.X, 1, rowSums(matrix.X), "/") ## RE-SCALE to avoid problems propagating from NAs
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment