Menu

Dimension mismatch in subset expression

Help
O.mykiss
2015-07-03
2015-07-06
  • O.mykiss

    O.mykiss - 2015-07-03

    I am fairly new to RJAGS and I am crawling my way through writing model code and data formatting to perform some hierarchical modeling but I have hit some road bumps. I am trying to run the model

    model <- function() {
      for(i in 1:z){
        for(j in 1:y){
          m[i,j] ~ dbin(p[i,j],n[i,j]) 
          logit(p[i,j]) <- etaP[i,j]
          etaP[i,j] ~ dnorm(etaP1[i],tauP)
    
          u[i,j] ~ dbin(p[i,j],U[i,j])
          U[i,j] <- round(exp(etaU[i,j])) 
          etaU[i,j]~ dnorm(etaU1[i],tauU)
    
        }
        etaP1[i] ~ dnorm(np,tp)
    
        etaU1[i] ~ dnorm(nu,tu)
      }
      tauP ~ dgamma(.001,.001)
      tauU ~ dgamma(.001,.001)
    
      np ~ dnorm(-2,1.22)
      tp ~ dgamma(.001,.001)
    
      nu ~ dnorm(-2,1.22)
      tu ~ dgamma(.001,.001)
    }
    
    ##########Example data
    DATA
    z = 20
    y = 10
    m = vector of 200 integers
    n = vector of 200 integers
    u = vector of 200 integers
    

    But I receive the error

    "Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
      RUNTIME ERROR:
    Compilation error on line 5.
    Dimension mismatch in subset expression of m"
    

    I have a hunch it has to do with my data classification more than my model (though my model might be off as well) but I am not sure. Any help would be hugely appreciated!

     

    Last edit: Martyn Plummer 2015-07-03
  • Martyn Plummer

    Martyn Plummer - 2015-07-03

    In your model, m, n, and u are matrices with z rows and y columns. When you supply them as data they should also be matrices with matching dimensions (i.e. z=20 rows and y=10 columns).

     
  • O.mykiss

    O.mykiss - 2015-07-03

    Thanks a ton Martyn. I reformatted the data for n, m, & u into 20x10 matrices and it cleared up the dimension mismatch. Now onto model errors (I haven't changed the model code from the original post);

    Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
      Error in node u[1,1]
    Observed node inconsistent with unobserved parents at initialization.
    Try setting appropriate initial values.
    

    I don't have any initial values set because I haven't needed them with my simpler models. I suspect the error has more to do with the model structure than the initial values but I am might be wrong.

     
  • Martyn Plummer

    Martyn Plummer - 2015-07-03

    I think this is straightforward: The unobserved size parameter U[i,j] must be at least as big as the observed count u[i,j] so set initial values for U[i,j] accordingly.

     
  • O.mykiss

    O.mykiss - 2015-07-04

    I haven't had to set initial values before so I tried

    model.inits <- function(){
        list("p" = runif(z*y, 0.1, 0.7) , "U" = round(runif(z*y, 2000, 4000))) 
    }
    

    and received

    Error in setParameters(init.values[[i]], i) : RUNTIME ERROR:
    Dimension mismatch in values supplied for U
    

    This should probably be straightforward but I am a JAGS novice and could use some advice.

    Also, thanks again for your help so far Martyn.

     
  • Martyn Plummer

    Martyn Plummer - 2015-07-06

    It's the same issue as with the data. The variable U is a z by y matrix, so when you supply initial values from R, these have to be in the form of a matrix with the same dimensions. Currently you are supplying a vector.

    Note that you cannot supply initial values for p because it is not a random variable. In your model, p is calculated deterministically from etaP.

    logit(p[i,j]) <- etaP[i,j]
    

    You can supply initial values for etaP however, since this is a random variable:

    etaU[i,j]~ dnorm(etaU1[i],tauU)
    
     

Log in to post a comment.