Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Cannot evaluate subset expression

sophie
2012-01-24
2012-09-01
  • sophie
    sophie
    2012-01-24

    Hi, anybody can help me with this? can JAGS solve the inverse of a matrix in
    the 3-way array? Thank you!

    for(i in 1:n){

    for(a in 1:t_){

    for(b in 1:t_){

    sigma<-pow(rho,t-t**)

    }

    }

    sigmainverse<-inverse(sigma) # this is where jags got error

    }**__

     
  • Martyn Plummer
    Martyn Plummer
    2012-01-24

    The forum software ate your subscripts. Here is your model with correct
    formatting.

    for(j in 1:n){
        for(a in 1:t[j]){
          for(b in 1:t[j]){
            sigma[j,a,c]<-pow(rho,t[a]-t[c])
          }
        }
        sigmainverse[j,,]<-inverse(sigma[j,,]) # this is where jags got error
      }
    

    You don't say what the error message is, but I'm guessing it has something to
    do with the compiler not being able to work out the dimensions of sigma or
    sigmainverse.

    You could try this

    for(j in 1:n){
        for(a in 1:t[j]){
          for(b in 1:t[j]){
            sigma[j,a,c]<-pow(rho,t[a]-t[c])
          }
        }
        sigmainverse[j,1:t[j], 1:t[j]]<-inverse(sigma[j, 1:t[j], 1:t[j]])
      }
    

    and take care not to reference any elements of sigma or sigmainverse that you
    have not defined.

     
  • sophie
    sophie
    2012-01-24

    Thank you very much for your reply. That is the error. I tried your code, it
    still doesn't work. I am new to JAGS. I guess maybe the JAGS doesn't solve the
    inverse for a matrix in a 3-way array? As my sigma is 3-way array. Any ideas?

     
  • sophie
    sophie
    2012-01-24

    Even I tried determinant_<-logdet(sigma[i, 1:t_,1:t_]), JAGS gives me "Non-
    conforming parameters in function logdet". I feel like JAGS can not do 3-way
    array calculation?___

     
  • Martyn Plummer
    Martyn Plummer
    2012-01-25

    No, you have the opposite problem. The inverse function does not accept
    scalars.

    You really haven't given enough information to solve the problem, but I'm
    guessing that you have t=1 for some j (presumably j=1 and t is an increasing
    sequence). In this case "sigma[j, 1:t, 1:t]" is a scalar quantity, not a
    matrix (JAGS follows S in dropping redundant dimensions when you take subsets
    of arrays). Currently the inverse and logdet functions do not work on scalars.
    I have fixed this for the next release, but in the meantime you are going to
    have to special case t = 1 by taking it outside the loop, e.g.

    sigmainverse[1,1,1] <- 1/sigma[1,1,1]
    for(j in 2:n) {
       for(a in 1:t[j]) {
          for(b in 1:t[j]){
             sigma[j,a,c]<-pow(rho, abs(t[a]-t[c]))
          } 
      } 
      sigmainverse[j,1:t[j], 1:t[j]]<-inverse(sigma[j, 1:t[j], 1:t[j]]) 
    }
    

    Note also that in order to get a symmetric matrix (required by inverse) you
    need to use abs(t - t) .

     
  • sophie
    sophie
    2012-01-25

    Oh, I think you are right. But sometimes t_=1 (i is not 1, it could be
    random). how can I specify that? I don't think the "if else" can work here.
    Thank you again._

     
  • Martyn Plummer
    Martyn Plummer
    2012-01-31

    If t is random then you cannot use 1:t as an index on the left hand side of a
    relation. All quantities in JAGS have to have fixed dimension.