non-consecutive indices for different parts of a model

227 views
Skip to first unread message

Glenn Stauffer

unread,
Dec 19, 2018, 4:53:28 PM12/19/18
to nimble-users
I am trying to write a model where I want to predict for some missing data (sample units are interspersed with those that do have data).
So I want to do something like:

for(i in indices1){
... code for non-missing data...
}

for(i in indices2){
... code for missing data...
}

where indices1 and indices2 index cells with and without observation data, e.g., indices1=c(2:10,12,14) and indices2=c(1,11,13). When I do this, I get the following error and warning:

defining model...
Error in replaceConstantsRecurse(x, constEnv, constNames) :
  Error, hit end
In addition: Warning message:
In replaceConstantsRecurse(x, constEnv, constNames) :
  Code indices1 was given as known but evaluates to a non-scalar.  This is probably not what you want.

when I provide index values for indices1, etc, like

for(i in indices1[1:n_indices1]{

then I get the following error:

defining model...
Error in parse(text = paste0("(", split.code[[1]][1], "):(", split.code[[1]][2],  :
  <text>:1:17: unexpected ')'
1: (indices1[1)
               ^

So my question is this - is it possible to index a model the way I am trying to do it, or do I need to rearrange the indices so I have all the indices consecutive (first cells with, and then cells without observation data)? I can do that, but it involves some bookkeeping I was hoping to avoid.

If my question is not clear, I'll put together a simple example that perhaps illustrates the issue better than I was able to explain. And, I think this might be more of a general BUGS question than specifically a nimble question, but I was trying this in nimble.

Thanks,
Glenn

Daniel Turek

unread,
Dec 19, 2018, 5:14:02 PM12/19/18
to Glenn Stauffer, nimble-users
Short answer: You need to do the book-keeping.

But truly, it's not bad at all:

indices1 <- c(1,3,5,7)        ## for example
indices2 <- c(2,4,6,8,10)     ## for example
nIndices1 <- 4
nIndices2 <- 5

## provide indices1, indices2, nIndices1, and nIndices2 
## as "constants" to the model in nimbleModel() call

## model code:

for(i in 1:nIndices1){
    ind <- indices1[i]
... code for non-missing data... using "ind" index
}

for(i in 1:nIndices2){
    ind <- indices2[i]
... code for missing data... using "ind" index
}

Keep in touch if this works.


--
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 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/55437cd6-edc6-4847-ae94-8214646be5bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Glenn Stauffer

unread,
Dec 19, 2018, 5:32:25 PM12/19/18
to Daniel Turek, nimble-users

OK, thanks. I will give that a try and see if I can make progress. One other questions though. Can I pass a vector of non-consecutive index values to a summary function, like:

 

MEAN <- mean(var1[indices1[1:n_indices1])

Daniel Turek

unread,
Dec 19, 2018, 5:45:20 PM12/19/18
to Glenn Stauffer, nimble-users
Negative.  Not supported, unless there's some way I'm not thinking about.  Gives an error like:

  getSymbolicParentNodesRecurse: only scalar random indices are allowed; vector random indexing found in var1[indices1[1:3]]

telling us that non-consecutive indexing (via a "random vector" of non-consecutive indices) is not supported.

However, once again with proper bookkeeping, the same could be accomplished.  Let me know if more details would be helpful.


Glenn Stauffer

unread,
Dec 19, 2018, 5:52:53 PM12/19/18
to Daniel Turek, nimble-users

OK, thanks again. I’ll be out for a bit over the holidays, but I’ll see first if I can get things working otherwise before I bother about that last part. But I’ll check back here if I have additional questions.

Frédéric LeTourneux

unread,
Feb 16, 2022, 4:09:12 PM2/16/22
to nimble-users
Hello Daniel and others that might run into this issue,

I had the same problem trying to specify some parameters at some occasions and not others in an attempt to reduce the number of nodes of a (way too large) CR model. I used your suggestion but ran into an error where it seems Nimble was unhappy I was associating multiple values to the same node (i.e. 'ind' must get defined multiple times when specified this way? I got this warning:
In genExpandedNodeAndParentNames3(debug = debug) : Multiple definitions for the same node. Did you forget indexing with 'i,co' on the left-hand side of 't <- summers[co]'?

Anyway, I managed to make it work by simply using the whole  'indices[i]' term in the code rather than associating it to an object. It's not as clean but does the trick. Thought I'd share this in case anyone runs into the same problem.

Cheers!

Fred

Chris Paciorek

unread,
Feb 17, 2022, 4:50:14 PM2/17/22
to Frédéric LeTourneux, nimble-users
I think Daniel meant for you to work with "indices1[i]" and "indices[2]" in those loops but without defining "ind" multiple times in the loops.

-chris

Reply all
Reply to author
Forward
0 new messages