Another "index out of range" issue

79 views
Skip to first unread message

Greg Werbin

unread,
Sep 17, 2016, 3:22:56 PM9/17/16
to Stan users mailing list
I'm working my way through understanding Gaussian processes in Stan, but I'm hitting a strange indexing exception during sampling. My model code is here: https://gist.github.com/greg-rocketrip/62ab15f2862fbb16650f7b84b2bcf089

The error is:

Error : Exception thrown at line 23: []: accessing element out of range. index 1 out of range; expecting index to be between 1 and 0; index position = 1x
failed to create the sampler
; sampling not done

for (i in 1:N_obs) x[i] = x_obs[i];
where I concatenate the observed x and sampling x values.


I don't really understand this error message. What's wrong with my code? Or is this a bug in Stan?

I'm using R 3.3.1 on Mac OS 10.11.16, with RStan version 2.12.1.

Ben Goodrich

unread,
Sep 17, 2016, 3:35:46 PM9/17/16
to Stan users mailing list
On Saturday, September 17, 2016 at 3:22:56 PM UTC-4, Greg Werbin wrote:
I don't really understand this error message. What's wrong with my code? Or is this a bug in Stan?

In the code chunk below, N is undefined as of the second and third lines and thus x and output_mean are the wrong size.

  int N;
  real x[N];
  vector[N] output_mean;  // process mean
  real output_noise_scale;  //  assumed measurement error on individual observations

  N = N_obs + N_sim;
  
  for (i in 1:N_obs) x[i] = x_obs[i];

Also, in the model block you can create y with append_row(y_obs, y_sim).

Ben

Greg Werbin

unread,
Sep 18, 2016, 3:52:19 PM9/18/16
to Stan users mailing list
Thanks for the tip on append_row.

So is the workaround to declare vector[N_obs+N_sim]? Since all variables have to be declared at the top of a block.

Ben Goodrich

unread,
Sep 18, 2016, 5:04:40 PM9/18/16
to Stan users mailing list
On Sunday, September 18, 2016 at 3:52:19 PM UTC-4, Greg Werbin wrote:
Thanks for the tip on append_row.

So is the workaround to declare vector[N_obs+N_sim]? Since all variables have to be declared at the top of a block.


Yeah, or pass N as data instead of defining it in transformed parameters.

Ben


 
Reply all
Reply to author
Forward
0 new messages