Skip to content

"No data for the selected variables." with optional variables #100

@mhollanders

Description

@mhollanders

Hi,

I've got a model with lots of optional variables but SBC throws an error when I try to plot their ranks. Here's a reproducible example with the following Stan program called test.stan.

data {
  int N, indicator;
  array[N] real y;
}

parameters {
  vector[indicator ? 1 : 0] mu1;
  vector[indicator ? 0 : 1] mu2;
  real<lower=0> sigma;
}

model {
  if (indicator) {
    target += normal_lpdf(y | mu1[1], sigma);
  } else {
    target += normal_lpdf(y | mu2[1], sigma);
  }
}

And the following R script reproduces the error:

# packages
library(cmdstanr)
library(SBC)

# compile model
mod <- cmdstan_model("test.stan")

# generator function and data
generator <- function(N, indicator) {
  mu <- rnorm(2)
  sigma <- rexp(1)
  if (indicator) {
    y <- rnorm(N, mu[1], sigma)
  } else {
    y <- rnorm(N, mu[2], sigma)
  }
  list(variables = list(mu1 = mu[1],
                        mu2 = mu[2],
                        sigma = sigma),
       generated = list(N = N,
                        indicator = indicator,
                        y = y))
}
gen <- SBC_generator_function(generator, N = 100, indicator = 1)
n_sim <- 10
data <- generate_datasets(gen, n_sim)

# backend and SBC
backend <- SBC_backend_cmdstan_sample(mod, chains = 3, 
                                         iter_warmup = 100, iter_sampling = 100)
sbc <- compute_SBC(data, backend)

# rank errors
plot_rank_hist(sbc)

Thanks for taking a look!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions