2016-09-28 70 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

是的,那也行! –