Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot insert node into ...[]. Dimension mismatch

Tags:

jags

I'm trying to replicate simulations with a piece of jags code by Mark the Ballot, but jags is sending me an error message..

If I understood it correctly, it should have a problem with indexing house effects for each party somewhere, but I'm unable to find it because the node seems to be already indexed. Does anyone have an idea what the error is like?

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

For replication

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)
like image 886
Marie-Eve Avatar asked Nov 01 '15 14:11

Marie-Eve


1 Answers

The problem is that you are passing house to the model as a parameter, and you are using a house variable in a loop. JAGS 4.0.1 is confused. If you re-code to replace "house" in the loop with (say) "h" it should work ... example follows ...

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] )
}
like image 84
Mark Graph Avatar answered Dec 22 '22 02:12

Mark Graph