<!-- Generated by pkgdown: do not edit by hand -->
<!DOCTYPE html>
<html lang="en">
  <head>
  <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>Model Predictions for HMC or MH — predict.hmclearn • hmclearn</title>


<!-- jquery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<!-- Bootstrap -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.4.0/sandstone/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>




<meta property="og:title" content="Model Predictions for HMC or MH — predict.hmclearn" />
<meta property="og:description" content="predict generates simulated data from the posterior predictive distribution.
This simulated data can be used for posterior predictive check diagnostics from the bayesplot package" />




<!-- 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]-->



  </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">hmclearn</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">0.0.2</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li>
  <a href="../index.html">
    <span class="fas fa fas fa-home fa-lg"></span>
     
  </a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
    Articles
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
    <li>
      <a href="../articles/linear_mixed_effects_hmclearn.html">hmclearn package:  Linear Mixed Effects Regression Example</a>
    </li>
    <li>
      <a href="../articles/linear_regression_hmclearn.html">hmclearn package:  Linear Regression Example</a>
    </li>
    <li>
      <a href="../articles/logistic_mixed_effects_hmclearn.html">hmclearn package:  Logistic Mixed Effects Regression Example</a>
    </li>
    <li>
      <a href="../articles/logistic_regression_hmclearn.html">hmclearn:  Logistic Regression Example</a>
    </li>
    <li>
      <a href="../articles/poisson_regression_hmclearn.html">hmclearn:  Poisson Regression Example</a>
    </li>
  </ul>
</li>
<li>
  <a href="../news/index.html">Changelog</a>
</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        
      </ul>
      
    </div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

      

      </header>

<div class="row">
  <div class="col-md-9 contents">
    <div class="page-header">
    <h1>Model Predictions for HMC or MH</h1>
    
    <div class="hidden name"><code>predict.hmclearn.Rd</code></div>
    </div>

    <div class="ref-description">
    <p><code>predict</code> generates simulated data from the posterior predictive distribution.
This simulated data can be used for posterior predictive check diagnostics from the <code>bayesplot</code> package</p>
    </div>

    <pre class="usage"><span class='co'># S3 method for hmclearn</span>
<span class='fu'><a href='https://rdrr.io/r/stats/predict.html'>predict</a></span>(<span class='no'>object</span>, <span class='no'>X</span>, <span class='kw'>fam</span> <span class='kw'>=</span> <span class='st'>"linear"</span>, <span class='kw'>burnin</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>draws</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='no'>...</span>)</pre>

    <h2 class="hasAnchor" id="arguments"><a class="anchor" href="#arguments"></a>Arguments</h2>
    <table class="ref-arguments">
    <colgroup><col class="name" /><col class="desc" /></colgroup>
    <tr>
      <th>object</th>
      <td><p>an object of class <code>hmclearn</code>, usually a result of a call to <code>mh</code> or <code>hmc</code></p></td>
    </tr>
    <tr>
      <th>X</th>
      <td><p>design matrix, either from fitting the model or new data</p></td>
    </tr>
    <tr>
      <th>fam</th>
      <td><p>generalized linear model family.  Currently "linear", "binomial", and "poisson" are supported</p></td>
    </tr>
    <tr>
      <th>burnin</th>
      <td><p>optional numeric parameter for the number of initial MCMC samples to omit from the summary</p></td>
    </tr>
    <tr>
      <th>draws</th>
      <td><p>Number of simulated values from the posterior conditioned on <code>X</code></p></td>
    </tr>
    <tr>
      <th>...</th>
      <td><p>additional parameters, currently unsupported</p></td>
    </tr>
    </table>

    <h2 class="hasAnchor" id="value"><a class="anchor" href="#value"></a>Value</h2>

    <p>An object of class <code>hmclearnpred</code>.</p>
    <h2 class="hasAnchor" id="elements-of-hmclearnpred-objects"><a class="anchor" href="#elements-of-hmclearnpred-objects"></a>Elements of <code>hmclearnpred</code> objects</h2>

    

<dl'>
  <dt><code>y</code></dt><dd><p>Median simulated values for each observation in <code>X</code></p></dd>
  <dt><code>yrep</code></dt><dd><p>Matrix of simulated values where each row is a draw from the posterior predictive distribution</p></dd>
  <dt><code>X</code></dt><dd><p>Numeric design matrix</p></dd>

</dl>

    <h2 class="hasAnchor" id="references"><a class="anchor" href="#references"></a>References</h2>

    <p>Gabry, Jonah and Mahr, Tristan (2019).  <em>bayesplot:  Plotting for Bayesian Models</em>.  <a href='https://mc-stan.org/bayesplot'>https://mc-stan.org/bayesplot</a></p>
<p>Gabry, J., Simpson, D., Vehtari, A., Betancourt, M., and Gelman, A (2019).  <em>Visualization in Bayesian Workflow</em>.  Journal of the Royal Statistical Society: Series A. Vol 182.  Issue 2.  p.389-402.</p>

    <h2 class="hasAnchor" id="examples"><a class="anchor" href="#examples"></a>Examples</h2>
    <pre class="examples"><div class='input'><span class='co'># Linear regression example</span>
<span class='fu'><a href='https://rdrr.io/r/base/Random.html'>set.seed</a></span>(<span class='fl'>521</span>)
<span class='no'>X</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/base/cbind.html'>cbind</a></span>(<span class='fl'>1</span>, <span class='fu'><a href='https://rdrr.io/r/base/matrix.html'>matrix</a></span>(<span class='fu'><a href='https://rdrr.io/r/stats/Normal.html'>rnorm</a></span>(<span class='fl'>300</span>), <span class='kw'>ncol</span><span class='kw'>=</span><span class='fl'>3</span>))
<span class='no'>betavals</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='fl'>0.5</span>, -<span class='fl'>1</span>, <span class='fl'>2</span>, -<span class='fl'>3</span>)
<span class='no'>y</span> <span class='kw'>&lt;-</span> <span class='no'>X</span><span class='kw'>%*%</span><span class='no'>betavals</span> + <span class='fu'><a href='https://rdrr.io/r/stats/Normal.html'>rnorm</a></span>(<span class='fl'>100</span>, <span class='kw'>sd</span><span class='kw'>=</span><span class='fl'>.2</span>)

<span class='no'>f1</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='hmc.html'>hmc</a></span>(<span class='kw'>N</span> <span class='kw'>=</span> <span class='fl'>500</span>,
          <span class='kw'>theta.init</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='fu'><a href='https://rdrr.io/r/base/rep.html'>rep</a></span>(<span class='fl'>0</span>, <span class='fl'>4</span>), <span class='fl'>1</span>),
          <span class='kw'>epsilon</span> <span class='kw'>=</span> <span class='fl'>0.01</span>,
          <span class='kw'>L</span> <span class='kw'>=</span> <span class='fl'>10</span>,
          <span class='kw'>logPOSTERIOR</span> <span class='kw'>=</span> <span class='no'>linear_posterior</span>,
          <span class='kw'>glogPOSTERIOR</span> <span class='kw'>=</span> <span class='no'>g_linear_posterior</span>,
          <span class='kw'>varnames</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='fu'><a href='https://rdrr.io/r/base/paste.html'>paste0</a></span>(<span class='st'>"beta"</span>, <span class='fl'>0</span>:<span class='fl'>3</span>), <span class='st'>"log_sigma_sq"</span>),
          <span class='kw'>param</span><span class='kw'>=</span><span class='fu'><a href='https://rdrr.io/r/base/list.html'>list</a></span>(<span class='kw'>y</span><span class='kw'>=</span><span class='no'>y</span>, <span class='kw'>X</span><span class='kw'>=</span><span class='no'>X</span>), <span class='kw'>parallel</span><span class='kw'>=</span><span class='fl'>FALSE</span>, <span class='kw'>chains</span><span class='kw'>=</span><span class='fl'>1</span>)

<span class='fu'><a href='https://rdrr.io/r/base/summary.html'>summary</a></span>(<span class='no'>f1</span>)</div><div class='output co'>#&gt; Summary of MCMC simulation
#&gt; </div><div class='output co'>#&gt;                    2.5%         5%        25%        50%        75%        95%
#&gt; beta0         0.3761942  0.4648189  0.5155872  0.5325537  0.5497568  0.5781873
#&gt; beta1        -1.0739748 -1.0488209 -1.0281105 -1.0118904 -0.9965476 -0.9627243
#&gt; beta2         0.9617633  1.8882191  1.9997614  2.0164956  2.0314843  2.0521531
#&gt; beta3        -3.0255286 -3.0154147 -2.9970442 -2.9807522 -2.9646554 -2.8305029
#&gt; log_sigma_sq -3.2893269 -3.2522864 -3.1503169 -3.0392322 -2.9052983 -0.4878351
#&gt;                   97.5%
#&gt; beta0         0.5854330
#&gt; beta1        -0.9602134
#&gt; beta2         2.0591168
#&gt; beta3        -1.8103326
#&gt; log_sigma_sq  1.5343091</div><div class='input'>
<span class='no'>p</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/stats/predict.html'>predict</a></span>(<span class='no'>f1</span>, <span class='no'>X</span>)
<span class='no'>predvals</span> <span class='kw'>&lt;-</span> <span class='no'>p</span>$<span class='no'>y</span>
<span class='fu'><a href='https://rdrr.io/r/base/plot.html'>plot</a></span>(<span class='no'>predvals</span>, <span class='no'>y</span>, <span class='kw'>xlab</span><span class='kw'>=</span><span class='st'>"predicted"</span>, <span class='kw'>ylab</span><span class='kw'>=</span><span class='st'>"actual"</span>)</div><div class='img'><img src='predict.hmclearn-1.png' alt='' width='700' height='433' /></div><div class='input'>
<span class='no'>X2</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/base/cbind.html'>cbind</a></span>(<span class='fl'>1</span>, <span class='fu'><a href='https://rdrr.io/r/base/matrix.html'>matrix</a></span>(<span class='fu'><a href='https://rdrr.io/r/stats/Normal.html'>rnorm</a></span>(<span class='fl'>30</span>), <span class='kw'>ncol</span><span class='kw'>=</span><span class='fl'>3</span>))
<span class='no'>p2</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/stats/predict.html'>predict</a></span>(<span class='no'>f1</span>, <span class='no'>X2</span>)
<span class='no'>p2</span>$<span class='no'>y</span></div><div class='output co'>#&gt;  [1] -0.6990180  7.0555558  1.0823677 -3.9274742  2.1074708  1.1030894
#&gt;  [7]  0.3838926  3.8462382 -2.4566947  2.3849477</div><div class='input'>
</div></pre>
  </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>Developed by Samuel Thomas.</p>
</div>

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

      </footer>
   </div>

  


  </body>
</html>


