Abstract

Dream uses a linear model model to increase power and decrease false positives for RNA-seq datasets with repeated measurements. Dream achieves this by combining multiple statistical concepts into a single statistical model. The model includes:

- flexible modeling of repeated measures gene expression data

- precision weights to model measurement error in RNA-seq counts

- ability to model multiple random effects

- random effects estimated separately for each gene

- hypothesis testing for fixed effects in linear mixed models

- small sample size hypothesis test

Dream also includes multi-threaded analysis across thousands of genes on a multi-core machine.

variancePartition v1.14.1

October 01, 2019 21:27:56

This tutorial assumes that the reader is familiar with the limma/voom workflow for RNA-seq. Process raw count data using limma/voom.

```
library('variancePartition')
library('edgeR')
library('BiocParallel')
data(varPartDEdata)
# filter genes by number of counts
isexpr = rowSums(cpm(countMatrix)>0.1) >= 5
# Standard usage of limma/voom
geneExpr = DGEList( countMatrix[isexpr,] )
geneExpr = calcNormFactors( geneExpr )
# make this vignette faster by analyzing a subset of genes
geneExpr = geneExpr[1:1000,]
```

Limma has a built-in approach for analyzing repeated measures data using `duplicateCorrelation()`

. The model can handle a single random effect, and forces the magnitude of the random effect to be the same across all genes.

```
# apply duplicateCorrelation is two rounds
design = model.matrix( ~ Disease, metadata)
vobj_tmp = voom( geneExpr, design, plot=FALSE)
dupcor <- duplicateCorrelation(vobj_tmp,design,block=metadata$Individual)
# run voom considering the duplicateCorrelation results
# in order to compute more accurate precision weights
# Otherwise, use the results from the first voom run
vobj = voom( geneExpr, design, plot=FALSE, block=metadata$Individual, correlation=dupcor$consensus)
# Estimate linear mixed model with a single variance component
# Fit the model for each gene,
dupcor <- duplicateCorrelation(vobj, design, block=metadata$Individual)
# But this step uses only the genome-wide average for the random effect
fitDupCor <- lmFit(vobj, design, block=metadata$Individual, correlation=dupcor$consensus)
# Fit Empirical Bayes for moderated t-statistics
fitDupCor <- eBayes( fitDupCor )
```

The dream method replaces two core functions of limma with a linear mixed model.

`voomWithDreamWeights()`

replaces`voom()`

to estimate precision weights`dream()`

replaces`lmFit()`

to estimate regression coefficients.

Otherwise dream uses the same workflow as limma with `topTable()`

, since any statistical differences are handled behind the scenes.

```
# Specify parallel processing parameters
# this is used implicitly by dream() to run in parallel
param = SnowParam(4, "SOCK", progressbar=TRUE)
register(param)
# The variable to be tested must be a fixed effect
form <- ~ Disease + (1|Individual)
# estimate weights using linear mixed model of dream
vobjDream = voomWithDreamWeights( geneExpr, form, metadata )
# Fit the dream model on each gene
# By default, uses the Satterthwaite approximation for the hypothesis test
fitmm = dream( vobjDream, form, metadata )
```

```
## (Intercept) Disease1
## sample_01 1 0
## sample_02 1 0
## sample_03 1 0
```

```
# Get results of hypothesis test on coefficients of interest
topTable( fitmm, coef='Disease1', number=3 )
```

```
## logFC AveExpr t P.Value adj.P.Val z.std
## ENST00000283033.5 gene=TXNDC11 1.556233 3.567624 68.62597 3.701359e-27 3.701359e-24 10.793329
## ENST00000257181.9 gene=PRPF38A 1.380549 4.398270 35.53037 6.311777e-21 3.155888e-18 9.384663
## ENST00000421974.2 gene=ATP6V0E2 1.386821 3.478030 22.35156 1.289338e-16 3.584404e-14 8.274557
```

Since dream uses an estimated degrees of freedom value for each hypothsis test, the degrees of freedom is different for each gene here. Therefore, the t-statistics are not directly comparable since they have different degrees of freedom. In order to be able to compare test statistics, we report `z.std`

which is the p-value transformed into a signed z-score. This can be used for downstream analysis.

Note that if a random effect is not specified, `dream()`

automatically uses `lmFit()`

and runs `eBayes()`

You can also perform a hypothesis test of the *difference* between two or more coefficients by using a contrast matrix. The contrasts are evaluated at the time of the model fit and the results can be extracted with `topTable()`

. This behaves like `makeContrasts()`

and `contrasts.fit()`

in limma.

Make sure to inspect your contrast matrix to confirm it is testing what you intend.

```
form <- ~ 0 + DiseaseSubtype + Sex + (1|Individual)
L = getContrast( vobjDream, form, metadata, c("DiseaseSubtype2", "DiseaseSubtype1"))
# Visualize contrast matrix
plotContrasts(L)
```

```
## Dividing work into 100 chunks...
##
## Total: 38 s
```

`## [1] "L1" "DiseaseSubtype0" "DiseaseSubtype1" "DiseaseSubtype2" "SexM"`

```
## logFC AveExpr t P.Value adj.P.Val z.std
## ENST00000355624.3 gene=RAB11FIP2 -0.9493146 5.260280 -5.384721 2.857024e-05 0.02857024 -4.184573
## ENST00000593466.1 gene=DDA1 -1.7265710 3.901579 -3.516847 2.168831e-03 0.99955523 -3.066083
## ENST00000200676.3 gene=CETP 1.4777420 3.723438 3.732329 8.085176e-03 0.99955523 2.648492
```

Multiple contrasts can be evaluated at the same time, in order to save computation time:

```
form <- ~ 0 + DiseaseSubtype + Sex + (1|Individual)
# define and then cbind contrasts
L1 = getContrast( vobjDream, form, metadata, c("DiseaseSubtype2", "DiseaseSubtype1"))
L2 = getContrast( vobjDream, form, metadata, c("DiseaseSubtype1", "DiseaseSubtype0"))
L = cbind(L1, L2)
# Visualize contrasts
plotContrasts(L)
```

```
## Dividing work into 100 chunks...
##
## Total: 39 s
```

```
## logFC AveExpr t P.Value adj.P.Val z.std
## ENST00000355624.3 gene=RAB11FIP2 -0.9493146 5.260280 -5.384721 2.857024e-05 0.02857024 -4.184573
## ENST00000593466.1 gene=DDA1 -1.7265710 3.901579 -3.516847 2.168831e-03 0.99955523 -3.066083
## ENST00000200676.3 gene=CETP 1.4777420 3.723438 3.732329 8.085176e-03 0.99955523 2.648492
```

Joint hypothesis testing of multiple coefficients at the same time can be performed by using an F-test. Just like in limma, the results can be extracted using `topTable()`

```
# extract results from first contrast
topTable( fit, coef=c("DiseaseSubtype2", "DiseaseSubtype1"), number=3 )
```

```
## DiseaseSubtype2 DiseaseSubtype1 AveExpr F P.Value
## ENST00000283033.5 gene=TXNDC11 4.364305 4.316435 3.567624 22739.435 2.693356e-34
## ENST00000257181.9 gene=PRPF38A 5.083019 5.091434 4.398270 9828.227 1.177149e-30
## ENST00000423994.2 gene=CACNA2D2 3.859983 3.752166 3.766314 4114.966 7.011262e-27
## adj.P.Val F.std
## ENST00000283033.5 gene=TXNDC11 2.693356e-31 77.29711
## ENST00000257181.9 gene=PRPF38A 5.885745e-28 68.91446
## ENST00000423994.2 gene=CACNA2D2 2.337087e-24 60.22228
```

Since dream uses an estimated degrees of freedom value for each hypothsis test, the degrees of freedom is different for each gene here. Therefore, the F-statistics are not directly comparable since they have different degrees of freedom. In order to be able to compare test statistics, we report `F.std`

which is the p-value transformed into an F-statistic with \(df_1={\text{number of coefficiets tested}}\) and \(df_2=\infty\). This can be used for downstream analysis.

For small datasets, the Kenward-Roger method can be more powerful. But it is **substantially** more computationally intensive.

Dream and variancePartition share the same underlying linear mixed model framework. A variancePartition analysis can indicate important variables that should be included as fixed or random effects in the dream analysis.

```
# Note: this could be run with either vobj from voom()
# or vobjDream from voomWithDreamWeights()
# The resuylts are similar
form = ~ (1|Individual) + (1|Disease)
vp = fitExtractVarPartModel( vobj, form, metadata)
```

```
## Dividing work into 100 chunks...
##
## Total: 25 s
```

In order to understand the empircal difference between dream and duplication correlation, we can plot the \(-\log_{10}\) p-values from both methods.

```
# Compare p-values and make plot
p1 = topTable(fitDupCor, coef="Disease1", number=Inf, sort.by="none")$P.Value
p2 = topTable(fitmm, number=Inf, sort.by="none")$P.Value
plotCompareP( p1, p2, vp$Individual, dupcor$consensus)
```

The duplicateCorrelation method estimates a single variance term genome-wide even though the donor contribution of a particular gene can vary substantially from the genome-wide trend. Using a single value genome-wide for the within-donor variance can reduce power and increase the false positive rate in a particular, reproducible way. Let \(\tau^2_g\) be the value of the donor component for gene \(g\) and \(\bar{\tau}^2\) be the genome-wide mean. For genes where \(\tau^2_g>\bar{\tau}^2\), using \(\bar{\tau}^2\) under-corrects for the donor component so that it increases the false positive rate compared to using \(\tau^2_g\). Conversely, for genes where \(\tau^2_g<\bar{\tau}^2\), using \(\bar{\tau}^2\) over-corrects for the donor component so that it decreases power. Increasing sample size does not overcome this issue. The dream method overcomes this issue by using a \(\tau^2_g\).

Here, the \(-\log_{10}\) p-values from both methods are plotted and colored by the donor contribution estiamted by variancePartition. The green value indicates \(\bar{\tau}^2\), while red and blue indicate higher and lower values, respectively. When only one variance component is used and the contrast matrix is simple, the effect of using dream versus duplicateCorrelation is determined by the comparison of \(\tau^2_g\) to \(\bar{\tau}^2\):

dream can increase the \(-\log_{10}\) p-value for genes with a lower donor component (i.e. \(\tau^2_g<\bar{\tau}^2\)) and decrease \(-\log_{10}\) p-value for genes with a higher donor component (i.e. \(\tau^2_g>\bar{\tau}^2\))

Note that using more variance components or a more complicated contrast matrix can make the relationship more complicated.

variancePartition functions including `dream()`

, `fitExtractVarPartModel()`

and `fitVarPartModel()`

can take advange of multicore machines to speed up analysis. It uses the BiocParallel package to manage the parallelization.

There are multiple ways to use parallel processing depending on your needs

- Specify parameters with the BPPARAM argument.

```
# Request 4 cores, and enable the progress bar
# This is the ideal for Linux, OS X and Windows
param = SnowParam(4, "SOCK", progressbar=TRUE)
fitmm = dream( vobjDream, form, metadata, BPPARAM=param)
# Or disable parallel processing and just do serial
param = SerialParam()
fitmm = dream( vobjDream, form, metadata, BPPARAM=param)
```

- Set parameters globally for the entire R session

```
param = SnowParam(4, "SOCK", progressbar=TRUE)
register(param)
# uses global parallel processing settings
fitmm = dream( vobjDream, form, metadata )
```

By default `BPPARAM`

and the global setttings are set the results of `bpparam()`

. But note that using `SnowParam()`

can *dramatically* reduce the memory usage needed for parallel processing because it reduces memory redundancy between threads.

```
## R version 3.6.1 (2019-07-05)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.3 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.9-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.9-bioc/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
## [4] LC_COLLATE=C LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
## [10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] parallel stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.18.1 edgeR_3.26.8 pander_0.6.3
## [4] variancePartition_1.14.1 Biobase_2.44.0 BiocGenerics_0.30.0
## [7] scales_1.0.0 foreach_1.4.7 limma_3.40.6
## [10] ggplot2_3.2.1 knitr_1.25
##
## loaded via a namespace (and not attached):
## [1] locfit_1.5-9.1 Rcpp_1.0.2 lattice_0.20-38 snow_0.4-3
## [5] prettyunits_1.0.2 gtools_3.8.1 assertthat_0.2.1 zeallot_0.1.0
## [9] digest_0.6.21 R6_2.4.0 plyr_1.8.4 backports_1.1.4
## [13] evaluate_0.14 pillar_1.4.2 gplots_3.0.1.1 rlang_0.4.0
## [17] progress_1.2.2 lazyeval_0.2.2 minqa_1.2.4 gdata_2.18.0
## [21] nloptr_1.2.1 Matrix_1.2-17 rmarkdown_1.16 labeling_0.3
## [25] splines_3.6.1 statmod_1.4.32 lme4_1.1-21 stringr_1.4.0
## [29] munsell_0.5.0 numDeriv_2016.8-1.1 compiler_3.6.1 xfun_0.10
## [33] pkgconfig_2.0.3 lmerTest_3.1-0 htmltools_0.3.6 tidyselect_0.2.5
## [37] tibble_2.1.3 codetools_0.2-16 crayon_1.3.4 dplyr_0.8.3
## [41] withr_2.1.2 MASS_7.3-51.4 bitops_1.0-6 grid_3.6.1
## [45] nlme_3.1-141 gtable_0.3.0 magrittr_1.5 KernSmooth_2.23-15
## [49] stringi_1.4.3 reshape2_1.4.3 doParallel_1.0.15 colorRamps_2.3
## [53] vctrs_0.2.0 boot_1.3-23 iterators_1.0.12 tools_3.6.1
## [57] glue_1.3.1 purrr_0.3.2 hms_0.5.1 pbkrtest_0.4-7
## [61] yaml_2.2.0 colorspace_1.4-1 caTools_1.17.1.2
```

Law, C. W., Y. Chen, W. Shi, and G. K. Smyth. 2014. “Voom: precision weights unlock linear model analysis tools for RNA-seq read counts.” *Genome Biology* 15 (2):R29. https://doi.org/10.1186/gb-2014-15-2-r29.