2017-04-05 85 views
1

我對R JAGS中的一個簡單問題感到困惑。例如,我有10個參數:d [1],d [2],...,d [10]。從數據中直觀地看出,它們應該在增加。所以我想對它們加以限制。限制R JAGS中的參數順序

這裏是我試圖做的,但它給錯誤信息,說「節點與父母不一致」:

model{ 
    ... 
    for (j in 1:10){ 
    d.star[j]~dnorm(0,0.0001) 
    } 
    d=sort(d.star) 
    } 

然後我嘗試這樣的:

d[1]~dnorm(0,0.0001) 
    for (j in 2:10){ 
    d[j]~dnorm(0,0.0001)I(d[j-1],) 
    } 

這個工作,但我不」不知道這是否是正確的做法。你能分享你的想法嗎?

謝謝!

回答

1

如果您對此類事情一直不確定,最好只模擬一些數據以確定您建議的模型結構是否有效(擾流警報:確實如此)。

這裏是我使用的模型:

cat('model{ 
    d[1] ~ dnorm(0, 0.0001) # intercept 
    d[2] ~ dnorm(0, 0.0001) 
    for(j in 3:11){ 
    d[j] ~ dnorm(0, 0.0001) I(d[j-1],) 
    } 
    for(i in 1:200){ 
    y[i] ~ dnorm(mu[i], tau) 
    mu[i] <- inprod(d, x[i,]) 
    } 
    tau ~ dgamma(0.01,0.01) 
    }', 
file = "model_example.R")``` 

這裏是我模擬了這個模型中使用的數據。

library(run.jags) 
library(mcmcplots) 

# intercept with sorted betas 
set.seed(161) 
betas <- c(1,sort(runif(10, -5,5))) 

# make covariates, 1 for intercept 
x <- cbind(1,matrix(rnorm(2000), nrow = 200, ncol = 10)) 

# deterministic part of model 
y_det <- x %*% betas 

# add noise 
y <- rnorm(length(y_det), y_det, 1) 

data_list <- list(y = as.numeric(y), x = x) 

# fit the model 
mout <- run.jags('model_example.R',monitor = c("d", "tau"), data = data_list) 

在此之後,我們就可以繪製出估計和覆蓋參數值爲true

caterplot(mout, "d", reorder = FALSE) 
points(rev(c(1:11)) ~ betas, pch = 18,cex = 0.9) 

黑點是真正的參數值,藍色的點和線的估計。只要有足夠的數據來估計所有這些參數,看起來這種設置沒有問題。 enter image description here

+0

謝謝!這非常有幫助。 – user3669725

1

它看起來像在第一個實現中有語法錯誤。請嘗試:

model{ 
    ... 
    for (j in 1:10){ 
    d.star[j]~dnorm(0,0.0001) 
    } 
    d[1:10] <- sort(d.star) # notice d is indexed. 
} 

並將結果與​​第二個實現的結果進行比較。根據文件,這些都是正確的,但建議使用功能sort

+0

感謝您指出!我的實際代碼實際上有這個d [1:10]一塊,但仍然有同樣的錯誤....我問了關於此問題的另一個問題:http://stackoverflow.com/questions/43216398/jags-model-with-錯誤節點不一致的,以父母 – user3669725