Mismatch in dimensions - 2x2 matrix reading as dimensionless object

ETA: Please ignore this entire post. After taking a break from this, I came back and realized my naming conventions were not the same between my json data list and the data block of the model. I tried to delete the post but it appears to still keep a record of it in the list of topics, so I figured I’d un-delete it and at least make it clear that this was entirely an issue on my end and nothing more than that. Sorry to bother!


I’m having an issue with reading in a simple 2x2 matrix from a cmdstan json file. I keep getting the error:

Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=iden_matrix; dims declared=(2,2); dims found=() 

Here’s a brief example that produces the issue. I’ve generated the data in R as follows:

x <- mvtnorm::rmvnorm(100, mean = c(0,0), 
                      sigma = matrix(c(1, 0.5, 0.5, 1), nrow = 2))
example_data <- list(X = x,
                 N = nrow(x),
                 d = ncol(x), 
                 iden_mat = matrix(c(1,0,0,1), nrow = 2),
                 rho_mat = matrix(c(0,1,1,0), nrow = 2))
cmdstanr::write_stan_json(example_data, "example.json")

The model code is as follows (saved as example.stan):

data {
  int<lower=0> N;
  int<lower = 0> d;
  matrix[N, d] X;
  matrix[d, d] iden_matrix;
  matrix[d, d] rho_matrix;
}

parameters {
  real<lower=0, upper =1> dep;
}

transformed parameters {
  corr_matrix[d] sigma = iden_matrix + dep * rho_matrix;
}

model {
  vector[d] mu = rep_vector(0, d);
  dep ~ beta(1.5, 1.5);
  for (n in 1:N) {
    target += multi_normal_lpdf(X[n,] | mu, sigma);
  }
}

I then run the model within a cmdstan conda environment:

cmdstan_model example
./example sample num_chains=3 data file="example.json"

I have no idea why I’m getting a message that seems to imply there are no dimensions to iden_matrix, while it seems to read the X matrix fine.

I’m running this model in cmdstan v2.34, with macOS Sonoma 14.3.1. Thank you in advance for your help!

Thanks for reposting. I copied this response so I could mark it as solved.