Last updated: 2023-03-31

Checks: 7 0

Knit directory: gsmash/

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(20220606) 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 706f6cd. 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/

Untracked files:
    Untracked:  analysis/compare_vga_iterations_pbmc_full.Rmd
    Untracked:  analysis/droplet_results_full.Rmd
    Untracked:  analysis/pbmc3k_10X_othermethods.Rmd
    Untracked:  data/tpm3.rds
    Untracked:  output/pbmc3k_k1.rds
    Untracked:  output/pbmc3k_k1_S1.rds
    Untracked:  output/plots/
    Untracked:  output/tpm3_fit_fasttopics.rds
    Untracked:  output/tpm3_fit_stm.rds
    Untracked:  output/tpm3_fit_stm_slow.rds

Unstaged changes:
    Modified:   analysis/index.Rmd
    Modified:   analysis/normal_mean_penalty.Rmd
    Modified:   analysis/overdispersed_splitting.Rmd
    Modified:   analysis/remove_log_in_penalty.Rmd
    Modified:   code/ebpmf_identity/gtex.R
    Modified:   code/ebpmf_identity/run_real.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/normal_mean_penalized_optimization.Rmd) and HTML (docs/normal_mean_penalized_optimization.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 706f6cd DongyueXie 2023-03-31 wflow_publish("analysis/normal_mean_penalized_optimization.Rmd")
html 90a42fb Dongyue Xie 2022-09-29 Build site.
Rmd c7c5d57 Dongyue Xie 2022-09-29 add gradient
Rmd b127423 Dongyue Xie 2022-09-28 fix path to code
html f193c4c Dongyue Xie 2022-09-28 Build site.
Rmd bb07d43 Dongyue Xie 2022-09-28 fixe plot title
html 4f44ec8 Dongyue Xie 2022-09-28 Build site.
Rmd d29ca8a Dongyue Xie 2022-09-28 revise knowned g case, fixed a bug in l_nm_d1
html be758f2 Dongyue Xie 2022-09-27 Build site.
Rmd 33ecc09 Dongyue Xie 2022-09-27 wflow_publish("analysis/normal_mean_penalized_optimization.Rmd")
Rmd e50df06 Dongyue Xie 2022-08-28 add un-knited files

Introduction

\[y|\mu\sim N(\mu,s^2),\mu\sim g(\cdot)\]

Optimization: \(\theta = E_q\mu\),

\[\min_{\theta,g}h(\theta,g) = \frac{1}{2s^2}(y-\theta)^2+\rho_{g,s}(\theta).\]

\[S_{g,s^2}(z) = z +s^2l'_{NM}(z;g,s^2)\]

The penalty term is only tractable at \(\rho(S(\cdot))\). There are two ways to accommodate this.

Define the penalty evaluated at \(S_{g,s^2}(\cdot)\) as \(c_{g,s^2}(\cdot):=\rho_{g,s^2}(S_{g,s^2}(\cdot))\).

  1. Compound way

Let \(\theta = S_{g,s^2}(z)\) such that \(\rho_{g,s^2}(\theta) = c_{g,s^2}(z)\), the optimization problem is

\[\min_{z,g} \tilde h(z,g) = \frac{1}{2s^2}(y-S_{g,s^2}(z))^2-l_{NM}(z;g,s^2)-(z-S_{g,s^2}(z))^2/(2s^2)\]

\[\min_{z,g} \tilde h(z,g) = \frac{1}{2s^2}(y-z-s^2l'_{NM}(z;g,s^2))^2-l_{NM}-s^2(l'_{NM})^2/2\]

Then set \(\hat\theta = S(z)\).

  1. Inversion way

Note that \(c_{g,s^2}(S^{-1}_{g,s^2}(\theta)) = \rho_{g,s^2}(\theta)\), so we can write the optimization problem as

\[\min_{\theta,g} h(\theta,g) = \frac{1}{2s^2}(y-\theta)^2-c_{g,s^2}(S^{-1}_{g,s^2}(\theta))\]

For the inversion method implementation and illustration, see here

library(vebpm)

generate data, and get grid by running ash

set.seed(12345)
n = 200
w0 = 0.9
mu = c(rep(0,round(n*w0)),rep(10,n-round(n*w0)))
w_true = c(w0,1-w0)
grid_true = c(0.01,7)
s = rep(1,n)
y = rnorm(n,mu,s)
library(ashr)
grids = ebnm:::get_ashr_grid(y,s)

system.time(fit.ash <- ash(y,s,mixcompdist = 'normal',pointmass=FALSE,prior='uniform',mixsd=grids))
   user  system elapsed 
  0.037   0.000   0.037 
system.time(fit_inv <- ebnm_penalized_inversion(y,s,g_init = list(sd=grids)))
   user  system elapsed 
  4.014   0.084   4.098 
system.time(fit_compound <- ebnm_penalized_compound(y,s,g_init = list(sd=grids)))
   user  system elapsed 
 45.542   0.143  45.685 
library(ggplot2)
library(gridExtra)

# plot 1
p1 <- ggplot() + 
  geom_point(aes(x = 1:length(y), y = y), col = 'grey80', pch = 20) + 
  geom_line(aes(x = 1:length(mu), y = mu), col = 'grey60') + 
  geom_line(aes(x = 1:length(fit.ash$result$PosteriorMean), y = fit.ash$result$PosteriorMean)) + 
  labs(title = 'ash', x = '', y = '') +
  scale_color_manual(values = c('grey80', 'grey60', 'black')) +
  guides(colour = guide_legend(override.aes = list(pch = c(20, NA, NA), lty = c(NA, 1, 1)))) +
  theme_bw()

# plot 2
p2 <- ggplot() + 
  geom_point(aes(x = 1:length(y), y = y), col = 'grey80', pch = 20) + 
  geom_line(aes(x = 1:length(mu), y = mu), col = 'grey60') + 
  geom_line(aes(x = 1:length(fit_inv$posterior$mean), y = fit_inv$posterior$mean)) + 
  labs(title = 'inversion', x = '', y = '') +
  scale_color_manual(values = c('grey80', 'grey60', 'black')) +
  guides(colour = guide_legend(override.aes = list(pch = c(20, NA, NA), lty = c(NA, 1, 1)))) +
  theme_bw()

# plot 3
p3 <- ggplot() + 
  geom_point(aes(x = 1:length(y), y = y), col = 'grey80', pch = 20) + 
  geom_line(aes(x = 1:length(mu), y = mu), col = 'grey60') + 
  geom_line(aes(x = 1:length(fit_compound$posterior$mean), y = fit_compound$posterior$mean)) + 
  labs(title = 'compound', x = '', y = '') +
  scale_color_manual(values = c('grey80', 'grey60', 'black')) +
  guides(colour = guide_legend(override.aes = list(pch = c(20, NA, NA), lty = c(NA, 1, 1)))) +
  theme_bw()

# arrange the plots
grid.arrange(p1, p2, p3, ncol = 1)

fit.ash$fitted_g$pi
 [1] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.6639519 0.1973092
 [8] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
[15] 0.1387388 0.0000000 0.0000000 0.0000000
fit_inv$fitted_g$pi
 [1] 1.741773e-07 3.162366e-07 5.916218e-07 2.294661e-06 5.728518e-05
 [6] 6.641689e-01 1.970535e-01 6.278710e-07 4.399936e-10 4.788833e-12
[11] 1.370181e-12 3.243880e-12 8.503757e-13 9.971222e-12 1.387154e-01
[16] 8.939451e-07 2.017274e-10 4.877657e-11
fit_compound$fitted_g$pi
 [1] 2.029749e-08 5.524990e-08 1.504608e-07 1.124650e-06 6.832218e-05
 [6] 6.641200e-01 1.970686e-01 7.464503e-11 9.854802e-21 1.171594e-27
[11] 6.139681e-31 9.358840e-32 1.014260e-30 1.005443e-24 1.387417e-01
[16] 1.946484e-16 1.105745e-24 2.119400e-28

sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS:   /software/R-4.1.0-no-openblas-el7-x86_64/lib64/R/lib/libRblas.so
LAPACK: /software/R-4.1.0-no-openblas-el7-x86_64/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C         LC_TIME=C           
 [4] LC_COLLATE=C         LC_MONETARY=C        LC_MESSAGES=C       
 [7] LC_PAPER=C           LC_NAME=C            LC_ADDRESS=C        
[10] LC_TELEPHONE=C       LC_MEASUREMENT=C     LC_IDENTIFICATION=C 

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gridExtra_2.3   ggplot2_3.4.1   ashr_2.2-54     vebpm_0.4.7    
[5] workflowr_1.6.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10        horseshoe_0.2.0    invgamma_1.1       lattice_0.20-44   
 [5] rprojroot_2.0.2    digest_0.6.31      utf8_1.2.3         truncnorm_1.0-8   
 [9] R6_2.5.1           evaluate_0.14      highr_0.9          pillar_1.8.1      
[13] rlang_1.0.6        rstudioapi_0.13    ebnm_1.0-11        irlba_2.3.5.1     
[17] whisker_0.4        jquerylib_0.1.4    nloptr_1.2.2.2     Matrix_1.5-3      
[21] rmarkdown_2.9      labeling_0.4.2     splines_4.1.0      stringr_1.5.0     
[25] munsell_0.5.0      mixsqp_0.3-48      compiler_4.1.0     httpuv_1.6.1      
[29] xfun_0.24          pkgconfig_2.0.3    SQUAREM_2021.1     htmltools_0.5.4   
[33] tidyselect_1.2.0   tibble_3.1.8       matrixStats_0.59.0 fansi_1.0.4       
[37] dplyr_1.1.0        withr_2.5.0        later_1.3.0        grid_4.1.0        
[41] jsonlite_1.8.4     gtable_0.3.1       lifecycle_1.0.3    git2r_0.28.0      
[45] magrittr_2.0.3     scales_1.2.1       cli_3.6.0          stringi_1.6.2     
[49] ebpm_0.0.1.3       farver_2.1.1       fs_1.5.0           promises_1.2.0.1  
[53] bslib_0.2.5.1      generics_0.1.3     vctrs_0.5.2        trust_0.1-8       
[57] tools_4.1.0        glue_1.6.2         parallel_4.1.0     fastmap_1.1.0     
[61] yaml_2.3.7         colorspace_2.1-0   deconvolveR_1.2-1  knitr_1.33        
[65] sass_0.4.0