cmdstanr
cmdstanr copied to clipboard
unconstrain_draws example does not work with extra variables
unconstrain_draws() doc has the following example
fit_mcmc$unconstrain_draws(draws = fit_mcmc$draws())
but this does not work if there are other variables (transformed parameters or generated quantities) stored in the draws, and fails with an error like
Error: Model has 469 unconstrained parameter(s), but 188800 were provided!
It is not trivial to figure out how to proceed after this error
Hmm I can't replicate this, the following works for me without issue:
modcode <- "
data {
real y_mean;
}
parameters {
real y;
}
model {
y ~ normal(y_mean, 1);
}
generated quantities {
vector[10] y_gq = rep_vector(y * 2, 10);
}
"
mod <- cmdstan_model(write_stan_file(modcode))
fit <- mod$sample(data = list(y_mean = 0), parallel_chains=4)
dr <- fit$draws()
fit$init_model_methods()
tes <- fit$unconstrain_draws()
tes <- fit$unconstrain_draws(draws = dr)
Sorry, it was grad_log_prob()
> fit$grad_log_prob(fit$unconstrain_draws(draws = dr))
Error: Model has 1 unconstrained parameter(s), but 4000 were provided!
After discussion with @andrjohns , this is just a case of bit unclear error message for invalid input