stan-dev / stan-dev/rstan

gqs() error with length 1 vector or array parameters

Open
#985 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Summary:

gqs() gives an error when one of the parameters is a vector or array of length 1

#> Exception: Variable d missing  (in 'model17545a0741ce_43effcd60c5e7661f052cf84d7a670d3' at line 5)
Description:

See above.

Reproducible Steps:

The following code works when nd = 2 or greater, but fails when nd = 1.

sm <- stan_model(model_code =
"data {
  int<lower=1> nd;
}
parameters {
  vector[nd] d;
}
generated quantities {
  real d_new[nd] = normal_rng(d, 1);
}")

# Data
nd <- 1 # works with 2+, fails with 1

# Dummy draws
iter <- 1000
d <- matrix(rnorm(iter * nd), ncol = nd, nrow = iter)
colnames(d) <- paste0("d[", 1:nd, "]")

# Generated quantities
gqs(sm, draws = d, data = list(nd = nd))
Current Output:

When nd = 1, the gqs() call fails with the following output:

> gqs(sm, draws = d, data = list(nd = nd))
#> Exception: Variable d missing  (in 'model17545a0741ce_43effcd60c5e7661f052cf84d7a670d3' at line 5)
#> 
#> Inference for Stan model: 43effcd60c5e7661f052cf84d7a670d3.
#> 1 chains, each with iter=1000; warmup=0; thin=1; 
#> post-warmup draws per chain=1000, total post-warmup draws=1000.
#> 
#>          mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
#> d_new[1]    0     NaN  0    0   0   0   0     0   NaN  NaN
#> 
#> Samples were drawn using  at Wed Jan 12 08:58:06 2022.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at 
#> convergence, Rhat=1).

The error occurs whether vector[nd] d or array d[nd] are specified in the parameters block.

Inspecting the gqs() function, the draws matrix seems to be constructed correctly and passed to sampler$standalone_gqs - so perhaps this is a core stan issue rather than rstan?

Expected Output:

When nd = 2 or greater, the gqs() call outputs as expected:

> gqs(sm, draws = d, data = list(nd = nd))
#> Inference for Stan model: 43effcd60c5e7661f052cf84d7a670d3.
#> 1 chains, each with iter=1000; warmup=0; thin=1; 
#> post-warmup draws per chain=1000, total post-warmup draws=1000.
#> 
#>           mean se_mean   sd  2.5%   25%   50%  75% 97.5% n_eff Rhat
#> d_new[1]  0.00    0.04 1.41 -2.73 -0.92  0.05 0.94  2.81   988    1
#> d_new[2] -0.02    0.04 1.33 -2.77 -0.85 -0.02 0.83  2.56   936    1
#> 
#> Samples were drawn using  at Wed Jan 12 08:58:15 2022.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at 
#> convergence, Rhat=1).
RStan Version:

The version of RStan you are running (e.g., from packageVersion("rstan"))

> packageVersion("rstan")
#> [1] ‘2.21.2’
R Version:

The version of R you are running (e.g., from R.version.string)

> R.version.string
#> [1] "R version 4.1.2 (2021-11-01)"
Operating System:

Windows 10

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the gqs() entry point and the sampler$standalone_gqs path mentioned in the issue, then reproduce the example with nd = 1 and nd = 2. Trace how the single-column draws matrix is passed to generated quantities and verify that both vector[nd] and array declarations complete without the missing-variable error.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.