Inconsistent dimensionality provided for node

496 views
Skip to first unread message

Andy Greene

unread,
Jul 15, 2021, 3:51:33 PM7/15/21
to nimble-users
Hi all,

I am trying to modify my existing NIMBLE model to account for spatial differences in the magnitude of my covariates. In particular, I want to add a random effect component to my delta variables, which control the effect of the sin and cosine waves (denoted w) on the different covariates in X. Below is the existing code for my model (I highlighted the first instance of delta and w in blue):

DEcode <- nimbleCode({
  #Drought Variable
  for(i in 1:I){
    for(t in 1:J){
      Y[i,t] ~ dinterval(Z[i,t], cut[])
    }
  }
  #Latent Gaussian Variable - includes ICAR U
  for(i in 1:I){
    Z[i,1] ~ dnorm(intercept + inprod(X[i,1,],beta[]) + U[i], tau = tau.z)
    for(t in 2:J){
      Z[i,t] ~ dnorm(intercept + inprod(X[i,t,],beta[]) + U[i] +
                       rho.z[i]*(Z[i,(t-1)] - (intercept + inprod(X[i,t,],beta[]) + U[i])), tau = tau.z)
    }
  }
  
  # ICAR prior for the spatial random effects
  U[1:I] ~ dcar_normal(adj[], weights[], num[], tau.U, zero_mean=1)
  
  #Covariate Models
  for(i in 1:I){
    # U[i] <- 0
    for(c in 1:C) {
      X[i,1,c] ~ dnorm(G[c,i] + delta[c,1]*w[1,1] + delta[c,2]*w[2,1], tau = tau.x[c])
    }
    for(t in 2:J){
      for(c in 1:C) {
        X[i,t,c] ~ dnorm(G[c,i] + delta[c,1]*w[1,t] + delta[c,2]*w[2,t] + rho.x[c]*(X[i,(t-1),c] - (G[c,i] + delta[c,1]*w[1,(t-1)] + delta[c,2]*w[2,(t-1)])), tau = tau.x[c])
      }
    }
  }
  #Random Effects
  for(i in 1:I){
    for(r in 1:3){G[r,i] ~ dnorm(0, tau = tau.G[r])}
  }
  for(i in 1:I){rho.z[i] ~ T(dnorm(rho.mean, tau = tau.rho),-1,1)}
  
  #Priors
  for(r in 1:3){
    tau.G[r] ~ dgamma(0.01, 0.01)
    tau.x[r] ~ dgamma(0.01, 0.01)
  }
  tau.rho ~ dgamma(0.01, 0.01)
  tau.z ~ dgamma(0.01, 0.01)
  tau.U ~ dgamma(0.01, 0.01)
  
  for(i in 1:3){rho.x[i] ~ dunif(-1,1)}
  rho.mean ~ dunif(-1,1)
  for(i in 1:3){
    delta[i,1] ~ dnorm(0, tau = 0.01)
    delta[i,2] ~ dnorm(0, tau = 0.01)
  }
  
  intercept ~ dnorm(0, tau = 0.01)
  for(i in 1:3){beta[i] ~ dnorm(0, tau = 0.01)}
  for(i in 1:4){d[i] ~ dnorm(0, tau = 0.01)}
  
  cut[1:5] <- c(0, rep(exp(d[1]), 4)) + c(0, 0, rep(exp(d[2]), 3)) + c(0, 0, 0, rep(exp(d[3]), 2)) + c(0,0,0, 0,exp(d[4]))
  
  #Predict Covariates, Generate Z's, Predict Drought
  for(i in 1:I){
    for(c in 1:C){
      X.p[i,1,c] ~ dnorm(G[c,i] + delta[c,1]*w[1,(J+1)] + delta[c,2]*w[1,(J+1)] + rho.x[c]*(X[i,J,c] - (G[c,i] + delta[c,1]*w[1,J] + delta[c,2]*w[2,J])), tau = tau.x[c])
    }
    Z.p[i,1] ~ dnorm(intercept + inprod(X.p[i,1,],beta[]) + U[i] + rho.z[i]*(Z[i,J] - (intercept + inprod(X[i,J,],beta[]) + U[i])), tau = tau.z)
    Y.p[i,1] <- 0 + 1*(Z.p[i,1]>cut[1]) + 1*(Z.p[i,1]>cut[2]) + 1*(Z.p[i,1]>cut[3]) + 1*(Z.p[i,1]>cut[4]) + 1*(Z.p[i,1]>cut[5])
    
    for(t in 2:p){
      for(c in 1:C){
        X.p[i,t,c] ~ dnorm(G[c,i] + delta[c,1]*w[1,(J+t)] + delta[c,2]*w[2,(J+t)]  + rho.x[c]*(X.p[i,(t-1),c] - (G[c,i] + delta[c,1]*w[1,(J+t-1)] + delta[c,2]*w[2,(J+t-1)])), tau = tau.x[c])
      }
      Z.p[i,t] ~ dnorm(intercept + inprod(X.p[i,t,],beta[]) + U[i] + rho.z[i]*(Z.p[i,(t-1)] - (intercept + inprod(X.p[i,(t-1),],beta[]) + U[i])), tau = tau.z)
      Y.p[i,t] <- 0 + 1*(Z.p[i,t]>cut[1]) + 1*(Z.p[i,t]>cut[2]) + 1*(Z.p[i,t]>cut[3]) + 1*(Z.p[i,t]>cut[4]) + 1*(Z.p[i,t]>cut[5])
    }
  }
})

I initialize delta with the following:
delta = matrix(0, 3, 2) # 3 covariates, 2 columns (sin & cos)

I changed the initialization to the following:
delta = array(data = 0, dim = c(3, 2, I)) # 3 covariates, 2 columns, 'I' different grid cells

This would allow each grid cell to have their own coefficient for each covariate and each sin/cos wave.

I made the following changes to the model (again the first instance of delta and w are highlighted in blue and the prior is highlighted in red):

DEcode <- nimbleCode({
  #Drought Variable
  for(i in 1:I){
    for(t in 1:J){
      Y[i,t] ~ dinterval(Z[i,t], cut[])
    }
  }
  #Latent Gaussian Variable - includes ICAR U
  for(i in 1:I){
    Z[i,1] ~ dnorm(intercept + inprod(X[i,1,],beta[]) + U[i], tau = tau.z)
    for(t in 2:J){
      Z[i,t] ~ dnorm(intercept + inprod(X[i,t,],beta[]) + U[i] +
                       rho.z[i]*(Z[i,(t-1)] - (intercept + inprod(X[i,t,],beta[]) + U[i])), tau = tau.z)
    }
  }
  
  # ICAR prior for the spatial random effects
  U[1:I] ~ dcar_normal(adj[], weights[], num[], tau.U, zero_mean=1)
  
  #Covariate Models
  for(i in 1:I){
    # U[i] <- 0
    for(c in 1:C) {
      X[i,1,c] ~ dnorm(G[c,i] + delta[c,1,i]*w[1,1] + delta[c,2,i]*w[2,1], tau = tau.x[c])
    }
    for(t in 2:J){
      for(c in 1:C) {
        X[i,t,c] ~ dnorm(G[c,i] + delta[c,1,i]*w[1,t] + delta[c,2,i]*w[2,t] + rho.x[c]*(X[i,(t-1),c] - (G[c,i] + delta[c,1]*w[1,(t-1),i] + delta[c,2]*w[2,(t-1),i])), tau = tau.x[c])
      }
    }
  }
  #Random Effects
  for(i in 1:I){
    for(r in 1:3){G[r,i] ~ dnorm(0, tau = tau.G[r])}
  }
  for(i in 1:I){rho.z[i] ~ T(dnorm(rho.mean, tau = tau.rho),-1,1)}
  
  #Priors
  for(r in 1:3){
    tau.G[r] ~ dgamma(0.01, 0.01)
    tau.x[r] ~ dgamma(0.01, 0.01)
  }
  tau.rho ~ dgamma(0.01, 0.01)
  tau.z ~ dgamma(0.01, 0.01)
  tau.U ~ dgamma(0.01, 0.01)
  
  for(i in 1:3){rho.x[i] ~ dunif(-1,1)}
  rho.mean ~ dunif(-1,1)
  for(i in 1:I) {
    for(c in 1:C){
      delta[c,1,i] ~ dnorm(0, tau = 0.01)
      delta[c,2,i] ~ dnorm(0, tau = 0.01)
    }
  }
  intercept ~ dnorm(0, tau = 0.01)
  for(i in 1:3){beta[i] ~ dnorm(0, tau = 0.01)}
  for(i in 1:4){d[i] ~ dnorm(0, tau = 0.01)}
  
  cut[1:5] <- c(0, rep(exp(d[1]), 4)) + c(0, 0, rep(exp(d[2]), 3)) + c(0, 0, 0, rep(exp(d[3]), 2)) + c(0,0,0, 0,exp(d[4]))
  
  #Predict Covariates, Generate Z's, Predict Drought
  for(i in 1:I){
    for(c in 1:C){
      X.p[i,1,c] ~ dnorm(G[c,i] + delta[c,1,i]*w[1,(J+1)] + delta[c,2,i]*w[1,(J+1)] + rho.x[c]*(X[i,J,c] - (G[c,i] + delta[c,1,i]*w[1,J] + delta[c,2,i]*w[2,J])), tau = tau.x[c])
    }
    Z.p[i,1] ~ dnorm(intercept + inprod(X.p[i,1,],beta[]) + U[i] + rho.z[i]*(Z[i,J] - (intercept + inprod(X[i,J,],beta[]) + U[i])), tau = tau.z)
    Y.p[i,1] <- 0 + 1*(Z.p[i,1]>cut[1]) + 1*(Z.p[i,1]>cut[2]) + 1*(Z.p[i,1]>cut[3]) + 1*(Z.p[i,1]>cut[4]) + 1*(Z.p[i,1]>cut[5])
    
    for(t in 2:p){
      for(c in 1:C){
        X.p[i,t,c] ~ dnorm(G[c,i] + delta[c,1,i]*w[1,(J+t)] + delta[c,2,i]*w[2,(J+t)]  + rho.x[c]*(X.p[i,(t-1),c] - (G[c,i] + delta[c,1,i]*w[1,(J+t-1)] + delta[c,2,i]*w[2,(J+t-1)])), tau = tau.x[c])
      }
      Z.p[i,t] ~ dnorm(intercept + inprod(X.p[i,t,],beta[]) + U[i] + rho.z[i]*(Z.p[i,(t-1)] - (intercept + inprod(X.p[i,(t-1),],beta[]) + U[i])), tau = tau.z)
      Y.p[i,t] <- 0 + 1*(Z.p[i,t]>cut[1]) + 1*(Z.p[i,t]>cut[2]) + 1*(Z.p[i,t]>cut[3]) + 1*(Z.p[i,t]>cut[4]) + 1*(Z.p[i,t]>cut[5])
    }
  }
})

I try to build this new model with the following command:

DEmodel <- nimbleModel(DEcode,
                       constants = DEconstants, dimensions = list(cut = 5, X = c(I,J,C+1), beta = 4, 
                       X.p = c(I,p,C+1)))

But I am prompted with this error message:
Error in addMissingIndexingRecurse(code[[i]], dimensionsList) : 
  inconsistent dimensionality provided for node 'w'

I don't see how I am providing inconsistent dimensionality for w. I have changed the object that I am multiplying w by, but I haven't made any changes to w itself.

Kind regards,
Andy Greene

Perry de Valpine

unread,
Jul 15, 2021, 4:07:06 PM7/15/21
to Andy Greene, nimble-users
Hi Andy,

Thanks for the thorough and clear explanation of what you're hitting.  I suspect the problem is in this line:

X[i,t,c] ~ dnorm(G[c,i] + delta[c,1,i]*w[1,t] + delta[c,2,i]*w[2,t] + rho.x[c]*(X[i,(t-1),c] - (G[c,i] + delta[c,1]*w[1,(t-1),i] + delta[c,2]*w[2,(t-1),i])), tau = tau.x[c])

I see here, in the last two terms of the mean, delta with 2 indices and w with 3 indices.  I am wondering if it is possible that you meant to insert the third index, 'i', into the delta's rather than the w's?

-Perry


--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/4b81b393-71f2-44ec-bbb5-acdc3ce4f79dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages