Last updated: 2020-01-15

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

Untracked files:
    Untracked:  analysis/admix_sim3.Rmd

Unstaged changes:
    Modified:   analysis/admix_sim2.Rmd
    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 93ec986 Jason Willwerscheid 2020-01-15 wflow_publish(“analysis/admix_sim4.Rmd”)
html fb2ed3f Jason Willwerscheid 2020-01-14 Build site.
Rmd 5657aa2 Jason Willwerscheid 2020-01-14 wflow_publish(“analysis/admix_sim4.Rmd”)

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

In this admixture simulation, I introduce a branching event. Namely, I allow two populations to drift until time \(t\), at which point the first population diverges into Populations 1 and 2. The three populations then each experience independent drift until time \(2t\), at which point Populations 2 and 4 admix in equal proportions:

Version Author Date
465356c Jason Willwerscheid 2020-01-14
898bc42 Jason Willwerscheid 2020-01-10

The covariance matrix appears as follows:

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

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14

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...
#> Factor doesn't significantly increase objective and won't be added.
#> Wrapping up...
#> Done.
#> Nullchecking 5 factors...
#> Done.
labs <- rep(c("A", "B", "C", "D"), each = 20)
plot_loadings(fl$flash.fit$EF[[1]], labs)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14

Drift results

drift.flg <- drift(init_from_flash(fl), miniter = 2, maxiter = 500, tol = 0.01, verbose = FALSE)
ggplot(drift.flg$elbo.df, aes(x = iter, y = elbo)) + geom_line()

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
drift.flg[c("elbo", "KL_l", "KL_f")]
#> $elbo
#> [1] -65978.01
#> 
#> $KL_l
#> [1]   -7.09820  -45.12987 -138.11891 -135.45788    0.00000
#> 
#> $KL_f
#> [1] -7733.409

lblr <- paste("s2:", round(drift.flg$prior_s2, 2))
names(lblr) <- 1:length(drift.flg$prior_s2)
lblr <- as_labeller(lblr)
plot_loadings(drift.flg$EL, labs, lblr)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
plot_cov(drift.flg$EL * rep(sqrt(drift.flg$prior_s2), each = 80))

Flash initialization (backfit)

Initial values

fl <- fl %>% flash.backfit() %>% flash.nullcheck(remove = TRUE)
#> Backfitting 5 factors (tolerance: 1.19e-03)...
#>   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...
#> Wrapping up...
#> Done.
#> Nullchecking 5 factors...
#> Wrapping up...
#> Done.
plot_loadings(fl$flash.fit$EF[[1]], labs)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14

Drift results

drift.flb <- drift(init_from_flash(fl), miniter = 2, maxiter = 500, tol = 0.01, verbose = FALSE)
ggplot(drift.flb$elbo.df, aes(x = iter, y = elbo)) + geom_line()

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
drift.flb[c("elbo", "KL_l", "KL_f")]
#> $elbo
#> [1] -66052.64
#> 
#> $KL_l
#> [1] -188.64520  -45.12987 -144.26448
#> 
#> $KL_f
#> [1] -7753.763

lblr <- paste("s2:", round(drift.flb$prior_s2, 2))
names(lblr) <- 1:length(drift.flb$prior_s2)
lblr <- as_labeller(lblr)
plot_loadings(drift.flb$EL, labs, lblr)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
plot_cov(drift.flb$EL * rep(sqrt(drift.flb$prior_s2), each = 80))

Hclust initialization

Initial values

init <- init_using_hclust(simple.admix$Y, k = 4)
plot_loadings(init$EL, labs)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14

Drift results

drift.hclust <- drift(init, miniter = 2, maxiter = 500, tol = 0.01, verbose = FALSE)
ggplot(drift.hclust$elbo.df, aes(x = iter, y = elbo)) + geom_line()

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
drift.hclust[c("elbo", "KL_l", "KL_f")]
#> $elbo
#> [1] -67207.06
#> 
#> $KL_l
#> [1]  1.307399e-12 -5.586292e+01 -5.586292e+01 -4.512987e+01 -4.512987e+01
#> [6] -7.105427e-15 -4.512987e+01
#> 
#> $KL_f
#> [1] -9026.252

lblr <- paste("s2:", round(drift.hclust$prior_s2, 2))
names(lblr) <- 1:length(drift.hclust$prior_s2)
lblr <- as_labeller(lblr)
plot_loadings(drift.hclust$EL, labs, lblr)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
plot_cov(drift.hclust$EL * rep(sqrt(drift.hclust$prior_s2), each = 80))

Initialization from “true” solution

Initial values

# I can't give init_from_EL a singular matrix, so I need to fudge the loadings a bit.
EL <- cbind(c(rep(1, 40), rep(0.25, 20), rep(0, 20)),
            c(rep(1, 20), rep(0, 60)),
            c(rep(0, 20), rep(1, 20), rep(0.5, 20), rep(0, 20)),
            c(rep(0, 40), rep(0.5, 20), rep(1, 20)))
init <- init_from_EL(simple.admix$Y, EL)
plot_loadings(init$EL, labs)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14

Drift results

drift.true <- drift(init, miniter = 2, maxiter = 500, tol = 0.01, verbose = FALSE)
ggplot(drift.true$elbo.df, aes(x = iter, y = elbo)) + geom_line()

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
drift.true[c("elbo", "KL_l", "KL_f")]
#> $elbo
#> [1] -66036.96
#> 
#> $KL_l
#> [1] -134.57291  -45.12987 -129.44262 -143.92071
#> 
#> $KL_f
#> [1] -7671.43

lblr <- paste("s2:", round(drift.true$prior_s2, 2))
names(lblr) <- 1:length(drift.true$prior_s2)
lblr <- as_labeller(lblr)
plot_loadings(drift.true$EL, labs, lblr)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
plot_cov(drift.true$EL * rep(sqrt(drift.true$prior_s2), each = 80))

Initialization using three factors

Initial values

EL <- cbind(c(rep(1, 20), rep(0, 60)),
            c(rep(0, 20), rep(1, 20), rep(0.5, 20), rep(0, 20)),
            c(rep(0, 40), rep(0.5, 20), rep(1, 20)))
init <- init_from_EL(simple.admix$Y, EL)
plot_loadings(init$EL, labs)

Drift results

drift.3factor <- drift(init, miniter = 2, maxiter = 500, tol = 0.01, verbose = FALSE)
ggplot(drift.3factor$elbo.df, aes(x = iter, y = elbo)) + geom_line()

drift.3factor[c("elbo", "KL_l", "KL_f")]
#> $elbo
#> [1] -66023.31
#> 
#> $KL_l
#> [1]  -45.12987 -139.63585 -143.91220
#> 
#> $KL_f
#> [1] -7772.31

lblr <- paste("s2:", round(drift.3factor$prior_s2, 2))
names(lblr) <- 1:length(drift.3factor$prior_s2)
lblr <- as_labeller(lblr)
plot_loadings(drift.3factor$EL, labs, lblr)

plot_cov(drift.3factor$EL * rep(sqrt(drift.3factor$prior_s2), each = 80))

Random initialization

Initial values

init <- init_using_hclust(simple.admix$Y, k = 4, as_tree = FALSE)
init$EL <- matrix(rnorm(80 * 4), ncol = 4)
init$EL2 <- init$EL^2
plot_loadings(init$EL, labs)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14

Drift results

drift.rand <- drift(init, miniter = 2, maxiter = 500, tol = 0.01, verbose = FALSE)
ggplot(drift.rand$elbo.df, aes(x = iter, y = elbo)) + geom_line()

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
drift.rand[c("elbo", "KL_l", "KL_f")]
#> $elbo
#> [1] -66456
#> 
#> $KL_l
#> [1] -2.108788e+02 -1.810760e+02 -1.566267e+02 -1.421085e-14
#> 
#> $KL_f
#> [1] -7983.783

lblr <- paste("s2:", round(drift.rand$prior_s2, 2))
names(lblr) <- 1:length(drift.rand$prior_s2)
lblr <- as_labeller(lblr)
plot_loadings(drift.rand$EL, labs, lblr)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
plot_cov(drift.rand$EL * rep(sqrt(drift.rand$prior_s2), each = 80))

One factor per population

Initial values

init <- init_using_hclust(simple.admix$Y, k = 4, as_tree = FALSE)
plot_loadings(init$EL, labs)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14

Drift results

drift.perpop <- drift(init, miniter = 2, maxiter = 500, tol = 0.01, verbose = FALSE)
ggplot(drift.perpop$elbo.df, aes(x = iter, y = elbo)) + geom_line()

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
drift.perpop[c("elbo", "KL_l", "KL_f")]
#> $elbo
#> [1] -67865.59
#> 
#> $KL_l
#> [1] -45.12987 -45.12987 -66.30345 -45.12987
#> 
#> $KL_f
#> [1] -9726.515

lblr <- paste("s2:", round(drift.perpop$prior_s2, 2))
names(lblr) <- 1:length(drift.perpop$prior_s2)
lblr <- as_labeller(lblr)
plot_loadings(drift.perpop$EL, labs, lblr)

Version Author Date
fb2ed3f Jason Willwerscheid 2020-01-14
plot_cov(drift.perpop$EL * rep(sqrt(drift.perpop$prior_s2), each = 80))

Results summary

all.drift <- list(drift.flg, drift.flb, drift.hclust, drift.true, drift.3factor, drift.rand, drift.perpop)

res.df <- data.frame(
  Name = c("flash.greedy", "flash.backfit", "hclust", "true.4factor", "three.factors", "random", "one.factor.per.pop"),
  InitialELBO = sapply(all.drift, function(x) x$elbo.df$elbo[1]),
  FinalELBO = sapply(all.drift, function(x) x$elbo),
  ELBOdiff = sapply(all.drift, function(x) x$elbo - x$elbo.df$elbo[1]),
  n_iter = sapply(all.drift, function(x) max(x$elbo.df$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 -66879.59 -65978.01 901.575 98 -325.805 -7733.409 0.249
flash.backfit -66054.71 -66052.64 2.070 16 -378.040 -7753.763 0.249
hclust -67704.69 -67207.06 497.625 194 -247.115 -9026.252 0.249
true.4factor -66206.07 -66036.96 169.116 28 -453.066 -7671.430 0.249
three.factors -66030.96 -66023.31 7.653 7 -328.678 -7772.310 0.249
random -133446.73 -66456.00 66990.736 156 -548.582 -7983.783 0.249
one.factor.per.pop -67889.72 -67865.59 24.138 50 -201.693 -9726.515 0.249


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