Last updated: 2019-06-04

Checks: 7 0

Knit directory: daarem/analysis/

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


Untracked files:
    Untracked:  analysis/mixem.R
    Untracked:  analysis/temp.R
    Untracked:  analysis/temp.R~

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 3d5bcee Peter Carbonetto 2019-06-04 wflow_publish(“mixem.Rmd”)
html 615197f Peter Carbonetto 2019-06-04 A few more small improvements to the “mixem” analysis.
Rmd a79dd49 Peter Carbonetto 2019-06-04 wflow_publish(“mixem.Rmd”)
html 52e323d Peter Carbonetto 2019-06-04 Made some revisions to the mixem analysis.
Rmd 4743aea Peter Carbonetto 2019-06-04 wflow_publish(“mixem.Rmd”)
html 936cdbb Peter Carbonetto 2019-06-04 Built initial draft of “mixem” analysis.
Rmd 7048a42 Peter Carbonetto 2019-06-04 wflow_publish(“mixem.Rmd”)
Rmd 1636e77 Peter Carbonetto 2019-06-04 wflow_publish(“index.Rmd”)

Here we illustrate the use of DAAREM to accelerate a very simple EM algorithm—the E and M steps are implemented in three lines of R code—for computing maximum-likelihood estimates of mixture proportions in a mixture model.

Set up environment

Load some packages and function definitions used in the example below.

library(ggplot2)
library(cowplot)
library(daarem)
source("../code/misc.R")
source("../code/mixem.R")

Load data set

Load the 100,000 x 100 conditional likelihood matrix computed from a simulated data set.

load("../data/mixdata.RData")
n <- nrow(L)
m <- ncol(L)
cat(sprintf("Loaded %d x %d data matrix.\n",n,m))
# Loaded 100000 x 10 data matrix.

Set the initial estimate of the mixture proportions.

x0 <- rep(1/m,m)

Run basic EM updates

Compute maximum-likelihood estimates of the mixture proportions by running 200 iterations of the standard EM updates. Note that the E and M steps are very simple, and easy to implement in R; in particular, in function mixem.update, the E step is implemented in 2 lines of R code, and the M step requires only one more line of code.

out <- system.time(fit1 <- mixem(L,x0,numiter = 200))
f1  <- mixobjective(L,fit1$x)
cat(sprintf("Computation took %0.2f seconds.\n",out["elapsed"]))
cat(sprintf("Log-likelihood at EM estimate is %0.12f.\n",f1))
# Computation took 9.38 seconds.
# Log-likelihood at EM estimate is -59912.068371303445.

Run accelerated EM

Re-run the EM updates, this time using DAAREM to accelerate convergence toward the solution.

out <- system.time(fit2 <- mixdaarem(L,x0,numiter = 200))
f2  <- mixobjective(L,fit2$x)
cat(sprintf("Computation took %0.2f seconds.\n",out["elapsed"]))
cat(sprintf("Objective value at DAAREM estimate is %0.12f.\n",f2))
# Computation took 7.03 seconds.
# Objective value at DAAREM estimate is -59895.960056733769.

Observe that the this second estimate has a much higher likelihood.

Plot improvement in solution over time

This plot shows the improvement in the solution over time for the two co-ordinate ascent algorithms: the vertical axis (“distance to best solution”) shows the difference between the largest log-likelihood obtained, and the log-likelihood at the “gold-standard” solution. The gold-standard solution was computed using mixsqp.

f    <- mixobjective(L,x)
pdat <-
  rbind(data.frame(iter = 1:200,dist = f - fit1$value,method = "EM"),
        data.frame(iter = 1:200,dist = f - fit2$value,method = "DAAREM"))
p <- ggplot(pdat,aes(x = iter,y = dist,col = method)) +
  geom_line(size = 1) +
  scale_y_continuous(trans = "log10",breaks = 10^seq(-4,4)) +
  scale_color_manual(values = c("darkorange","dodgerblue")) +
  labs(x = "iteration",y = "distance from solution")
print(p)

Version Author Date
52e323d Peter Carbonetto 2019-06-04
936cdbb Peter Carbonetto 2019-06-04

From this plot, we see that the accelerated EM method gets much closer to the solution, although it seems to “plateau” after about 100 iterations. Nonetheless, it is much improved over the basic EM algorithm.


sessionInfo()
# R version 3.4.3 (2017-11-30)
# Platform: x86_64-apple-darwin15.6.0 (64-bit)
# Running under: macOS High Sierra 10.13.6
# 
# Matrix products: default
# BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/3.4/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] daarem_0.3    cowplot_0.9.4 ggplot2_3.1.0
# 
# loaded via a namespace (and not attached):
#  [1] Rcpp_1.0.1           knitr_1.20           whisker_0.3-2       
#  [4] magrittr_1.5         workflowr_1.3.0.9000 tidyselect_0.2.5    
#  [7] munsell_0.4.3        colorspace_1.4-0     R6_2.2.2            
# [10] rlang_0.3.1          dplyr_0.8.0.1        stringr_1.3.1       
# [13] plyr_1.8.4           tools_3.4.3          grid_3.4.3          
# [16] gtable_0.2.0         withr_2.1.2          git2r_0.25.2.9008   
# [19] htmltools_0.3.6      assertthat_0.2.0     yaml_2.2.0          
# [22] lazyeval_0.2.1       rprojroot_1.3-2      digest_0.6.17       
# [25] tibble_2.1.1         crayon_1.3.4         purrr_0.2.5         
# [28] fs_1.2.6             glue_1.3.0           evaluate_0.11       
# [31] rmarkdown_1.10       labeling_0.3         stringi_1.2.4       
# [34] pillar_1.3.1         compiler_3.4.3       scales_0.5.0        
# [37] backports_1.1.2      pkgconfig_2.0.2