Last updated: 2020-05-14

Checks: 6 1

Knit directory: ebpmf_data_analysis/

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

The following chunks had caches available:
  • unnamed-chunk-3
  • unnamed-chunk-5
  • unnamed-chunk-7

To ensure reproducibility of the results, delete the cache directory ebpmf_bg_tutorial_cache and re-run the analysis. To have workflowr automatically delete the cache directory prior to building the file, set delete_cache = TRUE when running wflow_build() or wflow_publish().

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 442bdfe. 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:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    analysis/ebpmf_bg_tutorial_cache/

Untracked files:
    Untracked:  analysis/ebpmf_bg_initialization.Rmd
    Untracked:  code/misc.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 repository in which changes were made to the R Markdown (analysis/ebpmf_bg_tutorial.Rmd) and HTML (docs/ebpmf_bg_tutorial.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 442bdfe zihao12 2020-05-14 ebpmf_bg_tutorial.Rmd

Introduction

Our model \[\begin{align} & X_{ij} \sim Pois(l_{i0} f_{j0} \sum_k l_{ik} f_{jk})\\ & l_{ik} \sim g_{L, k}(.)\\ & f_{jk} \sim g_{F, k}(.) \end{align}\]

Although ebpmf_bg function has the option to use various prior families for \(l_{ik}, f_{jk}\), the focus is on \(g = \sum_l \pi_l Ga(1/\phi_l, 1/\phi_l)\) for both \(l_{ik}, f_{jk}\). The other options are not checked.

rm(list = ls())
library(Matrix)
library(ebpmf.alpha)
library(pheatmap)
library(gridExtra)
source("code/util.R")
source("code/misc.R")
set.seed(123)

load data and use subset for experiments

docname = "kos"
datadir = "data/uci_BoW"
filename = sprintf("docword.%s", docname)
format = "txt"
X = read_uci_bag_of_words(file= sprintf("%s/%s.%s",
          datadir,filename, format))
# dim(X)
# class(X)
# length(X@x)

## subset samples
row_sub = sample(x = 1:nrow(X), size = 500,replace = FALSE)
X_sub = X[row_sub,]
col_sub = which(colSums(X_sub) > 0)
X_sub = X_sub[, col_sub]
dim(X_sub)
[1]  500 6325
length(X_sub@x)
[1] 51487

run with default configuration

I specifically show how initialization is done (this is equivalent to using init = NULL).

K = 5
maxiter = 50
verbose = FALSE

## initialization
#init = ebpmf.alpha::initialize_qg_l0f0(X = X_sub, K = K, seed = 123)
initialize_qg_l0f0 <- function(X, K, seed = 123){
  set.seed(seed)
  n = nrow(X)
  p = ncol(X)
  L0 = matrix(exp(runif(n*K, min = -1.5, max = 1)), ncol = K)
  F0 = matrix(exp(runif(p*K, min = -1.5, max = 1)), ncol = K)
  qg = initialize_qg_from_LF(L0, F0)
  l0 = rowSums(X)
  denom <- colSums( t(qg$qfs_mean) * colSums(l0 * qg$qls_mean))
  f0 <- colSums(X)/denom
  ## initialize g
  aL = c(seq(0.01, 0.10, 0.01), seq(0.2, 0.9, 0.1), seq(1,15,2), 20, 50, 75, 100, 200, 1e3)
  D = length(aL)
  g = ebpm::gammamix(pi = replicate(D, 1/D), shape = aL, scale = 1/aL)
  qg$gls = replicate(K, list(g))
  qg$gfs = replicate(K, list(g))
  return(list(qg = qg, l0 = l0, f0 = f0))
}

init = initialize_qg_l0f0(X = X_sub, K = K, seed = 123)
names(init)
[1] "qg" "l0" "f0"
model1 = ebpmf.alpha::ebpmf_bg(X = X_sub, K = K,
                          maxiter = maxiter, verbose = verbose,
                          init = init,
                          pm_func = list(f = ebpm::ebpm_gamma_mixture,
                                         l = ebpm::ebpm_gamma_mixture),
                          fix_option = list(l0 = FALSE, f0 = FALSE, 
                                            gl = FALSE, ql = FALSE, 
                                            gf = FALSE, qf = FALSE))
plot(model1$ELBO)

get_prior_summary(model1$qg$gls)

get_prior_summary(model1$qg$gfs)

what if we want to fix \(f_{j0} f_{jk}\)

Sometimes, we run pNMF and a pair of \((l_{i0} l_{ik},f_{j0} f_{jk})\). We want to fix \(f\)s and shrink \(l\)s. How can we do this?

initialize_qg_l0f0_fixF <- function(X, L0, F0){
  l0 = apply(L0, 1, median)
  l0[l0 == 0] = 1e-8
  L0 = L0/l0
  qg = ebpmf.alpha::initialize_qg_from_LF(L0 = L0, F0 = F0)
  denom <- colSums( t(qg$qfs_mean) * colSums(l0 * qg$qls_mean))
  f0 <- colSums(X)/denom
  aL = c(seq(0.01, 0.10, 0.01), seq(0.2, 0.9, 0.1), seq(1,15,2), 20, 50, 75, 100, 200, 1e3)
  D = length(aL)
  g = ebpm::gammamix(pi = replicate(D, 1/D), shape = aL, scale = 1/aL)
  qg$gls = replicate(K, list(g))
  qg$gfs = replicate(K, list(NULL)) 
  return(list(qg = qg, l0 = l0, f0 = f0))
}

fit_pmf = NNLM::nnmf(A = as.matrix(X_sub), k = K, loss = "mkl", method = "scd", 
                     max.iter = 20, verbose = FALSE, show.warning = FALSE)
L0 = fit_pmf$W
F0 = t(fit_pmf$H)
init = initialize_qg_l0f0_fixF(X = X_sub, L0 = L0, F0 = F0)

model2 = ebpmf.alpha::ebpmf_bg(X = X_sub, K = K,
                          maxiter = maxiter, verbose = verbose,
                          init = init,
                          pm_func = list(f = ebpm::ebpm_gamma_mixture,## this will be ignored
                                         l = ebpm::ebpm_gamma_mixture),
                          fix_option = list(l0 = FALSE, f0 = TRUE, 
                                            gl = FALSE, ql = FALSE, 
                                            gf = TRUE, qf = TRUE))
## check if `f` remains unchanged
all(model2$qg$qfs_mean == init$qg$qfs_mean)
[1] TRUE
plot(model2$ELBO)

get_prior_summary(model2$qg$gls)

  • can we compare the two ELBOs, since the two models are different?
  • Seems that we can explore ways of initialization…

sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.14

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/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] gridExtra_2.3     pheatmap_1.0.12   ebpmf.alpha_0.3.9 Matrix_1.2-17    

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2          compiler_3.5.1      later_0.8.0        
 [4] RColorBrewer_1.1-2  git2r_0.26.1        workflowr_1.6.2    
 [7] tools_3.5.1         digest_0.6.22       evaluate_0.14      
[10] gtable_0.3.0        lattice_0.20-38     yaml_2.2.0         
[13] parallel_3.5.1      xfun_0.8            stringr_1.4.0      
[16] knitr_1.28          fs_1.3.1            gtools_3.8.1       
[19] RcppZiggurat_0.1.5  rprojroot_1.3-2     grid_3.5.1         
[22] Rfast_1.9.8         glue_1.3.1          R6_2.4.0           
[25] rmarkdown_2.1       mixsqp_0.3-40       irlba_2.3.3        
[28] magrittr_1.5        whisker_0.3-2       backports_1.1.5    
[31] scales_1.0.0        promises_1.0.1      htmltools_0.3.6    
[34] colorspace_1.4-1    httpuv_1.5.1        numDeriv_2016.8-1.1
[37] stringi_1.4.3       munsell_0.5.0       gsl_1.9-10.3       
[40] ebpm_0.0.0.9022