Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: mata: minindex vs permutation vector for finding closest values

 From László Sándor To statalist@hsphsun2.harvard.edu Subject Re: st: mata: minindex vs permutation vector for finding closest values Date Mon, 14 May 2012 10:54:59 -0400

```Thanks, Brendan, indeed this was the route to take.

However, for my purposes, I think I needed a more complicated
convolution of -order- and -invorder- and the permutation vectors they
provide, though I might be missing some potential simplification here.

Basically, I need to lookup the "rank" in the ordering of a vector
(for an application with matching on a single dimension, like the
propensity score), calculate ranks close to that rank, and get back
indices of observations with those ranks. I had a hard time noticing
that I had it backwards with -order- and -invorder-.

Below is a little example. Notice that real applications should
truncate the indices provided to be in the range of possible indices.
Also notice that if you run minindex on the now-selected, short
vector, you'll get back indices relevant to the selected vector, not
the original population, you need to translate those back. Though I
think the last argument that -minindex- returns is good as it is (FYI:
the matrix relevant for ties).

* test
mata:
x = 10*jumble(range(1,10,1))
x
y = range(20,11,-1)
y
p = order(x,1)
p
invp = invorder(p)
invp
invp[2]
p[|2-1,1 \ 2+1,1|]
yki = y[p[|invp[2]-1,1 \ invp[2]+1,1|]]
yki
end

On Sat, May 12, 2012 at 4:57 AM, Brendan Halpin <brendan.halpin@ul.ie> wrote:
>
> This may contain the kernel of a solution:
>
> mata: x = runiform(5,1)
> mata: y = (1,2,3,4,5)
> mata: x2 = x[order(x,1)']
> mata: y2 = y[order(x,1)']
>
> Brendan
> --
> Brendan Halpin,   Department of Sociology,   University of Limerick,
> Ireland
> Tel: w +353-61-213147  f +353-61-202569  h +353-61-338562;  Room F1-009 x
> 3147
> http://on.fb.me/fjIK9t
> http://teaching.sociology.ul.ie/bhalpin/wordpress
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```