无法将节点插入 ...[]。尺寸不匹配

Mar*_*Eve 5 jags

我正在尝试使用 Mark the Ballot 的一段 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
Run Code Online (Sandbox Code Playgroud)

用于复制

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)
Run Code Online (Sandbox Code Playgroud)

Mar*_*aph 5

问题是您将房屋作为参数传递给模型,并且您在循环中使用房屋变量。JAGS 4.0.1 很困惑。如果您重新编码以用(例如)“h”替换循环中的“house”,它应该可以工作......示例如下......

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] )
}
Run Code Online (Sandbox Code Playgroud)