Last updated: 2021-02-01
Checks: 7 0
Knit directory: PredictOutbredCrossVar/
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(20191123)
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 883b1d4. 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: analysis/.DS_Store
Ignored: code/.DS_Store
Ignored: data/.DS_Store
Ignored: output/.DS_Store
Ignored: output/crossRealizations/.DS_Store
Untracked files:
Untracked: .gitignore
Untracked: Icon
Untracked: PredictOutbredCrossVar.Rproj
Untracked: manuscript/
Untracked: output/crossPredictions/
Untracked: output/gblups_DirectionalDom_parentwise_crossVal_folds.rds
Untracked: output/gblups_geneticgroups.rds
Untracked: output/gblups_parentwise_crossVal_folds.rds
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/observedCrossVars.Rmd
) and HTML (docs/observedCrossVars.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 | 883b1d4 | wolfemd | 2021-02-01 | Update the syntax highlighting and code-block formatting throughout for |
Rmd | 6a10c30 | wolfemd | 2021-01-04 | Submission and GitHub ready version. |
html | 6a10c30 | wolfemd | 2021-01-04 | Submission and GitHub ready version. |
This step is to create validation data to which predictions of cross means and variances will be correlated.
Compute the empirical means and variances for each validation-family:
VarBV_a
VarBV_dirdom
VarTGV_ad
VarTGV_dirdom
Compute the empirical means and variances for each validation-family, based on the GEBV/GETGV of actual offspring, and using the marker-effects from models fit to the test-set for each fold/rep of parent-wise cross-validation.
rm(list=ls());
library(tidyverse); library(magrittr); library(predCrossVar); library(BGLR);
# Testing datasets -----------
<-readRDS(file = here::here("data","parentwise_crossVal_folds.rds")) %>%
parentfoldsrename(Repeat=id,Fold=id2) %>%
select(Repeat,Fold,testparents)
# Pedigree -----------
<-readRDS(here::here("data","ped_awc.rds")) %>%
pednest(FamilyMembers=FullSampleName)
# Crosses To Predict -------------
%<>%
parentfolds mutate(CrossesToPredict=map(testparents,~filter(ped,sireID %in% . | damID %in% .))) %>%
select(-testparents)
# Genomic BLUPs from testsets --------------
<-readRDS(here::here("output","gblups_parentwise_crossVal_folds.rds")) %>%
gblupsfilter(Dataset=="testset") %>% select(-Dataset,-outName)
<-readRDS(here::here("output","gblups_DirectionalDom_parentwise_crossVal_folds.rds")) %>%
gblups_ddfilter(Dataset=="testset") %>% select(-Dataset,-sampleIDs) %>%
mutate(Model="DirDom")
%<>%
gblups unnest(GBLUPs) %>%
nest(GBLUPs=-c(Repeat,Fold,Model,predOf)) %>%
left_join(parentfolds)
%<>%
gblups_dd select(Repeat,Fold,Model,GBLUPs) %>%
unnest(GBLUPs) %>%
nest(GBLUPs=-c(Repeat,Fold,Model,predOf)) %>%
left_join(parentfolds)
<-bind_rows(gblups,gblups_dd);
gblupsrm(gblups_dd,parentfolds,ped)
# Selection weights -----------
<-readRDS(file=here::here("data","selection_index_weights_4traits.rds"))
indices%>% count(Model,predOf) gblups
<-function(CrossesToPredict,GBLUPs,...){
getCrossMeansAndCovs%<>%
CrossesToPredict mutate(famgblups=map(FamilyMembers,~rename(.,germplasmName=FullSampleName) %>%
left_join(GBLUPs) %>%
%>%
as.data.frame column_to_rownames(var = "germplasmName")),
means=map(famgblups,~summarise_all(.,mean,na.rm=T)),
covMat=map(famgblups,~cov(.,use='pairwise.complete.obs'))) %>%
select(-FamilyMembers)
return(CrossesToPredict)
}
<-gblups %>%
realized_means_and_covsmutate(observedCrossMeansAndCovs=pmap(.,getCrossMeansAndCovs)) %>%
select(-CrossesToPredict,-GBLUPs)
%<>% unnest(observedCrossMeansAndCovs)
realized_means_and_covs saveRDS(realized_means_and_covs,file = here::here("output/crossRealizations","realized_cross_means_and_covs_traits.rds"))
%>% slice(1:3) realized_means_and_covs
<-realized_means_and_covs %>%
realized_means_and_covs_simutate(meanStdSI=map_dbl(means,~as.matrix(.)%*%indices$stdSI),
meanBiofortSI=map_dbl(means,~as.matrix(.)%*%indices$biofortSI),
varStdSI=map_dbl(covMat,~indices$stdSI%*%as.matrix(.)%*%indices$stdSI),
varBiofortSI=map_dbl(covMat,~indices$biofortSI%*%as.matrix(.)%*%indices$biofortSI)) %>%
select(-famgblups,-means,-covMat)
saveRDS(realized_means_and_covs_si,file = here::here("output/crossRealizations","realized_cross_means_and_vars_selindices.rds"))
%>% slice(1:3) realized_means_and_covs_si
Tidy / format the realized cross means and variances.
Combine traits and sel. indices. Split into two separate tibbles, one for means, one for variances.
Output will be ready to combine with predictions to measure accuracy.
library(tidyverse); library(magrittr);
<-readRDS(here::here("output/crossRealizations","realized_cross_means_and_covs_traits.rds"))
realized_means_and_covs_traits<-readRDS(here::here("output/crossRealizations","realized_cross_means_and_vars_selindices.rds")) realized_means_and_covs_si
# Means ---------------
## Traits
<-realized_means_and_covs_traits %>%
realized_means_traitsselect(Repeat,Fold,Model,predOf,sireID,damID,means) %>%
unnest(means) %>%
pivot_longer(cols = c(-Repeat,-Fold,-Model,-predOf,-sireID,-damID),names_to = "Trait", values_to = "obsMean") %>%
mutate(obsOf=ifelse(predOf=="BV","MeanBV","MeanTGV")) %>%
select(-predOf)
## SIs
<-realized_means_and_covs_si %>%
realized_means_siselect(Repeat,Fold,Model,predOf,sireID,damID,contains("mean")) %>%
rename(stdSI=meanStdSI,
biofortSI=meanBiofortSI) %>%
pivot_longer(cols = c(-Repeat,-Fold,-Model,-predOf,-sireID,-damID),names_to = "Trait", values_to = "obsMean") %>%
mutate(obsOf=ifelse(predOf=="BV","MeanBV","MeanTGV")) %>%
select(-predOf)
## Combine
<-bind_rows(realized_means_si,realized_means_traits)
realized_meansrm(realized_means_traits,realized_means_si)
%>% count(Model,obsOf) %>% spread(obsOf,n) realized_means
# (Co)variances -------------
## Traits
<-realized_means_and_covs_traits %>%
realized_vars_traitsselect(Repeat,Fold,Model,predOf,sireID,damID,covMat) %>%
mutate(covMat=map(covMat,function(covMat){
lower.tri(covMat, diag = F)]<-NA
covMat[%<>%
covMat %>%
as.data.frame rownames_to_column(var = "Trait1") %>%
pivot_longer(cols = -Trait1, names_to = "Trait2", values_to = "obsVar",values_drop_na = TRUE)
return(covMat)})) %>%
unnest(covMat) %>%
mutate(obsOf=ifelse(predOf=="BV","VarBV","VarTGV")) %>%
select(-predOf)
## SIs
<-realized_means_and_covs_si %>%
realized_vars_siselect(Repeat,Fold,Model,predOf,sireID,damID,contains("var")) %>%
rename(stdSI=varStdSI,
biofortSI=varBiofortSI) %>%
pivot_longer(cols = c(-Repeat,-Fold,-Model,-predOf,-sireID,-damID),names_to = "Trait1", values_to = "obsVar") %>%
mutate(obsOf=ifelse(predOf=="BV","VarBV","VarTGV"),
Trait2=Trait1) %>%
select(-predOf)
## Combine
<-bind_rows(realized_vars_si,realized_vars_traits)
realized_vars# rename(obsVar=VarSCA))
rm(realized_vars_si,realized_vars_traits)
%>% count(Model,obsOf) %>% spread(obsOf,n) realized_vars
saveRDS(realized_means,here::here("output/crossRealizations","realizedCrossMeans.rds"))
saveRDS(realized_vars,here::here("output/crossRealizations","realizedCrossVars.rds"))
rm(list=ls());
library(tidyverse); library(magrittr); library(predCrossVar); library(BGLR);
# Testing datasets -----------
<-readRDS(file = here::here("data","parentwise_crossVal_folds.rds")) %>%
parentfoldsrename(Repeat=id,Fold=id2) %>%
select(Repeat,Fold,testparents)
# Pedigree -----------
<-readRDS(here::here("data","ped_awc.rds")) %>%
pednest(FamilyMembers=FullSampleName)
# Crosses To Predict -------------
%<>%
parentfolds mutate(CrossesToPredict=map(testparents,~filter(ped,sireID %in% . | damID %in% .))) %>%
select(-testparents)
# Selection weights -----------
<-readRDS(file=here::here("data","selection_index_weights_4traits.rds"))
indices
# BLUPs -----------
<-readRDS(here::here("data","blups_forawcdata.rds")) %>%
blupsselect(Trait,blups) %>%
unnest(blups) %>%
select(Trait,germplasmName,BLUP) %>%
spread(Trait,BLUP)
<-parentfolds %>%
crossblupsunnest(CrossesToPredict) %>%
distinct(sireID,damID,FamilyMembers) %>%
unnest(FamilyMembers) %>%
rename(germplasmName=FullSampleName) %>%
left_join(blups) %>%
select(sireID,damID,germplasmName,all_of(indices$Trait)) %>%
nest(famblups=c(germplasmName,all_of(indices$Trait))) %>%
mutate(stdSI=map(famblups,~as.data.frame(.) %>%
column_to_rownames(var = "germplasmName") %>%
as.matrix(.)%*%indices$stdSI),
biofortSI=map(famblups,~as.data.frame(.) %>%
column_to_rownames(var = "germplasmName") %>%
as.matrix(.)%*%indices$biofortSI))
# Cross Means
<-crossblups %>%
crossmeansmutate(means=map(famblups,~summarize_if(.,is.numeric,mean,na.rm=T))) %>%
unnest(means) %>%
mutate(stdSI=map_dbl(stdSI,mean,na.rm=T),
biofortSI=map_dbl(biofortSI,mean,na.rm=T)) %>%
select(-famblups) %>%
pivot_longer(cols = c(-sireID,-damID), names_to = "Trait", values_to = "obsMean", values_drop_na = TRUE)
# Cross (Co)Vars
<-crossblups %>%
crossvarsmutate(varcovars=map(famblups,~cov(as.data.frame(.) %>%
column_to_rownames(var = "germplasmName") %>%
as.matrix, use = 'pairwise.complete.obs')),
varcovars=map(varcovars,function(varcovars){
lower.tri(varcovars, diag = F)]<-NA
varcovars[%<>%
varcovars %>%
as.data.frame rownames_to_column(var = "Trait1") %>%
pivot_longer(cols = -Trait1, names_to = "Trait2", values_to = "obsVar",values_drop_na = TRUE)
return(varcovars)})) %>%
select(-famblups,-stdSI,-biofortSI) %>%
unnest(varcovars)
<-bind_rows(crossvars,crossblups %>%
crossvarsmutate(stdSI=map_dbl(stdSI,var,na.rm=T),
biofortSI=map_dbl(biofortSI,var,na.rm=T)) %>%
select(-famblups) %>%
pivot_longer(cols = c(-sireID,-damID), names_to = "Trait1", values_to = "obsVar", values_drop_na = TRUE) %>%
mutate(Trait2=Trait1))
saveRDS(crossmeans,here::here("output/crossRealizations","realizedCrossMeans_BLUPs.rds"))
saveRDS(crossvars,here::here("output/crossRealizations","realizedCrossVars_BLUPs.rds"))
For each family, compute the proportion of offspring subsequently used as parents.
Compute the realized “Usefulness” of the cross in terms of population improvement / parent-development as the mean GEBV of family members that are themselves parents.
library(tidyverse); library(magrittr); library(predCrossVar)
<-readRDS(here::here("data","selection_index_weights_4traits.rds"))
indices<-readRDS(here::here("output/crossRealizations","realized_cross_means_and_covs_traits.rds"))
realized_means_and_covs
<-readRDS(here::here("data","ped_awc.rds"))
ped<-union(ped$sireID,ped$damID)
parents
<-realized_means_and_covs %>%
realized_cross_gebvs_sifilter(predOf=="BV") %>%
mutate(stdSI=map(famgblups,~as.matrix(.)%*%indices$stdSI),
biofortSI=map(famgblups,~as.matrix(.)%*%indices$biofortSI),
GEBV=map2(stdSI,biofortSI,~tibble(germplasmName=rownames(.x),stdSI=.x,biofortSI=.y))) %>%
select(-famgblups,-means,-covMat,-stdSI,-biofortSI)
<-realized_cross_gebvs_si %>%
realizedparentmetricsunnest(GEBV) %>%
mutate(IsParent=ifelse(germplasmName %in% parents,TRUE,FALSE)) %>%
nest(Family=c(germplasmName,IsParent,stdSI,biofortSI))
%<>%
realizedparentmetrics mutate(FamSize=map_dbl(Family,nrow),
NmembersUsedAsParent=map_dbl(Family,~length(which(.$IsParent==TRUE))),
propUsedAsParent=map_dbl(Family,~length(which(.$IsParent==TRUE))/nrow(.)),
realizedUCparent_stdSI=map_dbl(Family,~mean(.$stdSI[.$IsParent==TRUE],na.rm = T)),
realizedUCparent_biofortSI=map_dbl(Family,~mean(.$biofortSI[.$IsParent==TRUE],na.rm = T)),
# also compute the mean of top 1% of the family
# correspond ~std. intensity = 2.67
# provides alternative validation data for usefulness criteria
# invariant to actual selection intensity
meanTop1pctGEBV_stdSI=map_dbl(Family, ~slice_max(.data = .,
order_by = stdSI,
n=ceiling(0.01*nrow(.))) %$% mean(stdSI)),
meanTop1pctGEBV_biofortSI=map_dbl(Family, ~slice_max(.data = .,
order_by = biofortSI,
n=ceiling(0.01*nrow(.))) %$% mean(biofortSI))) %>%
select(-Family) %>%
ungroup()
rm(realized_cross_gebvs_si)
For each family, compute the proportion of offspring that have been advanced on the variety development pipeline (VDP) to different stages.
Compute the stage-specific “Usefulness” of the cross in terms of variety development as the mean GETGV of family members advanced to each stage.
# Read for web the raw plot-data used as of summer 2019 for IITA GS
<-readRDS(url("https://raw.github.com/wolfemd/IITA_2019GS/master/data/IITA_ExptDesignsDetected_72619.rds"))
trials<-ped %>%
realizedVDPmetricsrename(GID=FullSampleName) %>%
left_join(trials %>%
filter(Trait %in% indices$Trait) %>%
unnest_legacy(TrialData) %>%
select(germplasmName,observationUnitDbId,TrialType) %>%
%>%
distinct count(germplasmName,TrialType,name = "Nplots") %>%
filter(TrialType %in% c("GeneticGain","CET","ExpCET","PYT","AYT","UYT")) %>%
rename(GID=germplasmName) %>%
spread(TrialType,Nplots) %>%
select(c("GID","GeneticGain","CET","ExpCET","PYT","AYT","UYT"))) %>%
mutate(IsPhenotyped=rowSums(.[,c("GeneticGain","CET","ExpCET","PYT","AYT","UYT")],na.rm = T)>0,
advancedPastCET=rowSums(.[,c("GeneticGain","PYT","AYT","UYT")],na.rm = T)>0,
advancedPastPYT=rowSums(.[,c("GeneticGain","AYT","UYT")],na.rm = T)>0,
advancedPastAYT=rowSums(.[,c("GeneticGain","UYT")],na.rm = T)>0) %>%
select(-c("GeneticGain","CET","ExpCET","PYT","AYT","UYT")) %>%
rename(germplasmName=GID)
<-realized_means_and_covs %>%
realized_cross_getgvs_sifilter(predOf=="TGV") %>%
mutate(stdSI=map(famgblups,~as.matrix(.)%*%indices$stdSI),
biofortSI=map(famgblups,~as.matrix(.)%*%indices$biofortSI),
GETGV=map2(stdSI,biofortSI,~tibble(germplasmName=rownames(.x),stdSI=.x,biofortSI=.y))) %>%
select(-famgblups,-means,-covMat,-stdSI,-biofortSI)
<-realized_cross_getgvs_si %>%
realizedVDPmetricsunnest(GETGV) %>%
left_join(realizedVDPmetrics) %>%
nest(data=c(germplasmName,stdSI,biofortSI,IsPhenotyped,contains("advancedPast"))) %>%
mutate(FamSize=map_dbl(data,nrow),
NmembersPhenotyped=map_dbl(data,~length(which(.$IsPhenotyped==TRUE))),
NmembersPastCET=map_dbl(data,~length(which(.$advancedPastCET==TRUE))),
NmembersPastPYT=map_dbl(data,~length(which(.$advancedPastPYT==TRUE))),
NmembersPastAYT=map_dbl(data,~length(which(.$advancedPastAYT==TRUE))),
propPhenotyped=map_dbl(data,~length(which(.$IsPhenotyped==TRUE))/nrow(.)),
propPastCET=map_dbl(data,~length(which(.$advancedPastCET==TRUE))/nrow(.)),
propPastPYT=map_dbl(data,~length(which(.$advancedPastPYT==TRUE))/nrow(.)),
propPastAYT=map_dbl(data,~length(which(.$advancedPastAYT==TRUE))/nrow(.)),
realizedUCatCET_stdSI=map_dbl(data,~mean(.$stdSI[.$IsPhenotyped==TRUE],na.rm = T)),
realizedUCatPYT_stdSI=map_dbl(data,~mean(.$stdSI[.$advancedPastCET==TRUE],na.rm = T)),
realizedUCatAYT_stdSI=map_dbl(data,~mean(.$stdSI[.$advancedPastPYT==TRUE],na.rm = T)),
realizedUCatUYT_stdSI=map_dbl(data,~mean(.$stdSI[.$advancedPastAYT==TRUE],na.rm = T)),
realizedUCatCET_biofortSI=map_dbl(data,~mean(.$biofortSI[.$IsPhenotyped==TRUE],na.rm = T)),
realizedUCatPYT_biofortSI=map_dbl(data,~mean(.$biofortSI[.$advancedPastCET==TRUE],na.rm = T)),
realizedUCatAYT_biofortSI=map_dbl(data,~mean(.$biofortSI[.$advancedPastPYT==TRUE],na.rm = T)),
realizedUCatUYT_biofortSI=map_dbl(data,~mean(.$biofortSI[.$advancedPastAYT==TRUE],na.rm = T)),
# also compute the mean of top 1% of the family
# correspond ~std. intensity = 2.67
# provides alternative validation data for usefulness criteria
# invariant to actual selection intensity
meanTop1pctGETGV_stdSI=map_dbl(data, ~slice_max(.data = .,
order_by = stdSI,
n=ceiling(0.01*nrow(.))) %$% mean(stdSI)),
meanTop1pctGETGV_biofortSI=map_dbl(data, ~slice_max(.data = .,
order_by = biofortSI,
n=ceiling(0.01*nrow(.))) %$% mean(biofortSI))) %>%
select(-data) %>%
ungroup()
rm(realized_cross_getgvs_si,trials)
Combine parent + variety metrics
<-left_join(realizedparentmetrics %>%
realizedcrossmetricsselect(-predOf) %>%
mutate(Model=ifelse(Model=="A","ClassicAD",Model)),
%>%
realizedVDPmetrics select(-predOf) %>%
mutate(Model=ifelse(Model=="AD","ClassicAD",Model))) %>%
ungroup()
To compare realized and predicted “usefulness criteria”, we need to compute the realized, standardized selection intensity.
Compute the standrardized selection intensity from the proportion selected. This will give NaN for propSel==0. Progeny as crosses with NaN UC were never themselves used as parents. Their “usefulness” has not be “observed” / “evaluated”.
%<>%
realizedcrossmetrics mutate(realIntensityParent=map_dbl(propUsedAsParent,intensity),
realIntensityCET=map_dbl(propPhenotyped,intensity),
realIntensityPYT=map_dbl(propPastCET,intensity),
realIntensityAYT=map_dbl(propPastPYT,intensity),
realIntensityUYT=map_dbl(propPastAYT,intensity))
saveRDS(realizedcrossmetrics,file=here::here("output/crossRealizations","realizedCrossMetrics.rds"))
sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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] Rcpp_1.0.6 whisker_0.4 knitr_1.31 magrittr_2.0.1
[5] workflowr_1.6.2 R6_2.5.0 rlang_0.4.10 stringr_1.4.0
[9] tools_4.0.2 xfun_0.20 git2r_0.28.0 htmltools_0.5.1.1
[13] ellipsis_0.3.1 yaml_2.2.1 digest_0.6.27 rprojroot_2.0.2
[17] tibble_3.0.6 lifecycle_0.2.0 crayon_1.3.4 later_1.1.0.1
[21] vctrs_0.3.6 fs_1.5.0 promises_1.1.1 glue_1.4.2
[25] evaluate_0.14 rmarkdown_2.6 stringi_1.5.3 compiler_4.0.2
[29] pillar_1.4.7 httpuv_1.5.5 pkgconfig_2.0.3