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

fix gqs with transformed parameters #949

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

mm--
Copy link
Contributor

@mm-- mm-- commented Jun 24, 2021

Summary:

gqs() has an issue where it doesn't work with transformed parameters. (See #714)
If draws contains the transformed parameters, an error like the following occurs:

Wrong number of parameter values in draws from fitted model.  Expecting 2 columns, found 3 columns.

But if the offending transformed parameters are removed, we instead get the error:

Error in draws[, draws_colnames, drop = FALSE] : subscript out of bounds

Intended Effect:

This small change just strips the transformed parameters from draws (by looking at constrained_param_names(FALSE, FALSE)) before passing it to standalone_gqs.

How to Verify:

Here's some code (modified from #714) to test:

library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

stancode1 <- "
transformed data{
  vector[10] y = [4.65, 6.02, 4.92, 4.77, 8.12, 6.93, 7.39, 7.6, 5.68, 2.14]';
}
parameters{
  real mu;
  real log_sigma;
}
transformed parameters{
  real<lower=0> sigma = exp(log_sigma);
}
model{
  y ~ normal(mu, sigma);
  mu ~ normal(0, 10);
  log_sigma ~ normal(0, 2.5);
}
generated quantities{
  vector[10] y_rep;
  
  for (i in 1:10)
    y_rep[i] = normal_rng(mu, sigma);
}
"

stanmodel1 <- stan_model(model_code = stancode1)
fit1 <- sampling(stanmodel1)
rep <- gqs(stanmodel1, draws = as.matrix(fit1))
rep

Side Effects:

Documentation:

Reviewer Suggestions:

@bgoodri

Copyright and Licensing

Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company):
Joshua Moller-Mara

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

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

Successfully merging this pull request may close these issues.

1 participant