A Toy Example of Using Rstan
This post prepares a toy example for estimating probalistic models using Rstan.
Motivating Example
Suppose we observe data \(Y \in\mathbb{R}^{n\times d}\) and \(X\in \mathbb{R}^{n\times p}\).
We believe the underlying model is \(\begin{align} Y\sim N(X\beta, \Sigma) \end{align}\), where \(\beta\in\mathbb{R}^{p\times d}\) and \(\Sigma\) is a diagonal matrix.
How shall we estimate these parameters?
Why Rstan?
As I quote from the Stan documentation, Stan is a probabilistic programming language that provides high-performance computation (e.g. Bayesian inference) for problems like the above. It interfaces with many languages, and we will focus on R
in this post. In specific, we use CmdStanR
.
Installation
Install cmdstanr
following this documentation.
Example
Fitting a model using cmdstanr
typically takes three steps:
- Prepare an input data list.
- Prepare a stan model.
- Fit the model with the input data.
Code shown below can also be found on Github.
Data Preparation
Stan code
Model Compilation
Model Fitting
The $model
method provides MCMC sampling. After sampling, one may obtain draws by calling $draws()
from the fitted object.
You can also obtain the sampling summary by
Alternative to MCMC, one may consider a penalized optimization or variational inference.