<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Get a sample of posterior draws from a model as a tibble — tidy_draws • tidybayes</title><!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png"><link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png"><link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png"><link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png"><link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png"><link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png"><!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script><!-- Bootstrap --><link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.4.0/paper/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous"><script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css"><script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous"><!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet"><script src="../pkgdown.js"></script><link href="../extra.css" rel="stylesheet"><meta property="og:title" content="Get a sample of posterior draws from a model as a tibble — tidy_draws"><meta property="og:description" content='Extract draws from a Bayesian fit into a wide-format data frame with a
.chain, .iteration, and .draw column, as well as all variables
as columns. This function does not parse indices from variable names
(e.g. for variable names like "x[1]"); see spread_draws() or
gather_draws() for functions that parse variable indices.'><meta property="og:image" content="https://mjskay.github.io/tidybayes/logo.svg"><!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--><!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id=UA-93322-5"></script><script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-93322-5');
</script></head><body data-spy="scroll" data-target="#toc">


    <div class="container template-reference-topic">
      <header><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" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">tidybayes</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="">3.0.7</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav"><li>
  <a href="../articles/tidybayes.html">Get started</a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
    Articles

    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu"><li>
      <a href="../articles/tidy-brms.html">Extracting and visualizing tidy draws from brms models</a>
    </li>
    <li>
      <a href="../articles/tidy-posterior.html">Using tidybayes with the posterior package</a>
    </li>
    <li>
      <a href="../articles/tidy-rstanarm.html">Extracting and visualizing tidy draws from rstanarm models</a>
    </li>
    <li>
      <a href="../articles/tidybayes-residuals.html">Extracting and visualizing tidy residuals from Bayesian models</a>
    </li>
  </ul></li>
<li>
  <a href="../news/index.html">Changelog</a>
</li>
      </ul><ul class="nav navbar-nav navbar-right"><li>
  <a href="https://github.com/mjskay/tidybayes/" class="external-link">
    <span class="fab fa-github fa-lg"></span>

  </a>
</li>
      </ul></div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->



      </header><div class="row">
  <div class="col-md-9 contents">
    <div class="page-header">
    <h1>Get a sample of posterior draws from a model as a tibble</h1>
    <small class="dont-index">Source: <a href="https://github.com/mjskay/tidybayes/blob/HEAD/R/tidy_draws.R" class="external-link"><code>R/tidy_draws.R</code></a></small>
    <div class="hidden name"><code>tidy_draws.Rd</code></div>
    </div>

    <div class="ref-description">
    <p>Extract draws from a Bayesian fit into a wide-format data frame with a
<code>.chain</code>, <code>.iteration</code>, and <code>.draw</code> column, as well as all variables
as columns. This function does not parse indices from variable names
(e.g. for variable names like <code>"x[1]"</code>); see <code><a href="spread_draws.html">spread_draws()</a></code> or
<code><a href="spread_draws.html">gather_draws()</a></code> for functions that parse variable indices.</p>
    </div>

    <div id="ref-usage">
    <div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Default S3 method</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'draws'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'data.frame'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'mcmc.list'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'stanfit'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'stanreg'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'runjags'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'jagsUI'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'brmsfit'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'CmdStanFit'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'CmdStanMCMC'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'matrix'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'MCMCglmm'</span></span>
<span><span class="fu">tidy_draws</span><span class="op">(</span><span class="va">model</span>, <span class="va">...</span><span class="op">)</span></span></code></pre></div>
    </div>

    <div id="arguments">
    <h2>Arguments</h2>


<dl><dt id="arg-model">model<a class="anchor" aria-label="anchor" href="#arg-model"></a></dt>
<dd><p>A supported Bayesian model fit. Tidybayes supports a variety of model objects;
for a full list of supported models, see <a href="tidybayes-models.html">tidybayes-models</a>.</p></dd>


<dt id="arg--">...<a class="anchor" aria-label="anchor" href="#arg--"></a></dt>
<dd><p>Further arguments passed to other methods (mostly unused).</p></dd>

</dl></div>
    <div id="value">
    <h2>Value</h2>
    <p>A data frame (actually, a <a href="https://tibble.tidyverse.org/reference/tibble.html" class="external-link">tibble</a>) with a <code>.chain</code> column,
<code>.iteration</code> column, <code>.draw</code> column, and one column for every variable in <code>model</code>.</p>
    </div>
    <div id="details">
    <h2>Details</h2>
    <p>This function can be useful for quick glances at models (especially
combined with <code><a href="gather_variables.html">gather_variables()</a></code> and <code><a href="http://mjskay.github.io/ggdist/reference/point_interval.html" class="external-link">median_qi()</a></code>), and for models with
parameters without indices in their names (like <code>"x[1]"</code>).
<code><a href="spread_draws.html">spread_draws()</a></code> and <code><a href="spread_draws.html">gather_draws()</a></code>, which <em>do</em> parse variable name indices,
call this function internally if their input is not already a tidy data frame.</p>
<p>To provide support for new models in tidybayes,
you must provide an implementation of this function <em>or</em> an implementation
of <code><a href="https://rdrr.io/pkg/coda/man/mcmc.list.html" class="external-link">coda::as.mcmc.list()</a></code> (<code>tidy_draws</code> should work on any model
with an implementation of <code><a href="https://rdrr.io/pkg/coda/man/mcmc.list.html" class="external-link">coda::as.mcmc.list()</a></code>)</p>
<p><code>tidy_draws()</code> can be applied to a data frame that is already a tidy-format data frame
of draws, provided it has one row per draw. In other words, it can be applied to data frames
that have the same format it returns, and it will return the same data frame back, while
checking to ensure the <code>.chain</code>, <code>.iteration</code>, and <code>.draw</code> columns are all
integers (converting if possible) and that the <code>.draw</code> column is unique. This allows
you to pass already-tidy-format data frames into other tidybayes functions, like
<code><a href="spread_draws.html">spread_draws()</a></code> or <code><a href="spread_draws.html">gather_draws()</a></code>. This functionality can be useful if the
tidying step is expensive: you can tidy once, possibly subsetting to some
particular variables of interest, then call <code><a href="spread_draws.html">spread_draws()</a></code> or <code><a href="spread_draws.html">gather_draws()</a></code>
repeatedly to extract variables and indices from the already-tidied data frame.</p>
    </div>
    <div id="see-also">
    <h2>See also</h2>
    <div class="dont-index"><p><code><a href="spread_draws.html">spread_draws()</a></code> or <code><a href="spread_draws.html">gather_draws()</a></code>, which use this function
internally and provides a friendly interface for extracting tidy data frames from model fits.</p></div>
    </div>
    <div id="author">
    <h2>Author</h2>
    <p>Matthew Kay</p>
    </div>

    <div id="ref-examples">
    <h2>Examples</h2>
    <div class="sourceCode"><pre class="sourceCode r"><code><span class="r-in"><span></span></span>
<span class="r-in"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://magrittr.tidyverse.org" class="external-link">magrittr</a></span><span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/utils/data.html" class="external-link">data</a></span><span class="op">(</span><span class="va">line</span>, package <span class="op">=</span> <span class="st">"coda"</span><span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="va">line</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span></span>
<span class="r-in"><span>  <span class="fu">tidy_draws</span><span class="op">(</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #949494;"># A tibble: 400 × 6</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span>    .chain .iteration .draw alpha   beta  sigma</span>
<span class="r-out co"><span class="r-pr">#&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;int&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 1</span>      1          1     1  7.17 -<span style="color: #BB0000;">1.57</span>  11.2  </span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 2</span>      1          2     2  2.95  1.50   4.89 </span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 3</span>      1          3     3  3.67  0.628  1.40 </span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 4</span>      1          4     4  3.32  1.18   0.663</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 5</span>      1          5     5  3.71  0.490  1.36 </span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 6</span>      1          6     6  3.58  0.207  1.04 </span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 7</span>      1          7     7  2.70  0.883  1.29 </span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 8</span>      1          8     8  2.96  1.09   0.459</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 9</span>      1          9     9  3.53  1.07   0.634</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;">10</span>      1         10    10  2.09  1.48   0.913</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #949494;"># ℹ 390 more rows</span></span>
<span class="r-in"><span></span></span>
</code></pre></div>
    </div>
  </div>
  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
    <nav id="toc" data-toggle="toc" class="sticky-top"><h2 data-toc-skip>Contents</h2>
    </nav></div>
</div>


      <footer><div class="copyright">
  <p></p><p>Developed by <a href="https://www.mjskay.com" class="external-link">Matthew Kay</a>.</p>
</div>

<div class="pkgdown">
  <p></p><p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.1.0.</p>
</div>

      </footer></div>






  </body></html>

