want to work with 2-D indexed array

200 views
Skip to first unread message

Andrew Gelman

unread,
Oct 4, 2016, 1:47:48 PM10/4/16
to Stan users mailing list
Hi, I'm wondering if Stan can handle indexed arrays. I have the Stan and R programs attached, but here's the key line of code:

y ~ normal(theta[student] - difficulty[level[student,season]], sigma_y)

Here, y is a vector, theta is a vector, difficulty is a vector, level is a 2-D integer array, and student and season are 1-D integer arrays.

The above code doesn't work. Instead I have to do this:

# for (i in 1:N)
# y[i] ~ normal(theta[student[i]] - difficulty[level[student[i],season[i]]], sigma_y);

Which I'd rather avoid.

I know that something like this would be faster:

vector[N] yhat;
for (i in 1:N)
yhat[i] = theta[student[i]] - difficulty[level[student[i],season[i]]];
y ~ normal(yhat, sigma_y)

But what I'd really love to do is have all the subscripting suppressed, to just do it in one line. Is this possible?

Code attached.
See you
A

P.S. When I run the code as written I get the following error:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:

index must be integer or 1D integer array; found number of dimensions=2

ERROR at line 27

25: # for (i in 1:N)
26: # y[i] ~ normal(theta[student[i]] - difficulty[level[student[i],season[i]]], sigma_y);
27: y ~ normal(theta[student] - difficulty[level[student,season]], sigma_y)
^
28: theta ~ normal(mu_theta, sigma_theta);

PARSER EXPECTED: <one or more container indexes followed by ']'>
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model 'transitions' due to the above error.


When I comment out line 27 and un-comment lines 25-26, it runs.

transitions.stan
transitions.R

Bob Carpenter

unread,
Oct 4, 2016, 2:35:29 PM10/4/16
to stan-...@googlegroups.com
Stan works the same way as R in that situation:

> a = 1:3
> b = 2:4
> c = matrix(1:16, c(4,4))
> c[a, b]
[,1] [,2] [,3]
[1,] 5 9 13
[2,] 6 10 14
[3,] 7 11 15What you're asking


What you're asking for goes against the behavior of multi-indexing.

What you can do is write a function:

vector dbl_idx(vector level, int[] ii, int[] jj) {
vector[size(ii)] result;
for (k in 1:size(ii))
result[k] = level[ii[k], jj[k]];
return result;
}

and then you can use

dbl_idx(level, student, season)

in place of where you think you wanted to write level[student, season].

We could, of course, build such a function directly into Stan very
easily.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
> <transitions.stan><transitions.R>

Andrew Gelman

unread,
Oct 4, 2016, 9:35:35 PM10/4/16
to stan-...@googlegroups.com
B
Yes, you're right! My R code was wrong too. Thanks for pointing this out.
Not clear we need this function in Stan.
A
Reply all
Reply to author
Forward
0 new messages