Reputation: 2614
I'm using the following very simple Stan model saved as model.stan
,
data {
int<lower=1> J;
real x[J];
}
parameters {
real mu[J];
real<lower=0> sigma[J];
}
model {
sigma ~ inv_gamma(1, 1);
mu ~ normal(0, 10);
x ~ normal(mu, sigma);
}
In this model I have a single data point x[j]
that I model as coming from J
different Normal distributions.
When J > 1, the following R code works perfectly:
library(rstan)
model <- stan_model('~/model.stan')
data <- list(J = J, x = runif(J))
stan.fit <- rstan::sampling(model, data=data)
However, when J = 1 I get the following error:
failed to create the sampler; sampling not done
How can I write this Stan model so it works for all J >= 1?
Upvotes: 0
Views: 389
Reputation: 4990
This is a common problem (and one that is compounded by a bug where the description of the problem is suppressed in rstan 2.18.1). If you declare a real array in the data block of a Stan program, the corresponding R object must have a dimension attribute. Thus,
stan.fit <- sampling(model, data = list(J = J, x = as.array(runif(J))))
does run (although there are many divergent transitions) because x
has a dim
attribute that is J
.
Upvotes: 2