Last updated: 2020-05-09
Checks: 6 0
Knit directory: FLASHvestigations/
This reproducible R Markdown analysis was created with workflowr (version 1.2.0). The Report 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(20180714)
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! 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
Ignored: code/.DS_Store
Ignored: code/flashier_bench/.DS_Store
Ignored: data/.DS_Store
Ignored: data/flashier_bench/
Ignored: data/metabo3_gwas_mats.RDS
Ignored: output/jean/
Untracked files:
Untracked: analysis/batting_order.Rmd
Untracked: code/fasfunction.R
Untracked: code/nnmf.R
Untracked: code/wals.R
Untracked: data/BR_teams_2019.csv
Untracked: data/FG_teams_2019.csv
Untracked: data/batting_order.rds
Untracked: data/cole.rds
Untracked: data/odorizzi.rds
Untracked: data/pitcher.rds
Untracked: data/pitcher2.rds
Untracked: data/pitcher_all.rds
Untracked: mlb2.R
Untracked: mlb_standings.txt
Untracked: ottoneu.R
Untracked: phoible.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 | 28c2fd2 | Jason Willwerscheid | 2020-05-09 | wflow_publish(“analysis/ebnm_npmle.Rmd”) |
html | 9484aa5 | Jason Willwerscheid | 2020-05-09 | Build site. |
Rmd | 9ae95a8 | Jason Willwerscheid | 2020-05-09 | wflow_publish(“analysis/ebnm_npmle.Rmd”) |
html | ec4a616 | Jason Willwerscheid | 2020-05-09 | Build site. |
Rmd | 0d0744f | Jason Willwerscheid | 2020-05-09 | wflow_publish(“analysis/ebnm_npmle.Rmd”) |
html | af9055d | Jason Willwerscheid | 2020-05-08 | Build site. |
Rmd | bf4d8a5 | Jason Willwerscheid | 2020-05-08 | wflow_publish(“analysis/ebnm_npmle.Rmd”) |
html | 4291adf | Jason Willwerscheid | 2020-05-08 | Build site. |
Rmd | ff42bb0 | Jason Willwerscheid | 2020-05-08 | wflow_publish(“analysis/ebnm_npmle.Rmd”) |
html | 1af4141 | Jason Willwerscheid | 2020-05-08 | Build site. |
Rmd | 4afebf9 | Jason Willwerscheid | 2020-05-08 | wflow_publish(“analysis/ebnm_npmle.Rmd”) |
html | 93fda13 | Jason Willwerscheid | 2020-04-29 | Build site. |
Rmd | 8420f5d | Jason Willwerscheid | 2020-04-29 | wflow_publish(“analysis/ebnm_npmle.Rmd”) |
I want to test out approximations of NPMLEs using a dense ashr
grid. Let \(x_1, \ldots, x_n\) be \(n\) observations with standard errors equal to 1. Dicker and Zhao show that when the true NPMLE has compact support, then a good approximation can be obtained by optimizing over the family of distributions that’s supported on \(\sqrt{n}\) equally spaced points between \(\min (x)\) and \(\max (x)\). Instead of using point masses, I use an ashr
grid with uniform components of equal width. Let’s see how it works in practice.
Here’s the true distribution which I’ll be sampling from. It’s bimodal with peaks at -5 and 5, so a unimodal prior family wouldn’t work very well.
suppressMessages(library(tidyverse))
true_g <- ashr::normalmix(pi = rep(0.1, 10),
mean = c(rep(-5, 5), rep(5, 5)),
sd = c(0:4, 0:4))
cdf_grid <- seq(-20, 20, by = 0.1)
true_cdf <- drop(ashr::mixcdf(true_g, cdf_grid))
ggplot(tibble(x = cdf_grid, y = true_cdf), aes(x = x, y = y)) + geom_line()
Version | Author | Date |
---|---|---|
93fda13 | Jason Willwerscheid | 2020-04-29 |
I start by sampling 1000 observations and adding normally distributed noise.
samp_from_g <- function(g, n) {
comp <- sample(1:length(g$pi), n, replace = TRUE, prob = g$pi)
mean <- g$mean[comp]
sd <- g$sd[comp]
return(rnorm(n, mean = mean, sd = sd))
}
set.seed(666)
n <- 1000
samp <- samp_from_g(true_g, n) + rnorm(n)
ggplot(tibble(x = samp), aes(x = x)) + geom_histogram(binwidth = 1)
I want to see how grid density affects convergence properties and the quality of the solution. From a log likelihood perspective, using a grid of points spaced at a distance that’s about half the standard deviation of the noise gives a solution that’s pretty much just as good as a very fine grid. Note that the Dicker and Zhao recommendation — \(\sqrt{n} \approx 32\), which corresponds to a scale
of a little more than 0.8 — produces a noticeably worse solution.
mixsqp_control = list()
scale_vec <- (max(samp) - min(samp) + 1e-4) / 2^(5:9)
res_list <- list()
for (scale in scale_vec) {
ebnm_res <- ebnm::ebnm_npmle(samp, scale = scale, control = mixsqp_control)
res_list <- c(res_list, list(ebnm_res))
}
ggplot(tibble(scale = scale_vec,
llik = sapply(res_list, `[[`, "log_likelihood")),
aes(x = scale, y = llik)) +
geom_point()
Visually, the CDFs are very similar for all scale
s less than 0.5 SD:
cdf_df <- tibble(x = rep(cdf_grid, length(res_list)),
y = as.vector(sapply(res_list,
function(res) drop(ashr::mixcdf(res$fitted_g, cdf_grid)))),
scale = as.factor(rep(round(scale_vec, 2), each = length(cdf_grid))))
ggplot(cdf_df, aes(x = x, y = y, col = scale)) +
geom_line()
Interestingly, the number of nonzero components is pretty much constant even as the total number of components increases:
cat("Number of components:\n",
sapply(res_list, function(res) length(res$fitted_g$pi)), "\n",
"Number of nonzero components:\n",
sapply(res_list, function(res) sum(res$fitted_g$pi > 0)))
#> Number of components:
#> 32 64 128 256 512
#> Number of nonzero components:
#> 20 20 22 19 21
I redo with 10000 observations. The same conclusions still hold, more or less. That the likelihood appears to decrease when large numbers of components are used is, I think, due to numerical error, but it says something about how accurate of a solution we should be aiming for. A good rule of thumb might be to set scale
equal to \(\text{SD} / (\log_{10} (n) - 1)\) (with a maximum of, say, 300 components).
n <- 10000
samp <- samp_from_g(true_g, n) + rnorm(n)
scale_vec <- (max(samp) - min(samp) + 1e-4) / 2^(5:9)
res_list <- list()
for (scale in scale_vec) {
ebnm_res <- ebnm::ebnm_npmle(samp, scale = scale, control = mixsqp_control)
res_list <- c(res_list, list(ebnm_res))
}
ggplot(tibble(scale = scale_vec,
llik = sapply(res_list, `[[`, "log_likelihood")),
aes(x = scale, y = llik)) +
geom_point()
cdf_df <- tibble(x = rep(cdf_grid, length(res_list)),
y = as.vector(sapply(res_list,
function(res) drop(ashr::mixcdf(res$fitted_g, cdf_grid)))),
scale = as.factor(rep(round(scale_vec, 2), each = length(cdf_grid))))
ggplot(cdf_df, aes(x = x, y = y, col = scale)) +
geom_line()
cat("Number of components:\n",
sapply(res_list, function(res) length(res$fitted_g$pi)), "\n",
"Number of nonzero components:\n",
sapply(res_list, function(res) sum(res$fitted_g$pi > 0)))
#> Number of components:
#> 32 64 128 256 512
#> Number of nonzero components:
#> 24 27 29 28 28
I include two examples with verbose output for inspection. Compare \(n = 10000\) with scale = 1 / 3
:
set.seed(666)
n <- 10000
samp <- samp_from_g(true_g, n) + rnorm(n)
g10000 <- ebnm::ebnm_npmle(samp, scale = 1/3, control = list(verbose = TRUE))
#> Running mix-SQP algorithm 0.3-40 on 10000 x 106 matrix
#> convergence tol. (SQP): 1.0e-08
#> conv. tol. (active-set): 1.0e-10
#> zero threshold (solution): 1.0e-08
#> zero thresh. (search dir.): 1.0e-14
#> l.s. sufficient decrease: 1.0e-02
#> step size reduction factor: 7.5e-01
#> minimum step size: 1.0e-08
#> max. iter (SQP): 1000
#> max. iter (active-set): 20
#> number of EM iterations: 20
#> Computing SVD of 10000 x 106 matrix.
#> Matrix is not low-rank; falling back to full matrix.
#> iter objective max(rdual) nnz stepsize max.diff nqp nls
#> 1 +2.017844701e+00 -- EM -- 106 1.00e+00 2.01e-02 -- --
#> 2 +1.999198085e+00 -- EM -- 106 1.00e+00 6.22e-03 -- --
#> 3 +1.995764707e+00 -- EM -- 106 1.00e+00 3.14e-03 -- --
#> 4 +1.994475590e+00 -- EM -- 106 1.00e+00 1.97e-03 -- --
#> 5 +1.993778832e+00 -- EM -- 106 1.00e+00 1.42e-03 -- --
#> 6 +1.993342140e+00 -- EM -- 106 1.00e+00 1.12e-03 -- --
#> 7 +1.993049352e+00 -- EM -- 106 1.00e+00 9.35e-04 -- --
#> 8 +1.992844415e+00 -- EM -- 106 1.00e+00 8.02e-04 -- --
#> 9 +1.992695810e+00 -- EM -- 106 1.00e+00 7.00e-04 -- --
#> 10 +1.992584511e+00 -- EM -- 106 1.00e+00 6.20e-04 -- --
#> 11 +1.992498572e+00 -- EM -- 106 1.00e+00 5.55e-04 -- --
#> 12 +1.992430274e+00 -- EM -- 106 1.00e+00 5.01e-04 -- --
#> 13 +1.992374518e+00 -- EM -- 106 1.00e+00 4.55e-04 -- --
#> 14 +1.992327867e+00 -- EM -- 106 1.00e+00 4.17e-04 -- --
#> 15 +1.992287964e+00 -- EM -- 106 1.00e+00 3.84e-04 -- --
#> 16 +1.992253165e+00 -- EM -- 106 1.00e+00 3.56e-04 -- --
#> 17 +1.992222307e+00 -- EM -- 106 1.00e+00 3.32e-04 -- --
#> 18 +1.992194556e+00 -- EM -- 106 1.00e+00 3.11e-04 -- --
#> 19 +1.992169304e+00 -- EM -- 106 1.00e+00 2.93e-04 -- --
#> 20 +1.992146101e+00 -- EM -- 106 1.00e+00 2.77e-04 -- --
#> 1 +1.992124612e+00 +2.684e-02 106 ------ ------ -- --
#> 2 +1.992104166e+00 +1.625e-02 86 1.00e+00 2.44e-03 20 1
#> 3 +1.992085384e+00 +1.499e-02 66 1.00e+00 1.61e-02 20 1
#> 4 +1.992066831e+00 +1.215e-02 46 1.00e+00 7.34e-02 20 1
#> 5 +1.991536288e+00 +5.979e-04 29 1.00e+00 1.03e-01 20 1
#> 6 +1.991498046e+00 +2.760e-03 28 1.00e+00 3.19e-03 20 1
#> 7 +1.991476812e+00 -6.163e-06 28 1.00e+00 1.40e-02 20 1
#> Optimization took 0.84 seconds.
#> Convergence criteria met---optimal solution found.
and \(n = 100000\) with scale = 1 / 4
:
set.seed(666)
n <- 100000
samp <- samp_from_g(true_g, n) + rnorm(n)
g100000 <- ebnm::ebnm_npmle(samp, scale = 1/4, control = list(verbose = TRUE))
#> Running mix-SQP algorithm 0.3-40 on 100000 x 165 matrix
#> convergence tol. (SQP): 1.0e-08
#> conv. tol. (active-set): 1.0e-10
#> zero threshold (solution): 1.0e-08
#> zero thresh. (search dir.): 1.0e-14
#> l.s. sufficient decrease: 1.0e-02
#> step size reduction factor: 7.5e-01
#> minimum step size: 1.0e-08
#> max. iter (SQP): 1000
#> max. iter (active-set): 20
#> number of EM iterations: 20
#> Computing SVD of 100000 x 165 matrix.
#> Matrix is not low-rank; falling back to full matrix.
#> iter objective max(rdual) nnz stepsize max.diff nqp nls
#> 1 +2.025450905e+00 -- EM -- 165 1.00e+00 1.65e-02 -- --
#> 2 +2.006598306e+00 -- EM -- 165 1.00e+00 4.88e-03 -- --
#> 3 +2.003041504e+00 -- EM -- 165 1.00e+00 2.48e-03 -- --
#> 4 +2.001666403e+00 -- EM -- 165 1.00e+00 1.55e-03 -- --
#> 5 +2.000917158e+00 -- EM -- 165 1.00e+00 1.12e-03 -- --
#> 6 +2.000451914e+00 -- EM -- 165 1.00e+00 8.68e-04 -- --
#> 7 +2.000145975e+00 -- EM -- 165 1.00e+00 7.08e-04 -- --
#> 8 +1.999937370e+00 -- EM -- 165 1.00e+00 5.94e-04 -- --
#> 9 +1.999790761e+00 -- EM -- 165 1.00e+00 5.08e-04 -- --
#> 10 +1.999684739e+00 -- EM -- 165 1.00e+00 4.40e-04 -- --
#> 11 +1.999605894e+00 -- EM -- 165 1.00e+00 3.86e-04 -- --
#> 12 +1.999545631e+00 -- EM -- 165 1.00e+00 3.42e-04 -- --
#> 13 +1.999498334e+00 -- EM -- 165 1.00e+00 3.06e-04 -- --
#> 14 +1.999460269e+00 -- EM -- 165 1.00e+00 2.76e-04 -- --
#> 15 +1.999428913e+00 -- EM -- 165 1.00e+00 2.52e-04 -- --
#> 16 +1.999402535e+00 -- EM -- 165 1.00e+00 2.31e-04 -- --
#> 17 +1.999379927e+00 -- EM -- 165 1.00e+00 2.13e-04 -- --
#> 18 +1.999360233e+00 -- EM -- 165 1.00e+00 1.98e-04 -- --
#> 19 +1.999342839e+00 -- EM -- 165 1.00e+00 1.85e-04 -- --
#> 20 +1.999327297e+00 -- EM -- 165 1.00e+00 1.73e-04 -- --
#> 1 +1.999313278e+00 +1.489e-02 164 ------ ------ -- --
#> 2 +1.999300526e+00 +1.404e-02 144 1.00e+00 1.64e-04 20 1
#> 3 +1.999288840e+00 +1.303e-02 124 1.00e+00 2.11e-03 20 1
#> 4 +1.999278015e+00 +1.313e-02 104 1.00e+00 8.06e-03 20 1
#> 5 +1.999267948e+00 +1.423e-02 84 1.00e+00 1.73e-02 20 1
#> 6 +1.999258562e+00 +1.443e-02 64 1.00e+00 4.36e-02 20 1
#> 7 +1.999204995e+00 +7.209e-03 44 1.00e+00 6.93e-02 20 1
#> 8 +1.999002271e+00 +8.581e-05 34 1.00e+00 1.29e-01 20 1
#> 9 +1.999002268e+00 -5.900e-06 34 1.00e+00 3.78e-04 20 1
#> Optimization took 19.94 seconds.
#> Convergence criteria met---optimal solution found.
sessionInfo()
#> R version 3.5.3 (2019-03-11)
#> 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.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] forcats_0.4.0 stringr_1.4.0 dplyr_0.8.0.1 purrr_0.3.2
#> [5] readr_1.3.1 tidyr_0.8.3 tibble_2.1.1 ggplot2_3.2.0
#> [9] tidyverse_1.2.1
#>
#> loaded via a namespace (and not attached):
#> [1] tidyselect_0.2.5 xfun_0.6 ashr_2.2-50
#> [4] haven_2.1.1 lattice_0.20-38 colorspace_1.4-1
#> [7] generics_0.0.2 htmltools_0.3.6 yaml_2.2.0
#> [10] rlang_0.4.2 mixsqp_0.3-40 pillar_1.3.1
#> [13] glue_1.3.1 withr_2.1.2 modelr_0.1.5
#> [16] readxl_1.3.1 munsell_0.5.0 gtable_0.3.0
#> [19] workflowr_1.2.0 cellranger_1.1.0 rvest_0.3.4
#> [22] evaluate_0.13 labeling_0.3 knitr_1.22
#> [25] invgamma_1.1 irlba_2.3.3 broom_0.5.1
#> [28] Rcpp_1.0.1 scales_1.0.0 backports_1.1.3
#> [31] jsonlite_1.6 truncnorm_1.0-8 fs_1.2.7
#> [34] hms_0.4.2 digest_0.6.18 stringi_1.4.3
#> [37] ebnm_0.1-21 grid_3.5.3 rprojroot_1.3-2
#> [40] cli_1.1.0 tools_3.5.3 magrittr_1.5
#> [43] lazyeval_0.2.2 crayon_1.3.4 whisker_0.3-2
#> [46] pkgconfig_2.0.2 Matrix_1.2-15 SQUAREM_2017.10-1
#> [49] xml2_1.2.0 lubridate_1.7.4 assertthat_0.2.1
#> [52] rmarkdown_1.12 httr_1.4.0 rstudioapi_0.10
#> [55] R6_2.4.0 nlme_3.1-137 git2r_0.25.2
#> [58] compiler_3.5.3