<!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>Add rvars for the linear predictor, posterior expectation, posterior predictive, or residuals of a model to a data frame — add_epred_rvars • 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="Add rvars for the linear predictor, posterior expectation, posterior predictive, or residuals of a model to a data frame — add_epred_rvars"><meta property="og:description" content="Given a data frame and a model, adds rvars of draws from the linear/link-level predictor,
the expectation of the posterior predictive, or the posterior predictive to
the data frame."><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>Add <code>rvar</code>s for the linear predictor, posterior expectation, posterior predictive, or residuals of a model to a data frame</h1>
    <small class="dont-index">Source: <a href="https://github.com/mjskay/tidybayes/blob/HEAD/R/epred_rvars.R" class="external-link"><code>R/epred_rvars.R</code></a>, <a href="https://github.com/mjskay/tidybayes/blob/HEAD/R/linpred_rvars.R" class="external-link"><code>R/linpred_rvars.R</code></a>, <a href="https://github.com/mjskay/tidybayes/blob/HEAD/R/predicted_rvars.R" class="external-link"><code>R/predicted_rvars.R</code></a></small>
    <div class="hidden name"><code>add_predicted_rvars.Rd</code></div>
    </div>

    <div class="ref-description">
    <p>Given a data frame and a model, adds <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">rvar</a></code>s of draws from the linear/link-level predictor,
the expectation of the posterior predictive, or the posterior predictive to
the data frame.</p>
    </div>

    <div id="ref-usage">
    <div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">add_epred_rvars</span><span class="op">(</span></span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".epred"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  dpar <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="fu">epred_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".epred"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  dpar <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Default S3 method</span></span>
<span><span class="fu">epred_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".epred"</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  dpar <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'stanreg'</span></span>
<span><span class="fu">epred_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".epred"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  dpar <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'brmsfit'</span></span>
<span><span class="fu">epred_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".epred"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  dpar <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="fu">add_linpred_rvars</span><span class="op">(</span></span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".linpred"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  dpar <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="fu">linpred_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".linpred"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  dpar <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Default S3 method</span></span>
<span><span class="fu">linpred_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".linpred"</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  dpar <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'stanreg'</span></span>
<span><span class="fu">linpred_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".linpred"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  dpar <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'brmsfit'</span></span>
<span><span class="fu">linpred_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".linpred"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  dpar <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="fu">add_predicted_rvars</span><span class="op">(</span></span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".prediction"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="fu">predicted_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".prediction"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Default S3 method</span></span>
<span><span class="fu">predicted_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".prediction"</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'stanreg'</span></span>
<span><span class="fu">predicted_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".prediction"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for class 'brmsfit'</span></span>
<span><span class="fu">predicted_rvars</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">newdata</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  value <span class="op">=</span> <span class="st">".prediction"</span>,</span>
<span>  ndraws <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  seed <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  re_formula <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  columns_to <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span></code></pre></div>
    </div>

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


<dl><dt id="arg-newdata">newdata<a class="anchor" aria-label="anchor" href="#arg-newdata"></a></dt>
<dd><p>Data frame to generate predictions from.</p></dd>


<dt id="arg-object">object<a class="anchor" aria-label="anchor" href="#arg-object"></a></dt>
<dd><p>A supported Bayesian model fit that can provide fits and predictions. Supported models
are listed in the second section of <a href="tidybayes-models.html">tidybayes-models</a>: <em>Models Supporting Prediction</em>. While other
functions in this package (like <code><a href="spread_rvars.html">spread_rvars()</a></code>) support a wider range of models, to work with
<code>add_epred_rvars()</code>, <code>add_predicted_rvars()</code>, etc. a model must provide an interface for generating
predictions, thus more generic Bayesian modeling interfaces like <code>runjags</code> and <code>rstan</code> are not directly
supported for these functions (only wrappers around those languages that provide predictions, like <code>rstanarm</code>
and <code>brm</code>, are supported here).</p></dd>


<dt id="arg--">...<a class="anchor" aria-label="anchor" href="#arg--"></a></dt>
<dd><p>Additional arguments passed to the underlying prediction method for the type of
model given.</p></dd>


<dt id="arg-value">value<a class="anchor" aria-label="anchor" href="#arg-value"></a></dt>
<dd><p>The name of the output column:</p><ul><li><p>for <code>[add_]epred_rvars()</code>, defaults to <code>".epred"</code>.</p></li>
<li><p>for <code>[add_]predicted_rvars()</code>, defaults to <code>".prediction"</code>.</p></li>
<li><p>for <code>[add_]linpred_rvars()</code>, defaults to <code>".linpred"</code>.</p></li>
</ul></dd>


<dt id="arg-ndraws">ndraws<a class="anchor" aria-label="anchor" href="#arg-ndraws"></a></dt>
<dd><p>The number of draws to return, or <code>NULL</code> to return all draws.</p></dd>


<dt id="arg-seed">seed<a class="anchor" aria-label="anchor" href="#arg-seed"></a></dt>
<dd><p>A seed to use when subsampling draws (i.e. when <code>ndraws</code> is not <code>NULL</code>).</p></dd>


<dt id="arg-re-formula">re_formula<a class="anchor" aria-label="anchor" href="#arg-re-formula"></a></dt>
<dd><p>formula containing group-level effects to be considered in the prediction.
If <code>NULL</code> (default), include all group-level effects; if <code>NA</code>, include no group-level effects.
Some model types (such as <a href="https://paul-buerkner.github.io/brms/reference/brmsfit-class.html" class="external-link">brms::brmsfit</a> and <a href="https://mc-stan.org/rstanarm/reference/stanreg-objects.html" class="external-link">rstanarm::stanreg-objects</a>) allow
marginalizing over grouping factors by specifying new levels of a factor in <code>newdata</code>. In the case of
<code><a href="https://paul-buerkner.github.io/brms/reference/brm.html" class="external-link">brms::brm()</a></code>, you must also pass <code>allow_new_levels = TRUE</code> here to include new levels (see
<code><a href="https://paul-buerkner.github.io/brms/reference/posterior_predict.brmsfit.html" class="external-link">brms::posterior_predict()</a></code>).</p></dd>


<dt id="arg-dpar">dpar<a class="anchor" aria-label="anchor" href="#arg-dpar"></a></dt>
<dd><p>For <code>add_epred_rvars()</code> and <code>add_linpred_rvars()</code>: Should distributional regression
parameters be included in the output? Valid only for models that support distributional regression parameters,
such as submodels for variance parameters (as in <code><a href="https://paul-buerkner.github.io/brms/reference/brm.html" class="external-link">brms::brm()</a></code>). If <code>TRUE</code>, distributional regression
parameters are included in the output as additional columns named after each parameter
(alternative names can be provided using a list or named vector, e.g. <code>c(sigma.hat = "sigma")</code>
would output the <code>"sigma"</code> parameter from a model as a column named <code>"sigma.hat"</code>).
If <code>NULL</code> or <code>FALSE</code> (the default), distributional regression parameters are not included.</p></dd>


<dt id="arg-columns-to">columns_to<a class="anchor" aria-label="anchor" href="#arg-columns-to"></a></dt>
<dd><p>For <em>some</em> models, such as ordinal, multinomial, and multivariate models (notably, <code><a href="https://paul-buerkner.github.io/brms/reference/brm.html" class="external-link">brms::brm()</a></code> models but
<em>not</em> <code><a href="https://mc-stan.org/rstanarm/reference/stan_polr.html" class="external-link">rstanarm::stan_polr()</a></code> models), the column of predictions in the resulting data frame may include nested columns.
For example, for ordinal/multinomial models, these columns correspond to different categories of the response variable.
It may be more convenient to turn these nested columns into rows in the output; if this is desired, set
<code>columns_to</code> to a string representing the name of a column you would like the column names to be placed in.
In this case, a <code>.row</code> column will also be added to the result indicating which rows of the output
correspond to the same row in <code>newdata</code>.
See <code><a href="../articles/tidy-posterior.html">vignette("tidy-posterior")</a></code> for examples of dealing with output ordinal models.</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>) equal to the input <code>newdata</code> with
additional columns added containing <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">rvar</a></code>s representing the requested predictions or fits.</p>
    </div>
    <div id="details">
    <h2>Details</h2>
    <p>Consider a model like:</p>
<p>$$\begin{array}{rcl}
y &amp;\sim&amp; \textrm{SomeDist}(\theta_1, \theta_2)\\
f_1(\theta_1) &amp;=&amp; \alpha_1 + \beta_1 x\\
f_2(\theta_2) &amp;=&amp; \alpha_2 + \beta_2 x
\end{array}$$</p>
<p>This model has:</p><ul><li><p>an outcome variable, \(y\)</p></li>
<li><p>a response distribution, \(\textrm{SomeDist}\), having parameters \(\theta_1\)
(with link function \(f_1\)) and \(\theta_2\) (with link function \(f_2\))</p></li>
<li><p>a single predictor, \(x\)</p></li>
<li><p>coefficients \(\alpha_1\), \(\beta_1\), \(\alpha_2\), and \(\beta_2\)</p></li>
</ul><p>We fit this model to some observed data, \(y_\textrm{obs}\), and predictors,
\(x_\textrm{obs}\). Given new values of predictors, \(x_\textrm{new}\),
supplied in the data frame <code>newdata</code>, the functions for posterior draws are
defined as follows:</p><ul><li><p><code>add_predicted_rvars()</code> adds <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">rvar</a></code>s containing draws from the <strong>posterior predictive distribution</strong>,
\(p(y_\textrm{new} | x_\textrm{new}, y_\textrm{obs})\),
to the data.
It corresponds to <code><a href="https://mc-stan.org/rstanarm/reference/posterior_predict.stanreg.html" class="external-link">rstanarm::posterior_predict()</a></code> or <code><a href="https://paul-buerkner.github.io/brms/reference/posterior_predict.brmsfit.html" class="external-link">brms::posterior_predict()</a></code>.</p></li>
<li><p><code>add_epred_rvars()</code> adds <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">rvar</a></code>s containing draws from the <strong>expectation of the posterior predictive
distribution</strong>, aka the conditional expectation,
\(E(y_\textrm{new} | x_\textrm{new}, y_\textrm{obs})\),
to the data.
It corresponds to <code><a href="https://mc-stan.org/rstanarm/reference/posterior_linpred.stanreg.html" class="external-link">rstanarm::posterior_epred()</a></code> or <code><a href="https://paul-buerkner.github.io/brms/reference/posterior_epred.brmsfit.html" class="external-link">brms::posterior_epred()</a></code>.
Not all models support this function.</p></li>
<li><p><code>add_linpred_rvars()</code> adds <code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">rvar</a></code>s containing draws from the <strong>posterior linear predictors</strong> to the data.
It corresponds to <code><a href="https://mc-stan.org/rstanarm/reference/posterior_linpred.stanreg.html" class="external-link">rstanarm::posterior_linpred()</a></code> or <code><a href="https://paul-buerkner.github.io/brms/reference/posterior_linpred.brmsfit.html" class="external-link">brms::posterior_linpred()</a></code>.
Depending on the model type and additional parameters passed, this may be:</p><ul><li><p>The untransformed linear predictor, e.g.
\(p(f_1(\theta_1) | x_\textrm{new}, y_\textrm{obs})\) =
\(p(\alpha_1 + \beta_1 x_\textrm{new} | x_\textrm{new}, y_\textrm{obs})\).
This is returned by <code>add_linpred_rvars(transform = FALSE)</code> for <span class="pkg">brms</span> and <span class="pkg">rstanarm</span> models.
It is analogous to <code>type = "link"</code> in <code><a href="https://rdrr.io/r/stats/predict.glm.html" class="external-link">predict.glm()</a></code>.</p></li>
<li><p>The inverse-link transformed linear predictor, e.g.
\(p(\theta_1 | x_\textrm{new}, y_\textrm{obs})\) =
\(p(f_1^{-1}(\alpha_1 + \beta_1 x_\textrm{new}) | x_\textrm{new}, y_\textrm{obs})\).
This is returned by <code>add_linpred_rvars(transform = TRUE)</code> for <span class="pkg">brms</span> and <span class="pkg">rstanarm</span> models.
It is analogous to <code>type = "response"</code> in <code><a href="https://rdrr.io/r/stats/predict.glm.html" class="external-link">predict.glm()</a></code>.</p></li>
</ul><p><strong>NOTE:</strong> <code>add_linpred_rvars(transform = TRUE)</code> and <code>add_epred_rvars()</code> may be equivalent but
are not guaranteed to be. They are equivalent when the expectation of the response
distribution is equal to its first parameter, i.e. when \(E(y) = \theta_1\). Many
distributions have this property (e.g. Normal distributions, Bernoulli distributions),
but not all. If you want the expectation of the posterior predictive, it is best to
use <code>add_epred_rvars()</code> if available, and if not available, verify this property holds prior
to using <code>add_linpred_rvars()</code>.</p></li>
</ul><p>The corresponding functions without <code>add_</code> as a prefix are alternate spellings
with the opposite order of the first two arguments: e.g. <code>add_predicted_rvars(newdata, object)</code>
versus <code>predicted_rvars(object, newdata)</code>. This facilitates use in data
processing pipelines that start either with a data frame or a model.</p>
<p>Given equal choice between the two, the spellings prefixed with <code>add_</code>
are preferred.</p>
    </div>
    <div id="see-also">
    <h2>See also</h2>
    <div class="dont-index"><p><code><a href="add_predicted_draws.html">add_predicted_draws()</a></code> for the analogous functions that use a long-data-frame-of-draws
format instead of a data-frame-of-<code><a href="https://mc-stan.org/posterior/reference/rvar.html" class="external-link">rvar</a></code>s format. See <code><a href="spread_rvars.html">spread_rvars()</a></code> for manipulating posteriors directly.</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 class="co"># \dontrun{</span></span></span>
<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://ggplot2.tidyverse.org" class="external-link">ggplot2</a></span><span class="op">)</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://dplyr.tidyverse.org" class="external-link">dplyr</a></span><span class="op">)</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://mc-stan.org/posterior/" class="external-link">posterior</a></span><span class="op">)</span></span></span>
<span class="r-msg co"><span class="r-pr">#&gt;</span> This is posterior version 1.6.0</span>
<span class="r-msg co"><span class="r-pr">#&gt;</span> </span>
<span class="r-msg co"><span class="r-pr">#&gt;</span> Attaching package: 'posterior'</span>
<span class="r-msg co"><span class="r-pr">#&gt;</span> The following objects are masked from 'package:stats':</span>
<span class="r-msg co"><span class="r-pr">#&gt;</span> </span>
<span class="r-msg co"><span class="r-pr">#&gt;</span>     mad, sd, var</span>
<span class="r-msg co"><span class="r-pr">#&gt;</span> The following objects are masked from 'package:base':</span>
<span class="r-msg co"><span class="r-pr">#&gt;</span> </span>
<span class="r-msg co"><span class="r-pr">#&gt;</span>     %in%, match</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://github.com/paul-buerkner/brms" class="external-link">brms</a></span><span class="op">)</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://modelr.tidyverse.org" class="external-link">modelr</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://ggplot2.tidyverse.org/reference/theme_get.html" class="external-link">theme_set</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html" class="external-link">theme_light</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="va">m_mpg</span> <span class="op">=</span> <span class="fu"><a href="https://paul-buerkner.github.io/brms/reference/brm.html" class="external-link">brm</a></span><span class="op">(</span><span class="va">mpg</span> <span class="op">~</span> <span class="va">hp</span> <span class="op">*</span> <span class="va">cyl</span>, data <span class="op">=</span> <span class="va">mtcars</span>, family <span class="op">=</span> <span class="fu"><a href="https://paul-buerkner.github.io/brms/reference/brmsfamily.html" class="external-link">lognormal</a></span><span class="op">(</span><span class="op">)</span>,</span></span>
<span class="r-in"><span>  <span class="co"># 1 chain / few iterations just so example runs quickly</span></span></span>
<span class="r-in"><span>  <span class="co"># do not use in practice</span></span></span>
<span class="r-in"><span>  chains <span class="op">=</span> <span class="fl">1</span>, iter <span class="op">=</span> <span class="fl">500</span><span class="op">)</span></span></span>
<span class="r-msg co"><span class="r-pr">#&gt;</span> Compiling Stan program...</span>
<span class="r-msg co"><span class="r-pr">#&gt;</span> Start sampling</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> </span>
<span class="r-out co"><span class="r-pr">#&gt;</span> SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: </span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Gradient evaluation took 3.8e-05 seconds</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.38 seconds.</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Adjust your expectations accordingly!</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: </span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: </span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration:   1 / 500 [  0%]  (Warmup)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration:  50 / 500 [ 10%]  (Warmup)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration: 100 / 500 [ 20%]  (Warmup)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration: 150 / 500 [ 30%]  (Warmup)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration: 200 / 500 [ 40%]  (Warmup)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration: 250 / 500 [ 50%]  (Warmup)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration: 251 / 500 [ 50%]  (Sampling)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration: 300 / 500 [ 60%]  (Sampling)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration: 350 / 500 [ 70%]  (Sampling)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration: 400 / 500 [ 80%]  (Sampling)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration: 450 / 500 [ 90%]  (Sampling)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: Iteration: 500 / 500 [100%]  (Sampling)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: </span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1:  Elapsed Time: 0.576 seconds (Warm-up)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1:                0.577 seconds (Sampling)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1:                1.153 seconds (Total)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> Chain 1: </span>
<span class="r-wrn co"><span class="r-pr">#&gt;</span> <span class="warning">Warning: </span>There were 1 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. See</span>
<span class="r-wrn co"><span class="r-pr">#&gt;</span> https://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded</span>
<span class="r-wrn co"><span class="r-pr">#&gt;</span> <span class="warning">Warning: </span>Examine the pairs() plot to diagnose sampling problems</span>
<span class="r-wrn co"><span class="r-pr">#&gt;</span> <span class="warning">Warning: </span>Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.</span>
<span class="r-wrn co"><span class="r-pr">#&gt;</span> Running the chains for more iterations may help. See</span>
<span class="r-wrn co"><span class="r-pr">#&gt;</span> https://mc-stan.org/misc/warnings.html#bulk-ess</span>
<span class="r-wrn co"><span class="r-pr">#&gt;</span> <span class="warning">Warning: </span>Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.</span>
<span class="r-wrn co"><span class="r-pr">#&gt;</span> Running the chains for more iterations may help. See</span>
<span class="r-wrn co"><span class="r-pr">#&gt;</span> https://mc-stan.org/misc/warnings.html#tail-ess</span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># Look at mean predictions for some cars (epred) and compare to</span></span></span>
<span class="r-in"><span><span class="co"># the exponeniated mu parameter of the lognormal distribution (linpred).</span></span></span>
<span class="r-in"><span><span class="co"># Notice how they are NOT the same. This is because exp(mu) for a</span></span></span>
<span class="r-in"><span><span class="co"># lognormal distribution is equal to its median, not its mean.</span></span></span>
<span class="r-in"><span><span class="va">mtcars</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"><a href="https://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="va">hp</span>, <span class="va">cyl</span>, <span class="va">mpg</span><span class="op">)</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">add_epred_rvars</span><span class="op">(</span><span class="va">m_mpg</span><span class="op">)</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">add_linpred_rvars</span><span class="op">(</span><span class="va">m_mpg</span>, value <span class="op">=</span> <span class="st">"mu"</span><span class="op">)</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"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>expmu <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/Log.html" class="external-link">exp</a></span><span class="op">(</span><span class="va">mu</span><span class="op">)</span>, <span class="va">.epred</span> <span class="op">-</span> <span class="va">expmu</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #949494;"># A tibble: 32 × 7</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span>       hp   cyl   mpg     .epred           mu      expmu `.epred - expmu`</span>
<span class="r-out co"><span class="r-pr">#&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 style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span> <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;rvar[1d]&gt;</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 1</span>   110     6  21    20 <span style="color: #949494;">± 0.92</span>  3.0 <span style="color: #949494;">± 0.045</span>  20 <span style="color: #949494;">± 0.90</span>     0.26 <span style="color: #949494;">± 0.069</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 2</span>   110     6  21    20 <span style="color: #949494;">± 0.92</span>  3.0 <span style="color: #949494;">± 0.045</span>  20 <span style="color: #949494;">± 0.90</span>     0.26 <span style="color: #949494;">± 0.069</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 3</span>    93     4  22.8  26 <span style="color: #949494;">± 1.28</span>  3.2 <span style="color: #949494;">± 0.049</span>  26 <span style="color: #949494;">± 1.25</span>     0.34 <span style="color: #949494;">± 0.088</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 4</span>   110     6  21.4  20 <span style="color: #949494;">± 0.92</span>  3.0 <span style="color: #949494;">± 0.045</span>  20 <span style="color: #949494;">± 0.90</span>     0.26 <span style="color: #949494;">± 0.069</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 5</span>   175     8  18.7  16 <span style="color: #949494;">± 0.73</span>  2.7 <span style="color: #949494;">± 0.046</span>  15 <span style="color: #949494;">± 0.71</span>     0.20 <span style="color: #949494;">± 0.053</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 6</span>   105     6  18.1  20 <span style="color: #949494;">± 0.93</span>  3.0 <span style="color: #949494;">± 0.045</span>  20 <span style="color: #949494;">± 0.91</span>     0.26 <span style="color: #949494;">± 0.070</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 7</span>   245     8  14.3  15 <span style="color: #949494;">± 0.81</span>  2.7 <span style="color: #949494;">± 0.055</span>  15 <span style="color: #949494;">± 0.80</span>     0.19 <span style="color: #949494;">± 0.050</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 8</span>    62     4  24.4  29 <span style="color: #949494;">± 1.86</span>  3.3 <span style="color: #949494;">± 0.065</span>  28 <span style="color: #949494;">± 1.84</span>     0.37 <span style="color: #949494;">± 0.094</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;"> 9</span>    95     4  22.8  26 <span style="color: #949494;">± 1.31</span>  3.2 <span style="color: #949494;">± 0.050</span>  26 <span style="color: #949494;">± 1.28</span>     0.33 <span style="color: #949494;">± 0.088</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;">10</span>   123     6  19.2  20 <span style="color: #949494;">± 0.96</span>  3.0 <span style="color: #949494;">± 0.048</span>  19 <span style="color: #949494;">± 0.93</span>     0.25 <span style="color: #949494;">± 0.068</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #949494;"># ℹ 22 more rows</span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># plot intervals around conditional means (epred_rvars)</span></span></span>
<span class="r-in"><span><span class="va">mtcars</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"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</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"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span>hp <span class="op">=</span> <span class="fu"><a href="https://modelr.tidyverse.org/reference/seq_range.html" class="external-link">seq_range</a></span><span class="op">(</span><span class="va">hp</span>, n <span class="op">=</span> <span class="fl">101</span><span class="op">)</span><span class="op">)</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">add_epred_rvars</span><span class="op">(</span><span class="va">m_mpg</span><span class="op">)</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"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">hp</span>, color <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span>, fill <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span></span>
<span class="r-in"><span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_lineribbon.html" class="external-link">stat_lineribbon</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>dist <span class="op">=</span> <span class="va">.epred</span><span class="op">)</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.95</span>, <span class="fl">.8</span>, <span class="fl">.5</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">3</span><span class="op">)</span> <span class="op">+</span></span></span>
<span class="r-in"><span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">mpg</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span> <span class="op">+</span></span></span>
<span class="r-in"><span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span> <span class="op">+</span></span></span>
<span class="r-in"><span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Set2"</span><span class="op">)</span></span></span>
<span class="r-plt img"><img src="add_predicted_rvars-1.png" alt="" width="700" height="433"></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># plot posterior predictive intervals (predicted_rvars)</span></span></span>
<span class="r-in"><span><span class="va">mtcars</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"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</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"><a href="https://modelr.tidyverse.org/reference/data_grid.html" class="external-link">data_grid</a></span><span class="op">(</span>hp <span class="op">=</span> <span class="fu"><a href="https://modelr.tidyverse.org/reference/seq_range.html" class="external-link">seq_range</a></span><span class="op">(</span><span class="va">hp</span>, n <span class="op">=</span> <span class="fl">101</span><span class="op">)</span><span class="op">)</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">add_predicted_rvars</span><span class="op">(</span><span class="va">m_mpg</span><span class="op">)</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"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">hp</span>, color <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span>, fill <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html" class="external-link">ordered</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span></span>
<span class="r-in"><span>  <span class="fu"><a href="http://mjskay.github.io/ggdist/reference/stat_lineribbon.html" class="external-link">stat_lineribbon</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>dist <span class="op">=</span> <span class="va">.prediction</span><span class="op">)</span>, .width <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">.95</span>, <span class="fl">.8</span>, <span class="fl">.5</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">3</span><span class="op">)</span> <span class="op">+</span></span></span>
<span class="r-in"><span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html" class="external-link">geom_point</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>y <span class="op">=</span> <span class="va">mpg</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span> <span class="op">+</span></span></span>
<span class="r-in"><span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Dark2"</span><span class="op">)</span> <span class="op">+</span></span></span>
<span class="r-in"><span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html" class="external-link">scale_fill_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Set2"</span><span class="op">)</span></span></span>
<span class="r-plt img"><img src="add_predicted_rvars-2.png" alt="" width="700" height="433"></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># }</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>

