Last updated: 2020-01-16

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
    Ignored:    docs/assets/.DS_Store

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 3da801d Jason Willwerscheid 2020-01-16 wflow_publish(“analysis/admix_sim_full.Rmd”)

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

Time to try out drift on the full admixture graph from Pickrell and Pritchard. To make the problem easier, I set \(p\) large and \(\sigma_e\) small.

set.seed(666)
simple.admix <- admix_graph_sim(n_per_pop = 20, p = 20000, 
                                c1 = 1, c2 = 1, c3 = 1, c4 = 1,
                                c5 = 0.5, c6 = 1, c7 = 0.5,
                                w = 0.25, sigma_e = sqrt(0.1))
plot_cov(simple.admix$covmat, as.is = TRUE)

Flash initialization (greedy)

Initial values

fl <- flash(simple.admix$Y, prior.family = c(prior.bimodal(), prior.normal()))
#> Adding factor 1 to flash object...
#> Adding factor 2 to flash object...
#> Adding factor 3 to flash object...
#> Adding factor 4 to flash object...
#> Adding factor 5 to flash object...
#> Adding factor 6 to flash object...
#> Adding factor 7 to flash object...
#> Factor doesn't significantly increase objective and won't be added.
#> Wrapping up...
#> Done.
#> Nullchecking 6 factors...
#> Done.
labs <- rep(c("A", "B", "C", "D"), each = 20)
plot_loadings(fl$flash.fit$EF[[1]], labs)

Drift results

drift.flg <- drift(init_from_flash(fl), miniter = 1000, maxiter = 2000, tol = 0.005, verbose = FALSE)
ggplot(drift.flg$iterations, aes(x = iter, y = elbo)) + geom_line()

drift.flg[c("elbo", "KL_l", "KL_f")]
#> $elbo
#> [1] -657080.1
#> 
#> $KL_l
#> [1] -182.98713 -166.44697 -139.19227  -76.30371 -171.65373  -45.12987
#> 
#> $KL_f
#> [1] -228422.4

plot_loadings(drift.flg$EL, labs,  paste("s2:", round(drift.flg$prior_s2, 2)))

plot_cov(drift.flg)

Flash initialization (backfit)

Initial values

fl <- fl %>% flash.backfit() %>% flash.nullcheck(remove = TRUE)
#> Backfitting 6 factors (tolerance: 2.38e-02)...
#>   Difference between iterations is within 1.0e+05...
#>   Difference between iterations is within 1.0e+04...
#>   Difference between iterations is within 1.0e+03...
#> Warning in estimate_mixprop(data, g, prior, optmethod = optmethod, control
#> = control, : Optimization failed to converge. Results may be unreliable.
#> Try increasing maxiter and rerunning.

#> Warning in estimate_mixprop(data, g, prior, optmethod = optmethod, control
#> = control, : Optimization failed to converge. Results may be unreliable.
#> Try increasing maxiter and rerunning.

#> Warning in estimate_mixprop(data, g, prior, optmethod = optmethod, control
#> = control, : Optimization failed to converge. Results may be unreliable.
#> Try increasing maxiter and rerunning.

#> Warning in estimate_mixprop(data, g, prior, optmethod = optmethod, control
#> = control, : Optimization failed to converge. Results may be unreliable.
#> Try increasing maxiter and rerunning.

#> Warning in estimate_mixprop(data, g, prior, optmethod = optmethod, control
#> = control, : Optimization failed to converge. Results may be unreliable.
#> Try increasing maxiter and rerunning.
#>   Difference between iterations is within 1.0e+02...
#> Warning in estimate_mixprop(data, g, prior, optmethod = optmethod, control
#> = control, : Optimization failed to converge. Results may be unreliable.
#> Try increasing maxiter and rerunning.

#> Warning in estimate_mixprop(data, g, prior, optmethod = optmethod, control
#> = control, : Optimization failed to converge. Results may be unreliable.
#> Try increasing maxiter and rerunning.

#> Warning in estimate_mixprop(data, g, prior, optmethod = optmethod, control
#> = control, : Optimization failed to converge. Results may be unreliable.
#> Try increasing maxiter and rerunning.

#> Warning in estimate_mixprop(data, g, prior, optmethod = optmethod, control
#> = control, : Optimization failed to converge. Results may be unreliable.
#> Try increasing maxiter and rerunning.

#> Warning in estimate_mixprop(data, g, prior, optmethod = optmethod, control
#> = control, : Optimization failed to converge. Results may be unreliable.
#> Try increasing maxiter and rerunning.
#>   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...
#> Wrapping up...
#> Done.
#> Nullchecking 6 factors...
#> Wrapping up...
#> Done.
plot_loadings(fl$flash.fit$EF[[1]], labs)

Drift results

drift.flb <- drift(init_from_flash(fl), miniter = 1000, maxiter = 2000, tol = 0.005, verbose = FALSE)
#> Warning in estimate_mixprop(data, g, prior, optmethod = optmethod, control
#> = control, : Optimization failed to converge. Results may be unreliable.
#> Try increasing maxiter and rerunning.
ggplot(drift.flb$iterations, aes(x = iter, y = elbo)) + geom_line()

drift.flb[c("elbo", "KL_l", "KL_f")]
#> $elbo
#> [1] -657046.6
#> 
#> $KL_l
#> [1] -306.93790 -207.32379 -113.06100  -45.12987
#> 
#> $KL_f
#> [1] -228394.9

plot_loadings(drift.flb$EL, labs, paste("s2:", round(drift.flb$prior_s2, 2)))

plot_cov(drift.flb)

Results summary

all.drift <- list(drift.flg, drift.flb)

res.df <- data.frame(
  Name = c("flash.greedy", "flash.backfit"),
  InitialELBO = sapply(all.drift, function(x) x$iterations$elbo[1]),
  FinalELBO = sapply(all.drift, function(x) x$elbo),
  ELBOdiff = sapply(all.drift, function(x) x$elbo - x$iterations$elbo[1]),
  n_iter = sapply(all.drift, function(x) max(x$iterations$iter)),
  KL_l = sapply(all.drift, function(x) sum(x$KL_l)),
  KL_f = sapply(all.drift, function(x) x$KL_f),
  ResidS2 = sapply(all.drift, function(x) x$resid_s2)
)

knitr::kable(res.df, digits = 3)
Name InitialELBO FinalELBO ELBOdiff n_iter KL_l KL_f ResidS2
flash.greedy -660071.7 -657080.1 2991.597 1373 -781.714 -228422.4 0.1
flash.backfit -657839.4 -657046.6 792.862 1000 -672.453 -228394.9 0.1


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.6 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     highr_0.8         httr_1.4.0       
#> [16] pillar_1.3.1      rlang_0.4.2       lazyeval_0.2.2   
#> [19] pscl_1.5.2        readxl_1.3.1      rstudioapi_0.10  
#> [22] ebnm_0.1-24       whisker_0.3-2     Matrix_1.2-15    
#> [25] rmarkdown_1.12    labeling_0.3      munsell_0.5.0    
#> [28] mixsqp_0.3-10     broom_0.5.1       compiler_3.5.3   
#> [31] modelr_0.1.5      xfun_0.6          pkgconfig_2.0.2  
#> [34] SQUAREM_2017.10-1 htmltools_0.3.6   tidyselect_0.2.5 
#> [37] workflowr_1.2.0   codetools_0.2-16  crayon_1.3.4     
#> [40] withr_2.1.2       MASS_7.3-51.1     grid_3.5.3       
#> [43] nlme_3.1-137      jsonlite_1.6      gtable_0.3.0     
#> [46] git2r_0.25.2      magrittr_1.5      scales_1.0.0     
#> [49] cli_1.1.0         stringi_1.4.3     fs_1.2.7         
#> [52] doParallel_1.0.14 xml2_1.2.0        generics_0.0.2   
#> [55] iterators_1.0.10  tools_3.5.3       glue_1.3.1       
#> [58] hms_0.4.2         parallel_3.5.3    yaml_2.2.0       
#> [61] colorspace_1.4-1  ashr_2.2-38       rvest_0.3.4      
#> [64] knitr_1.22        haven_2.1.1