2015-11-01 79 views
3

我試圖通過標記選票複製模擬一塊jags代碼,但是jags發送給我一個錯誤消息。無法將節點插入... []。尺寸不匹配

如果我正確理解它,它應該與索引的房子有問題影響每個黨的某個地方,但我無法找到它,因爲該節點似乎已經索引。有沒有人有一個想法是什麼錯誤?

model <- jags.model(textConnection(model), 
         data = data, 
         n.chains=4, 
         n.adapt=10000 


    Compiling model graph 
     Resolving undeclared variables 
     Allocating nodes 
    Deleting model 

    Error in jags.model(textConnection(model2), data = data, n.chains = 4, : 
     RUNTIME ERROR: 
    Cannot insert node into houseEffect[1...4,2]. Dimension mismatch 

對於複製

model <- ' 
    model { 
    for(poll in 1:NUMPOLLS) { 
    adjusted_poll[poll, 1:PARTIES] <- walk[pollDay[poll], 1:PARTIES] + 
    houseEffect[house[poll], 1:PARTIES] 
    primaryVotes[poll, 1:PARTIES] ~ dmulti(adjusted_poll[poll, 1:PARTIES], n[poll]) 
    } 
    tightness <- 50000 
    discontinuity_tightness <- 50 

    for(day in 2:(discontinuity-1)) { 
    multinomial[day, 1:PARTIES] <- walk[day-1, 1:PARTIES] * tightness 
    walk[day, 1:PARTIES] ~ ddirch(multinomial[day, 1:PARTIES]) 
    } 
    multinomial[discontinuity, 1:PARTIES] <- walk[discontinuity-1, 1:PARTIES] * discontinuity_tightness 
    walk[discontinuity, 1:PARTIES] ~ ddirch(multinomial[discontinuity, 1:PARTIES]) 

    for(day in discontinuity+1:PERIOD) { 
    multinomial[day, 1:PARTIES] <- walk[day-1, 1:PARTIES] * tightness 
    walk[day, 1:PARTIES] ~ ddirch(multinomial[day, 1:PARTIES]) 
    } 

    for (party in 1:2) { 
    alpha[party] ~ dunif(250, 600) 
    } 
    for (party in 3:PARTIES) { 
    alpha[party] ~ dunif(10, 250) 
    } 
    walk[1, 1:PARTIES] ~ ddirch(alpha[]) 

    for(day in 1:PERIOD) { 
    CoalitionTPP[day] <- sum(walk[day, 1:PARTIES] * 
    preference_flows[1:PARTIES]) 
    } 

    for (party in 2:PARTIES) { 
    houseEffect[1, party] <- -sum(houseEffect[2:HOUSECOUNT, party]) 
    } 
    for(house in 1:HOUSECOUNT) { 
    houseEffect[house, 1] <- -sum(houseEffect[house, 2:PARTIES]) 
    } 
    # but note, we do not apply a double constraint to houseEffect[1, 1] 
    monitorHouseEffectOneSumParties <- sum(houseEffect[1, 1:PARTIES]) 
    monitorHouseEffectOneSumHouses <- sum(houseEffect[1:HOUSECOUNT, 1]) 

    for (party in 2:PARTIES) { 
    for(house in 2:HOUSECOUNT) { 
    houseEffect[house, party] ~ dnorm(0, pow(0.1, -2)) 
    } } } 
    ' 

    preference_flows <- c(1.0, 0.0, 0.1697, 0.533) 

    PERIOD = 26 
    HOUSECOUNT = 5 
    NUMPOLLS = 35 
    PARTIES = 4 
    discontinuity = 20 

    pollDay = c(1, 1, 2, 2, 6, 8, 8, 9, 9, 10, 10, 10, 10, 12, 12, 13, 14, 14, 16, 16, 17, 18, 19, 19, 20, 21, 22, 22, 24, 24, 24, 24, 24, 26, 26) 

    house = c(1, 2, 3, 4, 3, 3, 5, 1, 2, 1, 3, 4, 5, 3, 4, 2, 3, 4, 3, 4, 5, 3, 2, 4, 3, 5, 3, 4, 1, 2, 3, 4, 5, 3, 4) 

    n = c(1400, 1400, 1000, 1155, 1000, 1000, 3690, 1400, 1400, 1400, 1000, 1177, 3499, 1000, 1180, 1400, 1000, 1161, 1000, 1148, 2419, 1000, 1386, 1148, 1000, 2532, 1000, 1172, 1682, 1402, 1000, 1160, 3183, 1000, 1169) 

    preference_flows = c(1.0000, 0.0000, 0.1697, 0.5330) 

    primaryVotes = read.csv(text = c(
    'Coalition, Labor, Greens, Other 
    532,574,154,140 
    560,518,168,154 
    350,410,115,125 
    439,450,139,127 
    385,385,95,135 
    375,395,120,110 
    1465,1483,417,325 
    504,602,154,140 
    532,560,154,154 
    504,602,154,140 
    355,415,120,110 
    412,483,141,141 
    1345,1450,392,312 
    375,405,100,120 
    448,448,142,142 
    588,504,168,140 
    390,380,115,115 
    441,453,139,128 
    380,400,110,110 
    471,425,126,126 
    957,979,278,205 
    405,360,125,110 
    546,532,182,126 
    471,413,126,138 
    385,380,120,115 
    1008,995,301,228 
    400,375,115,110 
    457,410,141,164 
    690,656,185,151 
    603,491,182,126 
    415,355,125,105 
    464,429,139,128 
    1307,1218,385,273 
    410,370,130,90 
    479,433,152,105'), sep=",") 

    data = list(PERIOD = PERIOD, 
       HOUSECOUNT = HOUSECOUNT, 
       NUMPOLLS = NUMPOLLS, 
       PARTIES = PARTIES, 
       primaryVotes = primaryVotes, 
       pollDay = pollDay, 
       house = house, 
       discontinuity = discontinuity, 
       # manage rounding issues with df$Sample ... 
       n = rowSums(primaryVotes), 
       preference_flows = preference_flows 
    ) 
    print(data) 
+0

我有類似的問題JAGS 4.0.1那裏有JAGS 3.4.0 - 我已經編寫了一系列模型。 –

回答

4

的問題是要傳遞的房子模型作爲參數,並且使用的是循環中的房子變量。 JAGS 4.0.1很困惑。如果你重新編碼以替換(說)「h」循環中的「房子」它應該工作...示例如下...

for (h in 2:HOUSECOUNT) { 
    for (p in 2:PARTIES) { 
     # vague priors ... 
     houseEffect[h, p] ~ dnorm(0, pow(0.1, -2)) 
    } 
} 
for (p in 2:PARTIES) { 
    houseEffect[1, p] <- -sum(houseEffect[2:HOUSECOUNT, p]) 
} 
for(h in 1:HOUSECOUNT) { 
    houseEffect[h, 1] <- -sum(houseEffect[h, 2:PARTIES]) 
} 
+0

感謝這個答案 - 它解決了我在嘗試實施Kruschke練習8.1中的代碼時做的貝葉斯數據分析中遇到的問題。您能否介紹一下您在使用JAGS 4.0.1時遇到的其他問題,以及您如何解決這些問題或指出一個有用的鏈接?我很樂意再問一個單獨的問題,如果它更容易。 –

+0

也許最好問一個單獨的問題 –