Passing variable to WinBugs model in R

2019-05-19 03:06发布

I am using the R2WinBugs package. I would like to pass two parameter that are calculated previously in the R script to the model function

c0yy <- 0.1
syy <- 0.0001
#Model
model <- function(c0yy,syy){

  #Likelihood
  for(i in 1:n){
    y[i] ~ dnorm(mu[i],cyy)
  }

  #Regression formula
  for(i in 1:n){
    mu[i] <- alpha + gamma * x[i]
  }

  #Priors for the regression parameters
  alpha ~ dnorm(0,0.000001)
  gamma ~ dnorm(0,0.000001)

  #Priors for the precision parameter
  cyy ~ dnorm(c0yy,syy)

  #Monitored variables
  beta <- gamma/(alpha-1)  
}
filename <- file.path(tempdir(), "Olm.txt")
write.model(model, filename)

but I get this error

made use of undefined node c0yy

while if I substitute the values for c0yy and syy inside the model function it works.. Any help?

Thanks

1条回答
戒情不戒烟
2楼-- · 2019-05-19 03:57

The values you are tying to pass to the model are data. In BUGS (and R2WinBUGS) data is passed to the program as a separate entity from the model that you have defined. In order to include the data you can put them into a list, something like;

my.mcmc <- bugs(data = list(c0yy = 0.1, syy= 0.0001), params = "beta', model.file = "Olm.txt", n.iter=10000) 

You will also need to drop the <- function(c0yy,syy) from your model script.

查看更多
登录 后发表回答