Last updated: 2019-10-08

Checks: 7 0

Knit directory: SMF/

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


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    analysis/.DS_Store

Unstaged changes:
    Deleted:    analysis/SmoothMFpoisson.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
html 8c12025 Dongyue Xie 2019-07-23 Build site.
Rmd 2e047e3 Dongyue Xie 2019-07-23 wflow_publish(c(“analysis/SmoothMF.Rmd”, “analysis/SmoothPMD.Rmd”))

Introduction

Penalized matrix decomposition finds sparse \(L,F\) such that \(Y \approx LF^T\). It also has the option to give smooth estimate of factors in \(F\) using fused lasso. Here, we compare PMD-smooth, WaveletPMD and WaveletEBMF.

library(wavethresh)
library(flashr)
library(PMA)
# wavelet-based matrix factorization
#'@ y: observed matrix
#'@ k: number of factors
#'@ filter.number, family: wavelet type

WaveEBMF = function(y,k,filter.number = 1,family = 'DaubExPhase'){
  N=nrow(y)
  p=ncol(y)
  W = GenW(n=p,filter.number = filter.number,family = family)
  y_tilde = y%*%W
  f2 = flash(y_tilde,Kmax=k,var_type = 'constant',backfit = T,verbose=F)
  f2_fitted = flash_get_ldf(f2)
  f_hat = (W%*%f2_fitted$f)
  return(list(f=f_hat,l=f2_fitted$l))
}

WavePMD = function(y,k,filter.number = 1,family = 'DaubExPhase'){
  N=nrow(y)
  p=ncol(y)
  W = GenW(n=p,filter.number = filter.number,family = family)
  y_tilde = y%*%W
  param = PMD.cv(y_tilde,type='standard')
  f2 = PMD(y_tilde,type='standard',sumabs = param$bestsumabs,K=k)
  f_hat = (W%*%f2$v)
  return(list(f=f_hat,l=f2$u))
}

A single factor example

Simulate \(N=200\) and \(p=256\) under single-factor model \[l_i\sim \pi_0\delta_0+(1-\pi_0)\sum_{m=1}^5\frac{1}{5}N(0,\sigma^2_m)\]

Step function factor

\(f\) is a step function.

rmse = function(x1,x2){sqrt(mean((x1-x2)^2))}
set.seed(12345)
N = 200
p = 256
pi0 = 0.3
f = c(rep(2,p/4), rep(5, p/4), rep(6, p/4), rep(2, p/4))
l = c()
for (i in 1:N) {
  r = rbinom(1,1,pi0)
  if(r==1){
    l[i]=0
  }else{
      l[i] = mean(c(rnorm(1,0,sqrt(0.25)),rnorm(1,0,sqrt(0.5)),rnorm(1,0,sqrt(1)),
                    rnorm(1,0,sqrt(2)),rnorm(1,0,sqrt(4))))
      }
}
y = l%*%t(f)+matrix(rnorm(N*p,0,1),ncol=p)

# apply flash directly
f1 = WaveEBMF(y,k=1)
f2 = WavePMD(y,k=1)
 Fold  1  out of  5 
 Fold  2  out of  5 
 Fold  3  out of  5 
 Fold  4  out of  5 
 Fold  5  out of  5 

1
f3 = PMD.cv(y,type='ordered')
 Fold  1  out of  5 
12345678910 Fold  2  out of  5 
12345678910 Fold  3  out of  5 
12345678910 Fold  4  out of  5 
12345678910 Fold  5  out of  5 
12345678910
f3 = PMD(y,'ordered',sumabsu = f3$bestsumabsu, K=1)
1234
plot(f/norm(f,'2'),col='grey80',xlab='',ylab='',main='Estimate of factors',ylim=c(0,0.11))
lines(f1$f,col=4)
lines(-f2$f,col=2)
lines(f3$v,col=3)
legend('topleft',c('true f','WaveEBMF','WavePMD','PMD-smooth'),col=c('grey80',4,2,3),lty=c(1,1,1,1))

Version Author Date
8c12025 Dongyue Xie 2019-07-23

HeavySine function factor

f=DJ.EX(p,signal = 2)$heavi
y = l%*%t(f)+matrix(rnorm(N*p,0,1),ncol=p)

# apply flash directly
f1 = WaveEBMF(y,k=1,filter.number = 10,family = 'DaubLeAsymm')
f2 = WavePMD(y,k=1,filter.number = 10,family = 'DaubLeAsymm')
 Fold  1  out of  5 
 Fold  2  out of  5 
 Fold  3  out of  5 
 Fold  4  out of  5 
 Fold  5  out of  5 

1
f3 = PMD.cv(y,type='ordered')
 Fold  1  out of  5 
12345678910 Fold  2  out of  5 
12345678910 Fold  3  out of  5 
12345678910 Fold  4  out of  5 
12345678910 Fold  5  out of  5 
12345678910
f3 = PMD(y,'ordered',sumabsu = f3$bestsumabsu, K=1)
12345
plot(f/norm(f,'2'),col='grey80',xlab='',ylab='',main='Estimate of factors',ylim=c(-0.15,0.11))
lines(f1$f,col=4)
lines(f2$f,col=2)
lines(f3$v,col=3)
legend('topright',c('true f','WaveEBMF','WavePMD','PMD-smooth'),col=c('grey80',4,2,3),lty=c(1,1,1,1))

Version Author Date
8c12025 Dongyue Xie 2019-07-23

Spike function factor

spike.f = function(x) (0.75 * exp(-500 * (x - 0.23)^2) + 1.5 * exp(-2000 * (x - 0.33)^2) + 3 * exp(-8000 * (x - 0.47)^2) + 2.25 * exp(-16000 * 
    (x - 0.69)^2) + 0.5 * exp(-32000 * (x - 0.83)^2))

t = 1:p/p
f = 2*spike.f(t)

y = l%*%t(f)+matrix(rnorm(N*p,0,1),ncol=p)

# apply flash directly
f1 = WaveEBMF(y,k=1,filter.number = 10,family = 'DaubLeAsymm')
f2 = WavePMD(y,k=1,filter.number = 10,family = 'DaubLeAsymm')
 Fold  1  out of  5 
 Fold  2  out of  5 
 Fold  3  out of  5 
 Fold  4  out of  5 
 Fold  5  out of  5 

1
f3 = PMD.cv(y,type='ordered')
 Fold  1  out of  5 
12345678910 Fold  2  out of  5 
12345678910 Fold  3  out of  5 
12345678910 Fold  4  out of  5 
12345678910 Fold  5  out of  5 
12345678910
f3 = PMD(y,'ordered',sumabsu = f3$bestsumabsu, K=1)
12345
plot(f/norm(f,'2'),col='grey80',xlab='',ylab='',main='Estimate of factors',ylim=c(-0.05,0.5))
lines(f1$f,col=4)
lines(-f2$f,col=2)
lines(-f3$v,col=3)
legend('topright',c('true f','WaveEBMF','WavePMD','PMD-smooth'),col=c('grey80',4,2,3),lty=c(1,1,1,1))

Version Author Date
8c12025 Dongyue Xie 2019-07-23

sessionInfo()
R version 3.6.1 (2019-07-05)
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.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] PMA_1.1          flashr_0.6-6     wavethresh_4.6.8 MASS_7.3-51.4   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2        plyr_1.8.4        pillar_1.4.2     
 [4] compiler_3.6.1    git2r_0.26.1      workflowr_1.4.0  
 [7] iterators_1.0.12  tools_3.6.1       digest_0.6.21    
[10] tibble_2.1.3      evaluate_0.14     gtable_0.3.0     
[13] lattice_0.20-38   pkgconfig_2.0.3   rlang_0.4.0      
[16] Matrix_1.2-17     foreach_1.4.7     yaml_2.2.0       
[19] parallel_3.6.1    xfun_0.10         dplyr_0.8.3      
[22] stringr_1.4.0     knitr_1.25        fs_1.3.1         
[25] tidyselect_0.2.5  rprojroot_1.3-2   grid_3.6.1       
[28] impute_1.58.0     glue_1.3.1        R6_2.4.0         
[31] rmarkdown_1.16    mixsqp_0.1-97     reshape2_1.4.3   
[34] purrr_0.3.2       ggplot2_3.2.1     ashr_2.2-38      
[37] magrittr_1.5      whisker_0.4       backports_1.1.5  
[40] scales_1.0.0      codetools_0.2-16  htmltools_0.4.0  
[43] assertthat_0.2.1  softImpute_1.4    colorspace_1.4-1 
[46] stringi_1.4.3     lazyeval_0.2.2    doParallel_1.0.15
[49] pscl_1.5.2        munsell_0.5.0     truncnorm_1.0-8  
[52] SQUAREM_2017.10-1 crayon_1.3.4