<!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 simulation results</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> <li> <a href="license.html">License</a> </li> </ul> <ul class="nav navbar-nav navbar-right"> <li> <a href="https://github.com/jdblischak/workflowr"> <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 simulation results</h1> </div> <p><strong>Last updated:</strong> 2018-06-19</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/bdf2579326ce383f77f1d9ae89a21ab689bdd48c" target="_blank">bdf2579</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: docs/.DS_Store Ignored: docs/images/.DS_Store Ignored: output/.DS_Store Unstaged changes: Modified: code/fits.R Modified: code/main.R Modified: code/mashvflash.R Modified: output/sim1res.rds Modified: output/sim2res.rds Modified: output/sim3res.rds Modified: output/sim4res.rds Modified: output/sim5res.rds Modified: output/sim6res.rds Modified: output/sim7res.rds </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;"> html </td> <td style="text-align:left;"> <a href="https://cdn.rawgit.com/willwerscheid/MASHvFLASH/358412a77652ea852898bb1b735918b20f7e4b41/docs/MASHvFLASHsims.html" target="_blank">358412a</a> </td> <td style="text-align:left;"> Jason Willwerscheid </td> <td style="text-align:left;"> 2018-06-15 </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/a9a9e11a53e9b935379d8d4ce0c76537328d93d7/analysis/MASHvFLASHsims.Rmd" target="_blank">a9a9e11</a> </td> <td style="text-align:left;"> Jason Willwerscheid </td> <td style="text-align:left;"> 2018-06-15 </td> <td style="text-align:left;"> wflow_publish(“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/726168a9cec3280d38929ce716dcb7483b7af67c/docs/MASHvFLASHsims.html" target="_blank">726168a</a> </td> <td style="text-align:left;"> Jason Willwerscheid </td> <td style="text-align:left;"> 2018-06-15 </td> <td style="text-align:left;"> Build site. </td> </tr> <tr> <td style="text-align:left;"> html </td> <td style="text-align:left;"> <a href="https://cdn.rawgit.com/willwerscheid/MASHvFLASH/aa46b383b5ef586461f9e8ee43d9adf1c5cb24d8/docs/MASHvFLASHsims.html" target="_blank">aa46b38</a> </td> <td style="text-align:left;"> Jason Willwerscheid </td> <td style="text-align:left;"> 2018-06-15 </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/d3f8191a4d8323b10f600574ea8fefbb74b071ac/analysis/MASHvFLASHsims.Rmd" target="_blank">d3f8191</a> </td> <td style="text-align:left;"> Jason Willwerscheid </td> <td style="text-align:left;"> 2018-06-15 </td> <td style="text-align:left;"> wflow_publish(“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/c2699a6b3a91bf348a7780f395328f175ec3c759/docs/MASHvFLASHsims.html" target="_blank">c2699a6</a> </td> <td style="text-align:left;"> Jason Willwerscheid </td> <td style="text-align:left;"> 2018-06-15 </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/3554f17e60c34ba45c678226295c81b8804294e5/analysis/MASHvFLASHsims.Rmd" target="_blank">3554f17</a> </td> <td style="text-align:left;"> Jason Willwerscheid </td> <td style="text-align:left;"> 2018-06-15 </td> <td style="text-align:left;"> wflow_publish(“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/03dc24a7944aca134747c0bdfdb5bfd6875ea2dd/docs/MASHvFLASHsims.html" target="_blank">03dc24a</a> </td> <td style="text-align:left;"> Jason Willwerscheid </td> <td style="text-align:left;"> 2018-06-15 </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/0c0d6b22487b32387084dd31b4353c782cc56c3b/analysis/MASHvFLASHsims.Rmd" target="_blank">0c0d6b2</a> </td> <td style="text-align:left;"> Jason Willwerscheid </td> <td style="text-align:left;"> 2018-06-15 </td> <td style="text-align:left;"> wflow_publish(“analysis/MASHvFLASHsims.Rmd”) </td> </tr> </tbody> </table> </ul> <p></details></p> <hr /> <div id="fitting-methods" class="section level2"> <h2>Fitting methods</h2> <p>The MASH fit is produced following the recommendations in the MASH vignettes (using both canonical matrices and data-driven matrices).</p> <p>Two FLASH fits are produced. FLASH-OHL (for “one-hots last”) adds up to ten factors greedily, then adds a one-hot vector for each row in the data matrix, then backfits the whole thing. FLASH-OHF (for “one-hots first”) adds the one-hot vectors first (as you’ve probably already guessed), then backfits, then greedily adds up to ten factors. In the latter case, the greedily added factors are not subsequently backfit, so FLASH-OHF can be much faster than FLASH-OHL.</p> </div> <div id="simulations" class="section level2"> <h2>Simulations</h2> <p>All simulated datasets <span class="math inline">\(Y\)</span> are of dimension 25 x 1000. In each case, <span class="math inline">\(Y = X + E\)</span>, where <span class="math inline">\(X\)</span> is the matrix of “true” effects and <span class="math inline">\(E\)</span> is a matrix of <span class="math inline">\(N(0, 1)\)</span> noise.</p> </div> <div id="null-model" class="section level2"> <h2>Null model</h2> <p>Here the entries of <span class="math inline">\(X\)</span> are all zero.</p> <table> <thead> <tr class="header"> <th></th> <th align="right">MASH</th> <th align="right">FLASH_OHL</th> <th align="right">FLASH_OHF</th> <th align="right">FLASH_OHFplus</th> </tr> </thead> <tbody> <tr class="odd"> <td>MSE</td> <td align="right">0.003</td> <td align="right">0.003</td> <td align="right">0.003</td> <td align="right">0.003</td> </tr> <tr class="even"> <td>95% CI cov</td> <td align="right">0.880</td> <td align="right">1.000</td> <td align="right">1.000</td> <td align="right">1.000</td> </tr> </tbody> </table> <div class="figure"> <img src="images/sim1time.png" /> </div> </div> <div id="model-with-independent-effects" class="section level2"> <h2>Model with independent effects</h2> <p>Now the columns <span class="math inline">\(X_{:, j}\)</span> are either identically zero (with probability 0.8) or identically nonzero. In the latter case, the entries of the <span class="math inline">\(j\)</span>th column of <span class="math inline">\(X\)</span> are i.i.d. <span class="math inline">\(N(0, 1)\)</span>.</p> <table> <thead> <tr class="header"> <th></th> <th align="right">MASH</th> <th align="right">FLASH_OHL</th> <th align="right">FLASH_OHF</th> <th align="right">FLASH_OHFplus</th> </tr> </thead> <tbody> <tr class="odd"> <td>MSE</td> <td align="right">0.014</td> <td align="right">0.018</td> <td align="right">0.018</td> <td align="right">0.018</td> </tr> <tr class="even"> <td>95% CI cov</td> <td align="right">0.989</td> <td align="right">0.984</td> <td align="right">0.984</td> <td align="right">0.984</td> </tr> </tbody> </table> <p><img src="images/sim2ROC.png" /> <img src="images/sim2time.png" /></p> </div> <div id="model-with-independent-and-shared-effects" class="section level2"> <h2>Model with independent and shared effects</h2> <p>Again 80% of the columns of <span class="math inline">\(X\)</span> are identically zero. But now, only half of the nonzero columns have entries that are i.i.d. <span class="math inline">\(N(0, 1)\)</span>. The other half have entries that are identical across rows, with a value that is drawn from the <span class="math inline">\(N(0, 1)\)</span> distribution. (In other words, the covariance matrix for these columns is a matrix of all ones.)</p> <table> <thead> <tr class="header"> <th></th> <th align="right">MASH</th> <th align="right">FLASH_OHL</th> <th align="right">FLASH_OHF</th> <th align="right">FLASH_OHFplus</th> </tr> </thead> <tbody> <tr class="odd"> <td>MSE</td> <td align="right">0.016</td> <td align="right">0.019</td> <td align="right">0.021</td> <td align="right">0.020</td> </tr> <tr class="even"> <td>95% CI cov</td> <td align="right">0.991</td> <td align="right">0.985</td> <td align="right">0.984</td> <td align="right">0.984</td> </tr> </tbody> </table> <p><img src="images/sim3ROC.png" /> <img src="images/sim3time.png" /></p> </div> <div id="model-with-independent-shared-and-unique-effects" class="section level2"> <h2>Model with independent, shared, and unique effects</h2> <p>This model is similar to the above two, but now only a third of the nonnull columns have independently distributed entries and a third have shared entries. The other third have a unique nonzero entry. (This corresponds, for example, to a gene that is only expressed in a single condition.) The unique effects are distributed uniformly across rows.</p> <table> <thead> <tr class="header"> <th></th> <th align="right">MASH</th> <th align="right">FLASH_OHL</th> <th align="right">FLASH_OHF</th> <th align="right">FLASH_OHFplus</th> </tr> </thead> <tbody> <tr class="odd"> <td>MSE</td> <td align="right">0.016</td> <td align="right">0.020</td> <td align="right">0.021</td> <td align="right">0.021</td> </tr> <tr class="even"> <td>95% CI cov</td> <td align="right">0.988</td> <td align="right">0.983</td> <td align="right">0.983</td> <td align="right">0.983</td> </tr> </tbody> </table> <p><img src="images/sim4ROC.png" /> <img src="images/sim4time.png" /></p> </div> <div id="rank-1-flash-model" class="section level2"> <h2>Rank 1 FLASH model</h2> <p>This is the FLASH model <span class="math inline">\(X = LF\)</span>, where <span class="math inline">\(L\)</span> is an <span class="math inline">\(n\)</span> by <span class="math inline">\(k\)</span> matrix and <span class="math inline">\(F\)</span> is a <span class="math inline">\(k\)</span> by <span class="math inline">\(p\)</span> matrix. In this first simulation, <span class="math inline">\(k = 1\)</span>. 80% of the entries in <span class="math inline">\(F\)</span> and 50% of the entries in <span class="math inline">\(L\)</span> are equal to zero. The other entries are i.i.d. <span class="math inline">\(N(0, 1)\)</span>.</p> <table> <thead> <tr class="header"> <th></th> <th align="right">MASH</th> <th align="right">FLASH_OHL</th> <th align="right">FLASH_OHF</th> <th align="right">FLASH_OHFplus</th> </tr> </thead> <tbody> <tr class="odd"> <td>MSE</td> <td align="right">0.022</td> <td align="right">0.016</td> <td align="right">0.037</td> <td align="right">0.020</td> </tr> <tr class="even"> <td>95% CI cov</td> <td align="right">0.979</td> <td align="right">0.955</td> <td align="right">0.962</td> <td align="right">0.955</td> </tr> </tbody> </table> <p><img src="images/sim5ROC.png" /> <img src="images/sim5time.png" /></p> </div> <div id="rank-5-flash-model" class="section level2"> <h2>Rank 5 FLASH model</h2> <p>This is the same as above with <span class="math inline">\(k = 5\)</span> and with only 20% of the entries in <span class="math inline">\(L\)</span> equal to zero.</p> <table> <thead> <tr class="header"> <th></th> <th align="right">MASH</th> <th align="right">FLASH_OHL</th> <th align="right">FLASH_OHF</th> <th align="right">FLASH_OHFplus</th> </tr> </thead> <tbody> <tr class="odd"> <td>MSE</td> <td align="right">0.115</td> <td align="right">0.077</td> <td align="right">0.268</td> <td align="right">0.181</td> </tr> <tr class="even"> <td>95% CI cov</td> <td align="right">0.928</td> <td align="right">0.901</td> <td align="right">0.909</td> <td align="right">0.875</td> </tr> </tbody> </table> <p><img src="images/sim6ROC.png" /> <img src="images/sim6time.png" /></p> </div> <div id="rank-3-flash-model-with-uv" class="section level2"> <h2>Rank 3 FLASH model with UV</h2> <p>This is similar to the above with <span class="math inline">\(k = 3\)</span> and with 30% of the rows in <span class="math inline">\(L\)</span> equal to zero. In addition, a dense rank-one matrix <span class="math inline">\(W\)</span> is added to <span class="math inline">\(X\)</span> to mimic the effects of unwanted variation. Here, <span class="math inline">\(W = UV\)</span>, with <span class="math inline">\(U\)</span> an <span class="math inline">\(n\)</span> by 1 vector and <span class="math inline">\(V\)</span> a 1 by <span class="math inline">\(p\)</span> vector, both of which have entries distributed <span class="math inline">\(N(0, 0.25)\)</span>.</p> <table> <thead> <tr class="header"> <th></th> <th align="right">MASH</th> <th align="right">FLASH_OHL</th> <th align="right">FLASH_OHF</th> <th align="right">FLASH_OHFplus</th> </tr> </thead> <tbody> <tr class="odd"> <td>MSE</td> <td align="right">0.089</td> <td align="right">0.081</td> <td align="right">0.180</td> <td align="right">0.103</td> </tr> <tr class="even"> <td>95% CI cov</td> <td align="right">0.946</td> <td align="right">0.905</td> <td align="right">0.935</td> <td align="right">0.914</td> </tr> </tbody> </table> <p><img src="images/sim7ROC.png" /> <img src="images/sim7time.png" /></p> </div> <div id="code" class="section level2"> <h2>Code</h2> <p>for simulating datasets…</p> <pre class="r"><code>## SIMULATION FUNCTIONS ------------------------------------------------- # n is number of conditions, p is number of genes # Noise is i.i.d. N(0, 1) get_E <- function(n, p, sd = 1) { matrix(rnorm(n * p, 0, sd), n, p) } # Simulate from null model ---------------------------------------------- null_sim <- function(n, p, seed = NULL) { set.seed(seed) Y <- get_E(n, p) true_Y <- matrix(0, n, p) list(Y = Y, true_Y = true_Y) } # Simulate from MASH model ---------------------------------------------- # Sigma is list of covariance matrices # pi[j] is probability that effect j has covariance Sigma[[j]] # s is sparsity (percentage of null effects) mash_sim <- function(n, p, Sigma, pi = NULL, s = 0.8, seed = NULL) { set.seed(NULL) if (is.null(pi)) { pi = rep(1, length(Sigma)) # default to uniform distribution } assertthat::are_equal(length(pi), length(Sigma)) for (j in length(Sigma)) { assertthat::are_equal(dim(Sigma[j]), c(n, n)) } pi <- pi / sum(pi) # normalize pi to sum to one which_sigma <- sample(1:length(pi), p, replace=TRUE, prob=pi) nonnull_fx <- sample(1:p, floor((1 - s)*p), replace=FALSE) X <- matrix(0, n, p) for (j in nonnull_fx) { X[, j] <- MASS::mvrnorm(1, rep(0, n), Sigma[[which_sigma[j]]]) } Y <- X + get_E(n, p) list(Y = Y, true_Y = X) } # Simulate from FLASH model --------------------------------------------- # fs is sparsity of factors (percentage of null effects) # fvar is variance of effects (generated from normal distribution) # ls is sparsity of loadings # lvar is variance of loadings # UVvar is variance of dense rank-one matrix included to mimic something # like unwanted variation (set it to 0 to ignore it) flash_sim <- function(n, p, k, fs, fvar, ls, lvar, UVvar = 0, seed = NULL) { set.seed(seed) nonnull_ll <- matrix(sample(c(0, 1), n*k, TRUE, c(ls, 1 - ls)), n, k) LL <- nonnull_ll * matrix(rnorm(n*k, 0, sqrt(lvar)), n, k) nonnull_ff <- matrix(sample(c(0, 1), k*p, TRUE, c(fs, 1 - fs)), k, p) FF <- nonnull_ff * matrix(rnorm(k*p, 0, sqrt(fvar)), k, p) X <- LL %*% FF Y <- X + get_E(n, p) # add unwanted variation Y <- Y + outer(rnorm(n, 0, sqrt(UVvar)), rnorm(p, 0, sqrt(UVvar))) list(Y = Y, true_Y = X) } ## SIMULATIONS ---------------------------------------------------------- # Functions to generate seven types of datasets. One is null; three are # from the MASH model; three are from the FLASH model. sim_fns <- function(n, p, s, mashvar, fvar, lvar, UVvar) { # 1. Everything is null sim_null <- function(){ null_sim(n, p) } Sigma <- list() Sigma[[1]] <- diag(rep(mashvar, n)) # 2. Effects are independent across conditions sim_ind <- function(){ mash_sim(n, p, Sigma) } Sigma[[2]] <- matrix(mashvar, n, n) # 3. Effects are either independent or shared sim_indsh <- function(){ mash_sim(n, p, Sigma) } for (j in 1:n) { Sigma[[2 + j]] <- matrix(0, n, n) Sigma[[2 + j]][j, j] <- mashvar } pi <- c(n, n, rep(1, n)) # 4. Effects are independent, shared, or unique to a single condition sim_mash <- function(){ mash_sim(n, p, Sigma) } # 5. Rank one model sim_rank1 <- function(){ flash_sim(n, p, 1, s, fvar, 0.5, lvar) } # 6. Rank 5 model sim_rank5 <- function(){ flash_sim(n, p, 5, s, fvar, 0.2, lvar) } # 7. Rank 3 model with unwanted variation sim_UV <- function(){ flash_sim(n, p, 3, s, fvar, 0.3, lvar, UVvar) } c(sim_null, sim_ind, sim_indsh, sim_mash, sim_rank1, sim_rank5, sim_UV) } sim_names <- c("Null simulation", "All independent effects", "Independent and shared", "Independent, shared, and unique", "Rank 1 FLASH model", "Rank 5 FLASH model", "Rank 3 FLASH with UV")</code></pre> <p>…for fitting MASH and FLASH objects…</p> <pre class="r"><code># Fit using FLASH ------------------------------------------------------- fit_flash <- function(Y, Kmax, method) { n <- nrow(Y) data <- flash_set_data(Y, S = 1) timing <- list() t0 <- Sys.time() if (method %in% c("OHF", "OHFplus")) { fl <- flash_add_fixed_l(data, diag(rep(1, n))) fl <- flash_backfit(data, fl, nullcheck = F, var_type = "zero") t1 <- Sys.time() timing$backfit <- t1 - t0 fl <- flash_add_greedy(data, Kmax, fl, var_type = "zero") timing$greedy <- Sys.time() - t1 if (method == "OHFplus") { t2 <- Sys.time() fl <- flash_backfit(data, fl, nullcheck = F, var_type = "zero") timing$backfit <- timing$backfit + (Sys.time() - t2) } } else { fl <- flash_add_greedy(data, Kmax, var_type = "zero") t1 <- Sys.time() timing$greedy <- t1 - t0 fl <- flash_add_fixed_l(data, diag(rep(1, n)), fl) fl <- flash_backfit(data, fl, nullcheck = F, var_type = "zero") timing$backfit <- Sys.time() - t1 } timing$total <- Reduce(`+`, timing) list(fl = fl, timing = timing) } # Fit using MASH ------------------------------------------------------- fit_mash <- function(Y, ed=T) { data <- mash_set_data(t(Y)) timing <- list() # time to create canonical matrices is negligible U = cov_canonical(data) if (ed) { t0 <- Sys.time() m.1by1 <- mash_1by1(data) strong <- get_significant_results(m.1by1, 0.05) U.pca <- cov_pca(data, 5, strong) U.ed <- cov_ed(data, U.pca, strong) U <- c(U, U.ed) timing$ed <- Sys.time() - t0 } t0 <- Sys.time() m <- mash(data, U) timing$mash <- Sys.time() - t0 timing$total <- Reduce(`+`, timing) list(m = m, timing = timing) }</code></pre> <p>…for evaluating performance…</p> <pre class="r"><code># Evaluate methods based on MSE, CI coverage, and TPR vs. FPR ----------- flash_diagnostics <- function(fl, Y, true_Y, nsamp) { MSE <- flash_mse(fl, true_Y) # Sample from FLASH fit to estimate CI coverage and TPR vs. FPR fl_sampler <- flash_lf_sampler(Y, fl, ebnm_fn=ebnm_pn, fixed="loadings") fl_samp <- fl_sampler(nsamp) CI <- flash_ci(fl_samp, true_Y) ROC <- 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 <- function(m, true_Y) { MSE <- mash_mse(m, true_Y) CI <- mash_ci(m, true_Y) ROC <- 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 <- function(fl, true_Y) { mean((flash_get_lf(fl) - true_Y)^2) } # MSE for MASH ---------------------------------------------------------- mash_mse <- function(m, true_Y) { mean((get_pm(m) - t(true_Y))^2) } # 95% CI coverage for FLASH --------------------------------------------- flash_ci <- function(fl_samp, true_Y) { n <- nrow(true_Y) p <- ncol(true_Y) nsamp <- length(fl_samp) flat_samp <- matrix(0, nrow=n*p, ncol=nsamp) for (i in 1:nsamp) { flat_samp[, i] <- as.vector(fl_samp[[i]]) } CI <- t(apply(flat_samp, 1, function(x) {quantile(x, c(0.025, 0.975))})) mean((as.vector(true_Y) >= CI[, 1]) & (as.vector(true_Y) <= CI[, 2])) } # 95% CI coverage for MASH ---------------------------------------------- mash_ci <- function(m, true_Y) { Y <- t(true_Y) mean((Y > get_pm(m) - 1.96 * get_psd(m)) & (Y < get_pm(m) + 1.96 * get_psd(m))) } # LFSR for FLASH -------------------------------------------------------- flash_lfsr <- function(fl_samp) { nsamp <- length(fl_samp) n <- nrow(fl_samp[[1]]) p <- ncol(fl_samp[[1]]) pp <- matrix(0, nrow=n, ncol=p) pn <- matrix(0, nrow=n, ncol=p) for (i in 1:nsamp) { pp <- pp + (fl_samp[[i]] > 0) pn <- pn + (fl_samp[[i]] < 0) } 1 - pmax(pp, pn) / nsamp } # Quantities for plotting ROC curves ----------------------------------- flash_roc <- function(fl, fl_samp, true_Y, step=0.01) { roc_data(flash_get_lf(fl), true_Y, flash_lfsr(fl_samp), step) } mash_roc <- function(m, true_Y, step=0.01) { roc_data(get_pm(m), t(true_Y), get_lfsr(m), step) } roc_data <- function(pm, true_Y, lfsr, step) { correct_sign <- pm * true_Y > 0 is_null <- true_Y == 0 n_nulls <- sum(is_null) n_nonnulls <- length(true_Y) - n_nulls ts <- seq(0, 1, by=step) tp <- rep(0, length(ts)) fp <- rep(0, length(ts)) for (t in 1:length(ts)) { signif <- lfsr <= ts[t] tp[t] <- sum(signif & correct_sign) fp[t] <- sum(signif & 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 <- function(pm, true_Y, lfsr, step) { # pred_signs <- sign(pm) # pred_zeros <- pred_signs == 0 # pred_signs[pred_zeros] <- sample(c(0, 1), length(pred_zeros), replace=T) # # gotitright <- (pred_signs == sign(true_Y)) # # nsteps <- floor(.5 / step) # efsr_by_lfsr <- rep(0, nsteps) # for (k in 1:nsteps) { # idx <- (lfsr >= (step * (k - 1)) & lfsr < (step * k)) # efsr_by_lfsr[k] <- ifelse(sum(idx) == 0, NA, # 1 - sum(gotitright[idx]) / sum(idx)) # } # efsr_by_lfsr # }</code></pre> <p>…and some ugly functions that run everything and plot results.</p> <pre class="r"><code>run_sims <- function(sim_fn, nsims, plot_title, fpath) { #suppressMessages( #suppressWarnings( #capture.output( if (nsims == 1) { res = run_one_sim(sim_fn) } else { res = run_many_sims(sim_fn, nsims) } #) #) #) saveRDS(output_res_mat(res, plot_title), paste0(fpath, "res.rds")) if (!(plot_title == "Null simulation")) { png(paste0(fpath, "ROC.png")) plot_ROC(res, plot_title) dev.off() } png(paste0(fpath, "time.png")) plot_timing(res) dev.off() } run_many_sims <- function(sim_fn, nsims) { res <- list() combined_res <- list() for (i in 1:nsims) { res[[i]] <- run_one_sim(sim_fn) } list_elem <- names(res[[1]]) for (elem in list_elem) { combined_res[[elem]] <- list() sub_elems <- names(res[[1]][[elem]]) for (sub_elem in sub_elems) { tmp <- lapply(res, function(x) {x[[elem]][[sub_elem]]}) combined_res[[elem]][[sub_elem]] <- Reduce(`+`, tmp) combined_res[[elem]][[sub_elem]] <- combined_res[[elem]][[sub_elem]] / nsims } } combined_res } run_one_sim <- function(sim_fn, Kmax = 10, nsamp=200) { data <- do.call(sim_fn, list()) # If there are no strong signals, trying to run ED throws an error, so # we need to do some error handling to fit the MASH object try(mfit <- fit_mash(data$Y)) if (!exists("mfit")) { mfit <- fit_mash(data$Y, ed=F) mfit$timing$ed = as.difftime(0, units="secs") } flfit1 <- fit_flash(data$Y, Kmax, method = "OHL") flfit2 <- fit_flash(data$Y, Kmax, method = "OHF") flfit3 <- fit_flash(data$Y, Kmax, method = "OHFplus") message("Running MASH diagnostics") mres <- mash_diagnostics(mfit$m, data$true_Y) message("Running FLASH diagnostics") flres1 <- flash_diagnostics(flfit1$fl, data$Y, data$true_Y, nsamp) flres2 <- flash_diagnostics(flfit2$fl, data$Y, data$true_Y, nsamp) flres3 <- flash_diagnostics(flfit3$fl, data$Y, data$true_Y, nsamp) list(mash_timing = mfit$timing, mash_res = mres, flash_OHL_timing = flfit1$timing, flash_OHL_res = flres1, flash_OHF_timing = flfit2$timing, flash_OHF_res = flres2, flash_OHFplus_timing = flfit3$timing, flash_OHFplus_res = flres3) } output_res_mat <- function(res, caption) { data.frame(MASH = c(res$mash_res$MSE, res$mash_res$CI), FLASH_OHL = c(res$flash_OHL_res$MSE, res$flash_OHL_res$CI), FLASH_OHF = c(res$flash_OHF_res$MSE, res$flash_OHF_res$CI), FLASH_OHFplus = c(res$flash_OHFplus_res$MSE, res$flash_OHFplus_res$CI), row.names = c("MSE", "95% CI cov")) } plot_timing <- function(res) { data <- c(res$mash_timing$ed, res$mash_timing$mash, res$flash_OHL_timing$greedy, res$flash_OHL_timing$backfit, res$flash_OHF_timing$greedy, res$flash_OHF_timing$backfit, res$flash_OHFplus_timing$greedy, res$flash_OHFplus_timing$backfit) time_units <- units(data) data <- matrix(as.numeric(data), 2, 4) barplot(data, axes=T, main=paste("Average time to fit in", time_units), names.arg = c("MASH", "FLASH-OHL", "FLASH-OHF", "FLASH-OHF+"), legend.text = c("ED/Greedy", "MASH/Backfit"), ylim = c(0, max(colSums(data))*2)) # (increasing ylim is easiest way to deal with legend getting in way) } plot_ROC <- function(res, main="ROC curve") { m_y <- res$mash_res$TP / res$mash_res$n_nonnulls m_x <- res$mash_res$FP / res$mash_res$n_nulls ohl_y <- res$flash_OHL_res$TP / res$flash_OHL_res$n_nonnulls ohl_x <- res$flash_OHL_res$FP / res$flash_OHL_res$n_nulls ohf_y <- res$flash_OHF_res$TP / res$flash_OHF_res$n_nonnulls ohf_x <- res$flash_OHF_res$FP / res$flash_OHF_res$n_nulls ohfp_y <- res$flash_OHFplus_res$TP / res$flash_OHFplus_res$n_nonnulls ohfp_x <- res$flash_OHFplus_res$FP / res$flash_OHFplus_res$n_nulls plot(m_x, m_y, xlim=c(0, 1), ylim=c(0, 1), type='l', xlab='FPR', ylab='TPR', main=main) lines(ohl_x, ohl_y, lty=2) lines(ohf_x, ohf_y, lty=3) lines(ohfp_x, ohfp_y, lty=4) legend("bottomright", c("MASH", "FLASH-OHL", "FLASH-OHF", "FLASH-OHF+"), lty=1:4) }</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 Sierra 10.12.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] highr_0.6 stringi_1.1.6 whisker_0.3-2 [13] R.oo_1.21.0 R.utils_2.6.0 rmarkdown_1.8 [16] tools_3.4.3 stringr_1.3.0 yaml_2.1.17 [19] compiler_3.4.3 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>