Last updated: 2020-03-09

Checks: 7 0

Knit directory: mr-ash/analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.6.0.9000). 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 results in this page were generated with repository version 9817421. 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:


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 repository in which changes were made to the R Markdown (analysis/em_vs_mixsqp.Rmd) and HTML (docs/em_vs_mixsqp.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
html d837f44 Peter Carbonetto 2020-02-24 Adjusted plots once more.
Rmd 9dfd255 Peter Carbonetto 2020-02-24 wflow_publish(“em_vs_mixsqp.Rmd”)
html 1ecedf8 Peter Carbonetto 2020-02-24 Adjusted plot in em_vs_mixsqp example.
Rmd 2dbdb6b Peter Carbonetto 2020-02-24 wflow_publish(“em_vs_mixsqp.Rmd”)
html 87b14e2 Peter Carbonetto 2020-02-24 Revised plots in em_vs_mixsqp example.
Rmd 2effbd8 Peter Carbonetto 2020-02-24 wflow_publish(“em_vs_mixsqp.Rmd”)
html bdbb6c9 Peter Carbonetto 2020-02-24 Build site.
Rmd 8cb0fea Peter Carbonetto 2020-02-24 wflow_publish(“em_vs_mixsqp.Rmd”)
html 9d1d7d6 Peter Carbonetto 2020-02-24 Re-built em_vs_mixsqp after making the above updates.
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. In this example, we will see that the mix-SQP updates provide a much better fit to the data.

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 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. The “EM”, “mix” and “alpha” columns give, for each iteration, the number of co-ordinate ascent (“inner loop”) updates run, the number of mix-SQP iterations performed, and the step size for the mix-SQP update (as determined by backtracking line search).

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.809136000542e+02 5.782e-05 4.59556e-01  40  13 5.0e-01
#    2 -3.765215490860e+02 4.261e-05 2.12984e-01  13  16 5.0e-01
#    3 -3.763001180871e+02 7.374e-05 1.77119e-01  12  15 1.0e+00
#    4 -3.761143504846e+02 3.802e-05 1.15550e-02  14  15 5.0e-01
#    5 -3.760654586726e+02 9.657e-05 3.12331e-03  10  15 5.0e-01
#    6 -3.760630429798e+02 8.854e-05 1.31017e-03   8  15 5.0e-01
#    7 -3.760625540490e+02 3.380e-05 4.95211e-04   8  15 5.0e-01
#    8 -3.760624877281e+02 8.254e-05 1.92471e-04   6  15 5.0e-01
#    9 -3.760624769906e+02 5.168e-05 7.11962e-05   5  15 5.0e-01
#   10 -3.760624755695e+02 6.541e-05 1.65833e-05   3  15 2.5e-01

Review model fit

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

The algorithm with the mix-SQP mixture weight updates provides a much better fit to the data (as measured by the ELBO).

Next, compare the posterior mean estimates against the values used to simulate the data.

p1 <- ggplot(data.frame(true = beta,em = fit1$b),
             aes(x = true,y = em)) +
  geom_point(color = "darkblue") +
  geom_abline(intercept = 0,slope = 1,col = "magenta",lty = "dotted") +
  xlim(-4,4) +
  ylim(-4,4) +
  theme_cowplot()
p2 <- 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") +
  xlim(-4,4) +
  ylim(-4,4) +
  theme_cowplot()
plot_grid(p1,p2)

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

In this next plot, we directly compare the posterior mean coefficients provided by the two algorithms:

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") +
  xlim(-2.25,1) +
  ylim(-2.25,1) +
  theme_cowplot()

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

The EM estimates of the mixture weights cause the coefficients to be “shrunk” much more toward zero than the mix-SQP estimates. Additionally, the mix-SQP estimates of the mixture weights are much more sparse:

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

Version Author Date
d837f44 Peter Carbonetto 2020-02-24
1ecedf8 Peter Carbonetto 2020-02-24
87b14e2 Peter Carbonetto 2020-02-24

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-30 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        
#  [4] later_1.0.0          git2r_0.26.1         workflowr_1.6.0.9000
#  [7] tools_3.6.2          digest_0.6.23        lattice_0.20-38     
# [10] evaluate_0.14        lifecycle_0.1.0      tibble_2.1.3        
# [13] gtable_0.3.0         pkgconfig_2.0.3      rlang_0.4.2         
# [16] Matrix_1.2-18        yaml_2.2.0           xfun_0.11           
# [19] withr_2.1.2          stringr_1.4.0        dplyr_0.8.3         
# [22] knitr_1.26           fs_1.3.1             rprojroot_1.3-2     
# [25] grid_3.6.2           tidyselect_0.2.5     glue_1.3.1          
# [28] R6_2.4.1             rmarkdown_2.0        irlba_2.3.3         
# [31] farver_2.0.1         purrr_0.3.3          magrittr_1.5        
# [34] whisker_0.4          backports_1.1.5      scales_1.1.0        
# [37] promises_1.1.0       htmltools_0.4.0      assertthat_0.2.1    
# [40] colorspace_1.4-1     httpuv_1.5.2         labeling_0.3        
# [43] stringi_1.4.3        lazyeval_0.2.2       munsell_0.5.0       
# [46] crayon_1.3.4