簡體   English   中英

JAGS錯誤-可能涉及以下某些或所有節點的有向循環

[英]JAGS error - Possible directed cycle involving some or all of the following nodes

完整的數據集包含〜11,000行。 在檢查代碼是否運行的同時,我一直在以K = 400運行代碼。

所有行都與地圖上的特定單元格相關,並包含從Sentinel-2圖像和數字高程圖提取的信息。

117個細胞的子集還包含在實地考察中記錄的棲息地協變量。 因此,某些列(包括響應變量(S1和S2)和tussac)的特征是NA的比例很高。

編碼:

add_c4 <- "model{
for(i in 1:K) {
S1[i]~dpois(lambda1[i])
lambda1[i]<-exp(a0+a1*DEM_slope[i]+a2*DEM_elevation[i]+a3*tussac[i]+a4*S2[i])

S2[i]~dpois(lambda2[i])
lambda2[i]<-exp(c0+c1*DEM_slope[i]+c2*DEM_elevation[i]+c3*tussac[i]+c4*S1[i])

muLogit_tussac[i]<-b0 + sentinel1[i] + sentinel3[i] + sentinel7[i] + sentinel8[i] + sentinel9[i] + DEM_slope[i]

Logit_tussac[i]~dnorm(muLogit_tussac[i], tau)
logit(tussac[i])<-Logit_tussac[i]
}

# Priors

a0~dnorm(0, 10)
a1~dnorm(0, 10)
a2~dnorm(0, 10)
a3~dnorm(0, 10)
a4~dnorm(0, 10)

b0~dnorm(0, 10)
b1~dnorm(0, 10)
b2~dnorm(0, 10)
b3~dnorm(0, 10)

c0~dnorm(0, 10)
c1~dnorm(0, 10)
c2~dnorm(0, 10)
c3~dnorm(0, 10)
c4~dnorm(0, 10)

tau~dgamma(0.001, 0.001)

#data# S1, S2, K, sentinel1, sentinel3, sentinel7, sentinel8, sentinel9, DEM_slope, DEM_elevation
#inits# a0, a2, a3, a4, b0, b1, b2, b3, c0, c2, c3, c4
#monitor# a0, a1, a2, a3, a4, b0, b1, b2, b3, tau, ped, dic, c0, c1, c2, c3, c4
}"

當我包含'c4 * S1 [i]'時,出現以下錯誤:

Possible directed cycle involving some or all of the following nodes

然后,它繼續列出S1,S2,lambda1和lambda2的所有值。

刪除“ c4 * S1 [i]”將導致代碼運行。

我看過以下線程:

JAGS中可能的定向循環錯誤

https://stats.stackexchange.com/questions/220312/coding-a-jags-error-model-for-a-dependent-variable-that-has-increasing-variance

其中的問題似乎是由發布者在等式兩邊都使用“ y”引起的。 我認為我的問題是由於a4將代碼發送到S2部分,而c4將代碼發送回S1部分,這有點像定向循環。 任何想法如何解決這個問題?

我將數據集的前幾行包括在內,以防萬一:

S1 S2 Logit_tussac moisture DEM_slope DEM_aspect DEM_elevation sentinel1 sentinel2 sentinel3 sentinel4 sentinel5 sentinel6 sentinel7 sentinel8 sentinel9 sentinel10
NA NA     NA            NA  2.434239   168.5011   0.588606366    0.0413    0.0499    0.0531    0.1035    0.1862    0.1968    0.1808    0.1318    0.0400     0.0199
NA NA     NA            NA  3.705001   178.1289   1.007037127    0.0966    0.1108    0.1212    0.0855    0.0917    0.1063    0.0937    0.1842    0.0341     0.0161
NA NA     NA            NA  5.006181   180.0000   1.883010797    0.1309    0.1472    0.1361    0.0855    0.0917    0.1063    0.0937    0.1572    0.0341     0.0161
NA NA     NA            NA  5.006181   180.0000   2.758984468    0.0542    0.0512    0.0472    0.0145    0.0127    0.0092    0.0166    0.0510    0.0148     0.0080

數據集子集,以便僅包含遠程和本地感測數據的117行:

S1 S2 Logit_tussac moisture DEM_slope DEM_aspect DEM_elevation sentinel1 sentinel2 sentinel3 sentinel4 sentinel5 sentinel6 sentinel7 sentinel8 sentinel9 sentinel10
NA NA        NA        NA   14.917334   256.1612      12.24432    0.0513    0.0588    0.0541    0.1145    0.1676    0.1988    0.1977    0.1658    0.1566     0.0770
0  0  -9.210240         1   23.803741   225.1231      16.88028    0.1058    0.1370    0.2139    0.2387    0.2654    0.2933    0.3235    0.2928    0.3093     0.1601
NA NA        NA        NA   20.789165   306.0945      18.52480    0.0287    0.0279    0.0271    0.0276    0.0290    0.0321    0.0346    0.0452    0.0475     0.0219
NA NA -9.210240         1    6.689442   287.9641      36.08975    0.0462    0.0679    0.1274    0.1535    0.1797    0.2201    0.2982    0.2545    0.4170     0.2252
0  0  -9.210240         1   25.476444   203.0659      23.59964    0.0758    0.1041    0.1326    0.1571    0.2143    0.2486    0.2939    0.2536    0.3336     0.1937
1  0  -1.385919         3    1.672511   270.0000      39.55215    0.0466    0.0716    0.1227    0.1482    0.2215    0.2715    0.3334    0.2903    0.3577     0.1957

正確識別后,您的問題就是模型圖中的有向循環。 這是一個問題的原因是,事實證明,DAG(有向無環圖)不包含任何有向環非常重要,否則不能保證我們可以定義穩定的后驗樣本。

例如,采用以下包含定向循環的模型:

model <- 'model{

    for(i in 1:N){
        a[i] ~ dnorm(b[i], tau)
        b[i] ~ dnorm(a[i], tau)
    }

    tau ~ dgamma(0.01,0.01)

    #monitor# tau
    #data# N

}'

N <- 10
runjags::run.jags(model)

沒有明智的方法來估算此模型,JAGS會告訴您。 但理論上可以估計此模型:

model <- 'model{

    for(i in 1:N){
        a[i] ~ dnorm(b[i], tau)
        b[i] ~ dnorm(a[i], tau)
    }

    tau ~ dgamma(0.01,0.01)

    #monitor# tau
    #data# N, a

}'

N <- 10
a <- rnorm(N)
runjags::run.jags(model)

發生的變化是,所有a []現在都是固定的(觀察到的),因此我們實際上可以估計此模型。 但是JAGS仍會檢測到有向循環,因此需要一種解決方法:

model <- 'model{

    for(i in 1:N){
        a[i] ~ dnorm(b[i], tau)
        b[i] ~ dnorm(aa[i], tau)
    }

    tau ~ dgamma(0.01,0.01)

    #monitor# tau
    #data# N, a, aa

}'

N <- 10
a <- rnorm(N)
aa <- a
runjags::run.jags(model)

通過欺騙JAGS認為a []和aa []不相關,從而隱藏了有向循環。 但這僅在觀察/固定了所有a []時起作用,否則模型中不會估計或定義缺少的aa []。 在您的情況下,似乎部分觀察到了S1 []和S2 [],因此,除非您簡單地省略缺少S1或S2的行/觀測值,否則此技巧將不起作用(這可能不可行,因為您說它們具有較高的NA的比例)。

否則,您將不得不以某種方式重新構建模型以打破定向循環。 這將涉及考慮系統底層的生物過程,以及如何在不創建直接循環的情況下表示所需的關系,因此我們無法真正提供幫助。

希望能有所幫助,

馬特

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM