Error in genVarInfo3()

275 views
Skip to first unread message

Suzy Khaled

unread,
Sep 21, 2022, 12:07:34 AM9/21/22
to nimble-users
Hi all,
can you please help to solve the issue in my code:

SLMM <- nimbleCode({
  for (i in 1:S) {
    y[1:N, i] ~ dmnorm(mu[1:N, i],  tau_y0[1:N, 1:N, i])
    mu[1:N, i] <- b[i, 1] * x1[1:N] + b[i, 2] * x2[1:N] + b[i, 3] *
      x3[1:N] + b[i, 4] * x4[1:N] + b[i, 5] * x5[1:N]
    b[i, 1:5] <- bm[latent[i],1:5]
    latent[i] ~ dcat(pi[i,M])
  }
 
  for (i in 1:S) {
    tau_y0[1:N, 1:N, i] <- diag(psi_y[i] * exp(-Dist[1:N, i]/lambda))
    psi_y[i] ~ dgamma(1, 1)}
  lambda ~ dunif(0, D)
 
  for (k in 1:M) {
    bm[k, 1:5] ~ dmnorm(mu_bm[1:5], cov = var_bm[1:5, 1:5])
  }
  var_bm[1:5, 1:5] <- 1/tau_bm * diag(rep(1, 5))
  tau_bm ~ dgamma(1, 1)
  for (i in 1:5) {
    mu_bm[i] ~ dnorm(0, 1)
  }
 
  for (i in 1:S) {
    pi[i,1] <- vs[i, 1]
    for (j in 2 : M){ pi[i, j ] <- vs[i, j ]*prod(vsout[i, 1 : ( j - 1)])
  }}
 
   for(j in 1 : (M - 1)){
      V[ j] ~ dbeta(av, bv)
   
  for (i in 1:S) {
   
      weight[i,j] <- exp(-Dist[i, j]/phi)
  }
 
  for (i in 1:S){
 
  vs[i,j]<- weight[i,j]*V[j]
  vsout<- 1-vs[i,j]
  }}
 
 
   av ~ dlnorm(0, 10)
    bv ~ dlnorm(0, 10)
    lambda ~ dunif(0, D)
})



my_points <- matrix(c(data$Longitude,    
                      data$Latitude),ncol=2)

colnames(my_points) <- c("longitude", "latitude")

my_points

library(rdist)
df <- my_points
dist_mat <- pdist(df)
dist_1 <- pdist(df[, 1])
dist_2 <- pdist(df[, 2])
# product distance matrix
dist_prod <- product_metric(dist_1, dist_2)
# check equality
all.equal(dist_mat, dist_prod)
dist_mat_2<- (dist_prod )

Data <- list(y = y, x1 = data$x1, x2 = data$x2, x3 = data$x3, x4 =data$x4,
                x5 = rdata$x5,
                Dist = dist_mat_2)

Const <- list(S = 526, M = 50, N = 526, D = 50)

Inits <- list(psi_y = rep(1, Const$S), lambda = 10,
                 latent = rep(1,Const$S), alpha = 2,
                 tau_bm = 1,phi=1,
                 mu_bm = rnorm(5),
                 vlatent = rbeta(Const$M - 1, 1, 1))

mcmc.out <- nimbleMCMC(code = SLMM, data = Data, constants = Const,
                       inits = Inits, thin = 1, niter = 5000, nchains = 1, nburnin = 2000,
                       monitors = c("b","latent", "lambda"), setSeed = TRUE)

Defining model
Error in genVarInfo3() :
  Dimension of vsout is 0, which does not match its usage in 'pi[i, j] <- vs[i, j] * prod(vsout[i, 1:(j - 1)])'.

Daniel Turek

unread,
Sep 21, 2022, 7:00:35 AM9/21/22
to Suzy Khaled, nimble-users
Suzy, good question.  The warning is trying to tell you that the dimension of the "vsout" variable is 0, which means "vsout" is declared as a scalar quantity.  That's the case, in your model code vsout is declared as:

  vsout<- 1-vs[i,j]

By virtue of the left-hand-side having no square brackets or indexing, "vsout" is declared as a scalar quantity, but the fact that this declaration appears within loops over i and j means there are also repeated declarations of this scalar - which isn't ok either, to declare the scalar "vsout" more than once.  One guess is that you're missing [i,j] indexing (or indexing similar to that,) on the declaration of "vsout", which should instead be:

  vsout[i,j] <- 1-vs[i,j]

This would be consistent with the usage of "vsout" elsewhere in the model:

    for (j in 2 : M){ pi[i, j ] <- vs[i, j ]*prod(vsout[i, 1 : ( j - 1)])

where it's a 2-dimensional variable (an array of values), where the first index ranges from 1:S, and the second index goes up to (M-1).

Take a look, and think about if the proposed change (or something similar) is what you intended.

Cheers, keep at it,
Daniel


--
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/1d701315-053a-4396-86bf-291430f5b27en%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages