How to do it...

In this example, we will generate 1,000 Gaussian deviates with a mean equal to five and a standard deviation equal to three. We will assign a Gaussian prior to the mean parameter, and a gamma distribution to the standard deviation. The objective will be to characterize the marginal posterior densities for mu and sigma (mean and standard deviation of a Gaussian distribution):

  1. We declare our STAN model, and we will store it into a string as follows:
library(rstan)values = list(y = rnorm(1000,5,3))model ="data {real y[1000];}parameters {real mu;real sigma;}model {mu ~ normal(0,10);sigma ~ normal(0,10);y ~ normal(mu,sigma);}"
  1. We call the stan function, which fits the model. The first parameter, model_code=, obviously specifies ...

Get R Statistics Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.