Last updated: 2020-02-14

Checks: 7 0

Knit directory: IITA_2019GS/

This reproducible R Markdown analysis was created with workflowr (version 1.5.0.9000). 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(20191121) 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 version displayed above was the version of the Git repository at the time these results were generated.

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:    data/.DS_Store
    Ignored:    output/.DS_Store

Untracked files:
    Untracked:  analysis/GetGainEst.Rmd
    Untracked:  workflowr_log.R

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 R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
html f6e2d1f wolfemd 2020-02-13 Build site.
Rmd 3a93083 wolfemd 2020-02-13 Add / fix navigation links and misc
html 84cab28 wolfemd 2019-11-21 Build site.
html 57b19a9 wolfemd 2019-11-21 Build site.
html cb61b89 wolfemd 2019-11-21 Build site.
html 70242a6 wolfemd 2019-11-21 Build site.
html dacbcf9 wolfemd 2019-11-21 Build site.
html 43e9d5d wolfemd 2019-11-21 Build site.
html a869b9e wolfemd 2019-11-21 Build site.
Rmd bfffb51 wolfemd 2019-11-21 Publish the first set of analyses and files for IITA 2019 GS,

Objective

This time with the outliers-removed BLUPs. Based on results in round 1, did not continue with some of the traits.

Set-up training data

rm(list=ls()); gc()
library(tidyverse); library(magrittr); 
blups<-readRDS(file="data/iita_blupsForCrossVal_outliersRemoved_73019.rds")
K<-readRDS(file=paste0("/workdir/IITA_2019GS/Kinship_IITA_TrainingPop_72619.rds"))
blups %<>%
      rename(trainingData=blups) %>% 
      mutate(trainingData=map(trainingData,~filter(.,GID %in% rownames(K))),)
tms13f<-rownames(K) %>% grep("TMS13F|2013_",.,value = T); length(tms13f) # 2395
tms14f<-rownames(K) %>% grep("TMS14F",.,value = T); length(tms14f) # 2171
tms15f<-rownames(K) %>% grep("TMS15F",.,value = T); length(tms15f) # 835
gg<-setdiff(rownames(K),c(tms13f,tms14f,tms15f)); length(gg) # 1228 (not strictly gg)

blups %<>%
    mutate(seed_of_seeds=1:n(),
           seeds=map(seed_of_seeds,function(seed_of_seeds,reps=5){ 
               set.seed(seed_of_seeds); 
               outSeeds<-sample(1:1000,size = reps,replace = F); 
               return(outSeeds) }))
blups %<>%
      select(-varcomp); gc()

Cross-validation function

# trainingData<-blups$trainingData[[1]]; seeds<-blups$seeds[[1]]; nfolds<-5; reps<-5;
crossValidateFunc<-function(Trait,trainingData,seeds,nfolds=5,reps=5,ncores=50,...){
      trntstdata<-trainingData %>% 
            filter(GID %in% rownames(K))
      K1<-K[rownames(K) %in% trntstdata$GID,
            rownames(K) %in% trntstdata$GID]
      # rm(K,trainingData); gc()
      # seed<-seeds[[1]]
      # Nfolds=nfolds
      makeFolds<-function(Nfolds=nfolds,seed){
            genotypes<-rownames(K1)
            
            set.seed(seed)
            seed_per_group<-sample(1:10000,size = 4,replace = FALSE)
      
            set.seed(seed_per_group[1])
            FoldsThisRep_tms15<-tibble(CLONE=genotypes[genotypes %in% tms15f],
                                       Group="TMS15F") %>% 
                  mutate(Folds=sample(1:Nfolds,nrow(.),replace=T)) %>% 
                  arrange(Folds) %>% 
                  group_by(Group,Folds) %>% 
                  nest(.key = Test)
            set.seed(seed_per_group[2])
            FoldsThisRep_tms14<-tibble(CLONE=genotypes[genotypes %in% tms14f],
                                       Group="TMS14F") %>% 
                  mutate(Folds=sample(1:Nfolds,nrow(.),replace=T)) %>% 
                  arrange(Folds) %>% 
                  group_by(Group,Folds) %>% 
                  nest(.key = Test)
            set.seed(seed_per_group[3])
            FoldsThisRep_tms13<-tibble(CLONE=genotypes[genotypes %in% tms13f],
                                       Group="TMS13F") %>% 
                  mutate(Folds=sample(1:Nfolds,nrow(.),replace=T)) %>% 
                  arrange(Folds) %>% 
                  group_by(Group,Folds) %>% 
                  nest(.key = Test)
            set.seed(seed_per_group[4])
            FoldsThisRep_gg<-tibble(CLONE=genotypes[genotypes %in% gg],
                                       Group="GGetc") %>% 
                  mutate(Folds=sample(1:Nfolds,nrow(.),replace=T)) %>% 
                  arrange(Folds) %>% 
                  group_by(Group,Folds) %>% 
                  nest(.key = Test)
           FoldsThisRep<-bind_rows(FoldsThisRep_tms15,FoldsThisRep_tms14) %>% 
                  bind_rows(FoldsThisRep_tms13) %>% 
                  bind_rows(FoldsThisRep_gg) %>% 
                  mutate(Test=map(Test,~.$CLONE),
                         Train=map(Test,~genotypes[!genotypes %in% .]))
            return(FoldsThisRep) }
      
    crossval<-tibble(Rep=1:reps,seed=unlist(seeds)[1:reps]) %>% 
        mutate(Folds=map2(Rep,seed,~makeFolds(Nfolds=nfolds,seed=.y))) %>% 
        unnest()

    #Test<-crossval$Test[[1]]; Train<-crossval$Train[[1]]
    crossValidate<-function(Test,Train){ 
      train<-Train
      test<-Test
      trainingdata<-trntstdata %>% 
        filter(GID %in% train) %>% 
        mutate(GID=factor(GID,levels=rownames(K1)))
       
      require(sommer)
      proctime<-proc.time()
      fit <- mmer(fixed = drgBLUP ~1,
                  random = ~vs(GID,Gu=K1),
                  weights = WT,
                  data=trainingdata) 
      proc.time()-proctime
      
      x<-fit$U$`u:GID`$drgBLUP 
      gebvs<-tibble(GID=names(x),
                    GEBV=as.numeric(x))
      
      accuracy<-gebvs %>% 
            filter(GID %in% test) %>% 
            left_join(
                  trntstdata %>% 
                        dplyr::select(GID,BLUP) %>% 
                        filter(GID %in% test)) %$% 
            cor(GEBV,BLUP, use='complete.obs') 
    return(accuracy)
    }
    
    require(furrr)
    options(mc.cores=ncores)
    plan(multiprocess)
    crossval<-crossval %>% 
          mutate(accuracy=future_map2(Test,Train,~crossValidate(Test=.x,Train=.y)))
    saveRDS(crossval,file=paste0("/workdir/IITA_2019GS/CrossVal_73019/",
                                 "CrossVal_",Trait,"_OutliersRemoved_73019.rds"))
    rm(list=ls()); gc() 
    }

Run CV on two servers

cbsulm14 (112)

blups %>% 
      mutate(CVaccuracy=pmap(.,crossValidateFunc))
#saveRDS(cvresults_1,file="/workdir/IITA_2019GS/CrossValResults_IITA_TrainingPop_1_72719.rds")

Results

rm(list=ls());gc()
          used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
Ncells  605491 32.4    1262655 67.5         NA   902544 48.3
Vcells 1158680  8.9    8388608 64.0     102400  1957057 15.0
library(tidyverse); library(magrittr); library(cowplot); 
cvNoOutliers<-tibble(Files=list.files("output/CrossVal_73019/")) %>% 
                  mutate(Trait=gsub("CrossVal_","",Files),
                         Trait=gsub("_OutliersRemoved_73019.rds","",Trait),
                         Dataset="OutliersRemoved") %>% 
                  mutate(cvResults=map(Files,~readRDS(paste0("output/CrossVal_73019/",.)))) %>% 
      dplyr::select(-Files)
cvWithOutliers<-tibble(Files=list.files("output/CrossVal_72719/")) %>% 
      filter(grepl("HistoricalDataIncluded|BRNHT1|PLTHT",Files)) %>% 
      mutate(Trait=gsub("CrossVal_","",Files),
             Trait=gsub("_2013toPresent_72719.rds","",Trait),
             Trait=gsub("_HistoricalDataIncluded_72719.rds","",Trait),
             Dataset="NoOutlierRemoval") %>% 
      filter(Trait %in% cvNoOutliers$Trait) %>% 
      mutate(cvResults=map(Files,~readRDS(paste0("output/CrossVal_72719/",.)))) %>% 
      dplyr::select(-Files)
cv<-bind_rows(cvNoOutliers,
              cvWithOutliers)

cv %<>% 
  unnest(cols = cvResults) %>% 
  mutate(Ntrain=map_dbl(Train,length),
         Ntest=map_dbl(Test,length)) %>% 
  select(-Test,-Train) %>% 
  unnest(cols = accuracy)

Figure 1

I did an additional cross-validation, using BLUPs produced after two rounds of model-fitting, followed-by outlier removal. I defined outliers as observations with abs(studentized residuals)>3.3. Overall, the improvement is not consistent or large, but I’d probably trend towards using the data with outliers removed.

By genetic group

library(viridis)
cv %>% 
      ggplot(.,aes(x=Trait,y=accuracy,fill=Dataset)) + 
      geom_boxplot() + 
      facet_grid(.~Group,space='free_x',scale='free_x') +
      geom_hline(yintercept = 0,color='darkred',size=1.25) + 
      theme_bw() + 
      theme(axis.text.x = element_text(angle=90,face='bold',size=14)) +
      scale_fill_viridis_d()

Version Author Date
a869b9e wolfemd 2019-11-21

Figure 2

overall

library(viridis)
cv %>% 
      ggplot(.,aes(x=Trait,y=accuracy,fill=Dataset)) + 
      geom_boxplot() + 
      geom_hline(yintercept = 0,color='darkred',size=1.25) + 
      theme_bw() + 
      theme(axis.text.x = element_text(angle=90,face='bold',size=14)) +
      scale_fill_viridis_d()

Version Author Date
a869b9e wolfemd 2019-11-21

Next step

Stage II: Cross-validation Run 2


sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.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] viridis_0.5.1     viridisLite_0.3.0 cowplot_1.0.0     magrittr_1.5     
 [5] forcats_0.4.0     stringr_1.4.0     dplyr_0.8.3       purrr_0.3.3      
 [9] readr_1.3.1       tidyr_1.0.0       tibble_2.1.3      ggplot2_3.2.1    
[13] tidyverse_1.2.1  

loaded via a namespace (and not attached):
 [1] tidyselect_0.2.5     xfun_0.11            reshape2_1.4.3      
 [4] haven_2.2.0          lattice_0.20-38      colorspace_1.4-1    
 [7] vctrs_0.2.0          generics_0.0.2       htmltools_0.4.0     
[10] yaml_2.2.0           rlang_0.4.1          later_1.0.0         
[13] pillar_1.4.2         withr_2.1.2          glue_1.3.1          
[16] modelr_0.1.5         readxl_1.3.1         plyr_1.8.4          
[19] lifecycle_0.1.0      munsell_0.5.0        gtable_0.3.0        
[22] workflowr_1.5.0.9000 cellranger_1.1.0     rvest_0.3.5         
[25] evaluate_0.14        labeling_0.3         knitr_1.26          
[28] httpuv_1.5.2         broom_0.5.2          Rcpp_1.0.3          
[31] promises_1.1.0       backports_1.1.5      scales_1.1.0        
[34] jsonlite_1.6         farver_2.0.1         fs_1.3.1            
[37] gridExtra_2.3        hms_0.5.2            digest_0.6.22       
[40] stringi_1.4.3        grid_3.6.1           rprojroot_1.3-2     
[43] cli_1.1.0            tools_3.6.1          lazyeval_0.2.2      
[46] crayon_1.3.4         whisker_0.4          pkgconfig_2.0.3     
[49] zeallot_0.1.0        xml2_1.2.2           lubridate_1.7.4     
[52] assertthat_0.2.1     rmarkdown_1.17       httr_1.4.1          
[55] rstudioapi_0.10      R6_2.4.1             nlme_3.1-142        
[58] git2r_0.26.1         compiler_3.6.1