# Introduction

In the introductory mashr vignettes we assumed that the data were small enough that it was convenient to read them all in and do all the analyses on the same data.

In larger applications, particularly eQTL studies, it can be more convenient to do different parts of the analyses on subsets of the tests. Specifically, if you have millions of tests in dozens of conditions, it might be helpful to consider subsets of these millions of tests at any one time. Here we illustrate this idea.

Our suggested workflow is to extract (at least) two subsets of tests from your complete data set:

1. Results from a subset of “strong” tests corresponding to stronger effects in your study. For example, these tests might have been identified by taking the “top” eQTL in each gene based on univariate test results, or by some other approach such as a simple meta-analysis.

2. Results from a random subset of all tests. It is important that these be an unbiased representation of all the tests you are considering, including null and non-null tests, because mashr uses these tests to learn about the amount of signal in the data, and to “correct” estimates for the fact that many tests are null (analagous to a kind of multiple testing correction.)

We will call the data from these two sets of tests strong and random respectively.

To give some sense of the potential appropriate sizes of these datasets: in our eQTL application in Urbut et al, the strong data contained about 16k tests (the top eQTL per gene), and for the random data we used 20k randomly-selected tests. (If you suspect true effects are very sparse then you might want to increase the size of the random subset, say to 200k).

## Analysis strategy outline

The basic analysis strategy is now:

1. Learn correlation structure among null tests using random test.

2. Learn data-driven covariance matrices using strong tests.

3. Fit the mashr model to the random tests, to learn the mixture weights on all the different covariance matrices and scaling coefficients.

4. Compute posterior summaries on the strong tests, using the model fit from step 2. (At this stage you could actually compute posterior summaries for any sets of tests you like. For example you could read in all your tests in small batches and compute posterior summaries in batches. But for illustration we will just do it on the strong tests.)

# Example

First we simulate some data to illustrate the ideas. To make this convenient to run we simulate a small data. And we identify the strong hits using mash_1by1. But in practice you may want to use methods outside of R to extract the matrices of data corresponding to strong and random tests, and then read them in as you need them. For example, see here for scripts we use for processing fastQTL output.

library(ashr)
library(mashr)
set.seed(1)
simdata = simple_sims(10000,5,1) # simulates data on 40k tests

# identify a subset of strong tests
m.1by1 = mash_1by1(mash_set_data(simdata$Bhat,simdata$Shat))
strong.subset = get_significant_results(m.1by1,0.05)

# identify a random subset of 5000 tests

## Data driven covariances

Now we use the strong tests to set up data-driven covariances.

U.pca = cov_pca(data.strong,5)
U.ed = cov_ed(data.strong, U.pca)

## Fit mash model (estimate mixture proportions)

Now we fit mash to the random tests using both data-driven and canonical covariances. (Remember the Crucial Rule! We have to fit using a random set of tests, and not a dataset that is enriched for strong tests.) The outputlevel=1 option means that it will not compute posterior summaries for these tests (which saves time).

U.c = cov_canonical(data.random)
m = mash(data.random, Ulist = c(U.ed,U.c), outputlevel = 1)
#  - Computing 5000 x 241 likelihood matrix.
#  - Likelihood calculations took 0.20 seconds.
#  - Fitting model with 241 mixture components.
#  - Model fitting took 4.23 seconds.

## Compute posterior summaries

Now we can compute posterior summaries etc for any subset of tests using the above mash fit. Here we do this for the strong tests. We do this using the same mash function as above, but we specify to use the fit from the previous run of mash by specifying
g=get_fitted_g(m), fixg=TRUE. (In mash the parameter g is used to denote the mixture model which we learned above.)

m2 = mash(data.strong, g=get_fitted_g(m), fixg=TRUE)
#  - Computing 1428 x 241 likelihood matrix.
#  - Likelihood calculations took 0.05 seconds.
#  - Computing posterior matrices.
#  - Computation allocated took 0.01 seconds.
head(get_lfsr(m2))
#               condition_1  condition_2  condition_3  condition_4  condition_5
# effect_13096 9.837638e-06 5.072688e-01 4.242991e-01 3.957421e-01 6.073757e-01
# effect_29826 6.567291e-05 6.639266e-01 5.839528e-01 6.359974e-01 5.770477e-01
# effect_14042 6.994599e-02 6.496975e-03 2.483998e-03 5.562658e-02 6.838122e-06
# effect_12524 1.119205e-01 4.107521e-01 2.985498e-02 2.578507e-05 1.001811e-01
# effect_15456 4.911825e-05 4.382090e-01 2.735423e-01 5.167374e-01 3.611055e-01
# effect_35844 2.619621e-09 4.563744e-09 1.862051e-07 1.012529e-09 4.090084e-11