Compiling error with LHSnodes

18 views
Skip to first unread message

Fanny Empacher

unread,
Jan 26, 2021, 7:41:17 AM1/26/21
to nimble-users
Hi,

I'm quite new to nimble, but have tried to fit a few models and nimble really sped up computation time (a lot), so thank you!

Now I've run into an error that I don't understand when compiling:

Error in nimble:::nodeFunctionVector(model = model, nodeNames = LHSnodes,  : 
  In nodeFunctionVector from a case like model$doSomething(nodes[i]) where nodes may not contain well-defined node names.  Context is calculate(model = model, nodes = LHSnodes[i])

The uncompiled version runs fine (although too slow for any practical purposes). I'm hoping that this points to a very simple error that's easy to fix. If it helps I can also post my code, it's just a bit long.

Daniel Turek

unread,
Jan 26, 2021, 7:51:24 AM1/26/21
to Fanny Empacher, nimble-users
Fanny, thanks for your use and kind words about nimble.

We could try to speculate all day, perhaps something about looping over a vector of node names (for(i in 1:numberOfNodes) .... something using nodeNames[i] ....), but without your code, and most importantly a reproducible example of the problem, it's really difficult to say.  I know you mentioned your code is long, but is it possible to provide an example demonstrating the problem?

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/cb0189f0-6a46-4cfd-bc49-808c065ad99cn%40googlegroups.com.

Fanny Empacher

unread,
Jan 26, 2021, 8:28:50 AM1/26/21
to nimble-users
Hi Daniel,

Thanks for your quick reply! I can't spot anything where I'm looping over node names but I might be wrong. Here's my (almost unabridged) code:

totalT <- 5
theta_init <- c(0.3, 0.95, 0.9, 2.0e-04, 8, 120)
phi_p_init <- theta_init[1]
phi_a_init <- theta_init[2]
alpha_init <- theta_init[3]
beta_init <- theta_init[4]
rho_init <- theta_init[5]
tau_init <- theta_init[6]
y0 <- 500
y <-  c(668,649,834,747,903)

code <- nimbleCode({
  ##### Priors 
  phi_p ~ dbeta(2.87, 1.78)
  phi_a ~ dbeta(1.6, 1.2)
  alpha ~ dbeta(2, 1.5)
  beta ~ dgamma(2, 1/(3.7e-04/2))
  rho ~ dgamma(4, 1/2.5)
  tau ~ dgamma(2.1, 1/66.67)
  ##### Initialisation
  x_upd[, ] <- matrix(nrow = totalT+1, ncol = 2)
  x_pred[, ] <- matrix(nrow = totalT, ncol = 2)
  
  x0[1:2] <- y0*c(1, 1/alpha)
  sigma2 <- y0^2/tau
  P011 <- sigma2
  P022 <- 1/(alpha^2)*((1-alpha)*y0+sigma2)
  P012 <- 1/alpha*sigma2
  P021 <- 1/alpha*sigma2
  T12 <- alpha*phi_a
  T22 <- phi_a
  x_upd[1, 1:2] <- x0[1:2]
  P_upd11[1] <- P011
  P_upd12[1] <- P012
  P_upd21[1] <- P021
  P_upd22[1] <- P022
  for(t in 1:totalT){
    real_phi_p[t] <- phi_p/(1+(beta*x_upd[t, 1])^rho)
    T21[t] <- 0.5*real_phi_p[t]
    T11[t] <-  alpha*0.5*real_phi_p[t]
    Q11[t] <- alpha*(1-alpha)*(0.5*real_phi_p[t]*x_upd[t, 1]+phi_a*x_upd[t, 2])
    Q22[t] <- 0.5*real_phi_p[t]*(1-0.5*real_phi_p[t])*x_upd[t, 1] + phi_a*(1-phi_a)*x_upd[t, 2]
    Q12[t] <- alpha*(0.5*real_phi_p[t]*(1-0.5*real_phi_p[t])*x_upd[t, 1] + phi_a*(1-phi_a)*x_upd[t, 2])
    Q21[t] <- alpha*(0.5*real_phi_p[t]*(1-0.5*real_phi_p[t])*x_upd[t, 1] + phi_a*(1-phi_a)*x_upd[t, 2])
    R_matrix[t] <- x_upd[t, 1]^2/tau
    # Prediction
    x_pred[t, 1] <- T11[t]*x_upd[t, 1] + T12*x_upd[t, 2]
    x_pred[t, 2] <- T21[t]*x_upd[t, 1] + T22*x_upd[t, 2]
    P_pred11[t] <- (T11[t]*P_upd11[t]+T12*P_upd21[t])*T11[t]+(T11[t]*P_upd12[t]+T12*P_upd22[t])*T12+Q11[t]
    P_pred12[t] <- (T11[t]*P_upd11[t]+T12*P_upd21[t])*T21[t]+(T11[t]*P_upd12[t]+T12*P_upd22[t])*T22+Q12[t]
    P_pred21[t] <- (T21[t]*P_upd11[t]+T22*P_upd21[t])*T11[t]+(T21[t]*P_upd12[t]+T22*P_upd22[t])*T12+Q21[t]
    P_pred22[t] <- (T21[t]*P_upd11[t]+T22*P_upd21[t])*T21[t]+(T21[t]*P_upd12[t]+T22*P_upd22[t])*T22+Q22[t]
    # Aid variables
    y_pred[t] <- x_pred[t, 1]
    v_pred[t] <- y[t] - y_pred[t]
    F_matrix[t] <-P_pred11[t] + R_matrix[t]
    G1[t] <- P_pred11[t]
    G2[t] <- P_pred21[t]
    F_inv[t] <- F_matrix[t]^(-1)
    K1[t] <- G1[t]*F_inv[t]
    K2[t] <- G2[t]*F_inv[t]
    # Distribution of y
    y[t] ~ dnorm(y_pred[t], var = F_matrix[t])
    # Update
    x_upd[t+1, 1] <- x_pred[t, 1] + K1[t]*v_pred[t]
    x_upd[t+1, 2] <- x_pred[t, 2] + K2[t]*v_pred[t]
    P_upd11[t+1] <- P_pred11[t] - F_inv[t]*G1[t]^2
    P_upd12[t+1] <- P_pred12[t] - F_inv[t]*G1[t]*G2[t]
    P_upd21[t+1] <- P_pred21[t] - F_inv[t]*G1[t]*G2[t]
    P_upd22[t+1] <- P_pred22[t] - F_inv[t]*G2[t]^2
  }
})

mNimble_seal2 <- nimbleModel(code,
                             dimensions = list(x_upd = c(totalT+1,2),
                                               x_pred = c(totalT,2)),
                             data = list(y=y, y0 = y0),
                             constants = list(totalT = totalT),
                             inits = list(phi_p = phi_p_init,
                                          phi_a = phi_a_init,
                                          alpha = alpha_init,
                                          beta = beta_init,
                                          rho = rho_init,
                                          tau = tau_init),
                             check = FALSE)

compNimble_seal2 <- compileNimble(mNimble_seal2)
confNimble_seal2 <- configureMCMC(mNimble_seal2,
                                  print = TRUE, useConjugacy = FALSE)

mcmcNimble_seal2 <- buildMCMC(confNimble_seal2)

# This runs but slowly
# fNimble_seal2 <- runMCMC(mcmcNimble_seal2,
#                          nburnin = 10,
#                          nchains = 2,
#                          niter = 110)

# This is where the error happens
mcmccNimble_seal2 <- compileNimble(mcmcNimble_seal2,
                                   project = mNimble_seal2,
                                   resetFunctions = TRUE,
                                   showCompilerOutput = TRUE)

Daniel Turek

unread,
Jan 26, 2021, 9:58:52 AM1/26/21
to Fanny Empacher, nimble-users
Fanny, the issue is the inclusion of the matrix() declaration lines in your model code.  These are not necessary, and in fact are causing an issue down the line.  You should *not* include these two lines:

    x_upd[, ] <- matrix(nrow = totalT+1, ncol = 2)
    x_pred[, ] <- matrix(nrow = totalT, ncol = 2)


More troublesome, from the package development perspective, is how/why this slipped through model checking.  This should have been caught at the time of model building, and ideally issue a helpful message to correct the problem.

We'll look into that, improving the error-catching for this case, but for right now this should get your model and MCMC running.

Cheers,
Daniel


Fanny Empacher

unread,
Jan 26, 2021, 11:15:21 AM1/26/21
to nimble-users
Thank you, that fixed the problem!
Reply all
Reply to author
Forward
0 new messages