modified: Sat Jan 20 08:18:27 2018 compiled: Tue Nov 1 16:51:09 2022

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.917 0.0606 0.0229 0.15 3.05 -3.02    1.32    2.77    2.38
inflation(bc)
## sigma.0 
##    1.32
bias(bc)
## mu.0 
## 0.15

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.052,0.017,-0.074.
## ...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.064 2.67 -2.71 1.19 3.62 3.19
B 0.877 0.071 0.052 0.093 2.80 -2.75 1.30 3.03 3.70
C 0.853 0.081 0.066 0.088 2.60 -2.74 1.30 3.23 3.38
D 0.832 0.057 0.110 0.052 3.02 -1.12 1.33 1.53 4.59
E 0.882 0.059 0.059 0.017 2.68 -2.67 1.15 4.03 3.43
F 0.860 0.061 0.079 -0.074 2.77 -2.65 1.15 3.54 3.22
inflation(bc)
##    A    B    C    D    E    F 
## 1.19 1.30 1.30 1.33 1.15 1.15
bias(bc)
##       A       B       C       D       E       F 
##  0.0642  0.0932  0.0875  0.0524  0.0175 -0.0742
knitr::kable(tstat(bc)[1:5,])
A B C D E F
-0.668 0.608 -0.612 -0.723 0.182 -0.988
0.360 0.260 0.243 -3.212 -0.782 2.517
-0.487 -0.036 -0.133 -0.804 0.793 -0.274
0.116 -2.719 -0.910 -1.585 0.461 0.294
0.568 0.908 1.924 0.840 2.024 -1.193
knitr::kable(pval(bc)[1:5,])
A B C D E F
0.504 0.543 0.540 0.470 0.855 0.323
0.719 0.795 0.808 0.001 0.434 0.012
0.626 0.971 0.894 0.421 0.428 0.784
0.908 0.007 0.363 0.113 0.645 0.768
0.570 0.364 0.054 0.401 0.043 0.233
knitr::kable(se(bc)[1:5,])
A B C D E F
1.059 0.918 1.889 1.811 1.256 0.899
0.858 1.665 1.949 1.045 0.899 0.801
1.344 1.450 0.882 1.280 1.037 1.081
2.070 1.238 1.706 0.759 0.800 2.266
2.530 1.181 0.681 0.738 0.826 0.982
knitr::kable(es(bc)[1:5,])
A B C D E F
-0.707 0.558 -1.157 -1.31 0.229 -0.888
0.308 0.433 0.474 -3.35 -0.703 2.017
-0.655 -0.053 -0.118 -1.03 0.822 -0.296
0.240 -3.368 -1.552 -1.20 0.369 0.667
1.437 1.072 1.310 0.62 1.671 -1.172

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.54342 0.5404 0.46988 0.855 0.3230 0.4395
## 2 0.719 0.79493 0.8078 0.00132 0.434 0.0118 0.9658
## 3 0.626 0.97102 0.8939 0.42124 0.428 0.7843 0.7627
## 4 0.908 0.00654 0.3629 0.11293 0.645 0.7684 0.0620
## 5 0.570 0.36399 0.0544 0.40079 0.043 0.2328 0.0225
## 6 0.280 0.18871 0.7670 0.56440 0.023 0.3053 0.0111
print(topTable(bcm))
##      eff.size.meta std.err.meta pval.adj.meta pval.org.meta tstat.meta
## 4976         -5.87        0.359      3.06e-56      6.11e-60      -16.3
## 4820          4.19        0.322      5.04e-35      1.01e-38       13.0
## 4617          5.26        0.404      5.18e-35      1.04e-38       13.0
## 4520          3.89        0.321      4.05e-30      8.10e-34       12.1
## 4804          5.25        0.438      1.68e-29      3.36e-33       12.0
## 4919          4.54        0.379      2.04e-29      4.08e-33       12.0
## 4562          4.58        0.384      4.04e-29      8.09e-33       11.9
## 4918         -4.20        0.367      1.05e-26      2.11e-30      -11.5
## 4567         -4.32        0.395      3.12e-24      6.24e-28      -11.0
## 4585         -3.42        0.312      3.64e-24      7.27e-28      -10.9
##      eff.size.A std.err.A   pval.A tstat.A eff.size.B std.err.B   pval.B
## 4976    -0.6569     1.402 6.39e-01 -0.4686     -2.804     1.596 7.89e-02
## 4820     2.1478     0.806 7.74e-03  2.6634     -5.913     0.934 2.49e-10
## 4617     7.6373     0.949 8.42e-16  8.0479      1.248     0.977 2.01e-01
## 4520     0.6477     1.564 6.79e-01  0.4142      0.755     0.700 2.81e-01
## 4804     4.0383     1.031 8.98e-05  3.9167     -0.755     2.331 7.46e-01
## 4919     8.1049     0.572 1.41e-45 14.1695     -0.606     1.429 6.72e-01
## 4562     2.8168     0.849 9.12e-04  3.3163      8.665     0.607 3.60e-46
## 4918    -0.4037     1.469 7.83e-01 -0.2748     -7.779     1.047 1.07e-13
## 4567     0.0702     1.889 9.70e-01  0.0372     -6.144     0.862 1.02e-12
## 4585    -2.7342     0.968 4.72e-03 -2.8256      1.967     0.880 2.54e-02
##      tstat.B eff.size.C std.err.C   pval.C tstat.C eff.size.D std.err.D
## 4976  -1.757    -7.0700     1.337 1.24e-07 -5.2881      2.891     1.018
## 4820  -6.328    -9.1399     0.967 3.48e-21 -9.4472      1.067     2.113
## 4617   1.278    -2.2193     1.392 1.11e-01 -1.5944     -0.360     1.953
## 4520   1.079     1.4008     1.602 3.82e-01  0.8743     -0.502     0.672
## 4804  -0.324     1.7019     1.233 1.67e-01  1.3805      9.845     0.867
## 4919  -0.424     1.8069     0.849 3.32e-02  2.1294     -3.253     1.175
## 4562  14.265     5.7255     2.164 8.14e-03  2.6464      3.655     1.537
## 4918  -7.432    -1.4091     1.804 4.35e-01 -0.7812     -3.417     0.762
## 4567  -7.128    -0.0961     1.636 9.53e-01 -0.0587     -5.255     0.705
## 4585   2.236     5.1220     0.927 3.33e-08  5.5229      1.533     1.248
##        pval.D tstat.D eff.size.E std.err.E    pval.E tstat.E eff.size.F
## 4976 4.53e-03   2.838     -11.24     0.540  3.00e-96  -20.82     -2.260
## 4820 6.13e-01   0.505      10.65     0.455 2.69e-121   23.42      2.291
## 4617 8.54e-01  -0.184       5.17     0.862  2.04e-09    5.99      9.094
## 4520 4.55e-01  -0.747      10.24     0.527  3.57e-84   19.44     -0.351
## 4804 6.99e-30  11.355       5.76     0.841  7.57e-12    6.85      2.036
## 4919 5.61e-03  -2.770       5.30     1.035  3.03e-07    5.12      5.496
## 4562 1.74e-02   2.378      -4.46     1.364  1.08e-03   -3.27      2.189
## 4918 7.35e-06  -4.483      -6.21     0.637  1.79e-22   -9.75     -1.493
## 4567 9.23e-14  -7.452      -9.74     0.842  5.80e-31  -11.57      4.865
## 4585 2.19e-01   1.229      -7.20     0.425  3.25e-64  -16.92     -3.268
##      std.err.F   pval.F tstat.F
## 4976     0.729 1.94e-03   -3.10
## 4820     1.104 3.80e-02    2.07
## 4617     0.738 7.37e-35   12.32
## 4520     0.974 7.19e-01   -0.36
## 4804     1.200 8.98e-02    1.70
## 4919     1.626 7.22e-04    3.38
## 4562     0.814 7.17e-03    2.69
## 4918     0.798 6.13e-02   -1.87
## 4567     1.002 1.20e-06    4.86
## 4585     1.346 1.52e-02   -2.43
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.2.1 (2022-06-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.16-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.26.0        ellipse_0.4.3       BiocParallel_1.32.0
## [4] ggplot2_3.3.6       BiocStyle_2.26.0   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.9          highr_0.9           bslib_0.4.0        
##  [4] compiler_4.2.1      pillar_1.8.1        BiocManager_1.30.19
##  [7] jquerylib_0.1.4     tools_4.2.1         digest_0.6.30      
## [10] jsonlite_1.8.3      evaluate_0.17       lifecycle_1.0.3    
## [13] tibble_3.1.8        gtable_0.3.1        pkgconfig_2.0.3    
## [16] rlang_1.0.6         DBI_1.1.3           cli_3.4.1          
## [19] magick_2.7.3        parallel_4.2.1      yaml_2.3.6         
## [22] xfun_0.34           fastmap_1.1.0       withr_2.5.0        
## [25] stringr_1.4.1       dplyr_1.0.10        knitr_1.40         
## [28] generics_0.1.3      sass_0.4.2          vctrs_0.5.0        
## [31] tidyselect_1.2.0    grid_4.2.1          glue_1.6.2         
## [34] R6_2.5.1            fansi_1.0.3         rmarkdown_2.17     
## [37] bookdown_0.29       farver_2.1.1        magrittr_2.0.3     
## [40] codetools_0.2-18    scales_1.2.1        htmltools_0.5.3    
## [43] assertthat_0.2.1    colorspace_2.0-3    labeling_0.4.2     
## [46] utf8_1.2.2          stringi_1.7.8       munsell_0.5.0      
## [49] cachem_1.0.6

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.