Question about example indexing for compiler.

19 views
Skip to first unread message

Richard Golden

unread,
Jul 25, 2025, 4:56:37 PMJul 25
to nimble-users
Here is a phenomena that I would like to understand better.
The code in these two files compiles and executes fine
for both MCMC and MCEM.

When I delete the code on lines 86-103 in "New12runZCDM.R"
 the program bombs during "defining the model..."
with following warning:
[WARNING] Detected backwards indexing in 1:0

My theory is that the compiler had trouble interpreting the indexing
in my code and was worried that "nrparents[k]" could be zero but the
example code shows the compiler how things should work.

Any explanations? Any suggestions on writing NIMBLE code which is
more robust?
New12ZCDMmodelcode.R
New12runZCDM.R

Chris Paciorek

unread,
Jul 29, 2025, 4:18:00 PMJul 29
to Richard Golden, nimble-users
hi Richard,

I'm not really following your question, but let me make a few comments that might be relevant and we can go from there.

- When a nimble model is built (even before getting to compilation to a C++ version of the model), we must be able to fully resolve the extent of indexing in for loops so as to set up the (static model) graph structure. And if we determine that there is backward indexing, we should produce a warning and generally a subsequent error caused by the backward indexing.
- In your code specifically, when you use `nrparents[k]` as a index itself or as part of an index sequence it should never be 0 for any value of `k`.
- You can only use `if` in nimble model code as described here in the manual to be able to define multiple different models from the same code. You can't use `if` for internal logic in a nimble model, so I think you'll need to rework your model structure to remove that `if`.
- Your block of code in lines 88-103 modifies `parentnodes` AFTER it has been used to create `CDMInits`, which is then used as an input to `nimbleModel()`. So I don't see how that block of code could affect what nimble is doing. There might be something weird going on with variable scoping, as we've had some cases in the past where we weren't as careful as we should have been in preventing nimble processing from using variables from the R global environment.

-chris

--
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 visit https://groups.google.com/d/msgid/nimble-users/fb050307-af65-442f-8ca9-ae63969b7899n%40googlegroups.com.

Richard Golden

unread,
Jul 31, 2025, 12:33:40 PMJul 31
to paci...@stat.berkeley.edu, nimble-users
Hi..thanks for the feedback. Following your suggestions, I figured out how to do the code without the IF statement in the nimble logic
The revised version of the code with the correct initialization seems to resolve the "flaky performance issues". Thank you for
the feedback which was very helpful. 

Richard Golden, Ph.D., M.S.E.E., B.S.E.E.

Reply all
Reply to author
Forward
0 new messages