<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />




<title>MASH v FLASH analysis of GTEx data (strong tests)</title>

<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<script src="site_libs/navigation-1.1/codefolding.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<link href="site_libs/font-awesome-4.5.0/css/font-awesome.min.css" rel="stylesheet" />

<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
  pre:not([class]) {
    background-color: white;
  }
</style>
<script type="text/javascript">
if (window.hljs) {
  hljs.configure({languages: []});
  hljs.initHighlightingOnLoad();
  if (document.readyState && document.readyState === "complete") {
    window.setTimeout(function() { hljs.initHighlighting(); }, 0);
  }
}
</script>



<style type="text/css">
h1 {
  font-size: 34px;
}
h1.title {
  font-size: 38px;
}
h2 {
  font-size: 30px;
}
h3 {
  font-size: 24px;
}
h4 {
  font-size: 18px;
}
h5 {
  font-size: 16px;
}
h6 {
  font-size: 12px;
}
.table th:not([align]) {
  text-align: left;
}
</style>


</head>

<body>

<style type = "text/css">
.main-container {
  max-width: 940px;
  margin-left: auto;
  margin-right: auto;
}
code {
  color: inherit;
  background-color: rgba(0, 0, 0, 0.04);
}
img {
  max-width:100%;
  height: auto;
}
.tabbed-pane {
  padding-top: 12px;
}
button.code-folding-btn:focus {
  outline: none;
}
</style>


<style type="text/css">
/* padding for bootstrap navbar */
body {
  padding-top: 51px;
  padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar)  */
.section h1 {
  padding-top: 56px;
  margin-top: -56px;
}

.section h2 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h3 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h4 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h5 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h6 {
  padding-top: 56px;
  margin-top: -56px;
}
</style>

<script>
// manage active state of menu based on current page
$(document).ready(function () {
  // active menu anchor
  href = window.location.pathname
  href = href.substr(href.lastIndexOf('/') + 1)
  if (href === "")
    href = "index.html";
  var menuAnchor = $('a[href="' + href + '"]');

  // mark it active
  menuAnchor.parent().addClass('active');

  // if it's got a parent navbar menu mark it active as well
  menuAnchor.closest('li.dropdown').addClass('active');
});
</script>


<div class="container-fluid main-container">

<!-- tabsets -->
<script>
$(document).ready(function () {
  window.buildTabsets("TOC");
});
</script>

<!-- code folding -->
<style type="text/css">
.code-folding-btn { margin-bottom: 4px; }
</style>
<script>
$(document).ready(function () {
  window.initializeCodeFolding("hide" === "show");
});
</script>




<script>
$(document).ready(function ()  {

    // move toc-ignore selectors from section div to header
    $('div.section.toc-ignore')
        .removeClass('toc-ignore')
        .children('h1,h2,h3,h4,h5').addClass('toc-ignore');

    // establish options
    var options = {
      selectors: "h1,h2,h3",
      theme: "bootstrap3",
      context: '.toc-content',
      hashGenerator: function (text) {
        return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
      },
      ignoreSelector: ".toc-ignore",
      scrollTo: 0
    };
    options.showAndHide = true;
    options.smoothScroll = true;

    // tocify
    var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>

<style type="text/css">

#TOC {
  margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
  position: relative;
  width: 100%;
}
}


.toc-content {
  padding-left: 30px;
  padding-right: 40px;
}

div.main-container {
  max-width: 1200px;
}

div.tocify {
  width: 20%;
  max-width: 260px;
  max-height: 85%;
}

@media (min-width: 768px) and (max-width: 991px) {
  div.tocify {
    width: 25%;
  }
}

@media (max-width: 767px) {
  div.tocify {
    width: 100%;
    max-width: none;
  }
}

.tocify ul, .tocify li {
  line-height: 20px;
}

.tocify-subheader .tocify-item {
  font-size: 0.90em;
  padding-left: 25px;
  text-indent: 0;
}

.tocify .list-group-item {
  border-radius: 0px;
}


</style>

<!-- setup 3col/9col grid for toc_float and main content  -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>

<div class="toc-content col-xs-12 col-sm-8 col-md-9">




<div class="navbar navbar-default  navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="index.html">MASHvFLASH</a>
    </div>
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li>
  <a href="index.html">Home</a>
</li>
<li>
  <a href="about.html">About</a>
</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li>
  <a href="https://github.com/willwerscheid/MASHvFLASH">
    <span class="fa fa-github"></span>
     
  </a>
</li>
      </ul>
    </div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

<!-- Add a small amount of space between sections. -->
<style type="text/css">
div.section {
  padding-top: 12px;
}
</style>

<div class="fluid-row" id="header">

<div class="btn-group pull-right">
<button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span>Code</span> <span class="caret"></span></button>
<ul class="dropdown-menu" style="min-width: 50px;">
<li><a id="rmd-show-all-code" href="#">Show All Code</a></li>
<li><a id="rmd-hide-all-code" href="#">Hide All Code</a></li>
</ul>
</div>



<h1 class="title toc-ignore">MASH v FLASH analysis of GTEx data (strong tests)</h1>

</div>


<p><strong>Last updated:</strong> 2018-07-30</p>
<strong>workflowr checks:</strong> <small>(Click a bullet for more information)</small>
<ul>
<li>
<p><details> <summary> <strong style="color:blue;">✔</strong> <strong>R Markdown file:</strong> up-to-date </summary></p>
<p>Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.</p>
</details>
</li>
<li>
<p><details> <summary> <strong style="color:blue;">✔</strong> <strong>Environment:</strong> empty </summary></p>
<p>Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.</p>
</details>
</li>
<li>
<p><details> <summary> <strong style="color:blue;">✔</strong> <strong>Seed:</strong> <code>set.seed(20180609)</code> </summary></p>
<p>The command <code>set.seed(20180609)</code> was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.</p>
</details>
</li>
<li>
<p><details> <summary> <strong style="color:blue;">✔</strong> <strong>Session information:</strong> recorded </summary></p>
<p>Great job! Recording the operating system, R version, and package versions is critical for reproducibility.</p>
</details>
</li>
<li>
<p><details> <summary> <strong style="color:blue;">✔</strong> <strong>Repository version:</strong> <a href="https://github.com/willwerscheid/MASHvFLASH/tree/ed4cb94ce9da30015ab0b1e518b3de23491f1545" target="_blank">ed4cb94</a> </summary></p>
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated. <br><br> Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use <code>wflow_publish</code> or <code>wflow_git_commit</code>). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:
<pre><code>
Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    data/.DS_Store
    Ignored:    docs/.DS_Store
    Ignored:    docs/images/.DS_Store
    Ignored:    docs/images/.Rapp.history
    Ignored:    output/.DS_Store
    Ignored:    output/.Rapp.history
    Ignored:    output/MASHvFLASHgtex/.DS_Store
    Ignored:    output/MASHvFLASHrandom/
    Ignored:    output/MASHvFLASHsims/.DS_Store
    Ignored:    output/MASHvFLASHsims/backfit/.DS_Store
    Ignored:    output/MASHvFLASHsims/backfit/.Rapp.history

Untracked files:
    Untracked:  code/MASHvFLASHgtex2.R
    Untracked:  code/gtex3.R

Unstaged changes:
    Deleted:    analysis/mashvflash.Rmd

</code></pre>
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes. </details>
</li>
</ul>
<details> <summary> <small><strong>Expand here to see past versions:</strong></small> </summary>
<ul>
<table style="border-collapse:separate; border-spacing:5px;">
<thead>
<tr>
<th style="text-align:left;">
File
</th>
<th style="text-align:left;">
Version
</th>
<th style="text-align:left;">
Author
</th>
<th style="text-align:left;">
Date
</th>
<th style="text-align:left;">
Message
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/willwerscheid/MASHvFLASH/blob/ed4cb94ce9da30015ab0b1e518b3de23491f1545/analysis/MASHvFLASHgtex.Rmd" target="_blank">ed4cb94</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-07-30
</td>
<td style="text-align:left;">
wflow_publish(c(“analysis/MASHvFLASHgtex.Rmd”,
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/willwerscheid/MASHvFLASH/860aa52c7988a06c33df007c928bfce326e277fe/docs/MASHvFLASHgtex.html" target="_blank">860aa52</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-07-22
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/willwerscheid/MASHvFLASH/blob/3abd505e1c42acd32d1e7c9f8901f8f50efc9335/analysis/MASHvFLASHgtex.Rmd" target="_blank">3abd505</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-07-22
</td>
<td style="text-align:left;">
wflow_publish(c(“analysis/MASHvFLASHsims.Rmd”,
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/willwerscheid/MASHvFLASH/36a39f6841360fa0e59164042c3f7ae97935cddf/docs/MASHvFLASHgtex.html" target="_blank">36a39f6</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-07-22
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/willwerscheid/MASHvFLASH/blob/179099b4e78b6f5586b08e6aab9526e18ddd683e/analysis/MASHvFLASHgtex.Rmd" target="_blank">179099b</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-07-22
</td>
<td style="text-align:left;">
wflow_publish(c(“analysis/MASHvFLASHgtex.Rmd”,
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/willwerscheid/MASHvFLASH/b0585338798be5e57e71b9f1fcf5ff8db3503fc3/docs/MASHvFLASHgtex.html" target="_blank">b058533</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-07-22
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/willwerscheid/MASHvFLASH/blob/1ecdfadf50f08c908a327ea2cbfe803011b88bf1/analysis/MASHvFLASHgtex.Rmd" target="_blank">1ecdfad</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-07-22
</td>
<td style="text-align:left;">
wflow_publish(“analysis/MASHvFLASHgtex.Rmd”)
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/willwerscheid/MASHvFLASH/ed5a35b8df25b43abfd2ab3ae094250adeb84180/docs/MASHvFLASHgtex.html" target="_blank">ed5a35b</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-26
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/willwerscheid/MASHvFLASH/blob/8fa6b091a5b8f8207595a1e81477ec5ac6e10235/analysis/MASHvFLASHgtex.Rmd" target="_blank">8fa6b09</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-26
</td>
<td style="text-align:left;">
wflow_publish(“analysis/MASHvFLASHgtex.Rmd”)
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/willwerscheid/MASHvFLASH/97fa87ceb554d2998662e546e58eceebcea3ce5d/docs/MASHvFLASHgtex.html" target="_blank">97fa87c</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-24
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/willwerscheid/MASHvFLASH/blob/42cd89c485cdf6fb255679adcd67c8e2b098018e/analysis/MASHvFLASHgtex.Rmd" target="_blank">42cd89c</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-24
</td>
<td style="text-align:left;">
wflow_publish(c(“analysis/MASHvFLASHsims2.Rmd”,
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/willwerscheid/MASHvFLASH/0397c51fd4f4c65f9cb593dff226d5f432d47090/docs/MASHvFLASHgtex.html" target="_blank">0397c51</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-21
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/willwerscheid/MASHvFLASH/blob/aeaca04f5f52161df88b1ba71fc9c1877f5cea53/analysis/MASHvFLASHgtex.Rmd" target="_blank">aeaca04</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-21
</td>
<td style="text-align:left;">
wflow_publish(“analysis/MASHvFLASHgtex.Rmd”)
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/willwerscheid/MASHvFLASH/c599bfae88712b62380a918708c87c437dd2c067/docs/MASHvFLASHgtex.html" target="_blank">c599bfa</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-20
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/willwerscheid/MASHvFLASH/blob/122f83a25d9ea83f5c50e9b0f150ef5ecd854963/analysis/MASHvFLASHgtex.Rmd" target="_blank">122f83a</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-20
</td>
<td style="text-align:left;">
wflow_publish(“analysis/MASHvFLASHgtex.Rmd”)
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/willwerscheid/MASHvFLASH/b1ff37c45685ef0877b34a2f3ad1b036333fcddd/docs/MASHvFLASHgtex.html" target="_blank">b1ff37c</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-16
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/willwerscheid/MASHvFLASH/blob/eac4059fca5ec35717dfa5999b725b3a54a888ac/analysis/MASHvFLASHgtex.Rmd" target="_blank">eac4059</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-16
</td>
<td style="text-align:left;">
wflow_publish(“analysis/MASHvFLASHgtex.Rmd”)
</td>
</tr>
<tr>
<td style="text-align:left;">
html
</td>
<td style="text-align:left;">
<a href="https://cdn.rawgit.com/willwerscheid/MASHvFLASH/0aa5cc6cae8fe4ab3a0b500de260d2696d8f800e/docs/MASHvFLASHgtex.html" target="_blank">0aa5cc6</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-16
</td>
<td style="text-align:left;">
Build site.
</td>
</tr>
<tr>
<td style="text-align:left;">
Rmd
</td>
<td style="text-align:left;">
<a href="https://github.com/willwerscheid/MASHvFLASH/blob/d8b63311e6d1ebbc4ad381509a80e7185eb8b564/analysis/MASHvFLASHgtex.Rmd" target="_blank">d8b6331</a>
</td>
<td style="text-align:left;">
Jason Willwerscheid
</td>
<td style="text-align:left;">
2018-06-16
</td>
<td style="text-align:left;">
analysis/index.Rmd
</td>
</tr>
</tbody>
</table>
</ul>
<p></details></p>
<hr />
<div id="introduction" class="section level2">
<h2>Introduction</h2>
<p>Here I compare MASH and FLASH fits to some GTEx data. The dataset is comprised of <span class="math inline">\(z\)</span>-scores across 44 tissues for approximately 16000 tests, which correspond to the “top” eQTL for each gene (that is, the eQTL with the largest (absolute) raw <span class="math inline">\(z\)</span>-score).</p>
<p>To evaluate the fits, I randomly delete entries from the dataset, then I fit MASH and FLASH objects and use the fitted objects to impute the missing data. I run four experiments in which I delete 1%, 5%, 10%, and 25% of entries, and I compute the mean-squared error for the imputed values and the percentage of 95% confidence intervals that contain the “true” values.</p>
<p>Of course, the deleted entries are observed values rather than true values, so results here should be taken with a grain of salt!</p>
<p>For the code used in this analysis, see <a href="#code">below</a>.</p>
</div>
<div id="fitting-methods" class="section level2">
<h2>Fitting methods</h2>
<p>In addition to a MASH fit, I used the “OHF” and “Zero” methods described in my <a href="MASHvFLASHsims.html">simulation study</a> to fit FLASH objects. (Since the “Zero” and “OHL” methods give very similar results, I did not use the latter here.) For each of the FLASH methods, I produce one fit using point-normal priors (<code>ebnm_fn = &quot;ebnm_pn&quot;</code>) and another fit using a more flexible class of <code>ash</code> priors (<code>ebnm_fn = &quot;ebnm_ash&quot;</code>). In sum, I generated a total of five fits for each experiment, which I denote as <code>mash</code>, <code>pn.OHF</code>, <code>ash.OHF</code>, <code>pn.zero</code>, and <code>ash.zero</code>.</p>
<p>Each of these methods assumes that noise is independent among conditions. It is not, but it is still useful to see how the methods compare when applied to a real dataset.</p>
</div>
<div id="results" class="section level2">
<h2>Results</h2>
<p>Since the experiments take a long time to run, I pre-run them and load the results from file.</p>
<pre class="r"><code>fl_diag &lt;- readRDS(&quot;./output/MASHvFLASHgtex/fldiag.rds&quot;)
fl_t &lt;- readRDS(&quot;./output/MASHvFLASHgtex/flt.rds&quot;)
m_diag &lt;- readRDS(&quot;./output/MASHvFLASHgtex/mdiag.rds&quot;)
m_t &lt;- readRDS(&quot;./output/MASHvFLASHgtex/mt.rds&quot;)</code></pre>
<p>Comparisons of mean-squared error and confidence interval coverage are as follows. FLASH consistently outperforms MASH, with the “OHF” method outperforming the “Zero” method. Using <code>ash</code> priors gives slightly better results than using point-normal priors.</p>
<pre class="r"><code>fl_fit_names &lt;- c(&quot;pn.zero&quot;, &quot;pn.OHF&quot;, &quot;ash.zero&quot;, &quot;ash.OHF&quot;)
method.names &lt;- c(fl_fit_names, &quot;mash&quot;)

all_t &lt;- rbind(sapply(fl_t, unlist), unlist(m_t))
rownames(all_t) &lt;- method.names

fl_mse &lt;- sapply(fl_diag, function(x) {sapply(x, function(y) {y$mse})})
all_mse &lt;- rbind(fl_mse, sapply(m_diag, function(x) {x$mse}))
rownames(all_mse) &lt;- method.names

fl_ci &lt;- sapply(fl_diag, function(x) {sapply(x, function(y) {y$ci})})
all_ci &lt;- rbind(fl_ci, sapply(m_diag, function(x) {x$ci}))
rownames(all_ci) &lt;- method.names

plot.order &lt;- c(2, 4, 1, 3, 5)

boxplot(t(all_mse[plot.order, ]), ylim=c(0, 1), ylab = &quot;MSE&quot;,
        main = &quot;Mean-squared error&quot;)</code></pre>
<p><img src="figure/MASHvFLASHgtex.Rmd/res1-1.png" width="672" style="display: block; margin: auto;" /></p>
<pre class="r"><code>boxplot(t(all_ci[plot.order, ]), ylim=c(0, 1), ylab = &quot;coverage&quot;,
        main = &quot;Proportion of 95% CIs containing true value&quot;)</code></pre>
<p><img src="figure/MASHvFLASHgtex.Rmd/res1-2.png" width="672" style="display: block; margin: auto;" /></p>
<p>The total time required by each method (including both the time needed to produce a fit and the time needed to compute posterior summaries) is as follows. While <code>ash</code> priors give slightly better results than point-normal priors, they take approximately twice as long to fit.</p>
<pre class="r"><code>boxplot(t(all_t[plot.order, ]), ylim=c(0, 60), ylab=&quot;minutes&quot;,
        main=&quot;Time needed for fit and MSE and CI calculations&quot;)</code></pre>
<p><img src="figure/MASHvFLASHgtex.Rmd/res2-1.png" width="672" style="display: block; margin: auto;" /></p>
</div>
<div id="conclusions" class="section level2">
<h2>Conclusions</h2>
<p>Of all methods, only the “OHF” methods give satisfactory results for confidence interval coverage. They also do very well in terms of mean-squared error. Indeed, I am surprised that their MSE is so much smaller than 1. The observational error is supposed to be equal to 1, so I would expect it to be very difficult for any method to achieve an MSE smaller than 1. Indeed, if the errors were uncorrelated, then this would be true, but as I mentioned above, observational errors are almost certainly correlated across tissues. Thus, much of the reason for FLASH’s success here could be due to its ability to account for such correlations. Still, the results are very encouraging.</p>
</div>
<div id="code" class="section level2">
<h2>Code</h2>
<p>for fitting MASH and FLASH objects…</p>
<pre class="r"><code># Fit using FLASH -------------------------------------------------------
fit_flash_zero &lt;- function(data, Kmax, ebnm_fn=&quot;ebnm_pn&quot;,
                           init_fn=&quot;udv_svd&quot;, greedy=TRUE, backfit=TRUE,
                           warmstart=TRUE) {
  if (is.matrix(data)) {
    data &lt;- flash_set_data(Y, S = 1)
  }
  n &lt;- nrow(data$Y)

  t0 &lt;- Sys.time()
  if (greedy) {
    res &lt;- flash_add_greedy(data, Kmax, var_type=&quot;zero&quot;,
                            ebnm_fn=ebnm_fn, init_fn=init_fn,
                            warmstart=warmstart)
    fl &lt;- res$f
  } else {
    fl &lt;- flash_add_factors_from_data(data, Kmax, init_fn=init_fn)
  }
  t1 &lt;- Sys.time()
  if (backfit) {
    res &lt;- flash_backfit(data, fl, var_type=&quot;zero&quot;,
                         ebnm_fn=ebnm_fn)
    fl &lt;- res$f
  }
  t2 &lt;- Sys.time()

  t.greedy &lt;- t1 - t0
  t.backfit &lt;- t2 - t1

  list(f = fl, t.greedy = t.greedy, t.backfit = t.backfit)
}


fit_flash_OHL &lt;- function(data, Kmax, ebnm_fn=&quot;ebnm_pn&quot;,
                          init_fn=&quot;udv_svd&quot;, greedy=TRUE, backfit=TRUE,
                          warmstart=TRUE) {
  if (is.matrix(data)) {
    data &lt;- flash_set_data(Y, S = 1)
  }
  n &lt;- nrow(data$Y)
  canonical &lt;- cbind(rep(1, n), diag(rep(1, n)))

  zero.res &lt;- fit_flash_zero(Y, Kmax, ebnm_fn, init_fn,
                             greedy, backfit=FALSE, warmstart)

  fl &lt;- flash_add_fixed_l(data, canonical, zero.res$f, init_fn=init_fn)

  t0 &lt;- Sys.time()
  if (backfit) {
    res &lt;- flash_backfit(data, fl,
                         var_type=&quot;zero&quot;, ebnm_fn=ebnm_fn,
                         nullcheck=FALSE)
    fl &lt;- res$f
  } else {
    K &lt;- flashr:::flash_get_k(fl)
    res &lt;- flash_backfit(data, fl, kset=(K - ncol(canonical) + 1):K,
                         var_type=&quot;zero&quot;, ebnm_fn=ebnm_fn,
                         nullcheck=FALSE)
    fl &lt;- res$f
  }
  t1 &lt;- Sys.time()

  t.backfit &lt;- Sys.time() - t0

  list(f = fl, t.greedy = zero.res$t.greedy, t.backfit = t.backfit)
}


fit_flash_OHF &lt;- function(data, Kmax, ebnm_fn=&quot;ebnm_pn&quot;,
                          init_fn=&quot;udv_svd&quot;, greedy=TRUE, backfit=TRUE,
                          warmstart=TRUE) {
  if (is.matrix(data)) {
    data &lt;- flash_set_data(Y, S = 1)
  }
  n &lt;- nrow(data$Y)
  canonical &lt;- cbind(rep(1, n), diag(rep(1, n)))

  t0 &lt;- Sys.time()
  fl &lt;- flash_add_fixed_l(data, canonical)
  res &lt;- flash_backfit(data, fl, var_type=&quot;zero&quot;, ebnm_fn=ebnm_fn,
                       nullcheck=FALSE)
  fl &lt;- res$f
  t1 &lt;- Sys.time()

  if (greedy) {
    res &lt;- flash_add_greedy(data, Kmax, fl, var_type=&quot;zero&quot;,
                            ebnm_fn=ebnm_fn, init_fn=init_fn,
                            warmstart=warmstart)
    fl &lt;- res$f
  } else {
    fl &lt;- flash_add_factors_from_data(data, Kmax, fl, init_fn=init_fn)
  }
  t2 &lt;- Sys.time()
  K = flashr:::flash_get_k(fl)
  if (backfit &amp;&amp; K &gt; ncol(canonical)) {
    res &lt;- flash_backfit(data, fl,
                         kset=(ncol(canonical) + 1):K,
                         var_type=&quot;zero&quot;, ebnm_fn=ebnm_fn,
                         nullcheck=FALSE)
    fl &lt;- res$f
  }
  t3 &lt;- Sys.time()

  t.greedy &lt;- t2 - t1
  t.backfit &lt;- (t1 - t0) + (t3 - t2)

  list(f = fl, t.greedy = t.greedy, t.backfit = t.backfit)
}


# Fit using MASH -------------------------------------------------------
fit_mash &lt;- function(data) {
  if (is.matrix(data)) {
    data &lt;- mash_set_data(t(Y))
  }
  timing &lt;- list()

  # time to create canonical matrices is negligible
  U = cov_canonical(data)

  t0 &lt;- Sys.time()
  m.1by1 &lt;- mash_1by1(data)
  lvl &lt;- 0.05
  strong &lt;- get_significant_results(m.1by1, lvl)
  while (length(strong) &lt; 5 &amp;&amp; lvl &lt; 0.5) {
    lvl &lt;- lvl + 0.05
    strong &lt;- get_significant_results(m.1by1, lvl)
  }
  if (length(strong) &gt;= 5) {
    U.pca &lt;- cov_pca(data, 5, strong)
    U.ed &lt;- cov_ed(data, U.pca, strong)
    U &lt;- c(U, U.ed)
    t.ed &lt;- Sys.time() - t0
  } else {
    t.ed &lt;- as.difftime(0, units=&quot;secs&quot;)
  }

  t0 &lt;- Sys.time()
  m &lt;- mash(data, U)
  t.mash &lt;- Sys.time() - t0

  list(m = m, t.ed = t.ed, t.mash = t.mash)
}</code></pre>
<p>…for evaluating performance…</p>
<pre class="r"><code># Evaluate methods based on MSE, CI coverage, and TPR vs. FPR -----------

flash_diagnostics &lt;- function(fl, Y, true_Y, nsamp) {
  MSE &lt;- flash_mse(fl, true_Y)

  # Sample from FLASH fit to estimate CI coverage and TPR vs. FPR
  fl_sampler &lt;- flash_sampler(Y, fl, fixed=&quot;loadings&quot;)
  fl_samp &lt;- fl_sampler(nsamp)

  CI &lt;- flash_ci(fl_samp, true_Y)
  ROC &lt;- flash_roc(fl, fl_samp, true_Y)

  list(MSE = MSE, CI = CI, TP = ROC$TP, FP = ROC$FP,
       n_nulls = ROC$n_nulls, n_nonnulls = ROC$n_nonnulls)
}

mash_diagnostics &lt;- function(m, true_Y) {
  MSE &lt;- mash_mse(m, true_Y)
  CI &lt;- mash_ci(m, true_Y)
  ROC &lt;- mash_roc(m, true_Y)

  list(MSE = MSE, CI = CI, TP = ROC$TP, FP = ROC$FP,
       n_nulls = ROC$n_nulls, n_nonnulls = ROC$n_nonnulls)
}


# MSE of posterior means (FLASH) ----------------------------------------
flash_mse &lt;- function(fl, true_Y) {
  mean((flash_get_fitted_values(fl) - true_Y)^2)
}

# MSE for MASH ----------------------------------------------------------
mash_mse &lt;- function(m, true_Y) {
  mean((get_pm(m) - t(true_Y))^2)
}


# 95% CI coverage for FLASH ---------------------------------------------
flash_ci &lt;- function(fl_samp, true_Y) {
  n &lt;- nrow(true_Y)
  p &lt;- ncol(true_Y)
  nsamp &lt;- length(fl_samp)

  flat_samp &lt;- matrix(0, nrow=n*p, ncol=nsamp)
  for (i in 1:nsamp) {
    flat_samp[, i] &lt;- as.vector(fl_samp[[i]])
  }
  CI &lt;- t(apply(flat_samp, 1, function(x) {quantile(x, c(0.025, 0.975))}))
  mean((as.vector(true_Y) &gt;= CI[, 1]) &amp; (as.vector(true_Y) &lt;= CI[, 2]))
}

# 95% CI coverage for MASH ----------------------------------------------
mash_ci &lt;- function(m, true_Y) {
  Y &lt;- t(true_Y)
  mean((Y &gt; get_pm(m) - 1.96 * get_psd(m))
      &amp; (Y &lt; get_pm(m) + 1.96 * get_psd(m)))
}


# LFSR for FLASH --------------------------------------------------------
flash_lfsr &lt;- function(fl_samp) {
  nsamp &lt;- length(fl_samp)
  n &lt;- nrow(fl_samp[[1]])
  p &lt;- ncol(fl_samp[[1]])

  pp &lt;- matrix(0, nrow=n, ncol=p)
  pn &lt;- matrix(0, nrow=n, ncol=p)
  for (i in 1:nsamp) {
    pp &lt;- pp + (fl_samp[[i]] &gt; 0)
    pn &lt;- pn + (fl_samp[[i]] &lt; 0)
  }
  1 - pmax(pp, pn) / nsamp
}


# Quantities for plotting ROC curves -----------------------------------
flash_roc &lt;- function(fl, fl_samp, true_Y, step=0.01) {
  roc_data(flash_get_fitted_values(fl), true_Y, flash_lfsr(fl_samp), step)
}

mash_roc &lt;- function(m, true_Y, step=0.01) {
  roc_data(get_pm(m), t(true_Y), get_lfsr(m), step)
}

roc_data &lt;- function(pm, true_Y, lfsr, step) {
  correct_sign &lt;- pm * true_Y &gt; 0
  is_null &lt;- true_Y == 0
  n_nulls &lt;- sum(is_null)
  n_nonnulls &lt;- length(true_Y) - n_nulls

  ts &lt;- seq(0, 1, by=step)
  tp &lt;- rep(0, length(ts))
  fp &lt;- rep(0, length(ts))

  for (t in 1:length(ts)) {
    signif &lt;- lfsr &lt;= ts[t]
    tp[t] &lt;- sum(signif &amp; correct_sign)
    fp[t] &lt;- sum(signif &amp; is_null)
  }

  list(ts = ts, TP = tp, FP = fp, n_nulls = n_nulls, n_nonnulls = n_nonnulls)
}


# empirical false sign rate vs. local false sign rate
# efsr_by_lfsr &lt;- function(pm, true_Y, lfsr, step) {
#   pred_signs &lt;- sign(pm)
#   pred_zeros &lt;- pred_signs == 0
#   pred_signs[pred_zeros] &lt;- sample(c(0, 1), length(pred_zeros), replace=T)
#
#   gotitright &lt;- (pred_signs == sign(true_Y))
#
#   nsteps &lt;- floor(.5 / step)
#   efsr_by_lfsr &lt;- rep(0, nsteps)
#   for (k in 1:nsteps) {
#     idx &lt;- (lfsr &gt;= (step * (k - 1)) &amp; lfsr &lt; (step * k))
#     efsr_by_lfsr[k] &lt;- ifelse(sum(idx) == 0, NA,
#                               1 - sum(gotitright[idx]) / sum(idx))
#   }
#   efsr_by_lfsr
# }</code></pre>
<p>…for evaluating peformance on data imputation tasks…</p>
<pre class="r"><code>get_missing &lt;- function(data, seed, pct_missing = 0.05) {
  set.seed(seed)
  missing &lt;- rbinom(length(data), 1, prob = pct_missing)
  return(matrix(missing, nrow=nrow(data), ncol=ncol(data)))
}

set_fl_data_with_missing &lt;- function(data, missing) {
  data[missing] &lt;- NA
  return(flash_set_data(data, S = 1))
}

set_m_data_with_missing &lt;- function(data, missing) {
  data[missing] &lt;- 0
  Shat &lt;- matrix(1, nrow=nrow(data), ncol=ncol(data))
  Shat[missing] &lt;- 1e6
  return(mash_set_data(data, Shat))
}

imputation_mse &lt;- function(fitted, true_Y, missing) {
  sq_error &lt;- (fitted - true_Y)^2
  sq_error &lt;- sq_error[missing == 1]
  return(mean(sq_error))
}

flash_imputation_ci &lt;- function(data, fl, true_Y, missing, nsamp = 200) {
  true_Y &lt;- as.matrix(true_Y[missing == 1])

  fl_sampler &lt;- flash_sampler(data, fl, fixed=&quot;loadings&quot;)
  fl_samp &lt;- fl_sampler(nsamp)
  fl_samp &lt;- lapply(fl_samp, function(samp){as.matrix(samp[missing == 1])})
  return(flash_ci(fl_samp, true_Y))
}

mash_imputation_ci &lt;- function(m, true_Y, missing) {
  pm &lt;- get_pm(m)[missing == 1]
  psd &lt;- get_psd(m)[missing == 1]
  Y &lt;- true_Y[missing == 1]
  mean((Y &gt; pm - 1.96 * psd)
       &amp; (Y &lt; pm + 1.96 * psd))
}</code></pre>
<p>…and the main function calls.</p>
<pre class="r"><code># Make sure to use branch &quot;trackObj&quot; when loading flashr.

# devtools::install_github(&quot;stephenslab/flashr&quot;, ref=&quot;trackObj&quot;)
devtools::load_all(&quot;/Users/willwerscheid/GitHub/flashr/&quot;)
# devtools::install_github(&quot;stephenslab/ebnm&quot;)
devtools::load_all(&quot;/Users/willwerscheid/GitHub/ebnm/&quot;)

library(mashr)

source(&quot;./code/fits.R&quot;)
source(&quot;./code/utils.R&quot;)
source(&quot;./code/gtexutils.R&quot;)

fpath &lt;- &quot;./output/MASHvFLASHgtex/&quot;
# fpath &lt;- &quot;./output/MASHvFLASHrandom/&quot;

gtex &lt;- readRDS(gzcon(url(&quot;https://github.com/stephenslab/gtexresults/blob/master/data/MatrixEQTLSumStats.Portable.Z.rds?raw=TRUE&quot;)))

data &lt;- t(gtex$strong.z)
# data &lt;- t(gtex$random.z)

seeds &lt;- 1:4
pcts &lt;- c(0.01, 0.05, 0.1, 0.25)

fl_fits &lt;- c(function(fl_data) {fit_flash_zero(fl_data, Kmax = 50,
                                               ebnm_fn = &quot;ebnm_pn&quot;,
                                               init_fn = &quot;udv_si_svd&quot;,
                                               backfit = FALSE,
                                               warmstart = TRUE)},
             function(fl_data) {fit_flash_OHF(fl_data, Kmax = 50,
                                              ebnm_fn = &quot;ebnm_pn&quot;,
                                              init_fn = &quot;udv_si_svd&quot;,
                                              backfit = FALSE,
                                              warmstart = TRUE)},
             function(fl_data) {fit_flash_zero(fl_data, Kmax = 50,
                                               ebnm_fn = &quot;ebnm_ash&quot;,
                                               init_fn = &quot;udv_si_svd&quot;,
                                               backfit = FALSE,
                                               warmstart = TRUE)},
             function(fl_data) {fit_flash_OHF(fl_data, Kmax = 50,
                                              ebnm_fn = &quot;ebnm_ash&quot;,
                                              init_fn = &quot;udv_si_svd&quot;,
                                              backfit = FALSE,
                                              warmstart = TRUE)})
fl_fit_names &lt;- c(&quot;pn.zero&quot;, &quot;pn.OHF&quot;, &quot;ash.zero&quot;, &quot;ash.OHF&quot;)

fl_res &lt;- list()
fl_diag &lt;- list()
fl_t &lt;- list()

for (i in 1:length(seeds)) {
  message(paste0(&quot;Running experiment #&quot;, i))
  missing &lt;- get_missing(data, seed = seeds[i], pct_missing = pcts[i])
  fl_data &lt;- set_fl_data_with_missing(data, missing)

  fl_res[[i]] &lt;- list()
  fl_diag[[i]] &lt;- list()
  fl_t[[i]] &lt;- list()

  for (j in 1:length(fl_fits)) {
    message(paste(&quot;Fitting&quot;, fl_fit_names[j]))
    t0 &lt;- Sys.time()
    fl_fit &lt;- fl_fits[[j]](fl_data)
    fl_res[[i]][[fl_fit_names[j]]] &lt;- fl_fit$f
    diagnostics &lt;- list(mse = imputation_mse(flash_get_fitted_values(fl_fit$f),
                                             data, missing),
                        ci = flash_imputation_ci(fl_data, fl_fit$f,
                                                 data, missing))
    fl_diag[[i]][[fl_fit_names[j]]] &lt;- diagnostics
    fl_t[[i]][[fl_fit_names[j]]] &lt;- Sys.time() - t0
  }

  saveRDS(fl_res, paste0(fpath, &quot;res.rds&quot;))
  saveRDS(fl_diag, paste0(fpath, &quot;fldiag.rds&quot;))
  saveRDS(fl_t, paste0(fpath, &quot;flt.rds&quot;))
}

m_res &lt;- list()
m_diag &lt;- list()
m_t &lt;- list()

for (i in 1:length(seeds)) {
  message(paste0(&quot;Running experiment #&quot;, i))
  missing &lt;- get_missing(data, seed = seeds[i], pct_missing = pcts[i])
  m_data &lt;- set_m_data_with_missing(t(data), t(missing))

  message(&quot;Fitting mash&quot;)
  t0 &lt;- Sys.time()
  m_fit &lt;- fit_mash(m_data)
  m_res[[i]] &lt;- m_fit$m
  diagnostics &lt;- list(mse = imputation_mse(get_pm(m_fit$m),
                                           t(data), t(missing)),
                      ci = mash_imputation_ci(m_fit$m, t(data),
                                              t(missing)))
  m_diag[[i]] &lt;- diagnostics
  m_t[[i]] &lt;- Sys.time() - t0
  saveRDS(m_res, paste0(fpath, &quot;mres.rds&quot;))
  saveRDS(m_diag, paste0(fpath, &quot;mdiag.rds&quot;))
  saveRDS(m_t, paste0(fpath, &quot;mt.rds&quot;))
}</code></pre>
</div>
<div id="session-information" class="section level2">
<h2>Session information</h2>
<pre class="r"><code>sessionInfo()</code></pre>
<pre><code>R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] workflowr_1.0.1   Rcpp_0.12.17      digest_0.6.15    
 [4] rprojroot_1.3-2   R.methodsS3_1.7.1 backports_1.1.2  
 [7] git2r_0.21.0      magrittr_1.5      evaluate_0.10.1  
[10] stringi_1.1.6     whisker_0.3-2     R.oo_1.21.0      
[13] R.utils_2.6.0     rmarkdown_1.8     tools_3.4.3      
[16] stringr_1.3.0     yaml_2.1.17       compiler_3.4.3   
[19] htmltools_0.3.6   knitr_1.20       </code></pre>
</div>

<!-- Adjust MathJax settings so that all math formulae are shown using
TeX fonts only; see
http://docs.mathjax.org/en/latest/configuration.html.  This will make
the presentation more consistent at the cost of the webpage sometimes
taking slightly longer to load. Note that this only works because the
footer is added to webpages before the MathJax javascript. -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    "HTML-CSS": { availableFonts: ["TeX"] }
  });
</script>

<hr>
<p>
  This reproducible <a href="http://rmarkdown.rstudio.com">R Markdown</a>
  analysis was created with
  <a href="https://github.com/jdblischak/workflowr">workflowr</a> 1.0.1
</p>
<hr>


</div>
</div>

</div>

<script>

// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
  $('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
  bootstrapStylePandocTables();
});


</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>