modified: Sat Jan 20 08:18:27 2018 compiled: Tue Oct 26 16:51:08 2021

1 Introduction

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.

2 A single set of test-statistics

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.15.
## ...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.908 0.0658 0.0261 0.149 3.16 -3.02    1.29    2.78    2.07
inflation(bc)
## sigma.0 
##    1.29
bias(bc)
##  mu.0 
## 0.149

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)
Plot of Gibbs Sampling traces. Each panel represent of one the estimated parameters. Default plot shows the burin-in period as well.

Figure 1: Plot of Gibbs Sampling traces
Each panel represent of one the estimated parameters. Default plot shows the burin-in period as well.

posteriors(bc)
Gibbs Sampling posterior distributions of two estimated parameters the inflation (sigma 0) and proportion of null features (pi0 0). Posterior plots of the other parameters can be generated by using the `thetas` argument. The ellipical curves corresponding to a 75%, 90% and 95% probability regions for a bivariate normal distribution with mean and covariance estimated form the scatter-plot.

Figure 2: Gibbs Sampling posterior distributions of two estimated parameters the inflation (sigma 0) and proportion of null features (pi0 0)
Posterior plots of the other parameters can be generated by using the thetas argument. The ellipical curves corresponding to a 75%, 90% and 95% probability regions for a bivariate normal distribution with mean and covariance estimated form the scatter-plot.

fit(bc, n=100)
Fit to the data as estimated using the Gibbs Sampling algorithm. Black line represent to overall fit, red the fit of the null distribution and blue and green the alternatives.

Figure 3: Fit to the data as estimated using the Gibbs Sampling algorithm
Black line represent to overall fit, red the fit of the null distribution and blue and green the alternatives.

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`.
Histogram of z-scores. With on top standard normal (black) and estimated empirical null distribution (red).

Figure 4: Histogram of z-scores
With on top standard normal (black) and estimated empirical null distribution (red).

plot(bc, type="qq")
Quantile-quantile plot of $-log_{10}$ transformed P-values. Left panel using uncorrected P-values and right panel using bacon bias and inflation corrected P-values.

Figure 5: Quantile-quantile plot of \(-log_{10}\) transformed P-values
Left panel using uncorrected P-values and right panel using bacon bias and inflation corrected P-values.

3 Multiple sets of test-statistics

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.064,0.093,0.088,0.051,0.018,-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.070 0.061 0.064 2.69 -2.65 1.19 3.62 3.21
B 0.875 0.072 0.052 0.093 2.78 -2.73 1.29 3.01 3.70
C 0.853 0.080 0.067 0.088 2.66 -2.71 1.30 3.21 3.37
D 0.833 0.058 0.108 0.051 3.02 -1.15 1.33 1.54 4.62
E 0.880 0.058 0.061 0.018 2.74 -2.60 1.15 3.99 3.44
F 0.860 0.061 0.079 -0.075 2.78 -2.65 1.15 3.54 3.23
inflation(bc)
##    A    B    C    D    E    F 
## 1.19 1.29 1.30 1.33 1.15 1.15
bias(bc)
##       A       B       C       D       E       F 
##  0.0637  0.0926  0.0880  0.0514  0.0180 -0.0751
knitr::kable(tstat(bc)[1:5,])
A B C D E F
-0.668 0.610 -0.613 -0.722 0.182 -0.987
0.360 0.261 0.243 -3.209 -0.785 2.517
-0.487 -0.036 -0.134 -0.803 0.794 -0.273
0.116 -2.727 -0.911 -1.584 0.462 0.295
0.569 0.911 1.925 0.841 2.028 -1.192
knitr::kable(pval(bc)[1:5,])
A B C D E F
0.504 0.542 0.540 0.471 0.855 0.324
0.718 0.794 0.808 0.001 0.433 0.012
0.626 0.971 0.893 0.422 0.427 0.785
0.907 0.006 0.362 0.113 0.644 0.768
0.569 0.362 0.054 0.401 0.043 0.233
knitr::kable(se(bc)[1:5,])
A B C D E F
1.058 0.915 1.888 1.812 1.253 0.899
0.857 1.660 1.947 1.045 0.897 0.801
1.343 1.445 0.881 1.281 1.034 1.081
2.067 1.235 1.704 0.760 0.798 2.267
2.528 1.178 0.680 0.738 0.824 0.982
knitr::kable(es(bc)[1:5,])
A B C D E F
-0.707 0.558 -1.158 -1.307 0.229 -0.887
0.309 0.434 0.473 -3.355 -0.704 2.017
-0.654 -0.052 -0.118 -1.029 0.822 -0.295
0.241 -3.367 -1.553 -1.203 0.368 0.669
1.438 1.073 1.310 0.621 1.671 -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)
Plot of Gibbs Sampling traces. Each panel represent of one the estimated parameters. Default plot shows the burin-in period as well.

Figure 6: Plot of Gibbs Sampling traces
Each panel represent of one the estimated parameters. Default plot shows the burin-in period as well.

posteriors(bc, index=3)
Gibbs Sampling posterior distributions of two estimated parameters the inflation (sigma 0) and proportion of null features (pi0 0). Posterior plots of the other parameters can be generated by using the `thetas` argument. The ellipical curves corresponding to a 75%, 90% and 95% probability regions for a bivariate normal distribution with mean and covariance estimated form the scatter-plot.

Figure 7: Gibbs Sampling posterior distributions of two estimated parameters the inflation (sigma 0) and proportion of null features (pi0 0)
Posterior plots of the other parameters can be generated by using the thetas argument. The ellipical curves corresponding to a 75%, 90% and 95% probability regions for a bivariate normal distribution with mean and covariance estimated form the scatter-plot.

fit(bc, n=100, index=3)
Fit to the data as estimated using the Gibbs Sampling algorithm. Black line represent to overall fit, red the fit of the null distribution and blue and green the alternatives.

Figure 8: Fit to the data as estimated using the Gibbs Sampling algorithm
Black line represent to overall fit, red the fit of the null distribution and blue and green the alternatives.

plot(bc, type="hist")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Histogram of z-scores. With on top standard normal (black) and estimated empirical null distribution (red).

Figure 9: Histogram of z-scores
With on top standard normal (black) and estimated empirical null distribution (red).

plot(bc, type="qq")
Quantile-quantile plot of $-log_{10}$ transformed P-values. Left panel using uncorrected P-values and right panel using bacon bias and inflation corrected P-values.

Figure 10: Quantile-quantile plot of \(-log_{10}\) transformed P-values
Left panel using uncorrected P-values and right panel using bacon bias and inflation corrected P-values.

4 Fixed-effect meta-analysis

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.504 0.54186 0.5397 0.47053 0.8552 0.3235 0.4416
## 2 0.718 0.79395 0.8080 0.00133 0.4327 0.0118 0.9662
## 3 0.626 0.97130 0.8935 0.42187 0.4269 0.7849 0.7644
## 4 0.907 0.00639 0.3623 0.11326 0.6443 0.7679 0.0618
## 5 0.569 0.36225 0.0542 0.40059 0.0425 0.2332 0.0222
## 6 0.280 0.18748 0.7672 0.56507 0.0227 0.3058 0.0110
print(topTable(bcm))
##      eff.size.meta std.err.meta pval.adj.meta pval.org.meta tstat.meta
## 4976         -5.88        0.359      1.32e-56      2.63e-60      -16.4
## 4820          4.20        0.322      3.00e-35      6.00e-39       13.1
## 4617          5.26        0.404      4.82e-35      9.65e-39       13.0
## 4520          3.90        0.320      2.39e-30      4.77e-34       12.2
## 4804          5.25        0.437      1.57e-29      3.15e-33       12.0
## 4919          4.54        0.378      1.63e-29      3.26e-33       12.0
## 4562          4.59        0.383      2.60e-29      5.21e-33       12.0
## 4918         -4.21        0.366      7.65e-27      1.53e-30      -11.5
## 4567         -4.33        0.394      2.22e-24      4.45e-28      -11.0
## 4585         -3.42        0.312      2.52e-24      5.03e-28      -11.0
##      eff.size.A std.err.A   pval.A tstat.A eff.size.B std.err.B   pval.B
## 4976    -0.6563     1.400 6.39e-01 -0.4687     -2.803     1.591 7.80e-02
## 4820     2.1481     0.805 7.66e-03  2.6668     -5.913     0.932 2.19e-10
## 4617     7.6377     0.948 7.79e-16  8.0575      1.248     0.974 2.00e-01
## 4520     0.6484     1.562 6.78e-01  0.4151      0.755     0.697 2.79e-01
## 4804     4.0388     1.030 8.80e-05  3.9216     -0.754     2.324 7.46e-01
## 4919     8.1051     0.571 1.12e-45 14.1861     -0.605     1.424 6.71e-01
## 4562     2.8171     0.848 8.98e-04  3.3205      8.665     0.606 1.89e-46
## 4918    -0.4031     1.467 7.84e-01 -0.2747     -7.779     1.043 9.00e-14
## 4567     0.0711     1.887 9.70e-01  0.0377     -6.144     0.859 8.68e-13
## 4585    -2.7338     0.967 4.68e-03 -2.8284      1.968     0.877 2.49e-02
##      tstat.B eff.size.C std.err.C   pval.C tstat.C eff.size.D std.err.D
## 4976  -1.762    -7.0706     1.336 1.20e-07 -5.2930      2.892     1.019
## 4820  -6.347    -9.1403     0.967 3.21e-21 -9.4555      1.069     2.114
## 4617   1.282    -2.2199     1.391 1.10e-01 -1.5962     -0.359     1.954
## 4520   1.083     1.4001     1.601 3.82e-01  0.8746     -0.502     0.673
## 4804  -0.325     1.7014     1.232 1.67e-01  1.3813      9.846     0.867
## 4919  -0.425     1.8066     0.848 3.31e-02  2.1308     -3.252     1.175
## 4562  14.310     5.7246     2.162 8.09e-03  2.6482      3.656     1.538
## 4918  -7.455    -1.4099     1.802 4.34e-01 -0.7823     -3.416     0.762
## 4567  -7.150    -0.0968     1.635 9.53e-01 -0.0592     -5.254     0.706
## 4585   2.243     5.1217     0.927 3.25e-08  5.5272      1.534     1.248
##        pval.D tstat.D eff.size.E std.err.E    pval.E tstat.E eff.size.F
## 4976 4.54e-03   2.838     -11.24     0.538  1.04e-96  -20.87      -2.26
## 4820 6.13e-01   0.506      10.65     0.454 7.23e-122   23.48       2.29
## 4617 8.54e-01  -0.184       5.17     0.860  1.88e-09    6.01       9.09
## 4520 4.56e-01  -0.746      10.24     0.526  1.45e-84   19.49      -0.35
## 4804 7.35e-30  11.351       5.76     0.839  6.76e-12    6.86       2.04
## 4919 5.65e-03  -2.768       5.30     1.033  2.85e-07    5.13       5.50
## 4562 1.74e-02   2.378      -4.46     1.361  1.05e-03   -3.28       2.19
## 4918 7.44e-06  -4.481      -6.21     0.635  1.41e-22   -9.78      -1.49
## 4567 9.52e-14  -7.447      -9.74     0.840  4.17e-31  -11.60       4.87
## 4585 2.19e-01   1.229      -7.20     0.424  1.62e-64  -16.96      -3.27
##      std.err.F   pval.F tstat.F
## 4976     0.729 1.95e-03  -3.098
## 4820     1.105 3.80e-02   2.075
## 4617     0.738 7.52e-35  12.315
## 4520     0.975 7.19e-01  -0.359
## 4804     1.200 8.97e-02   1.697
## 4919     1.626 7.22e-04   3.381
## 4562     0.814 7.17e-03   2.689
## 4918     0.798 6.14e-02  -1.870
## 4567     1.002 1.20e-06   4.855
## 4585     1.346 1.52e-02  -2.426
plot(bcm, type="qq")
Quantile-quantile plot of $-log_{10}$ transformed P-values for each cohort and the meta-analysis P-values. Left panel using uncorrected P-values and right panel using bacon bias and inflation corrected P-values.

Figure 11: Quantile-quantile plot of \(-log_{10}\) transformed P-values for each cohort and the meta-analysis P-values
Left panel using uncorrected P-values and right panel using bacon bias and inflation corrected P-values.

5 Session Info

Here is the output of sessionInfo() on the system on which this document was compiled:

## R version 4.1.1 (2021-08-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.14-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.14-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              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.22.0        ellipse_0.4.2       BiocParallel_1.28.0
## [4] ggplot2_3.3.5       BiocStyle_2.22.0   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.7          highr_0.9           bslib_0.3.1        
##  [4] compiler_4.1.1      pillar_1.6.4        BiocManager_1.30.16
##  [7] jquerylib_0.1.4     tools_4.1.1         digest_0.6.28      
## [10] jsonlite_1.7.2      evaluate_0.14       lifecycle_1.0.1    
## [13] tibble_3.1.5        gtable_0.3.0        pkgconfig_2.0.3    
## [16] rlang_0.4.12        DBI_1.1.1           magick_2.7.3       
## [19] parallel_4.1.1      yaml_2.2.1          xfun_0.27          
## [22] fastmap_1.1.0       withr_2.4.2         stringr_1.4.0      
## [25] dplyr_1.0.7         knitr_1.36          generics_0.1.1     
## [28] sass_0.4.0          vctrs_0.3.8         tidyselect_1.1.1   
## [31] grid_4.1.1          glue_1.4.2          R6_2.5.1           
## [34] fansi_0.5.0         rmarkdown_2.11      bookdown_0.24      
## [37] farver_2.1.0        purrr_0.3.4         magrittr_2.0.1     
## [40] scales_1.1.1        htmltools_0.5.2     ellipsis_0.3.2     
## [43] assertthat_0.2.1    colorspace_2.0-2    labeling_0.4.2     
## [46] utf8_1.2.2          stringi_1.7.5       munsell_0.5.0      
## [49] crayon_1.4.1

References

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.