I am trying to fit a zero-one-inflated item response model for bounded continuous responses. I came up with a model syntax. It compiles with no issue, but when I fit the model, it gives me this error message.

I can’t seem to find the issue. I will attach the demo dataset, model syntax, and R code so you can reproduce the error (hopefully).

I appreciate any help and guidance.

demo.csv (292.6 KB)

sb_irt.stan (2.8 KB)

sb_irt_fit.r (1.1 KB)

The error is occurring because you’ve declared `delta`

as size `I`

:

```
vector<lower=0>[I] delta;
```

But you’re indexing in a loop over `n_obs`

:

```
for(i in 1:n_obs) {
...
real q = delta[i];
...
}
```

Should that be `delta[item[i]]`

?

1 Like

Definitely! You are right. I spent a good amount of time on this before I posted it, but it sometimes needs a fresh eye looking over it. I appreciate it. Thank you so much.

1 Like

I didn’t see the error message in your post. The error message should say there is an index out of bound and tell you the line number of the program where it occurred. Did you get that far and still have trouble debugging?

There are other issues in your code. First, we have a `log_inv_logit`

function which will be more efficient and stable and there are redundant calculations you do here that aren’t necessary. I’d rewrite your likelihood as follows, as a start:

```
for (n in 1:n_obs) {
if (Y[i] == 0) {
target += log_inv_logit(gamma0[item[i]] - alpha[item[i]] * theta[id[i]]);
} else if (Y[i] == 1) {
target += log1m_inv_logit(gamma1[item[i]] - alpha[item[i]] * theta[id[i]]);
} else {
real log_k0 = log_inv_logit(gamma0[item[i]] - alpha[item[i]] * theta[id[i]]);
real log_k1 = log_inv_logit(gamma1[item[i]] - alpha[item[i]] * theta[id[i]]);
real p = alpha[item[i]] * theta[id[i]] + beta[item[i]];
real q = delta[i];
target += log_diff_exp(k1, k0) + normal_lpdf(Y[i] | p, q);
}
}
```

The real win would come from sorting the `Y`

ahead of time into three bins in transformed data, then vectorizing these operations.

1 Like

Thank you, Bob! I fixed the original error and was able to fit the model with no issue.

The way you re-write the model segment is very beautiful. Thank you for showing us how to make it more elegant and efficient. I will modify the code and implement your suggestions.

You’re welcome, but I was actually curious about how the error message looked to you. We’re trying to make the messages better so that our users have an easier time debugging models.

This was the error message I got.

```
Chain 1 Assertion failed!
Chain 1
Chain 1 Program: B:\delete\sb_irt.exe
Chain 1 File: stan/lib/stan_math/lib/eigen_3.3.7/Eigen/src/Core/DenseCoeffsBase.h, Line 408
Chain 1
Chain 1 Expression: index >= 0 && index < size()
Warning: Chain 1 finished unexpectedly!
Warning message:
No chains finished successfully. Unable to retrieve the fit.
```

It didn’t give me a line number or a hint why this might be happening.

As Andrew pointed out, the issue was on Line 69 of the .stan file.

`delta`

had a size of `I`

, but `nobs`

is always larger than `I`

. When I call the for loop, `for(i in 1:nobs)`

, I guess it throws the error message when `i`

becomes larger than the size `I`

because `delta[I+1]`

doesn’t exist.

I mistyped it in the stan model syntax as I was supposed to pull the index values for `delta`

from another input vector, `item`

, which stores values from 1 to `I`

. So, `delta[item[i]]`

fixed the problem.