Missing values in Nimble

1,642 views
Skip to first unread message

Cindy ma

unread,
Mar 23, 2018, 9:54:57 PM3/23/18
to nimble-users
Hi all,

I am dealing with missing values, and follow the instructions in the user manual, simple regressions can be done successfully. But when I do something more complex, I get some errors. For example, here's a Nimble code:

thinData <- list(y=slope,x1=forest,x2=herb,x3=method,SE2=SE2,n=n)     # only "n" has missing values
thinConst <- list(N=N)
thinInits <- list(mu_y=1,var_tau=1,beta=c(1,1,1))
thinCode <- nimbleCode({
  for (i in 1:N){
    y[i] ~ dnorm(mu_y, var_y[i])
    var_y[i] <- var_tau + var_epsilon[i]
    a[i] <- (n[i]-1)/2
    b[i] <- n[i]*(n[i]-1)/(2*var_epsilon[i])
    SE2[i] ~ dgamma(a[i],b[i])
    var_epsilon[i] ~ dinvgamma(0.001,0.001)
    n[i] ~ dpois(lam[i])
    log(lam[i]) <- beta[1]+beta[2]*x1[i]+beta[3]*x2[i]+beta[3]*x3[i]
  }
  mu_y ~ dnorm(0,0.001)
  var_tau ~ dinvgamma(0.001,0.001)
  beta[1:3] ~ dnorm(0,0.001)
})

When I run the code by:

mcmc.out <- nimbleMCMC(code=thinCode,constants=thinConst,data=thinData,inits=thinInits,nchains=2, niter=10000,summary=TRUE,WAIC=TRUE)

I get message: 
"NAs were detected in model variables: var_epsilon, logProb_var_epsilon, var_y, lifted_d1_over_sqrt_oPvar_y_oBi_cB_cP_L2, n, logProb_n, logProb_y, b, a, lifted_d1_over_b_oBi_cB_L6, logProb_SE2.";
warnings like: 
"warning: problem initializing stochastic node var_epsilon[1], logProb is NA or NaN";
and get an error message:
"Error in quantile.default(x, 0.025) : 
  missing values and NaN's not allowed if 'na.rm' is FALSE"

Could you please help me with that? How I can deal with the NA values and get rid of the error message?

Thank you so much!

Best,
Cindy

Perry de Valpine

unread,
Mar 24, 2018, 10:18:33 AM3/24/18
to Cindy ma, nimble-users
The code
beta[1:3] ~ dnorm(0,0.001)
is not valid.  It should be
for(i in 1:3)
  beta[i] ~ dnorm(0, 0.001)


With that change, it looks like you are getting bad initial values for var_epsilon by relying on simulation.  This seems to sometimes generate Inf and other times huge values, which the MCMC never recovers from.  The quantile.default error is coming from the WAIC processing, I think.  On my system it is resolved by putting reasonable initial values in var_epsilon.

In general, a good way to diagnose issues in NIMBLE is to look at the model and its values after step-by-step creation of MCMC. For example:

## build the model
thinModel <- nimbleModel(
    thinCode,
    data = thinData,
    constants = thinConst,
    inits = thinInits)

## build the MCMC
thinMCMC <- buildMCMC(thinModel)

## run uncompiled to have R errors and debugging available
thinMCMC$run(1)

## I get:
## warning: problem initializing stochastic node var_epsilon[1], logProb is NA or NaN
## Warning message:
## In rnorm(1, mean = (prior_mean * prior_tau + contribution_mean)/(prior_tau +  :
##  NAs produced

## This suggests a problem triggered by something wrong with var_epsilon, so I will look at its value in the model:

thinModel$var_epsilon

## This reveals that var_epsilon contains one Inf and two crazy values, so I suggest putting reasonable values in inits.

-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+unsubscribe@googlegroups.com.
To post to this group, send email to nimble...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/cbe4ae3e-93bc-4604-a0f3-51ee72696f27%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages