Question

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
 }
}
Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top