0

When I used rjags to build my model, I got the error "Compilation error on line 18. Unknown variable state Either supply values for this variable with the data or define it on the left hand side of a relation." What's the error with my code?

model_string <- "model {
  dur <- Time[1]
  state[1] ~ dcat(pi)
  for (i in 1:N-1) {
    Speed[i] ~ dnorm(mu_speed[state[i]], prec_speed[state[i]])
    Course[i] ~ dnorm(mu_course[state[i]], prec_course[state[i]])
    Rota[1,1] <- cos(Course[i])
    Rota[1,2] <- -sin(Course[i])
    Rota[2,1] <- sin(Course[i])
    Rota[2,2] <- cos(Course[i])
    # Rota <- matrix(c(cos(Course[i]), -sin(Course[i]), sin(Course[i]), cos(Course[i])),nrow = 2, byrow = TRUE)
    Rota_before <- Position[i,] / sqrt(sum(Position[i,]^2))
    Distance <- a[state[i]] * Speed[i] * Rota_before %*% Rota * Time[i]
    Position[i+1,] ~ dmnorm(Position[i,] + Distance, sigma_position)
    
    q <- 1 - qnorm(dur+Time[i+1], mu_time[state[i]], prec_time[state[i]])
    p <- c(q,q,q)
    p[state[i]] <- 1 - q
    p <- A[state[i],] * p / sum(A[state[i],] * p)
    state[i+1] ~ dcat(p)
    
    dur <- ifelse(state[i]==state[i+1], dur+Time[i+1], Time[i+1])
  }
  

  
  Speed[N] ~ dnorm(mu_speed[state[N]], prec_speed[state[N]])
  Course[N] ~ dnorm(mu_course[state[N]], prec_speed[state[N]])
  
  
  pi ~ ddirich(c(1,1,1))
  mu_speed[1] ~ dnorm(2, 1.0/100.0) T(0,)
  mu_speed[2] ~ dnorm(30, 1.0/100.0) T(mu_speed[1],)
  mu_speed[3] ~ dnorm(50, 1.0/100.0) T(mu_speed[2],)
  R[1,1] = 1
  R[1,2] = 0
  R[2,1] = 0
  R[2,2] = 1
  sigma_position ~ dwish(R,3)
  
  for (j in 1:3) {
    prec_speed[j] ~ dgamma(1.0/2.0, 1.0*1.0/2.0)
    Course[j] ~ dunif(0,2*pi)
    prec_course[j] ~ dgamma(1.0/2.0, 1.0*1.0/2.0)
    a[j] ~ dunif(1e-6, 1e-5)
    mu_time[j] ~ dnorm(100, 1000)
    prec_time[j] ~ dgamma(1.0/2.0, 1.0*1.0/2.0)
    A[j,1:3] ~ ddirich(c(50, 50, 50))
  }
}"

I really don't know where it went wrong. Does anybody could help me please? Thank you a lot!!

1 Answers1

0

You have this line in your code:

Rota <- matrix(c(cos(Course[i]), -sin(Course[i]), sin(Course[i]), 
               cos(Course[i])),nrow = 2, byrow = TRUE)

which is R syntax, not JAGS syntax (although lots of JAGS syntax resembles R syntax, it's actually a completely different language). It's been a while since I did any JAGS coding, but based on this question I think you probably want something like

Rota[1,1] <-  cos(Course[i])
Rota[1,2] <- -sin(Course[i])
Rota[2,1] <-  sin(Course[i])
Rota[2,2] <-  cos(Course[i])

JAGS should figure out that you want Rota to be a matrix, and what its dimensions are, automatically.

(There is an example on p. 11 of the JAGS user manual.)


With respect to

Unknown variable state Either supply values for this variable with the data or define it on the left hand side of a relation

I'm not quite sure why state isn't recognized ...

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks you a lot. But after fixing this error, I got another error "Compilation error on line 18. Unknown variable state Either supply values for this variable with the data or define it on the left hand side of a relation." – Fangcheng Han May 23 '22 at 01:24
  • This is turning into a [chameleon question](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions); if you keep updating your question to incorporate my answers and then ask about the *next* problem, this could go on for a long time ... – Ben Bolker May 23 '22 at 15:07