2d array of integers from R...why is this failing?

I’m testing out some examples in the examples folder on cmdstanr and this multi_probit_good.stan does not like the input format

Example is at example-models/probit-multi-good.stan at master · stan-dev/example-models · GitHub

Specifically it does not like y as matrix, as an array, or even using structure(c(...), .Dim = c(500, 4))

library(MASS)

K <- 8;
D <- 4;
N <- 500;

# stick breaking construction to generate a random correlation matrix
L_Omega <- matrix(0,D,D);
L_Omega[1,1] <- 1;
for (i in 2:D) {
  bound <- 1;
  for (j in 1:(i-1)) {
    L_Omega[i,j] <- runif(1, -sqrt(bound), sqrt(bound));
    bound <- bound - L_Omega[i,j]^2;
  }
  L_Omega[i,i] <- sqrt(bound);
}

Omega <- L_Omega %*% t(L_Omega);

x <- matrix(rnorm(N * K, 0, 1), N, K);
beta <- matrix(rnorm(D * K, 0, 1), D, K);

z <- matrix(NA, N, D);
for (n in 1:N) {
  z[n,] <- mvrnorm(1, x[n,] %*% t(beta), Omega);
}

z <- matrix(NA, N, D);
for (n in 1:N) {
  z[n,] <- mvrnorm(1, x[n,] %*% t(beta), Omega);
}

y <- matrix(0, N, D); 
# or y <- array(0, dim = c(N, D));
for (n in 1:N) {
  for (d in 1:D) {
    y[n,d] <- ifelse((z[n,d] > 0), 1, 0);
  }
}

mod_probit <- cmdstan_model("probit_multi_good.stan", force_recompile = T)

fit_probit <- mod_probit$sample(
  data = list(K = K, 
              D = D, 
              N = N, 
              y = y, 
              x = x),
  step_size = 0.01,
  adapt_delta = 0.99
)

Error is dims found = (2000)

Chain 1 Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=y; dims declared=(500,4); dims found=(2000) (in '/var/folders/30/ddxkfs1x7bscp4_99c4db5jc0000gn/T/RtmpPA33tI/model-8e9526e504c2.stan', line 5, column 2 to column 30)

Its a bug. See Mismatch in number of dimensions declared and found - #5 by fusaroli

Working on it

2 Likes

I missed that post! Thanks!

1 Like

Just a quick follow up that this issue has been resolved in cmdstanr. If you encounter it, please upgrade to the latest version of cmdstanr:

remotes::install_github("stan-dev/cmdstanr")