Can’t run a gqs
on a model with parameter of variable vector length when that length becomes 1:
test.stan
data {
int N;
int K;
int<lower=0,upper=1> y[N];
matrix[N,K] X;
}
parameters {
vector[K] b;
}
model {
y ~ bernoulli_logit(X*b);
}
test_sim.stan
data {
int N;
int K;
matrix[N,K] X;
}
parameters {
vector[K] b;
}
generated quantities {
vector[N] y_est = rep_vector(0,N);
{
y_est = X*b;
}
}
R code:
require(rstan)
# R code
data<-list()
data$y <- c(rep(1,10),rep(0,5))
data$N <- length(data$y)
data$K <- 1
data$X <- array (1, c(data$N,data$K))
test_m<-stan_model(file="test.stan")
fit <- sampling(test_m, data)
test_sim_m<-stan_model(file="test_sim.stan")
as.matrix(fit)
fit_proj <- gqs(test_sim_m, data=data, draws=as.matrix(fit))
out<-rstan::extract(fit_proj)
The above works for K>1 but breaks when K=1. Specifically the gqs
step says:
Exception: Variable b missing (in 'model573821a07f15_test_sim' at line 7)
The variable is shown in as.matrix(fit)
.
> as.matrix(fit)[1:5,]
parameters
iterations b[1] lp__
[1,] 0.4345688 -9.662128
[2,] 0.4345688 -9.662128
[3,] 1.1326903 -9.852514
[4,] 1.3721195 -10.250517
[5,] 0.2968098 -9.819760
Session Info:
> sessionInfo()
R version 4.0.1 (2020-06-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)
Matrix products: default
...
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rstan_2.19.3 ggplot2_3.3.1 StanHeaders_2.21.0-5
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4.6 pillar_1.4.4 compiler_4.0.1 prettyunits_1.1.1 tools_4.0.1 pkgbuild_1.0.8 lifecycle_0.2.0
[8] tibble_3.0.1 gtable_0.3.0 pkgconfig_2.0.3 rlang_0.4.6 cli_2.0.2 rstudioapi_0.11 parallel_4.0.1
[15] loo_2.2.0 gridExtra_2.3 withr_2.2.0 dplyr_1.0.0 generics_0.0.2 vctrs_0.3.1 stats4_4.0.1
[22] grid_4.0.1 tidyselect_1.1.0 glue_1.4.1 inline_0.3.15 R6_2.4.1 processx_3.4.2 fansi_0.4.1
[29] callr_3.4.3 purrr_0.3.4 magrittr_1.5 scales_1.1.1 ps_1.3.3 codetools_0.2-16 ellipsis_0.3.1
[36] matrixStats_0.56.0 assertthat_0.2.1 colorspace_1.4-1 RcppParallel_5.0.1 munsell_0.5.0 crayon_1.3.4
Looks like it may be related to this: