2016-09-28 69 views
0

我在任何具有数据块和data =参数的模型中从run.jags()中获得了一些违反直觉的行为。它似乎使用数据参数run.jags为实际模型,但在环境中搜索数据块中使用的任何东西。这里是一个非常简单的模型的例子:run.jags数据搜索环境

data { 
    ylen <- length(y) 
} 
model { 
    for (i in 1:ylen) { 
     y[i] ~ dnorm(mu,1) 
    } 
    mu ~ dnorm(0,1/10^2) 
} 

如果我运行它像这样,我得到一个错误:

> run.jags('trivial.jags',data=list(y=c(5,5,5,6)),monitor=c('ylen','mu')) 
Error: The following error was obtained while attempting to parse the data: 
Error in eval(expr, envir, enclos) : object 'y' not found 

但是,如果我创建的调用环境变量“Y”它被使用,但在一个非常奇怪的方式:

> y<-c(-1,-1,-1) 
> run.jags('trivial.jags',data=list(y=c(5,5,5,6)),monitor=c('ylen','mu')) 
Compiling rjags model... 
Calling the simulation using the rjags method... 
Note: the model did not require adaptation 
Burning in the model for 4000 iterations... 
    |**************************************************| 100% 
Running the model for 10000 iterations... 
    |**************************************************| 100% 
Simulation complete 
Calculating summary statistics... 
Note: The monitored variable 'ylen' appears to be non-stochastic; it will not be included 
in the convergence diagnostic 
Calculating the Gelman-Rubin statistic for 2 variables.... 
Finished running the simulation 

JAGS model summary statistics from 20000 samples (chains = 2; adapt+burnin = 5000): 

    Lower95 Median Upper95 Mean  SD Mode  MCerr MC%ofSD SSeff AC.10 psrf 
ylen  3  3  3  3  0 3  --  -- -- --  -- 
mu 3.8339 4.9742 6.0987 4.9747 0.57625 -- 0.0040089  0.7 20661 0.011 1.0001 

所以你可以看到,它似乎已经用y在调用环境计算长度,到达3,但是从数据中使用的Y值列出实际数据,到达mu = 5。

如果我使用rjags,它可以像我期望的那样工作,对实际模型和数据块中派生变量的计算使用data =参数。

这是runjags中的错误吗?我怎样才能让它使用data =参数来运行数据块中的run.jags()以进行计算?

我想这对runjags_2.0.3-2和runjags_2.0.4-2

回答

1

是的,这是runjags一个bug,现在将固定下一个版本感谢您的明确和可重复的例子!问题的根源在于试图保持与BUGS模型文本的兼容性,该文本可以包含数据列表(这与JAGS使用的数据块不同)。

与此同时,可能的解决方法是计算R中的ylen,并将其传递给数据列表中的JAGS(也可参见模型中的#data#构造本身),或在模型中使用长度(y)直接如:

model { 
for (i in 1:length(y)) { 
    y[i] ~ dnorm(mu,1) 
} 
mu ~ dnorm(0,1/10^2) 
} 

希望帮助,

马特

+0

谢谢!由于我不需要Bug兼容性,我只是将read.jagsfile()中的调用移除到解析出数据块的winbugs.extract *中;这应该让我一直到下一个版本解决问题。 –

+0

是的,那也行! –