Passing variable to WinBugs model in R

人走茶凉 提交于 2019-12-07 19:09:31

问题


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:


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.



来源:https://stackoverflow.com/questions/27927118/passing-variable-to-winbugs-model-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!