Array indexing in STAN or a where statement?

592 views
Skip to first unread message

Jason Tilley

unread,
Feb 1, 2016, 12:26:53 PM2/1/16
to Stan users mailing list
I'm trying to calculate some parameters in my code. In this case I would like the mean of a parameter when it belongs to a particular group. Here's an example:
for (l in 1:G) // for each group
  group_mu[g] <- mean(mu[group==g])); 
Is there a way to do this in STAN? 

Thanks

Jonah Gabry

unread,
Feb 1, 2016, 12:52:58 PM2/1/16
to Stan users mailing list
Even with the new indexing functionality it's not possible to do R-style logical subverting like that. I think the best way to do it is to pass in integer arrays of the start and end positions of the group and use mu[start[g]:end[g]].

That assumes the values are ordered by group. If they're not then you can pass in an array of integer arrays containing the indices corresponding to each group and then use mu[idx[g]], where idx[g] is the array of indices for group g.

Hope that helps,

Jonah

Jonah Gabry

unread,
Feb 1, 2016, 12:53:41 PM2/1/16
to Stan users mailing list
That should say logical subsetting not subverting

Jason Tilley

unread,
Feb 1, 2016, 1:08:02 PM2/1/16
to Stan users mailing list
Thanks, that is very helpful.
Reply all
Reply to author
Forward
0 new messages