Hello,

I recently posted a question on a “mark-recapture-like” scenario with an accompanying STAN program. However, I am unable to run it as it stands.

I receive the error (in part)

```
Exception: vector[multi] indexing: accessing element out of range.
```

Here is some data along with the program.

```
### R script ###
library(rstan)
N <- 76
Hstar <- 4
probs <- c(53, 13, 9, 1) / N
perms <- 10000
fit <- stan("model.stan", data = list(N = N, Hstar = Hstar, prob = probs, perms = perms))
```

### STAN program

```
data {
int<lower=1> N;
int<lower=1> Hstar;
simplex[Hstar] probs; // elements sum to 1
int<lower=2> perms;
}
transformed data {
matrix[perms, N] pop;
simplex[N] distrib = rep_vector(1.0 / N, N);
int<lower = 1, upper = N> resample_idxs[N];
vector[perms] res;
for (i in 1:N)
resample_idxs[i] = categorical_rng(distrib);
for (j in 1:perms)
res = N * probs[resample_idxs];
pop = append_col(res, pop);
}
transformed parameters {
real<lower=N> Nstar;
vector[perms] H = col(pop, N);
Nstar = (N * Hstar) / mean(H);
}
model {
// empty since this is taken care of by categorical_rng
}
```

Essentially the program fills a matrix (**pop**) with integers between 1-4 and takes the mean across of the final column. This is followed by some substitutions into a formula to compute the quantity of interest (Nstar).

**Can someone point me to a solution regarding the Exception error above and anything else that may be funky about the STAN program, which appears to be syntactically correct (since I am a newcomer)?**