1

I'm trying to figure out how to estimate a changepoint in my data, and to do so I would like to estimate random effects for the period prior to the changepoint and then for the period after the changepoint. However, I do not know when the changepoint should be, so I'm trying to estimate it. The problem I'm running into is that I am using the changepoint (cp) in my looping structure, and I am getting an error 'Cannot evaluate upper index of counter i'. I am guessing that I cannot loop using a variable I also want to estimate? I'm wondering if anyone has done this and/or has a suggestion on how to do this. Another potential problem is that I'm not using the counter index i in my commands, however, when I tried using a while loop I also ran into problems of the loop not breaking. Thank you in advance for any thoughts!

subset of the code:

    cp ~ dunif(3,51)

for(i in 1:(cp-1)){
  for(j in 1:nsite){
    b[j] ~ dnorm(0,tau.site)
 }
}

for(i in cp:nyear){
  for(j in 1:nsite){
    b1[j] ~ dnorm(0,tau.site1) # random site effects
 }
}
3
  • It looks like your defining (~) b[j] multiple times in your loops ... each new iteration of i tries to redefine the elements of b. Commented May 6, 2014 at 19:30
  • yes, you are right. I suppose I need something like - while i= 1:cp do something, and while i>cp do something else. Commented May 7, 2014 at 20:04
  • I don't think jags uses much control flow (while, if). There is an ifelse, but other than that, no control flow, I don't think. Edit: see this link sourceforge.net/p/mcmc-jags/discussion/610037/thread/1abc09f3/… Commented May 7, 2014 at 20:12

1 Answer 1

1

Thank you for the comments. What I have found, which solves the simplest form of this problem (as far as I can tell) is as follows:

model {
changeyear ~ dunif(1,N)

  for(j in 1:nsite){
b[j] ~ dnorm(0,tau.site)
b1[j] ~ dnorm(0, tau.site1)
}
}

# Note priors for tau.site and tau.site1 are not shown
for (i in 1:nyear){
  for(j in 1:nsite){
  y[i,j] <- b[j] * step(i-changeyear) + b1[j] * step(changeyear-i)
}}

Where y[i,j] is the expected value from my model. The step function works as an indicator function such that is the command evaluates to a non-negative value then 1, otherwise 0. So far, it seems that the changeyear is estimated as the mid point of my time series - but that is likely just a model issue to overcome not a syntax one. Thank you again for your thoughts!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.