<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/r-stan-and-bayesian-statistics/"></div></div><div><a data-url="http://www.r-bloggers.com/r-stan-and-bayesian-statistics/" data-counturl="http://www.r-bloggers.com/r-stan-and-bayesian-statistics/" data-count="horizontal" data-text="R, Stan and Bayesian Statistics" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2016/06/r-stan-bayesian-stats.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>by Joseph Rickert</p>
<p>Just about two and a half years ago <a target="_blank" href="http://blog.revolutionanalytics.com/2013/11/r-and-bayesian-statistics.html">I wrote abou</a>t some resources for doing Bayesian statistics in R. Motivated by the tutorial <a target="_blank" href="http://tharte.github.io/mbt/">Modern Bayesian Tools for Time Series Analysis</a> by Harte and Weylandt that I attended at R/Finance last month, and the upcoming tutorial <a target="_blank" href="http://schedule.user2016.org/event/7BbC/an-introduction-to-bayesian-inference-using-r-interfaces-to-stan-part-1">An Introduction to Bayesian Inference using R Interfaces to Stan</a> that Ben Goodrich is going to give at useR! I thought I'd look into what's new. Well, Stan is what's new! Yes, Stan has been under development and available for some time. But somehow, while I wasn't paying close attention, two things happened: (1) the <a target="_blank" href="https://mran.revolutionanalytics.com/package/rstan/">rstan</a> package evolved to make the mechanics of doing Bayesian in R analysis really easy and (2) the <a target="_blank" href="http://mc-stan.org/team/">Stan team</a> produced and/or organized an amazing amount of documentation. </p>
<p>My impressions of doing Bayesian analysis in R were set in the <a target="_blank" href="http://www.mrc-bsu.cam.ac.uk/software/bugs/the-bugs-project-winbugs/">WinBUGS</a> era. The separate WinBUGs installation was always tricky, and then moving between the <a target="_blank" href="https://cran.r-project.org/web/packages/BRugs/index.html">BRugs</a> and <a target="_blank" href="https://cran.r-project.org/web/packages/R2WinBUGS/index.html">R2WinBUGS</a> packages presented some additional challenges. My recent Stan experience was nothing like this. I had everything up and running in just a few minutes. The <a target="_blank" href="https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started">directions</a> for getting started with rstan are clear and explicit about making sure that you have the right tool chain in place for your platform. Since I am running R 3.3.0 on Windows 10 I installed <a target="_blank" href="https://cran.r-project.org/bin/windows/Rtools/">Rtools34</a>. This went quickly and as expected except that C:\Rtools\gcc-4.x-y\bin did not show up in my path variable. Not a big deal: I used the menus in the Windows System Properties box to edit the Path statement by hand. After this, rstan installed like any other R package and I was able to run the 8schools example from the <a target="_blank" href="https://cran.r-project.org/web/packages/rstan/vignettes/rstan_vignette.pdf">package vignette</a>. The following 10 minute video by Ehsan Karim takes you through the install process and the vignette example.</p>
<p><iframe height="480" allowfullscreen="" width="854" src="https://www.youtube.com/embed/tLprFqSWS1w"></iframe> </p>
<p>The <a target="_blank" href="http://mc-stan.org/documentation/">Stan documentation</a> includes four major components: (1) The Stan Language Manual, (2) <a target="_blank" href="https://github.com/stan-dev/example-models/wiki">Examples </a>of fully worked out problems, (3) <a target="_blank" href="http://mc-stan.org/documentation/case-studies.html">Contributed Case Studies</a> and (4) both slides and video tutorials. This is an incredibly rich cache of resources that makes a very credible case for the ambitious project of teaching people with some R experience both Bayesian Statistics and Stan at the same time. The &quot;trick&quot; here is that the documentation operates at multiple levels of sophistication with entry points for students with different backgrounds. For example, a person with some R and the modest statistics background required for approaching Gelman and Hill's extraordinary text: <a target="_blank" href="http://www.stat.columbia.edu/~gelman/arm/">Data Analysis Using Regression and Multilevel/Hierarchical Models</a> can immediately beginning running rstan code for the book's examples. To run the rstan version of the example in section 5.1, Logistic Regression with One Predictor, with no changes a student only needs only to copy the R scripts and data into her local environment. In this case, she would need the R script:  <a target="_blank" href="https://github.com/stan-dev/example-models/blob/master/ARM/Ch.5/5.1_LogisticRegressionWithOnePredictor.R">5._LogisticRegressionWithOnePredictor. R</a>, the data: <a target="_blank" href="https://github.com/stan-dev/example-models/blob/master/ARM/Ch.5/nes1992_vote.data.R">nes1992_vote.data.R</a> and the Stan code: <a target="_blank" href="https://github.com/stan-dev/example-models/blob/master/ARM/Ch.5/nes_logit.stan">nes_logit.stan</a><strong>.</strong>  The Stan code for this simple model is about as straightforward as it gets: variable declarations, parameter identification and the model itself.</p>
<table data-tab-size="8" width="339">
<tbody>
<tr>
<td><span>data</span> {</td>
</tr>
<tr>
<td data-line-number="2"> </td>
<td><span>int</span><span>&lt;</span><span>lower</span><span>=</span><span>0</span><span>></span> N;</td>
</tr>
<tr>
<td data-line-number="3"> </td>
<td><span>vector</span>[N] income;</td>
</tr>
<tr>
<td data-line-number="4"> </td>
<td><span>int</span><span>&lt;</span><span>lower</span><span>=</span><span>0</span>,<span>upper</span><span>=</span><span>1</span><span>></span> vote[N];</td>
</tr>
<tr>
<td data-line-number="5"> </td>
<td>}</td>
</tr>
<tr>
<td data-line-number="6"> </td>
<td><span>parameters</span> {</td>
</tr>
<tr>
<td data-line-number="7"> </td>
<td><span>vector</span>[<span>2</span>] beta;</td>
</tr>
<tr>
<td data-line-number="8"> </td>
<td>}</td>
</tr>
<tr>
<td data-line-number="9"> </td>
<td><span>model</span> {</td>
</tr>
<tr>
<td data-line-number="10"> </td>
<td>vote <span>~</span> <span>bernoulli_logit</span>(beta[<span>1</span>] <span>+</span> beta[<span>2</span>] <span>*</span> income);</td>
</tr>
<tr>
<td data-line-number="11"> </td>
<td>}</td>
</tr>
</tbody>
</table>
<p>Running the script will produce the iconic logistic regression plot:</p>
<p><a target="_blank" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d1fb6511970c-pi"><img title="G_H_5-1" alt="G_H_5-1" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d1fb6511970c-800wi" border="0"></a></p>
<p>I'll wind down by curbing my enthusiasm just a little by pointing out that Stan is not the only game in town. <a target="_blank" href="http://mcmc-jags.sourceforge.net/">JAGS</a> is a popular alternative, and there is plenty that can be done with unaugmented R code alone as the <a target="_blank" href="https://cran.r-project.org/web/views/Bayesian.html">Bayesian Inference Task View</a> makes abundantly clear. </p>
<p>If you are a book person and new to Bayesian statistics, I highly recommend <a target="_blank" href="http://www.springer.com/statistics/computational+statistics/book/978-1-4614-8686-2?otherVersion=978-1-4614-8687-9">Bayesian Essentials with R</a> by Jean-Michel Marin and Christian Robert. The authors provide a compact introduction to Bayesian statistics that is backed up with numerous R examples. Also, the new book by Richard McElreath, <a target="_blank" href="http://xcelab.net/rm/statistical-rethinking/">Statistical Rethinking: A Bayesian Course with Examples in R and Stan</a> looks like it is going to be an outstanding read. The <a target="_blank" href="https://www.youtube.com/playlist?list=PLDcUM9US4XdMdZOhJWJJD4mDBMnbTWw_z">online supplements</a> to the book are certainly worth a look.</p>
<p>Finally, if you are a Bayesian or a thinking about becoming one and you are going to useR!, be sure to catch the following talks:</p>
<ul>
<li><a target="_blank" href="http://schedule.user2016.org/event/7BZq/bayesian-analysis-of-generalized-linear-mixed-models-with-jags">Bayesian analysis of generalized linear mixed models with JAGS</a>, by Martyn Plummer</li>
<li><a target="_blank" href="http://schedule.user2016.org/event/7BY0/bamdit-an-r-package-for-bayesian-meta-analysis-of-diagnostic-test-data">bamdit: An R Package for Bayesian meta-Analysis of diagnostic test data</a> by Pablo Emilio Verde</li>
<li><a target="_blank" href="http://schedule.user2016.org/event/7BRI/fitting-complex-bayesian-models-with-r-inla-and-mcmc">Fitting complex Bayesian models with R-INLA and MCMC</a> by Virgilio Gómez-Rubio</li>
<li><a target="_blank" href="http://schedule.user2016.org/event/7BZ1/bayesboot-an-r-package-for-easy-bayesian-bootstrapping">bayesboot: An R package for easy Bayesian bootstrappin</a>g by Rasmus Arnling Bååth</li>
<li><a target="_blank" href="http://schedule.user2016.org/event/7BbC/an-introduction-to-bayesian-inference-using-r-interfaces-to-stan-part-1">An Introduction to Bayesian Inference using R Interfaces to Stan</a> by Ben Goodrich</li>
<li><a target="_blank" href="http://schedule.user2016.org/event/7GxD/dilemma-distributed-learning-with-markov-chain-monte-carlo-algorithms-with-the-roar-package">DiLeMMa – Distributed Learning with Markov Chain Monte Carlo Algorithms Using the ROAR Package</a> by Ali Zaidi</li>
</ul>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/r-stan-and-bayesian-statistics/"></div></div><div><a data-url="http://www.r-bloggers.com/r-stan-and-bayesian-statistics/" data-counturl="http://www.r-bloggers.com/r-stan-and-bayesian-statistics/" data-count="vertical" data-text="R, Stan and Bayesian Statistics" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2016/06/r-stan-bayesian-stats.html"> Revolutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/d2Z9V-EbSuw"></a></div>