From bb7f73f56a2c7c1e3396b7e16755edf714879680 Mon Sep 17 00:00:00 2001
From: Francesco Sabatini <francesco.sabatini@idiv.de>
Date: Tue, 5 May 2020 19:04:53 +0200
Subject: [PATCH] Relaxed round - fixed

---
 01b_MesobromionCluster.R | 47 ++++++++++++++++++++--------------------
 session.R                | 12 +++++-----
 2 files changed, 30 insertions(+), 29 deletions(-)

diff --git a/01b_MesobromionCluster.R b/01b_MesobromionCluster.R
index 74c33e5..bf025d4 100644
--- a/01b_MesobromionCluster.R
+++ b/01b_MesobromionCluster.R
@@ -116,6 +116,7 @@ Mesobromion <- function(species.path, traits.path, output, myfunction="get.corXY
   
   
   if(combinations=="sequential"){
+   
     if(!is.na(start.round)){
       print(paste("Load data from previous round=", start.round-1))
       load(file = paste(output, "_round_", start.round-1, ".RData", sep=""))
@@ -127,23 +128,37 @@ Mesobromion <- function(species.path, traits.path, output, myfunction="get.corXY
         allcomb.t <- lapply(1:ncol(traits), function(x){return(x)})
         nall <- length(allcomb.t)
         }  else {
-          best.row <- corXY.ci %>% 
-            filter(Trait.comb==best) 
-          upper <- best.row$q975
-          lower <- best.row$q025
+          if(nround >= relax.round){
+            best <- corXY.ci %>% 
+              filter(ntraits==(nround-1)) %>% 
+              arrange(desc(Coef.obs)) %>% 
+              slice(1) %>% 
+              pull(Trait.comb)
+            
+            new.best.row <- corXY.ci %>% 
+              filter(Trait.comb==best) 
+            upper <- new.best.row$q975
+            lower <- new.best.row$q025
+            print(paste("Assumptions relaxed - new best at round=", nround, "is trait combination", best))
+          } else {
+            best.row <- corXY.ci %>% 
+              filter(Trait.comb==best) 
+            upper <- best.row$q975
+            lower <- best.row$q025
+            }
           best.split <- as.numeric(unlist(strsplit(best, "_")))
-          max.inter.t <- nround-length(best.split)
+          max.inter.ti <- nround-length(best.split)
           #n.traits <- ncol(traits)
           list.traits <- as.numeric(traits.sign.alone)
           list.traits <- list.traits[-which(list.traits %in% best.split)] ## list of remaining traits without best
-          if(length(list.traits) < max.inter.t) {
+          if(length(list.traits) < max.inter.ti) {
             save(corXY.output, file = paste(output, ".RData", sep=""))
             if(ncores>1) {stopCluster(cl)}
             stop("Tested all combo of significant traits")
           }
           allcomb.t <- NULL
           #for(n.inter in 1:max.inter.t){
-          allcomb.t <- c(allcomb.t, combn(list.traits, max.inter.t, simplify=F))
+          allcomb.t <- c(allcomb.t, combn(list.traits, max.inter.ti, simplify=F))
           #}
           #add best to all combo
           allcomb.t <- lapply(allcomb.t, function(x){return(c(best.split, x))})
@@ -196,7 +211,7 @@ Mesobromion <- function(species.path, traits.path, output, myfunction="get.corXY
         print(paste("new best at nround=", nround, "is trait combination", best))
       }
       
-      if(nround>1 & nround < relax.round){
+      if(nround>1){
        better <- corXY.ci %>% 
           filter(ntraits==nround) %>% 
           filter(q025>upper) %>% 
@@ -219,22 +234,6 @@ Mesobromion <- function(species.path, traits.path, output, myfunction="get.corXY
           print(paste("new best at round=", nround, "is trait combination", best))
         } else {print(paste("no new best at round=", nround))}
       }
-      
-      if(nround>1 & nround > relax.round){
-        better <- corXY.ci %>% 
-          filter(ntraits==nround) %>% 
-          arrange(desc(Coef.obs)) %>% 
-          slice(1) %>% 
-          pull(Trait.comb)
-        
-        new.best.row <- corXY.ci %>% 
-            filter(Trait.comb==better) 
-          upper <- new.best.row$q975
-          lower <- new.best.row$q025
-          best <- better
-          print(paste("Assumptions relaxed - new best at round=", nround, "is trait combination", best))
-        }
-      
     print(paste("save intermediate results at round", nround))
     save(corXY.output, best, traits.sign.alone, corXY.ci, file = paste(output, "_round_",nround, ".RData", sep=""))
     }    
diff --git a/session.R b/session.R
index c62e048..f17c1c5 100644
--- a/session.R
+++ b/session.R
@@ -1,17 +1,19 @@
 
 species.path <- "_data/Mesobromion/species.out.10perc.txt"
 traits.path  <- "_data/Mesobromion/traits.out.10perc.txt"
-output  <- "test99"
+output  <- "_derived/Mesobromion/HIDDEN"
 myfunction <- "get.corXY.bootstrap"
-max.inter.t <- 3
+max.inter.t <- 7
 chunk.i <- NA
-nperm <- 20
+nperm <- 2
 ncores <- 1
 chunkn <- 3*ncores
 combinations <- "sequential"
-start.round <- 4
+start.round <- 6
+relax.round <- 6
 
 source("01b_MesobromionCluster.R")
 #Mesobromion(species.path, traits.path, output, myfunction, max.inter.t, chunkn, chunk.i, nperm)
-Mesobromion(species.path, traits.path, output, myfunction, combinations, start.round, max.inter.t, 
+Mesobromion(species.path, traits.path, output, myfunction, 
+            combinations, start.round, relax.round, max.inter.t, 
             chunkn, chunk.i, nperm, ncores)
-- 
GitLab