# Accessing element out of range - rstan

**URL:** <https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659>\
**Category:** Modeling\
**Tags:** rstan, specification\
**Created:** [May 23, 2021, 11:26am UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659 "2021-05-23T11:26:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robin\_Brooker](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/robin_brooker/32/11761_2.png) [@Robin\_Brooker](https://discourse.mc-stan.org/u/Robin_Brooker)\
**Post date:** [May 23, 2021, 11:26am UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659/1 "2021-05-23T11:26:59Z")

</div>

Hi,

I’m fairly new to stan, R and Bayesian, so sorry if my mistake is very rudimentary.

I’m ‘attempting’ to estimate a hierarchical model in rstan, but I’m receiving the following error messages when attempting to executing the sampling:

'[1] "Error in sampler$call\_sampler(args\_list[[i]]) : ’

and,

'[2] “Exception: []: accessing element out of range. index 6 out of range; expecting index to be between 1 and 0; index position = 1alpha (in ‘model126678353ad\_Varying\_Slope\_Model\_NoCov’ at line 31)”

Here is the model:

```
data {
  int N; //number of participants
  real behaviour[N]; //outcome 
  int respondent[N]; //vector of respondent IDs
  real prevalence[N]; //predictor 
  int K; //number of respondents
}

```

```
parameters {
  real alpha[K]; //respondent intercepts
  real beta_prevalence[K]; //respondent slopes
  real sigma[K]; //respondent variance
  real alpha_top; //pooled intercept
  real<lower=0> alpha_sigma; //pooled intercept variance
  real beta_p_top; //pooled slope
  real beta_p_sigma; //pooled slope variance
}

```

```
model {
  for(i in 1:N) {
    int aRespondent;
    aRespondent = respondent[i];
    behaviour[i] ~ normal(alpha[aRespondent] + beta_prevalence[aRespondent]*prevalence[i], sigma[aRespondent]);
  }
alpha ~ normal(alpha_top, alpha_sigma); //priors
beta_prevalence ~ normal(beta_p_top, beta_p_sigma); //priors
sigma ~ normal(0,2); //priors

alpha_top ~ normal(0,2); //hyperpriors 
beta_p_top ~ normal(0,2); //hyperpriors
alpha_sigma ~ normal(0,2); //hyperpriors
beta_p_sigma ~ normal(0,2); //hyperpriors
}

```

```
generated quantities {
  real alpha_overall;
  real beta_p_overall;
  alpha_overall = normal_rng(alpha_top, alpha_sigma);
  beta_p_overall = normal_rng(beta_p_top, beta_p_sigma);

```

Does anyone know if i’ve made a silly coding error?

---

<div class="post-metadata">

**Author:** ![jtimonen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jtimonen/32/9034_2.png) [@jtimonen](https://discourse.mc-stan.org/u/jtimonen)\
**Post date:** [May 23, 2021, 11:44am UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659/2 "2021-05-23T11:44:31Z")

</div>

What values do you have for N and K and what values does the respondent vector contain?

---

<div class="post-metadata">

**Author:** ![jtimonen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jtimonen/32/9034_2.png) [@jtimonen](https://discourse.mc-stan.org/u/jtimonen)\
**Post date:** [May 23, 2021, 11:48am UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659/3 "2021-05-23T11:48:41Z")

</div>

> [@Robin\_Brooker](#):
>
> expecting index to be between 1 and 0

This makes me guess you have K=0

---

<div class="post-metadata">

**Author:** ![Robin\_Brooker](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/robin_brooker/32/11761_2.png) [@Robin\_Brooker](https://discourse.mc-stan.org/u/Robin_Brooker)\
**Post date:** [May 23, 2021, 11:51am UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659/4 "2021-05-23T11:51:41Z")

</div>

Hi @jtimonen,

N contains values 1 to 41756  
K contains values 1 to 4646  
‘respondent’ is an id variable with values 1 to K

---

<div class="post-metadata">

**Author:** ![Robin\_Brooker](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/robin_brooker/32/11761_2.png) [@Robin\_Brooker](https://discourse.mc-stan.org/u/Robin_Brooker)\
**Post date:** [May 23, 2021, 1:22pm UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659/5 "2021-05-23T13:22:36Z")

</div>

@jtimonen

You were right regarding the K=0.

The code used to create my data list in R reads:

data ← list(behaviour=behaviour, prevalence=prevalence, N=length(behaviour), respondent=respondent, K=nlevels(as.factor(PRINT\_DATA\_1\_IMP\_LONG\_NoNaN$respondent\_id)))

Originally the code was devoid of the ‘as.factor’ function, so the ‘nlevels’ function wasn’t picking up the number of levels.

Strangely, its now producing a similar error message, but it now states that respondent values must be be less than or equal to 4143.

Exception: model1267eb17627\_Varying\_Slope\_Model\_NoCov\_namespace::model1267eb17627\_Varying\_Slope\_Model\_NoCov: respondent[i\_0\_\_] is 4144, but must be less than or equal to 4143 (in ‘model1267eb17627\_Varying\_Slope\_Model\_NoCov’ at line 15)

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [May 23, 2021, 2:18pm UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659/6 "2021-05-23T14:18:06Z")

</div>

The first thing to do is to check the data that’s being passed to Stan. What do you get when you run in R:

```r
length(behaviour)
length(prevalence)
length(respondent)
nlevels(as.factor(PRINT_DATA_1_IMP_LONG_NoNaN$respondent_id))

```

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [May 23, 2021, 2:24pm UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659/7 "2021-05-23T14:24:28Z")

</div>

Oh and `range(respondent)` would probably be good to check as well

---

<div class="post-metadata">

**Author:** ![jtimonen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jtimonen/32/9034_2.png) [@jtimonen](https://discourse.mc-stan.org/u/jtimonen)\
**Post date:** [May 23, 2021, 4:43pm UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659/8 "2021-05-23T16:43:37Z")

</div>

> [@Robin\_Brooker](#):
>
> N contains values 1 to 41756  
> K contains values 1 to 4646

They should be only one integer so you probably mea N=41756 and K=4646? But you want to print their values to check what you are actually passing to Stan.

> [@Robin\_Brooker](#):
>
> ‘respondent’ is an id variable with values 1 to K

I suspect your `PRINT_DATA_1_IMP_LONG_NoNaN$respondent_id` contains only 4143 unique values, and then nlevels(as.factor()) for it is 4143. You can put  
`int<lower=1, upper=K> respondent[N];`  
to your model to make these things easier to catch

---

<div class="post-metadata">

**Author:** ![Robin\_Brooker](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/robin_brooker/32/11761_2.png) [@Robin\_Brooker](https://discourse.mc-stan.org/u/Robin_Brooker)\
**Post date:** [May 23, 2021, 7:15pm UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659/9 "2021-05-23T19:15:51Z")

</div>

@andrjohns @jtimonen I’ve added \<lower=1, upper=k\> and that appears to have done the trick. I encountered similar problems with ‘behaviour’ and ‘prevalence’ and this solution has worked for all.

Thanks for you help and solutions!

---

<div class="post-metadata">

**Author:** ![jtimonen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jtimonen/32/9034_2.png) [@jtimonen](https://discourse.mc-stan.org/u/jtimonen)\
**Post date:** [May 23, 2021, 10:05pm UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659/10 "2021-05-23T22:05:52Z")

</div>

Adding the bounds should have only thrown a more informative error and not solve the problem. So something weird is going on. And it makes no sense to add those bounds to prevalence and behaviour if they are continuous data. For sigma parameter you want to set \<lower=0\> if it is a variance parameter that should not be negative

---

<div class="post-metadata">

**Author:** ![jtimonen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jtimonen/32/9034_2.png) [@jtimonen](https://discourse.mc-stan.org/u/jtimonen)\
**Post date:** [May 23, 2021, 10:07pm UTC](https://discourse.mc-stan.org/t/accessing-element-out-of-range-rstan/22659/11 "2021-05-23T22:07:45Z")

</div>

I really recommend you to check the things @andrjohns said
