From 4fa25d73e83f612f5f7cf0e670c27e1041892d0e Mon Sep 17 00:00:00 2001
From: Francesco Sabatini <francesco.sabatini@idiv.de>
Date: Fri, 21 Aug 2020 08:35:08 +0200
Subject: [PATCH] Fix 99 for NAs in traits

---
 99_HIDDEN_functions.R | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/99_HIDDEN_functions.R b/99_HIDDEN_functions.R
index c5c919a..cb45b92 100644
--- a/99_HIDDEN_functions.R
+++ b/99_HIDDEN_functions.R
@@ -33,12 +33,6 @@ get.corXY.bootstrap <- function(comm, traits, trait.sel="all", bootstrap=199){
   
   ## caution
   ## ALL columns with only 0-1 values are AUTOMATICALLY considered as asym.bin sensu FD:gowdis
-  ##get all columns with binary variables
-  ## 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
-  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
   traits.ii <- traits[,ii, drop=F]
@@ -53,12 +47,15 @@ get.corXY.bootstrap <- function(comm, traits, trait.sel="all", bootstrap=199){
     out[is.na(out)] <- sample(toresample, replace=F)
     return(out)
   })
-  #index.traits <- lapply(1:(bootstrap+1), function(x){sample(1:nrow(traits), replace=F)})
-  syn.out.perm.tmp <- matrix.x(comm=comm, traits=traits[index.traits[[bootstrap+1]],ii,drop=F], 
-                               scale=T)$matrix.X #, asym.bin=binary.traits
-  
+  ##calculate corXY on observed values using all plots
+  syn.out.tmp <- matrix.x(comm=comm, traits=traits.ii, scale=T)$matrix.X #, asym.bin=binary.traits
+  W.beals <- as.data.frame(beals(comm, include=T, type=2))
   (RD.tmp <- dcor(W.beals, as.data.frame(syn.out.tmp))^2)
+  ##calculate corresponding permuted corXY using all plots
+  syn.out.perm.tmp <- matrix.x(comm=comm, traits=traits.ii[index.traits[[bootstrap+1]],], 
+                               scale=T)$matrix.X #, asym.bin=binary.traits
   (RD.perm.tmp <- dcor(W.beals, as.data.frame(syn.out.perm.tmp))^2)
+  ## calculate corXY for each bootstrap run
   corXY <- NULL
   corXY <- data.frame(Trait.comb=lab.tmp, bootstr.n=0, Test="RD", 
                             Coef.obs=RD.tmp, Coef.perm=RD.perm.tmp)
@@ -66,7 +63,7 @@ get.corXY.bootstrap <- function(comm, traits, trait.sel="all", bootstrap=199){
   index.bootstr <- lapply(1:bootstrap, function(x){sample(1:n.sites, replace=T)})
   for(b in 1:bootstrap){
     RD.tmp <- dcor(W.beals[index.bootstr[[b]],], as.data.frame(syn.out.tmp)[index.bootstr[[b]],])^2
-    syn.out.perm.tmp <- matrix.x(comm=comm, traits=traits[index.traits[[b]],ii,drop=F], 
+    syn.out.perm.tmp <- matrix.x(comm=comm, traits=traits.ii[index.traits[[b]],], 
                                  scale=T)$matrix.X #, asym.bin=binary.traits
     RD.perm.tmp <- dcor(W.beals[index.bootstr[[b]],], as.data.frame(syn.out.perm.tmp)[index.bootstr[[b]],])^2
     corXY <- rbind(corXY,
-- 
GitLab