From 194ea0a4abfe4dd3279770b3780d10db67095783 Mon Sep 17 00:00:00 2001
From: Francesco Sabatini <francesco.sabatini@idiv.de>
Date: Tue, 1 Dec 2020 19:53:22 +0100
Subject: [PATCH] Version v1.1 corrected

---
 code/04_buildHeader.Rmd        |  5 +++--
 code/05_ExtractEnvironment.Rmd |  5 ++++-
 code/07_buildCWMs.Rmd          | 24 +++++++++++++++---------
 3 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/code/04_buildHeader.Rmd b/code/04_buildHeader.Rmd
index 3aae245..ad7b3f8 100644
--- a/code/04_buildHeader.Rmd
+++ b/code/04_buildHeader.Rmd
@@ -151,7 +151,8 @@ Pfe-f-08 - 1707849
 Pfe-o-05- 1707854  
 ```{r, eval=T}
 header0 <- header0 %>% 
-  filter(!PlotObservationID %in% c(1707776, 1707779:1707782, 1707849, 1707854))
+  filter(!PlotObservationID %in% c(1707776, 1707779:1707782, 1707849, 1707854)) %>% 
+  filter(Dataset != "$Coastal_Borja")
 ```
 
 
@@ -394,7 +395,7 @@ header <- header %>%
             by="PlotObservationID") %>% 
   mutate(CONTINENT=factor(continent, 
                             levels=c("Africa", "Antarctica", "Australia", "Eurasia", "North America", "South America"),
-                            labels=c("AF", "AN", "AU", "EU", "NA", "SA"))) %>% 
+                            labels=c("AF", "AN", "AU", "EU", "N-A", "S-A"))) %>% 
   
   dplyr::select(-continent)
 ```
diff --git a/code/05_ExtractEnvironment.Rmd b/code/05_ExtractEnvironment.Rmd
index 3038aca..3bea535 100644
--- a/code/05_ExtractEnvironment.Rmd
+++ b/code/05_ExtractEnvironment.Rmd
@@ -295,7 +295,8 @@ elevation.out <- read_csv("../_derived/elevatr/elevation.out.csv")
 soilclim <- header %>% 
   dplyr::select(PlotObservationID) %>% 
   left_join(elevation.out %>% 
-              dplyr::select(PlotObservationID, Elevation_median, Elevation_q2.5, Elevation_q97.5, Elevation_DEM.res=DEM.res), 
+              dplyr::select(PlotObservationID, Elevation_median, Elevation_q2.5, Elevation_q97.5, Elevation_DEM.res=DEM.res) %>% 
+              filter(!is.na(Elevation_median)), 
             by="PlotObservationID") %>% 
   left_join(header.shp@data %>% 
               dplyr::select(PlotObservationID) %>% 
@@ -305,6 +306,8 @@ soilclim <- header %>%
               bind_cols(isric.sd.out) %>% 
               distinct(), 
             by="PlotObservationID")
+dim(soilclim)
+nrow(soilclim)==nrow(header)
 ```
 
 ```{r, echo=F}
diff --git a/code/07_buildCWMs.Rmd b/code/07_buildCWMs.Rmd
index 0e128cc..99c0eb6 100644
--- a/code/07_buildCWMs.Rmd
+++ b/code/07_buildCWMs.Rmd
@@ -15,7 +15,7 @@ output: html_document
 **Revised:**  
 **version:** 1.0
   
-This reports documents 1) the construction of Community Weighted Means (CWMs) and Variance (CWVs); and 2) the classification of plots into forest\\non-forest based on species growth forms. It complements species composition data from sPlot 3.0 and gap-filled plant functional traits from TRY 5.0, as received by [Jens Kattge](jkattge@bgc-jena.mpg.de) on Jan 21, 2020. 
+This report documents 1) the construction of Community Weighted Means (CWMs) and Variance (CWVs); and 2) the classification of plots into forest\\non-forest based on species growth forms. It complements species composition data from sPlot 3.0 and gap-filled plant functional traits from TRY 5.0, as received by [Jens Kattge](jkattge@bgc-jena.mpg.de) on Jan 21, 2020. 
 
 *Changes in version 1.1* - Standardized Growth form names in sPlot_traits.  
 
@@ -202,7 +202,7 @@ This results in the exclusion of `r length(unique(c(toexclude, toexclude2, toexc
 
 
 ## 1.4 Calculate species and genus level trait means and sd
-```{r}
+```{r, cache=T,  cache.lazy=F, warning=F}
 ## Calculate species level trait means and sd. 
 try.species.means <- try.individuals %>% 
   group_by(Name_short) %>% 
@@ -335,7 +335,10 @@ CWM0 <- DT2.comb %>%
               dplyr::rename(Species=Taxon_name) %>% 
               dplyr::select(Species, Rank_correct, ends_with("_mean")), 
             by=c("Species", "Rank_correct"))
+```
 
+
+```{r, cache=T,  cache.lazy=F, warning=F}
 # Calculate CWM for each trait in each plot
 CWM1 <- CWM0 %>%
   group_by(PlotObservationID) %>%
@@ -343,7 +346,10 @@ CWM1 <- CWM0 %>%
                .funs = list(~weighted.mean(., Relative_cover, na.rm=T))) %>%
   dplyr::select(PlotObservationID, order(colnames(.))) %>%
   gather(key=variable, value=CWM, -PlotObservationID)
+```
+
 
+```{r, cache=T,  cache.lazy=F, warning=F}
 # Calculate coverage for each trait in each plot
 CWM2 <- CWM0 %>%
   mutate_at(.funs = list(~if_else(is.na(.),0,1) * Relative_cover), 
@@ -353,7 +359,10 @@ CWM2 <- CWM0 %>%
                .funs = list(~sum(., na.rm=T))) %>%
   dplyr::select(PlotObservationID, order(colnames(.))) %>%
   gather(key=variable, value=trait.coverage, -PlotObservationID)
-  
+```
+
+
+```{r, cache=T,  cache.lazy=F, warning=F}
 # Calculate CWV
 # Ancillary function
 variance2.fun <- function(trait, abu){
@@ -377,7 +386,10 @@ CWM3 <- CWM0 %>%
                .funs = list(~variance2.fun(., Relative_cover))) %>%
   dplyr::select(PlotObservationID, order(colnames(.))) %>%
   gather(key=variable, value=CWV, -PlotObservationID)
+```
+
 
+```{r, cache=T,  cache.lazy=F, warning=F}
 ## Calculate proportion of species having traits
 CWM4 <- CWM0 %>%
   group_by(PlotObservationID) %>%
@@ -479,12 +491,6 @@ knitr::kable(CWM.summary,
                   full_width = F, position = "center")
 ```
 
-### 2.2 Export CWM and species mean trait values
-```{r}
-save(try.combined.means, CWM, file="../_output/Traits_CWMs_sPlot3.RData")
-```
-
-
 # 3 Classify plots in `is.forest` or `is.non.forest` based on species traits
 sPlot has two independent systems for classifying plots to vegetation types. The first relies on the expert opinion of data contributors and classifies plots into broad habitat types. These broad habitat types are coded using 5, non-mutually exclusive dummy variables:  
 1) Forest  
-- 
GitLab