Possible to return 1d result from multiple indexing instead of 2d?

I think this is not possible in stan but I am sometimes surprised, so:

I have a lot of data and a piece of code:

  pcorrect= 1.0 - 1.0 ./ ( 1.0 + exp(
    a[item[1:Nobs]] .*
      base + 
      age*ageeffect + square(age)*age2effect - 

the ability[id,scale] reference is the crucial piece, id is an Nobs length vec of subject references, while scale is an Nobs length vec of scale references. The code as it stands does not work, because ability[id,scale] returns a huge matrix. Is there any easy way to get it to treat [id,scale] in some kind of rowwise fashion, returning only a single real (and thus in the end a 1 dim array or vector) for every i,j pair?


I don’t think that’s possible out of the box - in any case, Stan would probably not implement such functionality by overriding the [] operator the way R does, but rather as a function (the philosophy so far in Stan seems to be to have many specific functions that do a single simple thing than big functions with lot of options) - so you can definitely write that function for yourself… And since Stan pays almost no penalty for loops, it will be almost exactly as fast as if Stan implemented it for you.

Best of luck with your model

I’m both happy and sad to see that my intuition was correct… putting it in loop form takes it from 2s per grad eval to 3s. Arranging the pieces needed into a long vector in a separate loop is much quicker, apparently…

1 Like

This is actually something that I’ve partway completed (but haven’t had the time to finish). It will eventually be a to_vector overload (as well as to_row_vector and to_array_1d):

to_vector(ability, id, scale)

This is the github issue if you want to keep an eye on it