2

我是一名学习贝叶斯统计的学生,刚刚开始使用我的讲师编写的介绍脚本使用 JAGS,我们(学生)只需输入数据和迭代次数。以下是添加了我的数据的脚本:

setwd("C:\\Users\\JohnSmith\\Downloads")

rawdata = read.table("bwt.txt",header=TRUE)
Birthweight = rawdata$Birthweight
Age = rawdata$Age

model = "model
{
  beta0 ~ dnorm(0, 1/1000^2)
  beta1 ~  dnorm(0, 1/1000^2)

  log_sigma ~ dunif(-10, 10)
  sigma <- exp(log_sigma)

 for(i in 1:N)
 {
   mu[i] <- beta0 + beta1 * Age[i]
   Birthweight[i] ~ dnorm(mu[i], 1/sigma^2) 
 } 
}
"

data = list(x=Birthweight, y=Age, N=24)

# Variables to monitor
variable_names = c('beta0','beta1')

# How many burn-in steps?
burn_in = 1000

# How many proper steps?
steps = 100000

# Thinning?
thin = 10

# Random number seed
seed = 2693795


    # NO NEED TO EDIT PAST HERE!!!
    # Just run it all and use the results list.

library('rjags')

# Write model out to file
fileConn=file("model.temp")
writeLines(model, fileConn)
close(fileConn)

if(all(is.na(data)))
{
    m = jags.model(file="model.temp", inits=list(.RNG.seed=seed,             .RNG.name="base::Mersenne-Twister"))
} else
{
m = jags.model(file="model.temp", data=data, inits=list(.RNG.seed=seed,     .RNG.name="base::Mersenne-Twister"))
}
update(m, burn_in)
draw = jags.samples(m, steps, thin=thin, variable.names = variable_names)
# Convert to a list
make_list <- function(draw)
{
    results = list()
    for(name in names(draw))
    {
        # Extract "chain 1"
        results[[name]] = as.array(draw[[name]][,,1])

        # Transpose 2D arrays
        if(length(dim(results[[name]])) == 2)
            results[[name]] = t(results[[name]])
    }
    return(results)
}
results = make_list(draw)

但是,当我运行以下代码时,我收到以下错误消息:

Error in jags.model(file = "model.temp", data = data, inits = list(.RNG.seed = seed,  : 
  RUNTIME ERROR:
Compilation error on line 11.
Unknown parameter Age

In addition: Warning messages:
1: In jags.model(file = "model.temp", data = data, inits = list(.RNG.seed =     seed,  :
  Unused variable "x" in data
2: In jags.model(file = "model.temp", data = data, inits = list(.RNG.seed = seed,  :
  Unused variable "y" in data

但据我所知,第 11 行是空白的,这让我不知道错误来自哪里。如果有人可以给我一些解决此问题的提示,将不胜感激。

4

1 回答 1

2

数据列表中元素的名称 ( data) 应与模型中变量的名称相匹配。

你有:

data = list(x=Birthweight, y=Age, N=24)

所以 JAGS 正在寻找你的模型中调用x的变量。y但是,在您的模型中,您有:

mu[i] <- beta0 + beta1 * Age[i]
Birthweight[i] ~ dnorm(mu[i], 1/sigma^2) 

也就是说,您的变量被称为Ageand Birthweight

因此,要么将您的列表更改为:

data <- list(Birthweight=Birthweight, Age=Age, N=24)

或将您的模型更改为:

mu[i] <- beta0 + beta1 * y[i]
x[i] ~ dnorm(mu[i], 1/sigma^2)

如果您完成readLines('model.temp')(或在文本编辑器中打开 model.temp),您会看到该文件的第 11 行引用包含 的行mu[i] <- beta0 + beta1 * Age[i],这是 JAGS 由于引用而遇到的第一个错误,Age这两个数据都没有也没有提供先验。

于 2015-10-01T09:07:11.907 回答