rmcelreath/rethinking

Problem with defining imputed variable as a vector in generated stan code

jhoupt opened this issue · 2 comments

jhoupt commented

I have an occasional issue In which the stan code does not identify a local variable as a vector when it depends on an imputed variable. I believe there is a bug the ulam code that is causing it. Below ii is the iterator for right_symbols but is used to index symbols on the third line. I tried changing the code to define j before that if statement, then index symbols with j, but it still resulted in the same error. I can keep chasing the issue, but I wanted to make sure I am not missing something. Let me know if you want me to generate something that reproduces the error.

for (ii in 1:length(right_symbols)) {
if (right_symbols[ii] %in% names(symbols)) {
if (symbols[[ii]]$type == "par") {
j <- which(names(symbols) == right_symbols[ii])
if (any(!is.na(symbols[[j]]$dims))) {
zzdims <- 0
if (length(symbols[[j]]$dims) == 1)
zzdims <- 1
else zzdims <- symbols[[j]]$dims[[2]]
the_dims <- max(the_dims, zzdims, na.rm = TRUE)
}
}
}
}

Yes please, if you show me the formula context that you'd like to work, I can maybe see a fix.

jhoupt commented

The example below causes the error. I explored a bit more this morning and it seems to only happen when I use a subscript in the name of the coefficient (i.e., if I use beta in place of beta_x below, it works).

dat <- list(
y =rnorm(100, .5, .2),
x=rnorm(100, 0, 1)
)
dat$x[sample(100, 2)] <- NA

mod <- alist(
y ~ dnorm(mu, sigma),
mu <- gm + beta_x * x,
gm ~ dnorm(.5, .3),
beta_x ~ dnorm(0,1),
x ~ dnorm(0,1),
sigma ~ dexp(1)
)

post_main <- ulam(mod, data=dat, iter=1000, chains=4, log_lik=TRUE)