Last updated: 2020-02-24

Checks: 7 0

Knit directory: mr-ash/analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.6.0). 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(1) 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 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:


working directory clean

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 d02866e Peter Carbonetto 2020-02-24 Fixed bugs in mr_ash_with_mixsqp and updated em_vs_mixsqp with Youngseok’s test case.
html d02866e Peter Carbonetto 2020-02-24 Fixed bugs in mr_ash_with_mixsqp and updated em_vs_mixsqp with Youngseok’s test case.
html 23ad0c2 Peter Carbonetto 2020-02-20 Made a few adjustments to the plots in the em_vs_mixsqp analysis.
Rmd 1e5af9f Peter Carbonetto 2020-02-20 wflow_publish(“em_vs_mixsqp.Rmd”)
html 4cbeb36 Peter Carbonetto 2020-02-20 Built first draft of the em_vs_mixsqp analysis.
html 38f8a38 Peter Carbonetto 2020-02-20 Build site.
Rmd 825688b Peter Carbonetto 2020-02-20 wflow_publish(“em_vs_mixsqp.Rmd”)
Rmd 582e465 Peter Carbonetto 2020-02-20 Created first draft of mr_ash_demo workflowr page.

Here we compare two different ways of implementing the mixture weight updates in mr-ash: EM and mix-SQP.

Script parameters

These are the data simulation settings.

n  <- 100
p  <- 400
sd <- c(0,   1,    2)
w  <- c(0.9, 0.05, 0.05)
s  <- 0.1

This specifies the variances for the mixture-of-normals prior on the regression coefficients.

s0 <- 10^seq(-4,0,length.out = 12)

Load functions

These are the packages used in this analysis.

library(ggplot2)
library(cowplot)
library(MASS)
library(mixsqp)

This R code provides a very simple implementation of the mr-ash algorithm.

source("../code/misc.R")
source("../code/mr_ash.R")
source("../code/mr_ash_with_mixsqp.R")

Simulate data

The predictors are drawn from the multivariate normal with zero mean and covariance matrix S, in which all diagonal entries are 1, and all off-diagonal entries are s. Setting s = 0.5 reproduces the simulation of the predictors used in Example 3 of Zou & Hastie (2005).

set.seed(2)
S       <- matrix(s,p,p)
diag(S) <- 1
X       <- mvrnorm(n,rep(0,p),S)
k       <- sample(length(w),p,replace = TRUE,prob = w)
beta    <- sd[k] * rnorm(p)
y       <- drop(X %*% beta + rnorm(n))

Fit model

These are the initial estimates of residual variance (s), mixture weights (w0), and posterior mean estimates of the regression coefficients (b).

k  <- length(s0)
se <- 1
w0 <- rep(1/k,k)
b  <- rep(0,p)

Fit the model by running 200 EM updates for the mixture weights.

fit1 <- mr_ash(X,y,se,s0,w0,b,maxiter = 200,verbose = FALSE)

Fit the model a second time using the mix-SQP updates for the mixture weights.

fit2 <- mr_ash_with_mixsqp(X,y,se,s0,w0,b,numiter = 10)
# iter                elbo max|b-b'| max|w0-w0'|  EM mix   alpha
#    1 -3.786110149612e+02 5.782e-05 4.59556e-01  40  14 5.0e-01
#    2 -3.742189639930e+02 4.261e-05 2.12984e-01  13  17 5.0e-01
#    3 -3.739975329896e+02 7.374e-05 1.77119e-01  12  15 1.0e+00
#    4 -3.738117653767e+02 3.802e-05 1.15550e-02  14  16 5.0e-01
#    5 -3.737628735795e+02 9.657e-05 3.12331e-03  10  15 5.0e-01
#    6 -3.737604578864e+02 8.854e-05 1.31017e-03   8  15 5.0e-01
#    7 -3.737599689561e+02 3.380e-05 4.95211e-04   8  15 5.0e-01
#    8 -3.737599026351e+02 8.254e-05 1.92471e-04   6  15 5.0e-01
#    9 -3.737598918976e+02 5.168e-05 7.11962e-05   5  15 5.0e-01
#   10 -3.737598904765e+02 6.541e-05 1.65833e-05   3  15 2.5e-01

Review model fit

Compare the posterior mean estimates from the two fits.

ggplot(data.frame(em = fit1$b,mixsqp = fit2$b),
             aes(x = em,y = mixsqp)) +
  geom_point(color = "darkblue") +
  geom_abline(intercept = 0,slope = 1,col = "magenta",lty = "dotted") +
  theme_cowplot()

Version Author Date
d02866e Peter Carbonetto 2020-02-24
23ad0c2 Peter Carbonetto 2020-02-20

Compare the posterior mean estimates against the values used to simulate the data.

ggplot(data.frame(true = beta,mixsqp = fit2$b),
             aes(x = true,y = mixsqp)) +
  geom_point(color = "darkblue") +
  geom_abline(intercept = 0,slope = 1,col = "magenta",lty = "dotted") +
  theme_cowplot()

Version Author Date
d02866e Peter Carbonetto 2020-02-24
23ad0c2 Peter Carbonetto 2020-02-20

Plot the improvement in the solution over time.

elbo.best <- max(c(fit1$elbo,fit2$elbo))
pdat      <- rbind(data.frame(update = "em",
                              iter   = 1:length(fit1$elbo),
                              elbo   = fit1$elbo),
                   data.frame(update = "mixsqp",
                              iter   = cumsum(fit2$numem),
                              elbo   = fit2$elbo))
pdat$elbo <- elbo.best - pdat$elbo + 1e-4
ggplot(pdat,aes(x = iter,y = elbo,color = update)) +
  geom_line() +
  geom_point() +
  scale_y_log10() +
  scale_color_manual(values = c("royalblue","darkorange")) +
  labs(y = "distance to \"best\" elbo") +
  theme_cowplot()

Version Author Date
d02866e Peter Carbonetto 2020-02-24
23ad0c2 Peter Carbonetto 2020-02-20

sessionInfo()
# R version 3.6.2 (2019-12-12)
# Platform: x86_64-apple-darwin15.6.0 (64-bit)
# Running under: macOS Catalina 10.15.3
# 
# Matrix products: default
# BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/3.6/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] mixsqp_0.3-17 MASS_7.3-51.4 cowplot_1.0.0 ggplot2_3.2.1
# 
# loaded via a namespace (and not attached):
#  [1] Rcpp_1.0.3       compiler_3.6.2   pillar_1.4.3     later_1.0.0     
#  [5] git2r_0.26.1     workflowr_1.6.0  tools_3.6.2      digest_0.6.23   
#  [9] lattice_0.20-38  evaluate_0.14    lifecycle_0.1.0  tibble_2.1.3    
# [13] gtable_0.3.0     pkgconfig_2.0.3  rlang_0.4.2      Matrix_1.2-18   
# [17] yaml_2.2.0       xfun_0.11        withr_2.1.2      stringr_1.4.0   
# [21] dplyr_0.8.3      knitr_1.26       fs_1.3.1         rprojroot_1.3-2 
# [25] grid_3.6.2       tidyselect_0.2.5 glue_1.3.1       R6_2.4.1        
# [29] rmarkdown_2.0    irlba_2.3.3      farver_2.0.1     purrr_0.3.3     
# [33] magrittr_1.5     whisker_0.4      backports_1.1.5  scales_1.1.0    
# [37] promises_1.1.0   htmltools_0.4.0  assertthat_0.2.1 colorspace_1.4-1
# [41] httpuv_1.5.2     labeling_0.3     stringi_1.4.3    lazyeval_0.2.2  
# [45] munsell_0.5.0    crayon_1.3.4