bacon 1.16.0
modified: Sat Jan 20 08:18:27 2018 compiled: Mon Apr 27 22:15:43 2020
bacon can be used to remove inflation and bias often observed in epigenome- and transcriptome-wide association studies (Iterson, Zwet, and Heijmans 2017).
To this end bacon constructs an empirical null distribution using a Gibbs Sampling algorithm by fitting a three-component normal mixture on z-scores. One component is forced, using prior knowledge, to represent the null distribution with mean and standard deviation representing the bias and inflation. The other two components are necessary to capture the amount of true associations present in the data, which we assume unknown but small.
bacon provides functionality to inspect the output of the Gibbs Sampling algorithm, i.e., plots of traces, posterior distributions and the mixture fit, are provided. Furthermore, inflation- and bias-corrected test-statistics or P-values are extracted easily. In addition, functionality for performing fixed-effect meta-analysis are provided as well.
The function bacon
requires a vector or a matrix of z-scores, e.g.,
those extracted from association analyses using a linear regression
approach. For fixed-effect meta-analysis a matrix of effect-sizes and
standard-errors is required.
This vignette illustrates the use of bacon using simulated z-scores, effect-sizes and standard errors to avoid long run-times. If multiple sets of test-statisics or effect-sizes and standard-errors are provided, the Gibbs Sampler algorithm can be executed in parallel to reduce computation time using functionality provide by BiocParallel-package.
A vector containing \(5000\) z-scores is generated from a normal mixture
distribution, \(90\%\) of the z-scores were drawn from a biased and
inflated null distribution, \(\mathcal{N}(0.2, 1.3)\), and the remaining
z-scores from \(\mathcal{N}(\mu, 1)\), where \(\mu \sim \mathcal{N}(4, 1)\). The rnormmix
-function provided by Bacon
generates a vector of
random test-statistics described above optionally with different
parameters.
y <- rnormmix(5000, c(0.9, 0.2, 1.3, 1, 4, 1))
The function bacon
executes the Gibbs Sampler algorithm and stores
all in- and out-put in an object of class Bacon
. Several
accessor-functions are available to access data contained in the
Bacon
-object, e.g. for obtaining the estimated parameters of the
mixture fit or explicitly the bias and inflation. Actually, the latter
two are the mean and standard deviation of the null component (mu.0
and sigma.0).
bc <- bacon(y)
bc
## Bacon-object containing 1 set(s) of 5000 test-statistics.
## ...estimated bias: 0.2.
## ...estimated inflation: 1.3.
##
## Empirical null estimates are based on 5000 iterations with a burnin-period of 2000.
estimates(bc)
## p.0 p.1 p.2 mu.0 mu.1 mu.2 sigma.0 sigma.1 sigma.2
## [1,] 0.909 0.0551 0.0359 0.195 3.01 -2.95 1.28 2.87 2.75
inflation(bc)
## sigma.0
## 1.28
bias(bc)
## mu.0
## 0.195
Several methods are provided to inspect the output of the Gibbs Sampler algorithm, such as traces-plots of all estimates, plots of posterior distributions, provide as a scatter plot between two parameters, and the actual fit of the three component mixture to the histogram of z-scores.
traces(bc, burnin=FALSE)
posteriors(bc)
fit(bc, n=100)
The previous three plots can be use as diagnostic tools to inspect the Gibbs sampling process.
There is also a generic plot function that can generate two types of plots; a histogram of the z-scores and a qq-plot. The histogram of the z-scores shows on top the standard normal distribution and the Gibbs Sampling estimated empirical null distribution. The quantile-quantile plot shows the \(-log_{10}\) transformed P-values. Default values are raw, not controlled for bias and inflation, z-scores and P-values.
plot(bc, type="hist")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
plot(bc, type="qq")
Matrices containing \(5000\times6\) effect-sizes and standard errors are
generated to simulated data for a fixed-effect meta-analyses. This is
a toy-example just to illustrate the capabilities of bacon
in
handling multiple sets of test-statics.
set.seed(12345)
biases <- runif(6, -0.2, 0.2)
inflations <- runif(6, 1, 1.3)
es <- matrix(nrow=5000, ncol=6)
for(i in 1:6)
es[,i] <- rnormmix(5000, c(0.9, biases[i], inflations[i], 0, 4, 1), shuffle=FALSE)
se <- replicate(6, 0.8*sqrt(4/rchisq(5000,df=4)))
colnames(es) <- colnames(se) <- LETTERS[1:ncol(se)]
rownames(es) <- rownames(se) <- 1:5000
head(rownames(es))
## [1] "1" "2" "3" "4" "5" "6"
head(colnames(es))
## [1] "A" "B" "C" "D" "E" "F"
By default the function bacon
detects the number of cores/nodes
registered, as described in the BiocParallel, to
perform bacon in parallel. To run the vignette in general we set it
here for convenience to 1 node.
library(BiocParallel)
register(MulticoreParam(1, log=TRUE))
bc <- bacon(NULL, es, se)
## Did you registered a biocparallel back-end?
## Continuing serial!
bc
## Bacon-object containing 6 set(s) of 5000 test-statistics.
## ...estimated bias: 0.065,0.094,0.089,0.053,0.041,-0.075.
## ...estimated inflation: 1.2,1.3,1.3,1.3,1.1,1.1.
##
## Empirical null estimates are based on 5000 iterations with a burnin-period of 2000.
knitr::kable(estimates(bc))
p.0 | p.1 | p.2 | mu.0 | mu.1 | mu.2 | sigma.0 | sigma.1 | sigma.2 | |
---|---|---|---|---|---|---|---|---|---|
A | 0.869 | 0.072 | 0.059 | 0.065 | 2.63 | -2.69 | 1.19 | 3.66 | 3.20 |
B | 0.878 | 0.070 | 0.052 | 0.094 | 2.82 | -2.77 | 1.30 | 3.04 | 3.70 |
C | 0.855 | 0.079 | 0.066 | 0.089 | 2.66 | -2.73 | 1.30 | 3.24 | 3.39 |
D | 0.833 | 0.058 | 0.109 | 0.053 | 3.01 | -1.15 | 1.34 | 1.57 | 4.60 |
E | 0.878 | 0.094 | 0.028 | 0.041 | 1.06 | -2.98 | 1.12 | 4.91 | 1.06 |
F | 0.860 | 0.061 | 0.079 | -0.075 | 2.76 | -2.65 | 1.15 | 3.55 | 3.24 |
inflation(bc)
## A B C D E F
## 1.19 1.30 1.30 1.34 1.12 1.15
bias(bc)
## A B C D E F
## 0.0653 0.0942 0.0890 0.0526 0.0407 -0.0748
knitr::kable(tstat(bc)[1:5,])
A | B | C | D | E | F |
---|---|---|---|---|---|
-0.669 | 0.606 | -0.612 | -0.722 | 0.167 | -0.989 |
0.359 | 0.259 | 0.242 | -3.208 | -0.824 | 2.520 |
-0.488 | -0.037 | -0.134 | -0.804 | 0.793 | -0.273 |
0.115 | -2.718 | -0.909 | -1.584 | 0.452 | 0.295 |
0.567 | 0.906 | 1.918 | 0.839 | 2.057 | -1.194 |
knitr::kable(pval(bc)[1:5,])
A | B | C | D | E | F |
---|---|---|---|---|---|
0.503 | 0.544 | 0.541 | 0.470 | 0.868 | 0.323 |
0.720 | 0.796 | 0.809 | 0.001 | 0.410 | 0.012 |
0.625 | 0.970 | 0.893 | 0.422 | 0.428 | 0.785 |
0.908 | 0.007 | 0.363 | 0.113 | 0.651 | 0.768 |
0.571 | 0.365 | 0.055 | 0.401 | 0.040 | 0.233 |
knitr::kable(se(bc)[1:5,])
A | B | C | D | E | F |
---|---|---|---|---|---|
1.059 | 0.919 | 1.894 | 1.813 | 1.224 | 0.898 |
0.857 | 1.667 | 1.953 | 1.046 | 0.876 | 0.801 |
1.344 | 1.451 | 0.884 | 1.282 | 1.010 | 1.080 |
2.069 | 1.239 | 1.710 | 0.760 | 0.779 | 2.264 |
2.529 | 1.182 | 0.682 | 0.739 | 0.804 | 0.981 |
knitr::kable(es(bc)[1:5,])
A | B | C | D | E | F |
---|---|---|---|---|---|
-0.708 | 0.557 | -1.159 | -1.31 | 0.204 | -0.888 |
0.308 | 0.432 | 0.472 | -3.36 | -0.721 | 2.017 |
-0.656 | -0.054 | -0.119 | -1.03 | 0.801 | -0.295 |
0.238 | -3.369 | -1.554 | -1.20 | 0.353 | 0.668 |
1.435 | 1.072 | 1.309 | 0.62 | 1.655 | -1.171 |
The accessor-function return as expected matrices of estimates. For the plotting functions an additional index of the ith study or z-score is required.
traces(bc, burnin=FALSE, index=3)
posteriors(bc, index=3)
fit(bc, n=100, index=3)
plot(bc, type="hist")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
plot(bc, type="qq")
The following code chunk shows how to perform fixed-effect meta-analysis and the inspection of results.
bcm <- meta(bc)
head(pval(bcm))
## A B C D E F meta
## 1 0.503 0.54420 0.5406 0.47028 0.8677 0.3229 0.43793
## 2 0.720 0.79566 0.8091 0.00134 0.4100 0.0117 0.94593
## 3 0.625 0.97043 0.8932 0.42167 0.4276 0.7845 0.77080
## 4 0.908 0.00656 0.3634 0.11330 0.6509 0.7679 0.06508
## 5 0.571 0.36471 0.0551 0.40140 0.0397 0.2325 0.02131
## 6 0.280 0.18874 0.7684 0.56474 0.0186 0.3051 0.00973
print(topTable(bcm))
## eff.size.meta std.err.meta pval.adj.meta pval.org.meta tstat.meta
## 4976 -6.00 0.355 2.67e-60 5.34e-64 -16.9
## 4820 4.37 0.318 3.34e-39 6.67e-43 13.7
## 4617 5.26 0.402 1.82e-35 3.65e-39 13.1
## 4520 4.01 0.318 7.74e-33 1.55e-36 12.6
## 4804 5.25 0.435 6.02e-30 1.20e-33 12.1
## 4919 4.55 0.378 9.87e-30 1.97e-33 12.0
## 4562 4.54 0.383 1.22e-28 2.44e-32 11.8
## 4918 -4.24 0.363 9.49e-28 1.90e-31 -11.7
## 4585 -3.54 0.308 8.52e-27 1.70e-30 -11.5
## 4567 -4.39 0.393 2.53e-25 5.06e-29 -11.2
## eff.size.A std.err.A pval.A tstat.A eff.size.B std.err.B pval.B
## 4976 -0.6582 1.401 6.39e-01 -0.4697 -2.805 1.597 7.90e-02
## 4820 2.1470 0.806 7.73e-03 2.6635 -5.914 0.935 2.54e-10
## 4617 7.6364 0.949 8.26e-16 8.0502 1.247 0.977 2.02e-01
## 4520 0.6463 1.563 6.79e-01 0.4134 0.754 0.700 2.81e-01
## 4804 4.0374 1.031 8.95e-05 3.9173 -0.757 2.333 7.46e-01
## 4919 8.1043 0.572 1.32e-45 14.1743 -0.607 1.430 6.71e-01
## 4562 2.8160 0.849 9.11e-04 3.3167 8.664 0.608 4.17e-46
## 4918 -0.4051 1.469 7.83e-01 -0.2758 -7.780 1.047 1.10e-13
## 4585 -2.7351 0.967 4.69e-03 -2.8277 1.967 0.881 2.55e-02
## 4567 0.0685 1.889 9.71e-01 0.0363 -6.145 0.863 1.05e-12
## tstat.B eff.size.C std.err.C pval.C tstat.C eff.size.D std.err.D
## 4976 -1.757 -7.0715 1.340 1.31e-07 -5.2771 2.891 1.020
## 4820 -6.324 -9.1410 0.970 4.24e-21 -9.4266 1.067 2.115
## 4617 1.276 -2.2208 1.395 1.11e-01 -1.5919 -0.360 1.955
## 4520 1.078 1.3990 1.606 3.84e-01 0.8712 -0.502 0.673
## 4804 -0.325 1.7006 1.236 1.69e-01 1.3762 9.845 0.868
## 4919 -0.424 1.8060 0.851 3.37e-02 2.1234 -3.253 1.176
## 4562 14.255 5.7231 2.168 8.31e-03 2.6392 3.655 1.539
## 4918 -7.428 -1.4111 1.808 4.35e-01 -0.7805 -3.417 0.763
## 4585 2.233 5.1210 0.930 3.61e-08 5.5092 1.533 1.249
## 4567 -7.124 -0.0979 1.640 9.52e-01 -0.0597 -5.255 0.706
## pval.D tstat.D eff.size.E std.err.E pval.E tstat.E eff.size.F
## 4976 4.58e-03 2.835 -11.25 0.526 1.59e-101 -21.39 -2.260
## 4820 6.14e-01 0.505 10.64 0.443 1.66e-127 24.02 2.292
## 4617 8.54e-01 -0.184 5.15 0.840 8.63e-10 6.13 9.094
## 4520 4.55e-01 -0.746 10.23 0.513 2.00e-88 19.94 -0.351
## 4804 8.09e-30 11.342 5.74 0.819 2.42e-12 7.01 2.036
## 4919 5.66e-03 -2.767 5.28 1.009 1.63e-07 5.24 5.497
## 4562 1.75e-02 2.375 -4.49 1.329 7.34e-04 -3.38 2.189
## 4918 7.52e-06 -4.479 -6.23 0.621 1.09e-23 -10.03 -1.492
## 4585 2.20e-01 1.227 -7.21 0.414 9.90e-68 -17.39 -3.267
## 4567 9.82e-14 -7.443 -9.75 0.820 1.20e-32 -11.90 4.866
## std.err.F pval.F tstat.F
## 4976 0.728 1.92e-03 -3.10
## 4820 1.104 3.78e-02 2.08
## 4617 0.738 6.42e-35 12.33
## 4520 0.974 7.19e-01 -0.36
## 4804 1.199 8.94e-02 1.70
## 4919 1.624 7.14e-04 3.38
## 4562 0.813 7.11e-03 2.69
## 4918 0.797 6.11e-02 -1.87
## 4585 1.345 1.51e-02 -2.43
## 4567 1.001 1.17e-06 4.86
plot(bcm, type="qq")
Here is the output of sessionInfo()
on the system on which this
document was compiled:
## R version 4.0.0 (2020-04-24)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.4 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.11-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.11-bioc/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] bacon_1.16.0 ellipse_0.4.1 BiocParallel_1.22.0
## [4] ggplot2_3.3.0 BiocStyle_2.16.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.4.6 highr_0.8 pillar_1.4.3
## [4] compiler_4.0.0 BiocManager_1.30.10 tools_4.0.0
## [7] digest_0.6.25 evaluate_0.14 lifecycle_0.2.0
## [10] tibble_3.0.1 gtable_0.3.0 pkgconfig_2.0.3
## [13] rlang_0.4.5 magick_2.3 yaml_2.2.1
## [16] parallel_4.0.0 xfun_0.13 withr_2.2.0
## [19] stringr_1.4.0 dplyr_0.8.5 knitr_1.28
## [22] vctrs_0.2.4 grid_4.0.0 tidyselect_1.0.0
## [25] glue_1.4.0 R6_2.4.1 rmarkdown_2.1
## [28] bookdown_0.18 farver_2.0.3 purrr_0.3.4
## [31] magrittr_1.5 scales_1.1.0 ellipsis_0.3.0
## [34] htmltools_0.4.0 assertthat_0.2.1 colorspace_1.4-1
## [37] labeling_0.3 stringi_1.4.6 munsell_0.5.0
## [40] crayon_1.3.4
Iterson, M. van, E. W. van Zwet, and B. T. Heijmans. 2017. “Controlling bias and inflation in epigenome- and transcriptome-wide association studies using the empirical null distribution.” Genome Biol. 18 (1):19.