Last updated: 2020-02-20

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:


Unstaged changes:
    Modified:   analysis/mr_ash_demo.Rmd

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 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 updating the mixture weights in mr-ash: using EM, and using mix-SQP.

Script parameters

These are the data simulation settings.

I suggest trying this example with s = 0 (all predictors are independent) and s = 0.4 (the correlation between all predictors is 0.4).

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

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")

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,maxiter.inner = 50,
                           tol.inner = 1e-8)
# iter                elbo max|w0-w0'| niter
#  100 -7.873409197069e+02 7.60892e-01    15
#  100 -7.597870942033e+02 3.92902e-02    11
#  100 -7.596906035040e+02 1.10491e-04     9
#  100 -7.596906001827e+02 4.52321e-07     6
#  100 -7.596906001829e+02 2.11100e-09     3
#  100 -7.596906001829e+02 4.93716e-12     1
#  100 -7.596906001829e+02 3.64420e-12     1
#  100 -7.596906001829e+02 1.62259e-13     1
#  100 -7.596906001829e+02 1.62134e-13     1
#  100 -7.596906001829e+02 3.88856e-14     1

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") +
  labs(title = "coefs") +
  theme_cowplot()

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") +
  labs(title = "coefs") +
  theme_cowplot()

Plot the improvement in the solution over time. The EM updates are shown in blue, and the mix-SQP updates are shown in orange.

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$niter),
                              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",title = paste("s =",s)) +
  theme_cowplot()


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