在R中将变量传递给WinBugs模型

在R中将变量传递给WinBugs模型

本文介绍了在R中将变量传递给WinBugs模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R2WinBugs软件包.我想将先前在R脚本中计算出的两个参数传递给模型函数

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)

但我收到此错误

made use of undefined node c0yy

如果我在模型函数中替换c0yysyy的值,它将起作用.有帮助吗?

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

谢谢

推荐答案

您要传递给模型的值是数据.在BUGS(和R2WinBUGS)中,数据作为与定义的模型分离的单独实体传递到程序.为了包含数据,您可以将它们放入列表中,例如;

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)

您还需要从模型脚本中删除<- function(c0yy,syy).

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

这篇关于在R中将变量传递给WinBugs模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:21