diff --git a/00_Mesobromion_DataPreparation.R b/00_Mesobromion_DataPreparation.R index 682828e711ce5f601744723a8798bc143da14763..391b9a4d33ff2847851acd95f6d1fd5fc9783c67 100644 --- a/00_Mesobromion_DataPreparation.R +++ b/00_Mesobromion_DataPreparation.R @@ -12,9 +12,10 @@ source("99_HIDDEN_functions.R") ##### PART 1 #### #### 1. traits data #### -traits0 <- read_delim("_data/Mesobromion/traits3_cov.txt", delim =";", ## manually corrected vowels with umlaut +traits0 <- read_delim("_data/Mesobromion/traits4.csv", delim =",", ## manually corrected vowels with umlaut col_names = T, locale = locale(encoding = 'UTF-8')) %>% column_to_rownames("X1") %>% + dplyr::select(-starts_with("X")) %>% dplyr::select(colnames(.)[which(colSums(., na.rm=T)!=0)]) %>% dplyr::select(!starts_with("BLM")) %>% dplyr::select(!starts_with("ZWT")) %>% @@ -30,15 +31,22 @@ traits0 <- read_delim("_data/Mesobromion/traits3_cov.txt", delim =";", ## manua mutate(species=gsub(pattern="_", replacement = " ", x = species)) %>% mutate(species=gsub(pattern=" agg | x | spec$| agg$| s | Sec | ", replacement=" ", x=species)) %>% mutate(species=gsub(pattern=" $", replacement = "", x = species)) %>% - mutate(species=ifelse(is.na(word(species, 1, 2)), species, word(species, 1, 2))) -dim(traits0) #907 obs. of 75 variables: - - - -#keep only traits with >=88 completeness + mutate(species=ifelse(is.na(word(species, 1, 2)), species, word(species, 1, 2))) %>% + ungroup() +dim(traits0) #902 obs. of 67 variables: + +## remove species with NAs +species.to.remove <- traits0 %>% + filter(all_of(is.na({.} %>% dplyr::select(-species, -species0)))) %>% + pull(species0) traits0 <- traits0 %>% - dplyr::select_if(~mean(!is.na(.)) >= 0.88) -dim(traits0)# 902 x 67 + filter(!species0 %in% species.to.remove) + + +# #keep only traits with >=88 completeness +# traits0 <- traits0 %>% +# dplyr::select_if(~mean(!is.na(.)) >= 0.88) +# dim(traits0)# 902 x 67 @@ -88,8 +96,8 @@ all.traits <- traits0 %>% by="species") traits <- all.traits %>% filter(!is.na(LeafArea)) -dim(all.traits) #[1] 902 82 -dim(traits) #[1] 801 82 +dim(all.traits) #[1] 898 82 +dim(traits) #[1] 799 82 @@ -114,22 +122,23 @@ env.all <- env ### 3. Import species data #### # columns in species correspond to those in env # there is no PlotObservationID (yet) -species0 <- read_csv("_data/Mesobromion/GVRD_Mes2_proz2.csv", locale = locale(encoding = 'latin1')) -dim(species0) #6868 obs. of 903 variables: -rownames(species0) <- env0$RELEVE_NR +species0 <- read_csv("_data/Mesobromion/GVRD_Mes2_proz2.csv", locale = locale(encoding = 'latin1')) %>% + dplyr::select(-species.to.remove) %>% + mutate(RELEVE_NR=env0$RELEVE_NR) +dim(species0) #6868 obs. of 899 variables: + ## select only plots already selected in env species <- env %>% dplyr::select(RELEVE_NR) %>% - left_join(species0 %>% - mutate(RELEVE_NR=env0$RELEVE_NR), + left_join(species0, by="RELEVE_NR") %>% column_to_rownames("RELEVE_NR") %>% ## delete species not appearing in any plot dplyr::select(colnames(.)[which(colSums(.)!=0)]) #dplyr::select(traits$species0) -dim(species) # [1] 5810 877 +dim(species) # [1] 5810 873 releve08trait <- species %>% rownames_to_column("RELEVE_NR") %>% @@ -310,7 +319,6 @@ species.cov <- species.cov %>% dplyr::select(-sumVar) dim(species.cov) #[1] 581 510 - ## export write_delim(species.pa, path="_data/Mesobromion/species.v2.10perc.pa.txt", delim="\t") write_delim(species.cov, path="_data/Mesobromion/species.v2.10perc.cov.txt", delim="\t") @@ -323,18 +331,16 @@ write_delim(species %>% path="_data/Mesobromion/ReleveList.txt", delim="\t") - ##check for species without trait info traits %>% filter_at(.vars=vars(-"species0"), all_vars(is.na(.))) %>% nrow() ## [1] no species with no trait info ## -## simply because those 7 species without TRY data were excluded already traits %>% filter_at(.vars=vars(-"species0"), any_vars(is.na(.))) %>% - nrow() ## [1] 109 # species with at least 1 NA in traits + nrow() ## [1] 36 # species with at least 1 NA in traits #### CORRELATION BETWEEN FUZZY WEIGHTED AND BEALS MATRICES @@ -350,7 +356,7 @@ traits %>% source("01b_MesobromionCluster.R") #### 1. Traits individually significant for COVER data#### na.exclude=T ######## traits <- read_delim("_data/Mesobromion/traits.v2.10perc.txt", delim="\t") -myfilelist <- list.files(path="_derived/Mesobromion/Cover", pattern="HIDDENcov_[0-9]+_.RData", full.names = T) +myfilelist <- list.files(path="_derived/Mesobromion/Cover", pattern="HIDDENcov-nona2_[0-9]+_.RData", full.names = T) dataFiles = purrr::map(myfilelist, function(x){get(load(x))}) corXY = bind_rows(dataFiles) %>% as_tibble() @@ -373,7 +379,7 @@ traits.sign <- traits %>% dplyr::select(species0, any_of(traits.sign.alone)) #write_delim(traits.sign, path="_data/Mesobromion/traits.out.10perc.cov.sign.txt", delim="\t") -### COV - NONAs +### COV - NONAs - all species with at least 1 NAs in traits excluded BEFORE The analysis " Trait.comb Coef.obs Coef.perm q025 q975 greater.than.perm n sign_plus sign_minus ntraits trait.name <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <lgl> <lgl> <int> <fct> 1 36 0.316 0.154 0.290 0.370 0.997 999 TRUE FALSE 1 PlantHeight @@ -385,6 +391,18 @@ traits.sign <- traits %>% 7 32 0.251 0.127 0.226 0.303 0.968 999 TRUE FALSE 1 SLA 8 35 0.241 0.128 0.217 0.289 0.970 999 TRUE FALSE 1 LeafP " +### COV - NONA2 - modified Matrix.x to deal with NAs INSIDE the analysis +" Trait.comb Coef.obs Coef.perm q025 q975 greater.than.perm n sign_plus sign_minus ntraits trait.name + <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <lgl> <lgl> <int> <fct> + 1 36 0.304 0.134 0.281 0.356 0.990 199 TRUE FALSE 1 PlantHeight + 2 50 0.290 0.126 0.261 0.339 0.985 199 TRUE FALSE 1 BL_ANAT + 3 2 0.252 0.0621 0.214 0.303 0.985 199 TRUE FALSE 1 LEB_F_Nanophaneroph… + 4 20 0.251 0.145 0.206 0.320 0.975 199 TRUE FALSE 1 V_VER_Fragmentation + 5 30 0.250 0.137 0.228 0.298 0.970 199 TRUE FALSE 1 BL_DAU + 6 32 0.247 0.0932 0.225 0.292 0.965 199 TRUE FALSE 1 SLA + 7 49 0.236 0.0784 0.212 0.284 0.975 199 TRUE FALSE 1 STRAT_T + 8 35 0.241 0.127 0.218 0.288 0.940 199 FALSE FALSE 1 LeafP " + ### COV - without deleting NAs "# A tibble: 53 x 11 Trait.comb Coef.obs Coef.perm q025 q975 greater.than.perm n sign_plus sign_minus ntraits trait.name