<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/fancy-plot-with-posterior-samples-for-bayesian-regressions/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://neuropsychology.github.io/psycho.R//2018/06/03/plot_bayesian_model.html"> Dominique Makowski</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<ul>
<li><a rel="nofollow" href="https://neuropsychology.github.io/psycho.R//2018/06/03/plot_bayesian_model.html#the-model" target="_blank">The Model</a></li>
<li><a rel="nofollow" href="https://neuropsychology.github.io/psycho.R//2018/06/03/plot_bayesian_model.html#plot" target="_blank">Plot</a></li>
<li><a rel="nofollow" href="https://neuropsychology.github.io/psycho.R//2018/06/03/plot_bayesian_model.html#credits" target="_blank">Credits</a></li>
</ul>
<p>As Bayesian models usually generate a lot of samples (<em>iterations</em>), one could want to plot them as well, instead (or along) the posterior “summary” (with indices like the 90% HDI). This can be done quite easily by extracting all the iterations in <code>get_predicted</code> from the <code>psycho</code> package.</p>
<h1>The Model</h1>
<div>
<div>
<pre><code><span># devtools::install_github(&quot;neuropsychology/psycho.R&quot;) # Install the last psycho version if needed</span><span>
</span><span># Load packages</span><span>
</span><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>psycho</span><span>)</span><span>
</span><span># Import data</span><span>
</span><span>df</span><span> </span><span>&lt;-</span><span> </span><span>psycho</span><span>::</span><span>affective</span><span>
</span><span># Fit a logistic regression model</span><span>
</span><span>fit</span><span> </span><span>&lt;-</span><span> </span><span>rstanarm</span><span>::</span><span>stan_glm</span><span>(</span><span>Sex</span><span> </span><span>~</span><span> </span><span>Adjusting</span><span>,</span><span> </span><span>data</span><span>=</span><span>df</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;binomial&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>We fitted a Bayesian logistic regression to predict the sex (<em>W / M</em>) with one’s ability to flexibly adjust to his/her emotional reaction.</p>
<h1>Plot</h1>
<p>To visualize the model, the most neat way is to extract a “reference grid” (<em>i.e.</em>, a theorethical dataframe with balanced data). Our refgrid is made of equally spaced predictor values. With it, we can make predictions using the previously fitted model. This will compute the median of the posterior prediction, as well as the 90% credible interval. However, we’re interested in keeping all the prediction samples (iterations). Note that <code>get_predicted</code> automatically transformed log odds ratios (the values in which the model is expressed) to probabilities, easier to apprehend.</p>
<div>
<div>
<pre><code><span># Generate a new refgrid</span><span>
</span><span>refgrid</span><span> </span><span>&lt;-</span><span> </span><span>df</span><span> </span><span>%>%</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>Adjusting</span><span>)</span><span> </span><span>%>%</span><span> </span><span>psycho</span><span>::</span><span>refdata</span><span>(</span><span>length.out</span><span>=</span><span>10</span><span>)</span><span>
</span><span># Get predictions and keep iterations</span><span>
</span><span>predicted</span><span> </span><span>&lt;-</span><span> </span><span>psycho</span><span>::</span><span>get_predicted</span><span>(</span><span>fit</span><span>,</span><span> </span><span>newdata</span><span>=</span><span>refgrid</span><span>,</span><span> </span><span>keep_iterations</span><span>=</span><span>TRUE</span><span>)</span><span>
</span><span># Reshape this dataframe to have iterations as factor</span><span>
</span><span>predicted</span><span> </span><span>&lt;-</span><span> </span><span>predicted</span><span> </span><span>%>%</span><span> </span><span>tidyr</span><span>::</span><span>gather</span><span>(</span><span>Iteration</span><span>,</span><span> </span><span>Iteration_Value</span><span>,</span><span> </span><span>starts_with</span><span>(</span><span>&quot;iter&quot;</span><span>))</span><span>
</span><span># Plot all iterations with the median prediction</span><span>
</span><span>ggplot</span><span>(</span><span>predicted</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span>=</span><span>Adjusting</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>aes</span><span>(</span><span>y</span><span>=</span><span>Iteration_Value</span><span>,</span><span> </span><span>group</span><span>=</span><span>Iteration</span><span>),</span><span> </span><span>size</span><span>=</span><span>0.3</span><span>,</span><span> </span><span>alpha</span><span>=</span><span>0.01</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>aes</span><span>(</span><span>y</span><span>=</span><span>Sex_Median</span><span>),</span><span> </span><span>size</span><span>=</span><span>1</span><span>)</span><span> </span><span>+</span><span> </span><span>ylab</span><span>(</span><span>&quot;Probability of being a man\n&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_classic</span><span>()</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i2.wp.com/raw.githubusercontent.com/neuropsychology/psycho.R/master/docs/_posts/2018-06-03-plot_bayesian_model_files/figure-markdown_github/unnamed-chunk-3-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h1>Credits</h1>
<p>This package helped you? Don’t forget to cite the various packages you used <img alt="🙂" src="https://s.w.org/images/core/emoji/2.4/72x72/1f642.png" class="wp-smiley"></p>
<p>You can cite <code>psycho</code> as follows:</p>
<ul>
<li>Makowski, (2018). <em>The psycho Package: an Efficient and Publishing-Oriented Workflow for Psychological Science</em>. Journal of Open Source Software, 3(22), 470. <a rel="nofollow" href="https://doi.org/10.21105/joss.00470" target="_blank">https://doi.org/10.21105/joss.00470</a></li>
</ul>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://neuropsychology.github.io/psycho.R//2018/06/03/plot_bayesian_model.html"> Dominique Makowski</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/bysCtmNNfwM" height="1"></a></div>