nimble icon indicating copy to clipboard operation
nimble copied to clipboard

Dynamic indexing with imputed nodes

Open mhollanders opened this issue 2 years ago • 3 comments

Hi there,

Sorry for the vague title. I'm trying to run a model with an ordinal covariate, but some missing values are present in the covariate. When I try to impute the missing values from a categorical distribution, I get the following error when I try to include dynamic indexing later on in the model:

Dynamic indexing found in a vector of indices, 1:x[i]. Only scalar indices, such as 'idx' in 'x[idx]', can be dynamic. One can instead use dynamic indexing in a vector of indices inside a nimbleFunction.

I'm following McElreath for including an ordinal covariate, e.g.:

mu[i] <- mu.alpha + mu.beta * sum(delta[1:x[i]])

I'm attaching a reproducible example showing that the problem only occurs when the covariate needs to be imputed. I suppose this is more of a feature request than anything, but I just wanted to see if anything can be done.

Cheers,

Matt impute-dynamic.txt

mhollanders avatar Aug 08 '22 03:08 mhollanders

@MHollanders Give this code a try, in the attached file. Needed to push the dynamic indexing into a nimbleFunction, but it should work the same as your code.

impute-dynamic_DT.txt

danielturek avatar Aug 08 '22 13:08 danielturek

Thanks Daniel! That did the trick. I'm going to get into nimbleFunctions for real!

mhollanders avatar Aug 09 '22 02:08 mhollanders

@MHollanders Good to hear, you won't regret it.

danielturek avatar Aug 09 '22 10:08 danielturek

I've noted this situation in our converting_to_nimble example.

paciorek avatar Mar 11 '23 22:03 paciorek