error when using wishart in nimble function

162 views
Skip to first unread message

Zhiduo Chen

unread,
Sep 28, 2022, 8:36:31 PM9/28/22
to nimble-users
Hi, all. When I use the wishart distribution as the prior of precision of multivariate normal, I always have the error like: Error in chol.default(model$item_den[1:2, 1:2]) :
  the leading minor of order 1 is not positive definite. And I cannot find problems. Below is my code. 

code=nimbleCode({
  for (n in 1:1000) {
    for (i in 1:30) {
      for (k in 1:7) {w[n, i, k] <- pow(attribute[n, k], Q[i, k])}
      logit(prob[n, i]) <- beta[i] + delta[i] * prod(w[n, i, 1:7])
      y[n, i] ~ dbern(prob[n, i])
    }}
  for (n in 1:1000) {
    for (k in 1:7) {
      logit(att_prob[n, k]) <- gamma[k] * theta[n] - lambda[k]
      attribute[n, k] ~ dbern(att_prob[n, k])
    }}
 
 
  for (n in 1:1000) {theta[n]~ dnorm(0,1)}
  for (i in 1:30) {
    item_parameter[i, 1:2] ~ dmnorm(item_mu[1:2], item_den[1:2, 1:2])
    beta[i] <- item_parameter[i, 1]
    delta[i] <- item_parameter[i, 2]
  }
  for (k in 1:7) {
    lambda[k] ~ dnorm(0, 0.25)#dnorm(0, 0.25)
    gamma[k] ~ T(dnorm(0, 0.25),0,)#T(dnorm(0, 0.25),0,)
  }
  item_mu[1] ~ dnorm(-2.197, 0.5)#item_mu[1] ~ dnorm(-2.197, 0.5)
  item_mu[2] ~ T(dnorm(4.394, 0.5),0,)#T(dnorm(4.394, 0.5),0,)
  R[1, 1] <- 1
  R[2, 2] <- 1
  R[1, 2] <- 0
  R[2, 1] <- 0
  item_den[1:2, 1:2] ~ dwish(R[1:2, 1:2], 2)
})


Thank you!

Daniel Turek

unread,
Sep 29, 2022, 6:32:13 AM9/29/22
to Zhiduo Chen, nimble-users
Zhiduo, one quick question, are you providing an initial value for the item_den variable?  Nimble does some reparameteriations of the dmnorm distribution parameters at the time of model building, and that involves taking the cholesky factorization of the precision matrix.  If you don't provide an initial value for item_den, then it attempts to find the cholesky factor of a matrix of NA's, which gives the error that you're seeing.  Try also providing an initial values list to the nimbleModel call:

inits <- list(item_den = diag(2))

Then when you call nimbleModel:

Rmodel <- nimbleModel(code, inits = inits, ....)

Try that out, and see if it avoids this error.

Cheers,
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/431671e1-9f06-4ff8-ab73-5a7f73690a00n%40googlegroups.com.

Zhiduo Chen

unread,
Sep 29, 2022, 1:28:44 PM9/29/22
to nimble-users
Hi, Dan. I just tried the  inits <- list(item_den = diag(2)), and it worked! I am also wondering that if I want to give other initials like beta and zeta, will they influence the final result (estimation accuracy)? And for the sampler, if I need to change to use the " RW_wishart"? By the way, if you have any suggestions to improve the estimation accuracy?  THank you for quick response!

Daniel Turek

unread,
Sep 30, 2022, 10:16:46 AM9/30/22
to Zhiduo Chen, nimble-users
Great, I'm glad that fixed the error for you.

Providing an initial value for beta won't change anything, because beta is a deterministic node (defined in the model using <-), so its initial value will be calculated using other values in the model (in this case, using item_parameter).  And as for zeta, I don't see the variable zeta anywhere in your model code, so perhaps that was a typo.  However, providing initial values for other non-data stochastic nodes (for example, lambda, gamma, or item_mu) would have an effect on the results.  Depending on the posterior distribution, initial values might help the MCMC chains converge faster, but it depends on the distributions and initial values you might choose, so nothing can be said for certain.  In general, it's recommended that you try using a variety of different initial values, and inspecting the resulting chains to assess convergence.

No, you do not need to manually assign a RW_wishart sampler for item_den.  In this case, it looks like the item_den variable appears in a conjugate relationship, with a Wishart prior and being used as the precision matrix of a dmnorm distribution.  So nimble should recognize this conjugacy, and automatically assign a conjugate sampler for item_den, which will operate more efficiently than the RW_wishart sampler (which is only applied for non-conjugate Wishart distributions).  Do you see, in the listing of samplers, a "conjugate" sampler being applied to the item_den variable?

Offhand, it's difficult to predict what will affect estimation accuracy of any model, so I'm sorry but I have no suggestions offhand.

Thanks for your questions,
Daniel




Reply all
Reply to author
Forward
0 new messages