How to do it...

We will use the same house prices example that we used in the previous recipes for linear regression. We have a price variable, and several regressors, such as the number of bedrooms, bathrooms, and so on:

  1. We first run our example, but with 5,000 iterations and different priors. Let's use a Gaussian prior on each beta coefficient and a lower boundary equal to 0:
library(rstan)library(coda)library(DrBats)data = read.csv("/Users/admin/Documents/R_book/chapter3/house_prices.csv")model ="data {real y[125];real x[125,6];}parameters {real <lower=0> beta[6];real sigma;real alpha;}model {beta ~ normal(5,20);for (n in 1:125)y[n] ~ normal(alpha + beta[1]*x[n,1] + beta[2]*x[n,2] + beta[3]*x[n,3] + beta[4]*x[n,4] + beta[5]*x[n,5] ...

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.