Vector of length 1

data {
  int N1;
  int N1_cens;
  real tt1[N1];
  real tt1_cens[N1_cens];
  real beta0;
  real <lower=0>sigma;
    target+=normal_lpdf(tt1| beta0, sigma);
    target+=normal_lccdf(tt1_cens| beta0, sigma);
  beta0 ~ normal(0,10);
  sigma ~normal(0,5);

I am running some simulations where I vary the size of N1 and N1_cens. The above code does not work when either size is 1. I get the following error:

Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=tt1; dims declared=(1); dims found=()  (in 'model37dc1e501dd5_Vector_Example' at line 4)

Why is it an issue if I input a vector of size 1?

A small reproducible example, where the stan file is called “Vector_Example.stan”:

x = sort(rlnorm(50,10,0.9))
tt1 = x[1]
tt1_cens = x[2:50]
N1 = length(tt1)
N1_cens = length(tt1_cens)

stan_list = list(tt1=log(tt1),tt1_cens=log(tt1_cens),N1=N1, N1_cens = N1_cens)

#Load librarys
#Setting the options to allow parallel computation
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

Run_Model= stan("Vector_Example.stan", data=stan_list, iter =4000, seed = 1, chains = 1, warmup = 2000)

I had no issues fitting your reproducible example, but using Python + cmdstanpy. My best guess is it’s some way that rstan handles passing a list of length 1 to Stan?

The error specifically is saying that tt1 within Stan is expecting a 1 dimensional array but did not get one. I was able to recreate the issue by passing tt1 directly as a real and not as an array of length 1. So somehow when passing your data from R to Stan, tt1 becomes a real value and not an array.

Hopefully that helps your investigation.

1 Like

Thank you for checking.

It is straightforward to just declare tt1 as a real number, but, for the problem I am interested in, I have multiple variables which vary in length from one simulation to the next.

I guess I could just create multiple .stan files and use an if statement to choose between them but I hoped there was a less tedious way.

It may be useful for you to check out this rstan documentation. In particular:

Stan treats a vector of length 1 in R as a scalar. So technically if, for example, "real y[1];" is defined in the data block, an array such as "y = array(1.0, dim = 1)" in R should be used. This is also the case for specifying initial values since the same underlying approach for reading data from R in Stan is used, in which vector of length 1 is treated as a scalar

You can handle this in R, you just have to ensure that if you have a length 1 vector in R, that you explicitly pass it as such to Stan.

1 Like

This works! Good answer.