How to define a temporary local variable in jags?

2019-08-01 07:03发布

问题:

I want to create a temp variable in jags, but it doesn't work as it would work in R

for (cid in 1:CAMPAIGN_N) {
  for (time in 1:DATE_N){      
    index <- time * CAMPAIGN_N + cid - 2
    positives[index] ~ dbin( k[time]*ctr[cid], tries[index])
  }
}

Gives error, because index variable is being defined only once. So I had to write it the following ugly way:

for (cid in 1:CAMPAIGN_N) {
  for (time in 1:DATE_N){      
    positives[time * CAMPAIGN_N + cid - 2] 
      ~ dbin( k[time]*ctr[cid], tries[time * CAMPAIGN_N + cid - 2])
  }
}

Is there a way I can create temp variable in jags?

回答1:

You'd need to let index vary with time and cid.

index[time,cid] <- time * CAMPAIGN_N + cid - 2
positives[index[time,cid]] ~ dbin( k[time]*ctr[cid], tries[index[time,cid]])


标签: jags