data {
int N1;
int N1_cens;
real tt1[N1];
real tt1_cens[N1_cens];
}
parameters{
real beta0;
real <lower=0>sigma;
}
model{
target+=normal_lpdf(tt1| beta0, sigma);
target+=normal_lccdf(tt1_cens| beta0, sigma);
//priors
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”:
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.
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.