Last updated: 2020-06-01

Checks: 7 0

Knit directory: drift-workflow/analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.6.1). 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(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 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 39fcbb8. 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:    .snakemake/
    Ignored:    data/datasets/
    Ignored:    data/raw/
    Ignored:    data/simulations/
    Ignored:    notebooks/.ipynb_checkpoints/
    Ignored:    output/
    Ignored:    sandbox/.ipynb_checkpoints/

Unstaged changes:
    Modified:   snakefiles/coalsim/Snakefile

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/OutOfAfrica_3G09_fix.Rmd) and HTML (docs/OutOfAfrica_3G09_fix.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 39fcbb8 Joseph Marcus 2020-06-01 added warnings to cov
html b65e497 Joseph Marcus 2020-06-01 Build site.
Rmd 5b4cd63 Joseph Marcus 2020-06-01 updated to larger sample size for ooa fixed sim
html cb9009e Joseph Marcus 2020-05-21 Build site.
Rmd 3549c6d Joseph Marcus 2020-05-21 wflow_publish(“index.Rmd”)
html f66648a Joseph Marcus 2020-05-20 Build site.
Rmd e4cdfd8 Joseph Marcus 2020-05-20 wflow_publish(“OutOfAfrica_3G09_fix.Rmd”)
html 1dde8eb Joseph Marcus 2020-05-20 Build site.
Rmd e42d06d Joseph Marcus 2020-05-20 wflow_publish(“OutOfAfrica_3G09_fix.Rmd”)
html 5f94116 Joseph Marcus 2020-05-18 Build site.
Rmd 59c3de9 Joseph Marcus 2020-05-18 wflow_publish(“OutOfAfrica_3G09_fix.Rmd”)
html 484ce56 Joseph Marcus 2020-05-18 Build site.
Rmd 36c05dd Joseph Marcus 2020-05-18 wflow_publish(“analysis/OutOfAfrica_3G09_fix.Rmd”)
Rmd 865cef4 Joseph Marcus 2020-05-18 added fixed shared factor
html 865cef4 Joseph Marcus 2020-05-18 added fixed shared factor

Here I visualize population structure with simulated data from the OutOfAfrica_3G09 scenario. See Figure 2. from Gutenkunst et al. 2009.

Below, I show a number of EBMF solutions and in each of them I don’t display the first shared factor which is prefixed to the one-vector and scale the loadings by the prior variance. I only describe the loadings that remain after the shared factor.

Imports

Import the required libraries and scripts:

suppressMessages({
  library(lfa)
  library(flashier)
  library(drift.alpha)
  library(ggplot2)
  library(RColorBrewer)
  library(reshape2)
  library(tidyverse)
  library(alstructure)
  source("../code/structure_plot.R")
})

Data

data_path <- "../output/simulations/OutOfAfrica_3G09/rep1.txt"
G <- t(as.matrix(read.table(data_path, sep=" ")))
colnames(G) <- NULL
rownames(G) <- NULL
n <- nrow(G)
daf <- colSums(G) / (2 * n)
colors <- brewer.pal(8, "Set2")

# filter out too rare and too common SNPs
Y <- G[,((daf>=.05) & (daf <=.95))]
p <- ncol(Y)
print(n)
[1] 300
print(p)
[1] 21492
# sub-population labels from stdpop
labs <- rep(c("YRI", "CEU", "HAN"), each=100)

we end up with 300 individuals and ~20000 SNPs. View fitted the sample covariance matrix:

plot_cov((1.0 / p) * Y %*% t(Y), as.is=T) + 
  scale_fill_viridis_c() + 
  labs(fill="Cov")
Scale for 'fill' is already present. Adding another scale for 'fill',
which will replace the existing scale.

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
865cef4 Joseph Marcus 2020-05-18

plot allele frequencies of Africa vs OOA populations:

daf_afr <- colSums(G[1:100,]) / (2*100)
daf_ooa <- colSums(G[101:300,]) / (2*200)
qplot(daf_ooa, daf_afr, alpha=.1)

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18
865cef4 Joseph Marcus 2020-05-18
hist(daf_afr)

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18
hist(daf_ooa)

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18

flash [greedy]

Run the greedy algorithm:

ones <- matrix(1, nrow = n, ncol = 1)
ls.soln <- t(solve(crossprod(ones), crossprod(ones, Y)))
fl <- flash.init(Y) %>%
  flash.init.factors(EF = list(ones, ls.soln), 
                     prior.family=c(prior.bimodal(), prior.normal())) %>%
  flash.fix.loadings(kset = 1, mode = 1L) %>%
  flash.backfit() %>%
  flash.add.greedy(Kmax=6, prior.family=c(prior.bimodal(), prior.normal()))
Backfitting 1 factors (tolerance: 9.61e-02)...
Wrapping up...
Done.
Adding factor 2 to flash object...
Adding factor 3 to flash object...
Adding factor 4 to flash object...
Adding factor 5 to flash object...
Factor doesn't significantly increase objective and won't be added.
Wrapping up...
Done.
sd <- unlist(lapply(fl$fitted.g[[2]], '[[', 3))
L <- fl$flash.fit$EF[[1]]
s2 <- fl$residuals.sd^2
LDsqrt <- L %*% diag(sd)
K <- ncol(LDsqrt)
plot_loadings(LDsqrt[,2:K], labs) + scale_color_brewer(palette="Set2")

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18
865cef4 Joseph Marcus 2020-05-18

view structure plot:

create_structure_plot(L=LDsqrt[,2:K], labels=labs, colors=colors)

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18
865cef4 Joseph Marcus 2020-05-18

view fitted covariance matrix:

plot_cov(LDsqrt %*% t(LDsqrt) + s2*diag(n), as.is=T) + 
  scale_fill_viridis_c() +
  labs(fill="Cov") 

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18
865cef4 Joseph Marcus 2020-05-18

the greedy algorithm finds 3 population specific factors.

flash [backfit]

Run flash [backfit] initializing from the greedy solution:

flbf <- fl %>% 
  flash.backfit() %>% 
  flash.nullcheck(remove=TRUE)
Backfitting 4 factors (tolerance: 9.61e-02)...
  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...
Wrapping up...
Done.
Nullchecking 4 factors...
Factor 2 removed, increasing objective by 5.814e-05.
Wrapping up...
Done.
sd <- unlist(lapply(flbf$fitted.g[[2]], '[[', 3))
s2 <- flbf$residuals.sd^2
L <- flbf$flash.fit$EF[[1]]
LDsqrt <- L %*% diag(sd)
K <- ncol(LDsqrt)
plot_loadings(LDsqrt[,2:K], labs) + scale_color_brewer(palette="Set2")

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18
865cef4 Joseph Marcus 2020-05-18

view structure plot:

create_structure_plot(L=LDsqrt[,2:K], labels=labs, colors=colors)

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18
865cef4 Joseph Marcus 2020-05-18

view fitted covariance matrix:

plot_cov(LDsqrt %*% t(LDsqrt) + s2*diag(n), as.is=T) + 
  scale_fill_viridis_c() +
  labs(fill="Cov")

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18
865cef4 Joseph Marcus 2020-05-18

the backfitting algorithm represents the data with a sparser solution and finds a factor represented by YRI and a small loading from Han and

drift

Run drift initializing from the greedy solution:

init <- init_from_data(Y, Kmax=6)
dr <- drift(init, miniter=2, 
            maxiter=1000, 
            tol=1e-5, 
            verbose=FALSE) 

sd <- sqrt(dr$prior_s2)
L <- dr$EL
LDsqrt <- L %*% diag(sd)
s2 <- dr$resid_s2
K <- ncol(LDsqrt)
plot_loadings(LDsqrt[,2:K], labs) + scale_color_brewer(palette="Set2")

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18
865cef4 Joseph Marcus 2020-05-18

view structure plot:

create_structure_plot(L=LDsqrt[,2:K], labels=labs, colors=colors)

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18
865cef4 Joseph Marcus 2020-05-18

view fitted covariance matrix:

plot_cov(LDsqrt %*% t(LDsqrt) + s2*diag(n), as.is=T) + 
  scale_fill_viridis_c() +
  labs(fill="Cov")

Version Author Date
b65e497 Joseph Marcus 2020-06-01
1dde8eb Joseph Marcus 2020-05-20
484ce56 Joseph Marcus 2020-05-18

the drift algorithm converges quickly and maintains the greedy solution. Lets try adding a factor:

dr2 <- drift(add_factor(dr), 
             miniter=2, 
             maxiter=1000, 
             tol=1e-5, 
             verbose=FALSE) 

sd <- sqrt(dr2$prior_s2)
L <- dr2$EL
LDsqrt <- L %*% diag(sd)
s2 <- dr2$resid_s2
K <- ncol(LDsqrt)
plot_loadings(LDsqrt[,2:K], labs) + scale_color_brewer(palette="Set2")

Version Author Date
b65e497 Joseph Marcus 2020-06-01

The additional factor does seem to capture much interesting structure.


sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Scientific Linux 7.4 (Nitrogen)

Matrix products: default
BLAS/LAPACK: /software/openblas-0.2.19-el7-x86_64/lib/libopenblas_haswellp-r0.2.19.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] alstructure_0.1.0  forcats_0.5.0      stringr_1.4.0     
 [4] dplyr_0.8.5        purrr_0.3.4        readr_1.3.1       
 [7] tidyr_1.0.2        tibble_3.0.1       tidyverse_1.3.0   
[10] reshape2_1.4.3     RColorBrewer_1.1-2 ggplot2_3.3.0     
[13] drift.alpha_0.0.9  flashier_0.2.4     lfa_1.9.0         

loaded via a namespace (and not attached):
 [1] httr_1.4.1        viridisLite_0.3.0 jsonlite_1.6     
 [4] modelr_0.1.6      assertthat_0.2.1  mixsqp_0.3-43    
 [7] cellranger_1.1.0  yaml_2.2.0        ebnm_0.1-24      
[10] pillar_1.4.3      backports_1.1.6   lattice_0.20-38  
[13] glue_1.4.0        digest_0.6.25     promises_1.0.1   
[16] rvest_0.3.5       colorspace_1.4-1  htmltools_0.3.6  
[19] httpuv_1.4.5      Matrix_1.2-15     plyr_1.8.4       
[22] pkgconfig_2.0.3   invgamma_1.1      broom_0.5.6      
[25] haven_2.2.0       corpcor_1.6.9     scales_1.1.0     
[28] whisker_0.3-2     later_0.7.5       git2r_0.26.1     
[31] farver_2.0.3      generics_0.0.2    ellipsis_0.3.0   
[34] withr_2.2.0       ashr_2.2-50       cli_2.0.2        
[37] magrittr_1.5      crayon_1.3.4      readxl_1.3.1     
[40] evaluate_0.14     fansi_0.4.1       fs_1.3.1         
[43] nlme_3.1-137      xml2_1.3.2        truncnorm_1.0-8  
[46] tools_3.5.1       hms_0.5.3         lifecycle_0.2.0  
[49] munsell_0.5.0     reprex_0.3.0      irlba_2.3.3      
[52] compiler_3.5.1    rlang_0.4.5       grid_3.5.1       
[55] rstudioapi_0.11   labeling_0.3      rmarkdown_1.10   
[58] gtable_0.3.0      DBI_1.0.0         R6_2.4.1         
[61] lubridate_1.7.4   knitr_1.20        workflowr_1.6.1  
[64] rprojroot_1.3-2   stringi_1.4.6     parallel_3.5.1   
[67] SQUAREM_2020.2    Rcpp_1.0.4.6      vctrs_0.2.4      
[70] dbplyr_1.4.3      tidyselect_1.0.0