R-like subset selection

541 views
Skip to first unread message

dje

unread,
Apr 6, 2016, 3:38:34 PM4/6/16
to Stan users mailing list
Hi,

I'd like to R-style subset selection. For example, in R, if you have a vector foo and you want to select some elements, you can supply it with another logical vector x using the braket [ ] operators: you can pull out the values of foo where x is true. So for x <- c(T,F,T,T), foo[x] will return elements 1,3,4 of foo because those are the values of foo where x is true. Is there a stan-ified way to do thise?

Likewise, is there an analogue to argmax of a vector? So if foo is 10,20,30,40,50 argmax(foo) would return 5 because the 5th element is the largest among elements of foo.

These two convenience issues would really improve my quality of life w.r.t. stan.

I've checked the archives but didn't find anything that matched. Hope this isn't too much of a bother.

V/r,

dje

Bob Carpenter

unread,
Apr 6, 2016, 5:41:35 PM4/6/16
to stan-...@googlegroups.com
You can easily write your own function to do this:

vector r_style_subset(vector x, vector cond) {
vector[sum(cond)] y;
int pos;
pos <- 1;
for (i in 1:rows(x)) {
if (cond[i]) {
y[pos] <- x[i];
pos <- pos + 1;
}
}
return y;
}

It should really test that the entries in cond are 0 or 1,
and that x and cond have the same number of rows.

But I'm guessing you really want it to work for all possible
container shapes and you want us to extend all of our comparison
operators to allow R-like idioms such as y[y > 10].

It's not that hard to do, but will require a different syntax
because we don't have a proper bool type. Nobody has any time
soon to do it, though.

- 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.

David Elkind

unread,
Apr 8, 2016, 10:22:41 AM4/8/16
to stan-...@googlegroups.com
Thanks for the tips, Bob. I'm sure you all are hard-pressed for time and that implementing all convenience idioms from all languages certainly is a lower priority than, you know, making stan do stan-specific stuff.

V/r,
dje

You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/quCUh0sHbgA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.

Bob Carpenter

unread,
Apr 8, 2016, 10:40:37 AM4/8/16
to stan-...@googlegroups.com
Extending Stan's programming language is the main kind of
Stan-specific stuff that I do! The other big priorities are
ragged arrays, sparse matrices, and list-like structures.
We're also going to have to start thinking about meta-annotations
to facilitate things like data-streaming VB or data-parallel EP.

- Bob

David Elkind

unread,
Apr 8, 2016, 1:10:15 PM4/8/16
to stan-...@googlegroups.com
Hm, Bob, the code snippet you sent doesn't seem to be working. The parser doesn't like that we have a real number in the declaration for y. Rounding, perplexingly, returns another real. Any tips?

Ben Goodrich

unread,
Apr 8, 2016, 1:27:04 PM4/8/16
to Stan users mailing list
On Friday, April 8, 2016 at 1:10:15 PM UTC-4, dje wrote:
Hm, Bob, the code snippet you sent doesn't seem to be working. The parser doesn't like that we have a real number in the declaration for y. Rounding, perplexingly, returns another real. Any tips?

cond needs to be an int [] rather than a vector

Ben

Bob Carpenter

unread,
Apr 8, 2016, 2:21:37 PM4/8/16
to stan-...@googlegroups.com
Thanks for the correction!

- Bob
Reply all
Reply to author
Forward
0 new messages