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

Two-stage genomic prediction refers to the following procedure:

Stage 1: Fit a linear mixed model to the data without genomic data. Individuals (e.g. clones / accessions) are modeled as independent and identically distributed (i.i.d.) random effects. The BLUPs for this random effect represent the measurable total genetic values of each individual. All the experimental design variation, e.g. replication and blocking effects have been controlled for in the creation of our new response variable, the BLUPs from the gneotype random effect.

Stage 2: Using a modified version of the BLUPs from step 1 as the response variable, fit a genomic prediction model, which now has reduced size because the number of observations is now the same as the number of individuals.

NOTE: In the animal breeding literature single-step often refers to predictions that combine pedigree and marker information simultaneously. That is not our meaning here.

The code below represents Stage I.

Set-up training datasets

Read in the trial data and group it by trait

rm(list=ls()); gc()
library(tidyverse);library(magrittr)
trials<-readRDS("data/IITA_ExptDesignsDetected_72619.rds")
phenos<-trials %>% 
  unnest(TrialData) %>% 
  select(programName,locationName,studyYear,TrialType,studyName,
         CompleteBlocks,IncompleteBlocks,
         yearInLoc,trialInLocYr,repInTrial,blockInRep,observationUnitDbId,
         germplasmName,FullSampleName,
         Trait,Value,MaxNOHAV,NOHAV,PropHAV,
         TCHARTcovar,CMDcovar) %>% 
  mutate(GID=ifelse(!is.na(FullSampleName),FullSampleName,germplasmName),
         IncompleteBlocks=ifelse(IncompleteBlocks==TRUE,"Yes","No"),
         CompleteBlocks=ifelse(CompleteBlocks==TRUE,"Yes","No")) %>% 
  group_by(Trait) %>% 
  nest(.key = "TrainingData")
rm(trials); gc()

For certain traits, made alternative versions after discussion with IYR.

Curates yield traits based on PropHAV, and CMD severity.

Splits DM according to TCHART, with yellow when >2, else white.

# Set yield traits to missing if <5% or 75% plants harvested (PropHAV)
phenos %<>% 
  bind_rows(
    phenos %>% 
      filter(Trait %in% c("logRTNO","logFYLD","logTOPYLD")) %>% 
      mutate(TrainingData=map(TrainingData,~filter(.,!is.na(PropHAV) & PropHAV>=0.5)),
             Trait=paste0(Trait,"_propHAVpt5"))
  ) %>% 
  bind_rows(
    phenos %>% 
      filter(Trait %in% c("logRTNO","logFYLD","logTOPYLD")) %>% 
      mutate(TrainingData=map(TrainingData,~filter(.,!is.na(PropHAV) & PropHAV>=0.75)),
             Trait=paste0(Trait,"_propHAVpt75"))
  ) %>% 
  bind_rows(
    phenos %>% # Or set missing when NOHAV<5
      filter(Trait %in% c("logRTNO","logFYLD","logTOPYLD")) %>% 
      mutate(TrainingData=map(TrainingData,~filter(.,!is.na(NOHAV) & NOHAV>5)),
             Trait=paste0(Trait,"_nohav5"))
  )
phenos %<>% 
  bind_rows( 
    phenos %>% # Or set missing if CMD severity was>2
      filter(Trait %in% c("logRTNO","logFYLD","logTOPYLD")) %>% 
      mutate(TrainingData=map(TrainingData,~filter(.,!is.na(CMDcovar) & CMDcovar<=2)),
             Trait=paste0(Trait,"_lowCMD"))
  ) %>% 
  bind_rows(
    phenos %>% # white roots only
      filter(Trait=="DM") %>% 
      mutate(TrainingData=map(TrainingData,~filter(.,!is.na(TCHARTcovar) & TCHARTcovar<=2)),
             Trait=paste0(Trait,"_white"))
  ) %>% 
  bind_rows(
    phenos %>% # yellow roots only
      filter(Trait=="DM") %>% 
      mutate(TrainingData=map(TrainingData,~filter(.,!is.na(TCHARTcovar) & TCHARTcovar>2)),
             Trait=paste0(Trait,"_yellow"))
  )

In addition, we wanted to do at list a basic check on the cost/benefit of continuing to use data from earlier than 2012. So for every trait possible, I

  1. All Historical Data Included
  2. IITA data post-2012
phenos %<>% 
  mutate(TrainingData=map(TrainingData,~filter(.,as.numeric(studyYear)>2012)),
         Dataset="2013toPresent") %>% 
  bind_rows(phenos %>% 
              mutate(Dataset="HistoricalDataIncluded") %>% 
              filter(!Trait %in% c("BRNHT1","PLTHT"))) # BRNHT1 and PLTHT lacked "historical" observations
# phenos %>% 
#   mutate(Nobs=map_dbl(TrainingData,~nrow(.))) %>% 
#   select(Trait,Nobs,Dataset) %>% 
#   spread(Dataset,Nobs) %>% 
#   mutate(HowManyHistoricalObs=HistoricalDataIncluded-`2013toPresent`) %>% # %$% summary(HowManyHistoricalObs)
#   arrange(HowManyHistoricalObs)
# Basically, only BRNHT1 and PLTHT lacked "historical" observations

Fit Stage I mixed-model

IID models, get BLUPs from asreml

Set-up the models to be fit for each data chunk

library(furrr) # for parallel processing using purrr functions
options(mc.cores=12)
plan(multiprocess)
library(asreml) # cbsurobbins license as of July 2019
phenos %<>%
  mutate(asFixedFormula="Value ~ yearInLoc",
         asFixedFormula=ifelse(grepl("logRTNO",Trait) | grepl("logFYLD",Trait) | grepl("logTOPYLD",Trait),
                               paste0(asFixedFormula," + PropHAV"),asFixedFormula),
         asRandFormula=paste0("~idv(GID) + idv(trialInLocYr) + at(CompleteBlocks,'Yes'):repInTrial ",
                              "+ at(IncompleteBlocks,'Yes'):blockInRep"))

Fit the models in parallel, keeping only key information to save space

Save for future analysis.

asModelsFit<-phenos %>%
  mutate(fitAS=future_pmap(.,function(asFixedFormula,asRandFormula,TrainingData,...){
    # debugging 
    # -------------
    # asFixedFormula<-phenos$asFixedFormula[[1]]
    # asRandFormula<-phenos$asRandFormula[[1]]
    # TrainingData<-phenos$TrainingData[[1]]
    # -------------
    out<-asreml(as.formula(asFixedFormula),
                random = as.formula(asRandFormula),
                data = TrainingData, maxiter = 40,workspace=400e6)
    ll<-summary(out,all=T)$loglik
    varcomp<-summary(out,all=T)$varcomp
      Vg<-varcomp["GID!GID.var","component"]
      Ve<-varcomp["R!variance","component"]
      H2=Vg/(Vg+Ve)
      blups<-summary(out,all=T)$coef.random %>% 
        as.data.frame %>% 
        rownames_to_column(var = "GID") %>% 
        select(GID,solution,`std error`) %>% 
        filter(grepl("GID",GID)) %>% 
        rename(BLUP=solution) %>% 
        mutate(GID=gsub("GID_","",GID),
               PEV=`std error`^2, # asreml specific
               REL=1-(PEV/Vg), # Reliability 
               drgBLUP=BLUP/REL, # deregressed BLUP
               WT=(1-H2)/((0.1 + (1-REL)/REL)*H2)) # weight for use in Stage 2
      out<-tibble(loglik=ll,Vg,Ve,H2,
                  blups=list(blups),varcomp=list(varcomp))
    return(out) }))
asModelsFit %<>% 
  select(-TrainingData,-asFixedFormula,-asRandFormula) %>% 
  unnest(fitAS)
saveRDS(asModelsFit,file="data/iita_blupsForCrossVal_72619.rds")

Curate field data

Redo set-up of training data chunks. Based on preliminary results, discontinued anlaysis for the yield trait variants. Curation in this case is just outlier removal based on residuals, followed by refitting of the Stage I mixed-model to get new BLUPs.

rm(list=ls()); gc()
library(tidyverse);library(magrittr)
trials<-readRDS("data/IITA_ExptDesignsDetected_72619.rds")
phenos<-trials %>% 
  unnest(TrialData) %>% 
  dplyr::select(programName,locationName,studyYear,TrialType,studyName,
         CompleteBlocks,IncompleteBlocks,
         yearInLoc,trialInLocYr,repInTrial,blockInRep,observationUnitDbId,
         germplasmName,FullSampleName,
         Trait,Value,MaxNOHAV,NOHAV,PropHAV,
         TCHARTcovar,CMDcovar) %>% 
  mutate(GID=ifelse(!is.na(FullSampleName),FullSampleName,germplasmName),
         IncompleteBlocks=ifelse(IncompleteBlocks==TRUE,"Yes","No"),
         CompleteBlocks=ifelse(CompleteBlocks==TRUE,"Yes","No")) %>% 
  group_by(Trait) %>% 
  nest(.key = "TrainingData")
rm(trials); gc()
phenos %<>%
  bind_rows(
    phenos %>% 
      filter(Trait=="DM") %>% 
      mutate(TrainingData=map(TrainingData,~filter(.,!is.na(TCHARTcovar) & TCHARTcovar<=2)),
             Trait=paste0(Trait,"_white"))
  ) %>% 
  bind_rows(
    phenos %>% 
      filter(Trait=="DM") %>% 
      mutate(TrainingData=map(TrainingData,~filter(.,!is.na(TCHARTcovar) & TCHARTcovar>2)),
             Trait=paste0(Trait,"_yellow"))
  )
phenos %<>%
  mutate(asFixedFormula="Value ~ yearInLoc",
         asFixedFormula=ifelse(grepl("logRTNO",Trait) | grepl("logFYLD",Trait) | grepl("logTOPYLD",Trait),
                               paste0(asFixedFormula," + PropHAV"),asFixedFormula),
         asRandFormula=paste0("~idv(GID) + idv(trialInLocYr) + at(CompleteBlocks,'Yes'):repInTrial ",
                              "+ at(IncompleteBlocks,'Yes'):blockInRep"))

Function to detect outliers

Refit mixed-models and this time recover studentized residuals and flag outliers as observations with |studentized-residuals|>3.3

fitASmodelsWithOutlierDetect<-function(asFixedFormula,asRandFormula,TrainingData,...){
  # debug function
  # ---------------------------
  # asFixedFormula<-phenos$asFixedFormula[[1]]
  # asRandFormula<-phenos$asRandFormula[[1]]
  # TrainingData<-phenos$TrainingData[[1]]
  #rm(asFixedFormula,asRandFormula,TrainingData); gc()
  # ---------------------------
  out<-asreml(as.formula(asFixedFormula),
              random = as.formula(asRandFormula),
              data = TrainingData, 
              maxiter = 40,workspace=400e6,aom=T)
  stdRes <- out$aom$R[,"stdCondRes"]
  nedf <- out$nedf 
  studRes <- stdRes / sqrt( (nedf - stdRes^2)/(nedf - 1) ) 
  outliers<-which(abs(studRes)>3.3)
  ll<-summary(out,all=T)$loglik
  varcomp<-summary(out,all=T)$varcomp
  vg<-varcomp["GID!GID.var","component"]
  ve<-varcomp["R!variance","component"]
  H2tmp<-vg/(vg+ve)
  blups<-summary(out,all=T)$coef.random %>% 
    as.data.frame %>% 
    rownames_to_column(var = "GID") %>% 
    dplyr::select(GID,solution,`std error`) %>% 
    filter(grepl("GID",GID)) %>% 
    rename(BLUP=solution) %>% 
    mutate(GID=gsub("GID_","",GID),
           PEV=`std error`^2,
           REL=1-(PEV/vg),
           drgBLUP=BLUP/REL,
           WT=(1-H2tmp)/((0.1 + (1-REL)/REL)*H2tmp))
  out<-list(loglik=ll,Vg=vg,Ve=ve,H2=H2tmp,
            blups=list(blups),
            varcomp=list(varcomp),
            Outliers=list(outliers))
  return(out) }

Run two cycles of outlier removal

Fit models with asreml

library(furrr)
options(mc.cores=12); plan(multiprocess)
library(asreml)
asModelsFit<-phenos %>%
      mutate(fitAS=future_pmap(.,fitASmodelsWithOutlierDetect))

Count and remove outliers

asModelsFit %<>% 
      mutate(NoutR1=map_dbl(fitAS,~length(.$Outliers[[1]])))
asModelsFit %<>% 
      mutate(OutliersR1=map(fitAS,~.$Outliers[[1]]))
asModelsFit %<>% 
      mutate(TrainingData=map2(TrainingData,fitAS,function(TrainingData,fitAS){
            outliers2remove<-fitAS$Outliers[[1]]
            out<-TrainingData[-outliers2remove,]
            return(out) }))

Refit models with asreml after removing outliers

asModelsFit %<>%
      mutate(fitAS=future_pmap(.,fitASmodelsWithOutlierDetect))

Repeat for a second cycle of outlier removal

asModelsFit %<>% 
      mutate(NoutR2=map_dbl(fitAS,~length(.$Outliers[[1]])),
             OutliersR2=map(fitAS,~.$Outliers[[1]]),
             TrainingData=map2(TrainingData,fitAS,function(TrainingData,fitAS){
                   outliers2remove<-fitAS$Outliers[[1]]
                   out<-TrainingData[-outliers2remove,]
                   return(out) }),
             fitAS=future_pmap(.,fitASmodelsWithOutlierDetect))

Format and save blups for cross-validation, fit after 2 rounds of outlier removal

asModelsFit %<>%
      dplyr::select(-TrainingData,-asFixedFormula,-asRandFormula) %>% 
      mutate(fitAS=map(fitAS,as_tibble)) %>% 
      unnest(fitAS)
saveRDS(asModelsFit,file="data/iita_blupsForCrossVal_outliersRemoved_73019.rds")

Next step

Stage II: Cross-validation Run 1


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     

loaded via a namespace (and not attached):
 [1] workflowr_1.5.0.9000 Rcpp_1.0.3           rprojroot_1.3-2     
 [4] digest_0.6.22        later_1.0.0          R6_2.4.1            
 [7] backports_1.1.5      git2r_0.26.1         magrittr_1.5        
[10] evaluate_0.14        stringi_1.4.3        rlang_0.4.1         
[13] fs_1.3.1             promises_1.1.0       whisker_0.4         
[16] rmarkdown_1.17       tools_3.6.1          stringr_1.4.0       
[19] glue_1.3.1           httpuv_1.5.2         xfun_0.11           
[22] yaml_2.2.0           compiler_3.6.1       htmltools_0.4.0     
[25] knitr_1.26