There's more...

We have used gamma priors in order to force the beta coefficients to be greater than zero. But in STAN, we have a different option: we can put a boundary via real<lower=0> beta[6]. This is sufficient to ensure that the posterior densities are bounded by zero. Apart from this, we may or may not want to specify a prior density, which we can do, for example, using beta ~ normal(5,3). It is very important to note that, in these cases (when using the <lower> approach), that we are actually specifying improper priors, which are priors that have a density that doesn't sum up to one (a Gaussian density is defined from -∞ to ∞ and has an area of 1). But if the permissible range for this variable is now from 0 to ∞, that area-integral of the density will be 0.5). STAN allows us to use either improper or proper priors. The STAN code for this model is as follows:

model ="
data {
real y[125];
real x[125,6];
}
parameters {
real<lower=0> beta[6];
real sigma;
real alpha;
}
model {
beta ~ normal(5,3);
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] + beta[6]*x[n,6], sigma);
}"
xy = list(y=data[,1],x=data[,2:7])
fit = stan(model_code = model, data = xy, warmup = 500, iter = 1000, chains = 4, cores = 2, thin = 1,verbose=FALSE)
summary(fit)

The following screenshot shows a summary of the posterior densities:

We can get the posterior densities using the following code:

stan_dens(fit)

The following screenshot shows the posterior densities:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.101.25