I've read the JAGS manual but did not find a way to assign the same prior distribution to multiple parameters in a JAGS / R2JAGS model.
For example, currently I have to repeat a lot of code like this:
reg.model <- function() {
# Model structure
for(i in 1:N){
Y[i] ~ dnorm(mu[i], phi)
mu[i] <- beta0 + beta1*x1[i] + beta2*x2[i]
}
sigma2 <- 1 / phi
# Priors (Lots of re-typing here for beta0, beta1, beta2)
phi ~ dgamma(1,1)
beta0 ~ dnorm(0, 0.01*phi)
beta1 ~ dnorm(0, 0.01*phi)
beta2 ~ dnorm(0, 0.01*phi)
}
How to DRY this code?
You can do this if you pass your independent variables as a matrix, put your coefficients into a vector, and use matrix multiplication (with
inprod
or%*%
) to calculate your linear predictor.For example:
We generate some dummy data, below:
And fit the model:
Now compare the estimated coefficients and standard deviation to their true values: