Last updated: 2020-04-24

Checks: 7 0

Knit directory: stat34800/analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.6.1). 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(20180411) 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 63128c9. 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:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    .sos/
    Ignored:    exams/
    Ignored:    temp/

Untracked files:
    Untracked:  analysis/hmm_final2019.Rmd
    Untracked:  analysis/neanderthal.Rmd
    Untracked:  analysis/pca_cell_cycle.Rmd
    Untracked:  data/reduced.chr12.90-100.data.txt
    Untracked:  data/reduced.chr12.90-100.snp.txt
    Untracked:  homework/fdr.aux
    Untracked:  homework/fdr.log
    Untracked:  tempETA_1_parBayesC.dat
    Untracked:  temp_ETA_1_parBayesC.dat
    Untracked:  temp_mu.dat
    Untracked:  temp_varE.dat
    Untracked:  tempmu.dat
    Untracked:  tempvarE.dat

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/eb_vs_soft.Rmd) and HTML (docs/eb_vs_soft.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 63128c9 Matthew Stephens 2020-04-24 workflowr::wflow_publish(“analysis/eb_vs_soft.Rmd”)
html d66f560 Matthew Stephens 2019-04-16 Build site.
Rmd 8d23226 Matthew Stephens 2019-04-16 workflowr::wflow_publish(“analysis/eb_vs_soft.Rmd”)
html 8f13d93 stephens999 2018-05-21 Build site.
Rmd 5b61f90 stephens999 2018-05-21 workflowr::wflow_publish(“eb_vs_soft.Rmd”)
Rmd fcfdba2 stephens999 2018-04-09 add comparison of eb vs soft thresholding

Prerequisites

Before running this you will need to install the packages ashr from CRAN and ebnm from github, by running something like the following code chunk.

install.packages("ashr")
install.packages("devtools")
devtools::install_github("stephenslab/ebnm")
library("ashr")
library("ebnm")

Simulate some data

Here we simulate a pretty sparse situation: 90% sparsity.

set.seed(1)
theta = c(rep(0,900),rnorm(100,0,4))
x = theta + rnorm(1000)
hist(theta,breaks = seq(-10,10,length=20),xlim=c(-10,10))

Version Author Date
d66f560 Matthew Stephens 2019-04-16
8f13d93 stephens999 2018-05-21

Now do inference under the EBNM model. Notice how the small values are shrunk hard, and the big values are hardly shrunk at all.

x.pn = ebnm_point_normal(x,1)
plot(x,x.pn$posterior$mean,main="observation (x) vs posterior mean", ylab="posterior mean")
abline(a=0,b=1,col="red")

Version Author Date
d66f560 Matthew Stephens 2019-04-16
8f13d93 stephens999 2018-05-21

What about soft thresholding?

soft_thresh = function(x,lambda){
  ifelse(abs(x)>lambda, sign(x)*(abs(x)-lambda), 0)
}
plot(x,soft_thresh(x,1.5),main="observation (x) vs soft-thresholding estimate (lambda=1.5)",ylab="soft-thresholded estimate")
abline(a=0,b=1,col="red")

Version Author Date
d66f560 Matthew Stephens 2019-04-16

Now look at Mean Squared Error. Notice that both do much better than no shrinkage!!

mse = rep(0,100)
l = seq(0,5,length=100)
for(i in 1:100){
  mse[i] = mean((theta-soft_thresh(x,l[i]))^2)
}
plot(l,mse,ylim=c(0,1.2), main="blue = no shrinkage; red=Empirical Bayes; points= soft-thresholding", xlab="lambda")
abline(h=mean((theta-get_pm(x.pn))^2),col="red")
abline(h=mean((theta-x)^2),col="blue")

Version Author Date
d66f560 Matthew Stephens 2019-04-16

sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.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] ebnm_0.1-24     ashr_2.2-47     workflowr_1.6.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4      knitr_1.28      whisker_0.4     magrittr_1.5   
 [5] SQUAREM_2020.2  lattice_0.20-40 R6_2.4.1        rlang_0.4.5    
 [9] stringr_1.4.0   tools_3.6.0     grid_3.6.0      xfun_0.12      
[13] irlba_2.3.3     invgamma_1.1    git2r_0.26.1    htmltools_0.4.0
[17] yaml_2.2.1      digest_0.6.25   rprojroot_1.3-2 mixsqp_0.3-31  
[21] Matrix_1.2-18   later_1.0.0     promises_1.1.0  fs_1.3.2       
[25] glue_1.4.0      evaluate_0.14   rmarkdown_2.1   stringi_1.4.6  
[29] compiler_3.6.0  backports_1.1.5 truncnorm_1.0-8 httpuv_1.5.2