From d40f5b14f0b9741ee120e1405207c1ae5876c015 Mon Sep 17 00:00:00 2001
From: Francesco Sabatini <francesco.sabatini@idiv.de>
Date: Wed, 15 Apr 2020 09:54:38 +0200
Subject: [PATCH] Fixed bug in 99, started alignign 01

---
 01_Mesobromion.R      | 20 ++++++++++++--------
 99_HIDDEN_functions.R |  4 ++--
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/01_Mesobromion.R b/01_Mesobromion.R
index 238aae5..9d6bf71 100644
--- a/01_Mesobromion.R
+++ b/01_Mesobromion.R
@@ -170,20 +170,25 @@ traits <- traits %>%
 
 #### ## Import output #### 
 
-myfilelist <- list.files(path="_derived/Mesobromion/", pattern="HIDDEN_[0-9]+.RData", full.names = T)
+myfilelist <- list.files(path="_derived/Mesobromion/", pattern="HIDDEN(\\.[A-Za-z]+|_[A-Za-z]+)_[0-9]+.RData", full.names = T)
 corXY <- NULL
-corXY.perm <- NULL
-corXY.bootstr <- NULL
+#corXY.perm <- NULL
+#corXY.bootstr <- NULL
 for(ff in myfilelist){
   index <- as.numeric(regmatches(ff, gregexpr("[[:digit:]]+", ff))[[1]])
   load(ff)
-  corXY <- rbind(corXY, cor.obs)
-  corXY.perm <- rbind(corXY.perm, cor.perm)
-  corXY.bootstr <- rbind(corXY.bootstr, cor.bootstr)
+  corXY <- bind_rows(corXY, cor.obs)
+  #corXY.perm <- rbind(corXY.perm, cor.perm)
+  #corXY.bootstr <- rbind(corXY.bootstr, cor.bootstr)
   print(index)
 }
 
 
+dataFiles = map(myfilelist, load) 
+dat = bind_rows(dataFiles)
+
+
+
 diffs <- corXY.bootstr - corXY.perm
 sign <- apply(diffs, MARGIN=1, function(x){sum(x>0)>190})
 sign <- data.frame(Trait.comb=names(sign), sign=sign)
@@ -293,8 +298,7 @@ ggplot(data=mydata) +
 
 
 traits.sign <- traits %>% 
-  rownames_to_column("species0") %>% 
-  dplyr::select(traits.sign.alone)
+  dplyr::select(species0, traits.sign.alone)
 write_delim(traits.sign, path="_data/Mesobromion/traits.out.10perc.sign.txt", delim="\t")
 
 break()
diff --git a/99_HIDDEN_functions.R b/99_HIDDEN_functions.R
index 354b3b5..00659b1 100644
--- a/99_HIDDEN_functions.R
+++ b/99_HIDDEN_functions.R
@@ -47,8 +47,7 @@ get.corXY <- function(comm, traits, trait.sel="all", stat=c("mantel", "RV", "pro
 }
 
 
-### Cor XY bootstrap
-#### Function 1 - CorXY ####
+#### Function 1b - CorXY bootstrap####
 get.corXY.bootstrap <- function(comm, traits, trait.sel="all", stat="RV", bootstrap=199){
   if(identical(trait.sel, "all")) {trait.sel <- 1:ncol(traits)}
   ii <- trait.sel
@@ -77,6 +76,7 @@ get.corXY.bootstrap <- function(comm, traits, trait.sel="all", stat="RV", bootst
   index.bootstr <- lapply(1:bootstrap, function(x){sample(1:n.sites, replace=T)})
   for(b in 1:bootstrap){
     RV.tmp <- RV.rtest(W.beals[index.bootstr[[b]],], as.data.frame(syn.out.tmp)[index.bootstr[[b]],])
+    syn.out.perm.tmp <- matrix.x(comm=comm, traits=traits[index.traits[[bootstrap]],ii,drop=F], scale=T)$matrix.X
     RV.perm.tmp <- RV.rtest(W.beals[index.bootstr[[b]],], as.data.frame(syn.out.perm.tmp)[index.bootstr[[b]],])
     corXY <- rbind(corXY,
                    data.frame(Trait.comb=lab.tmp, bootstr.n=b, Test="RV", Coef.obs=RV.tmp$obs, Coef.perm=RV.perm.tmp$obs))
-- 
GitLab