Error occurred during calling the sampler; sampling not done

Hello I am simulating some data for checking my model in Rstan suing rstansim. I get the following error message but I cannot understand why is it the case.
Can someone help me with it?
Thank you!

"Error in sampler$call_sampler(args_list[[i]]) : "
error occurred during calling the sampler; sampling not done
Stan model ‘rstan_simplesimul’ does not contain samples.

regression_input_data <- list("N" = 924,
                              "K" = 4,
                              "exp_Mat"=exp_Mat,
                              "Dalp"=c(1,1,1,1))

data_listFakeCP <- list("alpha"=3,
                        "delta"=8,
                        "W"=c(1,0,0,0),
                        "sig"=4) Preformatted text`
# simulate data CP
uncorrelated_data <- simulate_data(
  file = "rstan_simplesimul.stan",
  data_name = "sim1",
  input_data = regression_input_data,
  param_values = data_listFakeCP,
  nsim = 3,
  vars = c("N", "K", "sim_Y"),
  path = "Rstan BMI/sim", 
  seed = 1
)

///this is rstan_simplesimul.stan
data{
int<lower=0> N;
int<lower=0> K;
matrix[N,K] exp_Mat;
vector[K] Dalp;
}
//parameters
parameters{
real alpha;
real delta;
simplex[K] W;
real<lower=0> sig;
}
transformed parameters{
  vector[N] xb;
    for (n in 1:N) {
    xb[n]=alpha+exp_Mat[n]*W*delta;
    }
}
//generate quantities 
generated quantities{
  vector[N] sim_Y;
  for (n in 1:N)  sim_Y[n] = normal_rng(mean(xb), sig)';
}

What did you use for "exp_Mat"=exp_Mat?

It is a matrix N by K with the original data

If it helps the error suggests also:

Rejecting initial value:
Log probability evaluates to log(0), i.e. negative infinity.
Stan can’t start sampling from this initial value.

Initialization between (-2, 2) failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] “Error in sampler$call_sampler(args_list[[i]]) : Initialization failed.”

Is it typical to have the rng inside of your .stan file this way?

generated quantities{
  vector[N] sim_Y;
  for (n in 1:N)  sim_Y[n] = normal_rng(mean(xb), sig)';
}

What package is this simulate_data function coming from? It doesn’t look like it is doing what you think it is doing (calling sampling with the "fixed_param" option and setting the parameters equal to the values in data_listFakeCP).

The "W" = c(1,0,0,0) line is causing you problems. Change it to "W" = c(0.25, 0.25, 0.25, 0.25) or something (simplex probabilities can’t be exactly 1 or 0).

You can use the _rng functions in the generated quantities block.

The generated quantities block doesn’t affect the inference, so I don’t usually think of it as part of the model so much. Generated quantities is for making predictions based on the output of your model.

Edit: That make sense? I’m not sure I said that clearly.

1 Like

Ah, I wasn’t aware that transformed parameters could be used that way. I better understand now that this would still sample.

Just to be clear you can’t use _rng functions in transformed parameters, but I’m being too lazy to read the full thread so I’m probably missing something.