Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jhoupt opened this issue Sep 11, 2023 · 2 comments
Open

Comments

@jhoupt
Copy link

jhoupt commented Sep 11, 2023

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)
}
}
}
}

@rmcelreath
Copy link
Owner

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

@jhoupt
Copy link
Author

jhoupt commented Sep 11, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants