Last updated: 2021-04-30

Checks: 7 0

Knit directory: CassavaNIRS/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20210419) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 88fee14. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    Hershberger_CassavaNIRS_2021.zip
    Ignored:    analysis/.DS_Store
    Ignored:    code/.DS_Store
    Ignored:    data/.DS_Store
    Ignored:    data/Cassavabase_phenotypes_20210419.csv
    Ignored:    data/Corrected_metadata/
    Ignored:    data/README.html
    Ignored:    data/README.txt
    Ignored:    data/Spectra/
    Ignored:    data/TrialNameKey.csv
    Ignored:    data/raw_pheno.csv
    Ignored:    data/raw_scans.csv
    Ignored:    output/.DS_Store
    Ignored:    output/Figure2_DMC_distributions.png
    Ignored:    output/Figure4_within_predictions.png
    Ignored:    output/Figure5_Subsamples.png
    Ignored:    output/Figure6_RF_Importance.png
    Ignored:    output/Figure7_CV_predictions.png
    Ignored:    output/FigureS2_within_trial_prediction_all.png
    Ignored:    output/S1_overlapping_accession_counts.csv
    Ignored:    output/S3_removed_scans.csv
    Ignored:    output/Table2_DMC_statistics.csv
    Ignored:    output/Table3_performance_summary.csv
    Ignored:    output/TableS2_within_trial_predictions.csv
    Ignored:    output/TableS4_cv_results.csv
    Ignored:    output/cv_base.png
    Ignored:    output/cv_results.csv
    Ignored:    output/full_filtered_plots.csv
    Ignored:    output/full_filtered_subsamples.csv
    Ignored:    output/full_filtered_unaggregated.csv
    Ignored:    output/subsampling_prediction_results_2021.csv
    Ignored:    output/within_trial_waves_PLSR.csv
    Ignored:    output/within_trial_waves_RF.csv
    Ignored:    output/within_trial_waves_RF_importance.csv
    Ignored:    output/within_trial_waves_SVM.csv

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/filter_aggregate.Rmd) and HTML (docs/filter_aggregate.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 88fee14 Jenna Hershberger 2021-04-30 Build workflowr site
html 88fee14 Jenna Hershberger 2021-04-30 Build workflowr site
Rmd 759b463 Jenna Hershberger 2021-04-22 Update curation code

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.3.3     ✓ purrr   0.3.4
✓ tibble  3.1.1     ✓ dplyr   1.0.5
✓ tidyr   1.1.2     ✓ stringr 1.4.0
✓ readr   1.4.0     ✓ forcats 0.5.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
library(magrittr)

Attaching package: 'magrittr'
The following object is masked from 'package:purrr':

    set_names
The following object is masked from 'package:tidyr':

    extract
library(waves)

pheno <- read.csv("data/raw_pheno.csv")
scans_all <- read.csv("data/raw_scans.csv")
colnames_to_keep <- c("studyYear", "programName", "studyName", "studyDesign", "plotWidth",
                      "plotLength", "plantingDate", "harvestDate", "MAP", "locationName",
                      "germplasmName", "observationLevel", "observationUnitName", 
                      "replicate", "blockNumber", "plotNumber", "rowNumber", "colNumber", 
                      "entryType", "sample.prep",
                      "dry.matter.content.percentage.CO_334.0000092")

joined_colnames <- c(colnames_to_keep, "rootNumber", "subsample", "scanTimestamp", 
                     "device_id", "comments", paste0("X", 740:1070))

joined_colnames_no_subsample <- c(colnames_to_keep, "scanTimestamp", 
                     "device_id", "comments", paste0("X", 740:1070))

scan_colnames_plots <- c("studyName", "plotNumber", "rootNumber", "subsample", 
                         "scanTimestamp", "device_id", "comments")

Filter scans based on Mahalanobis distance

scans_all$X740 <- as.numeric(scans_all$X740)
scans_filtered <- scans_all %>% 
  # have to remove columns with missing values before FilterSpectra() 
  # because of waves requirement
  dplyr::select(rowname, starts_with("X")) %>% 
  FilterSpectra(filter = TRUE,
                return.distances = FALSE, 
                num.col.before.spectra = 1,
                window.size = 10) %>% 
  left_join(x = ., y = scans_all[,1:8], by = "rowname") %>% 
  mutate(subsample = ifelse(is.na(subsample), rootNumber, subsample)) %>% 
  dplyr::select(all_of(scan_colnames_plots), starts_with("X")) %>% 
  distinct()

Removed 8 rows.
scans_removed_df <- scans_all %>% 
  # have to remove columns with missing values before FilterSpectra() 
  # because of waves requirement
  dplyr::select(rowname, starts_with("X")) %>% 
  FilterSpectra(filter = FALSE,
                return.distances = TRUE, 
                num.col.before.spectra = 1,
                window.size = 10) %>% 
  left_join(x = ., y = scans_all[,1:8], by = "rowname") %>% 
  mutate(subsample = ifelse(is.na(subsample), rootNumber, subsample)) %>% 
  dplyr::select(all_of(scan_colnames_plots), h.distances, starts_with("X"), -comments) %>% 
  filter(h.distances > 300) %>%
  rename(Mahalanobis.distance = h.distances) %>% 
  arrange(-Mahalanobis.distance) %>% 
  distinct()
write.csv(scans_removed_df, "output/S3_removed_scans.csv", row.names = F)

Aggregate by subsample

scans_filtered_subsample <- scans_filtered %>%
  # AggregateSpectra() requires a column named "reference"
  mutate(reference = 1) %>%
  drop_na(subsample) %>% 
  # have to remove columns with missing values before AggregateSpectra() 
  # because of waves requirement
  dplyr::select(studyName, plotNumber, subsample, reference, starts_with("X")) %>% 
  AggregateSpectra(grouping.colnames = c("studyName", "plotNumber", "subsample"),
                   reference.value.colname = "reference", 
                   agg.function = "mean") %>%
  dplyr::select(-reference) %>% 
  left_join(x = ., y = scans_filtered[1:7], by = c("studyName", 
                                                   "plotNumber", 
                                                   "subsample")) %>% 
  dplyr::select(all_of(scan_colnames_plots), starts_with("X")) %>% 
  distinct()

Aggregate by plot

scans_filtered_plots <- scans_filtered %>%
  # AggregateSpectra() requires a column named "reference"
  mutate(reference = 1) %>%
  drop_na(plotNumber) %>% 
  # have to remove columns with missing values before AggregateSpectra() 
  # because of waves requirement
  dplyr::select(studyName, plotNumber, reference, starts_with("X")) %>% 
  AggregateSpectra(grouping.colnames = c("studyName", "plotNumber"),
                   reference.value.colname = "reference", 
                   agg.function = "mean") %>%
  dplyr::select(-reference) %>% 
  # join only with the relevant metadata from scans_filtered (studyName, plotNumber, scanTimestamp, device_id, comments)
  left_join(x = ., y = scans_filtered[c(1,2,5,6,7)], 
            by = c("studyName", "plotNumber")) %>% 
  dplyr::select(all_of(scan_colnames_plots[c(1,2,5,6,7)]), starts_with("X")) %>% 
  distinct()

Join scans with phenotypes and field metadata

full_filtered <- pheno %>% 
  full_join(scans_filtered, by = c("studyName", "plotNumber")) %>% 
  dplyr::select(all_of(joined_colnames)) %>% 
  drop_na(dry.matter.content.percentage.CO_334.0000092, X740) %>%  
  # comment out the line above to get counts of missingness
  distinct()

full_subsamples <- pheno %>% 
  full_join(scans_filtered_subsample, by = c("studyName", "plotNumber")) %>% 
  dplyr::select(all_of(joined_colnames)) %>% 
  drop_na(dry.matter.content.percentage.CO_334.0000092, X740) %>% 
  distinct()

full_plots <- pheno %>% filter(programName == "IITA") %>% 
  full_join(scans_filtered_plots, by = c("studyName", "plotNumber")) %>% 
  dplyr::select(all_of(joined_colnames_no_subsample)) %>% 
  drop_na(dry.matter.content.percentage.CO_334.0000092, X740) %>% 
  distinct()

nrow(full_filtered)
[1] 25514
nrow(full_subsamples)
[1] 6522
nrow(full_plots)
[1] 1250

Save

write.csv(full_filtered, "output/full_filtered_unaggregated.csv", row.names = F)
write.csv(full_subsamples, "output/full_filtered_subsamples.csv", row.names = F)
write.csv(full_plots, "output/full_filtered_plots.csv", row.names = F)

sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin18.2.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS/LAPACK: /usr/local/Cellar/openblas/0.3.6_1/lib/libopenblasp-r0.3.6.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] waves_0.1.0     magrittr_2.0.1  forcats_0.5.0   stringr_1.4.0  
 [5] dplyr_1.0.5     purrr_0.3.4     readr_1.4.0     tidyr_1.1.2    
 [9] tibble_3.1.1    ggplot2_3.3.3   tidyverse_1.3.0 workflowr_1.6.2

loaded via a namespace (and not attached):
 [1] nlme_3.1-151         fs_1.5.0             lubridate_1.7.9.2   
 [4] httr_1.4.2           rprojroot_2.0.2      tools_3.5.2         
 [7] backports_1.2.1      utf8_1.2.1           R6_2.5.0            
[10] rpart_4.1-15         DBI_1.1.1            colorspace_2.0-0    
[13] nnet_7.3-15          withr_2.4.2          tidyselect_1.1.0    
[16] compiler_3.5.2       git2r_0.28.0         spectacles_0.5-3    
[19] cli_2.4.0            rvest_0.3.6          BiasedUrn_1.07      
[22] SparseM_1.78         xml2_1.3.2           scales_1.1.1        
[25] quadprog_1.5-8       randomForest_4.6-14  digest_0.6.27       
[28] rmarkdown_2.6        pkgconfig_2.0.3      htmltools_0.5.1     
[31] dbplyr_2.0.0         rlang_0.4.10         readxl_1.3.1        
[34] rstudioapi_0.13      generics_0.1.0       jsonlite_1.7.2      
[37] wesanderson_0.3.6    ModelMetrics_1.2.2.2 Matrix_1.2-18       
[40] Rcpp_1.0.6           munsell_0.5.0        fansi_0.4.2         
[43] lifecycle_1.0.0      stringi_1.5.3        whisker_0.4         
[46] pROC_1.17.0.1        yaml_2.2.1           MASS_7.3-53         
[49] plyr_1.8.6           recipes_0.1.15       grid_3.5.2          
[52] pls_2.7-3            promises_1.1.1       crayon_1.4.1        
[55] lattice_0.20-41      haven_2.3.1          splines_3.5.2       
[58] pander_0.6.3         hms_1.0.0            epiR_2.0.19         
[61] knitr_1.29           pillar_1.6.0         lpSolve_5.6.15      
[64] prospectr_0.2.0      stats4_3.5.2         reshape2_1.4.4      
[67] codetools_0.2-18     reprex_0.3.0         glue_1.4.2          
[70] evaluate_0.14        data.table_1.13.6    modelr_0.1.8        
[73] vctrs_0.3.7          httpuv_1.5.5         foreach_1.5.1       
[76] cellranger_1.1.0     gtable_0.3.0         assertthat_0.2.1    
[79] xfun_0.20            gower_0.2.2          limSolve_1.5.6      
[82] prodlim_2019.11.13   broom_0.7.3          baseline_1.3-1      
[85] later_1.1.0.1        class_7.3-18         survival_3.2-7      
[88] timeDate_3043.102    signal_0.7-6         iterators_1.0.13    
[91] lava_1.6.8.1         ellipsis_0.3.1       caret_6.0-86        
[94] ipred_0.9-9