Last updated: 2020-01-10

Checks: 6 0

Knit directory: drift-workflow/analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.2.0). The Report 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(20190211) 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! 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:    docs/.DS_Store

Untracked files:
    Untracked:  analysis/admix_sim2.Rmd

Unstaged changes:
    Modified:   drift-workflow.Rproj

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
Rmd 6138c6e Jason Willwerscheid 2020-01-10 correct cov calculation
html fe3a797 Jason Willwerscheid 2020-01-10 Build site.
Rmd 8f061f6 Jason Willwerscheid 2020-01-10 workflowr::wflow_publish(“analysis/admix_sim1.Rmd”)
html 65769eb Jason Willwerscheid 2020-01-10 Build site.
Rmd e32e21c Jason Willwerscheid 2020-01-10 workflowr::wflow_publish(“analysis/admix_sim1.Rmd”)

suppressMessages({
  library(flashier)
  library(drift.alpha)
  library(ggplot2)
  library(reshape2)
  library(tidyverse)
})

I want to do a series of simulations to see whether driftr can handle very simple admixture events. I start with the simplest event I could imagine: allow two populations to drift until time \(t\), admix them in equal proportions (still at time \(t\)), and then terminate immediately. Means for Populations 1 and 3 (the non-admixed populations) will be independent, while means for Population 2 will simply be averaged from the means for Populations 1 and 3:

Version Author Date
be4c90a Jason Willwerscheid 2020-01-10

The covariance matrix appears as follows:

set.seed(666)
simple.admix <- admix_graph_sim(n_per_pop = 20, p = 500)
plot_cov(simple.admix$CovMat, as.is = TRUE)

Version Author Date
65769eb Jason Willwerscheid 2020-01-10

Flash: greedy

Even in this simple case, greedy flash does not do well. It appears more as if Populations 1 and 3 split off from a main trunk and less as if a subsequent admixture event generated Population 2.

Y <- simple.admix$Y
fl.greed <- flash.init(Y) %>%
  flash.add.greedy(Kmax = 20, prior.family = c(prior.bimodal(), prior.normal()), tol = 1)
#> Adding factor 1 to flash object...
#> Adding factor 2 to flash object...
#> Adding factor 3 to flash object...
#> Adding factor 4 to flash object...
#> Factor doesn't significantly increase objective and won't be added.
#> Wrapping up...
#> Done.

labs <- rep(c("A", "B", "C"), each = 20)
plot_loadings(fl.greed$flash.fit$EF[[1]], labs)

Version Author Date
65769eb Jason Willwerscheid 2020-01-10
plot_cov(fl.greed$flash.fit$EF[[1]] * rep(sapply(fl.greed$fitted.g[[2]], `[[`, "sd"), each = 60))

Version Author Date
fe3a797 Jason Willwerscheid 2020-01-10
65769eb Jason Willwerscheid 2020-01-10

Flash: backfit

Backfitting doesn’t solve the problem:

fl.bf <- fl.greed %>% flash.backfit(maxiter = 30)
#> Backfitting 3 factors (tolerance: 4.47e-04)...
#>   Difference between iterations is within 1.0e+03...
#>   Difference between iterations is within 1.0e+02...
#>   Difference between iterations is within 1.0e+01...
#>   Difference between iterations is within 1.0e+00...
#>   Difference between iterations is within 1.0e-01...
#>   Difference between iterations is within 1.0e-02...
#>   Difference between iterations is within 1.0e-03...
#>   --Maximum number of iterations reached!
#> Wrapping up...
#> Done.
plot_loadings(fl.bf$flash.fit$EF[[1]], labs)

Version Author Date
65769eb Jason Willwerscheid 2020-01-10
plot_cov(fl.bf$flash.fit$EF[[1]] * rep(sapply(fl.greed$fitted.g[[2]], `[[`, "sd"), each = 60))

Version Author Date
fe3a797 Jason Willwerscheid 2020-01-10
65769eb Jason Willwerscheid 2020-01-10

Drift: \(k\) correctly specified

When it’s initialized to a tree with two leaves, driftr almost exactly recovers the correct covariance matrix. Further, the loadings are easily interpretable as the result of an admixture event:

drift.res <- init_using_hclust(simple.admix$Y, k = 2) %>%
  drift(miniter = 2, maxiter = 30)
#>    1 :       -1062.472 
#>    2 :       14785.771 
#>    3 :       21948.179 
#>    4 :       22189.537 
#>    5 :       22225.170 
#>    6 :       22255.595 
#>    7 :       22280.569 
#>    8 :       22297.774 
#>    9 :       22308.707 
#>   10 :       22315.724 
#>   11 :       22320.603 
#>   12 :       22324.315 
#>   13 :       22327.361 
#>   14 :       22330.136 
#>   15 :       22333.208 
#>   16 :       22337.342 
#>   17 :       22341.704 
#>   18 :       22345.266 
#>   19 :       22348.987 
#>   20 :       22353.588 
#>   21 :       22358.211 
#>   22 :       22360.427 
#>   23 :       22361.833 
#>   24 :       22362.771 
#>   25 :       22363.462 
#>   26 :       22364.382 
#>   27 :       22365.220 
#>   28 :       22366.047 
#>   29 :       22366.649 
#>   30 :       22367.367
plot_loadings(drift.res$EL, labs)

Version Author Date
65769eb Jason Willwerscheid 2020-01-10
plot_cov(drift.res$EL * rep(sqrt(drift.res$prior_s2), each = 60))

Version Author Date
fe3a797 Jason Willwerscheid 2020-01-10
65769eb Jason Willwerscheid 2020-01-10

Drift: \(k\) over-specified

Finally, I over-specify \(k\) by initializing to a three-leaf tree. The loadings incorrectly suggest that Population 3 split from 1 and 2 first, and then Populations 1 and 2 diverged. Note, however, that the ELBO is quite a bit lower than the ELBO of the driftr solution initialized using the correct \(k\):

drift.res <- init_using_hclust(simple.admix$Y, k = 3) %>%
  drift(miniter = 2, maxiter = 30)
#>    1 :       20844.551 
#>    2 :       21010.666 
#>    3 :       21091.488 
#>    4 :       21134.886 
#>    5 :       21161.693 
#>    6 :       21179.963 
#>    7 :       21193.468 
#>    8 :       21204.050 
#>    9 :       21212.566 
#>   10 :       21219.477 
#>   11 :       21225.115 
#>   12 :       21229.752 
#>   13 :       21233.606 
#>   14 :       21236.846 
#>   15 :       21239.600 
#>   16 :       21241.964 
#>   17 :       21244.014 
#>   18 :       21245.804 
#>   19 :       21247.381 
#>   20 :       21248.779 
#>   21 :       21250.027 
#>   22 :       21251.146 
#>   23 :       21252.155 
#>   24 :       21253.069 
#>   25 :       21253.902 
#>   26 :       21254.662 
#>   27 :       21255.359 
#>   28 :       21256.001 
#>   29 :       21256.594 
#>   30 :       21257.143
plot_loadings(drift.res$EL, labs)

Version Author Date
65769eb Jason Willwerscheid 2020-01-10
plot_cov(drift.res$EL * rep(sqrt(drift.res$prior_s2), each = 60))

Version Author Date
fe3a797 Jason Willwerscheid 2020-01-10
65769eb Jason Willwerscheid 2020-01-10


sessionInfo()
#> R version 3.5.3 (2019-03-11)
#> 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.5/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.5/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] forcats_0.4.0     stringr_1.4.0     dplyr_0.8.0.1    
#>  [4] purrr_0.3.2       readr_1.3.1       tidyr_0.8.3      
#>  [7] tibble_2.1.1      tidyverse_1.2.1   reshape2_1.4.3   
#> [10] ggplot2_3.2.0     drift.alpha_0.0.5 flashier_0.2.2   
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.1        lubridate_1.7.4   lattice_0.20-38  
#>  [4] assertthat_0.2.1  rprojroot_1.3-2   digest_0.6.18    
#>  [7] foreach_1.4.4     truncnorm_1.0-8   R6_2.4.0         
#> [10] cellranger_1.1.0  plyr_1.8.4        backports_1.1.3  
#> [13] evaluate_0.13     httr_1.4.0        pillar_1.3.1     
#> [16] rlang_0.4.2       lazyeval_0.2.2    pscl_1.5.2       
#> [19] readxl_1.3.1      rstudioapi_0.10   ebnm_0.1-24      
#> [22] whisker_0.3-2     Matrix_1.2-15     rmarkdown_1.12   
#> [25] labeling_0.3      munsell_0.5.0     mixsqp_0.3-10    
#> [28] broom_0.5.1       compiler_3.5.3    modelr_0.1.5     
#> [31] xfun_0.6          pkgconfig_2.0.2   SQUAREM_2017.10-1
#> [34] htmltools_0.3.6   tidyselect_0.2.5  workflowr_1.2.0  
#> [37] codetools_0.2-16  crayon_1.3.4      withr_2.1.2      
#> [40] MASS_7.3-51.1     grid_3.5.3        nlme_3.1-137     
#> [43] jsonlite_1.6      gtable_0.3.0      git2r_0.25.2     
#> [46] magrittr_1.5      scales_1.0.0      cli_1.1.0        
#> [49] stringi_1.4.3     fs_1.2.7          doParallel_1.0.14
#> [52] xml2_1.2.0        generics_0.0.2    iterators_1.0.10 
#> [55] tools_3.5.3       glue_1.3.1        hms_0.4.2        
#> [58] parallel_3.5.3    yaml_2.2.0        colorspace_1.4-1 
#> [61] ashr_2.2-38       rvest_0.3.4       knitr_1.22       
#> [64] haven_2.1.1