I'm trying to run a simple linear regression analysis using "R2OpenBUGS" package in R. when i run the "bugs" command, i face with error.
After adding "debug = T" to the command line, i get this error in OpenBUGS:
expected right parenthesis error pos 130
here's my codes in R:
library(R2OpenBUGS)
library(coda)
MODEL <- function() {
for (i in 1:N) {
y[i] ~ dnorm(mu[i], s2)
mu[i] <- b0+b1*(x[i])
}
b0 ~ dnorm(0, 1/9)
b1 ~ dnorm(0, 1/9)
s2 ~ dgamma(3, 1)
s2 <- 1/s2
}
write.model (model,"MODEL.txt")
INIT <- function() {
list(b0 = runif(1,-9, 9),
b1 = runif(1,-9, 9),
s2 = rexp(1,1/3))
}
DATA=list(y=c(15,33,26,21,39,40,14,38,20,32),
x=c(3,7,6,3,9,9,2,9,5,6),N=10)
BUGS=bugs(data = DATA, inits = INIT,
parameters.to.save = c("b0", "b1", "s2"),
model.file = "MODEL.txt", n.chains = 1,
n.iter = 10000, n.burnin = 100, codaPkg=TRUE, debug = T)
You need to set the precision parameters of b0
and b1
to 0.11, not 1/9. The precision parameter must be a floating point number or OpenBUGS will return an error.
In addition, you have defined s2
as a random variable and then assigned a value to it. BUGS does not like that either. You should create a new variable tau
to which to assign the value of the inverse of s2
. Your model code should read as follows:
MODEL <- function() {
for (i in 1:N) {
y[i] ~ dnorm(mu[i], tau)
mu[i] <- b0+b1*(x[i])
}
b0 ~ dnorm(0, 0.11)
b1 ~ dnorm(0, 0.11)
s2 ~ dgamma(3, 1)
tau <- 1/s2
}