Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Sort each column of a matrix to have the same rank order as another matrix


From   "Scott Merryman" <smerryman@kc.rr.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Sort each column of a matrix to have the same rank order as another matrix
Date   Thu, 19 May 2005 10:51:08 -0500

I am trying to construct a new matrix Y that has the reordered elements of
each column of matrix X, to match the rank order of the corresponding
columns of matrix T.

For example:

X	T
6	1
9	-1
2	-2
4	2
7	0

So the ascending order of T is (2, 4, 5, 1, 3)

Therefore, the first element of Y will be the 2nd element of X, the second
the 4th, the third the 5th, the fourth the 1st, and the fifth the 3rd. 

Resulting in:

Y	
9
4
7
6
2


Is there an easy way to reorder the elements of X based on the rank order of
T?

Below is the code I am using for X (hog futures prices) and T (transformed
van der Waerden scores).

Thanks,
Scott


*Iman and Conover (1982) method
*Example using hog futures prices
clear
qui {
input hog_7	hog_8	hog_10 hog_12
60.88	53.8	61.04	53.11
72.39	55.51	46.49	54.52
62.02	50.96	45.58	56.11
63.19	62.67	51.36	42.38
62.26	55.36	51.99	53.24
62.88	63.12	69.97	47.46
82.92	71.71	64.72	50.74
53.55 55.15 60.06 47.26
64.98 64.67 48.46 64.49
68.76 71.94 53.13 48.23
66.17 62.34 48.07 55.49
68.86 72.64 49.12 40.78
60.91 54.44 55.06 42.66
53.08 56.96 41.61 39.62
60.7  53.1  46.47 41.12
52.99 62.4  47.97 36.38
63.94 54.09 42.23 61.66
70.31 65.71 48.66 48.2
52.17 54.48 56.85 55.46
59.68 72.63 48.71 48.05
end

spearman, matrix
matrix C = r(Rho)
matrix C = cholesky(C)'
}
mata
X= st_data( ., .)
C= st_matrix("C")
score1 = (1..20)'
score1 = invnormal(score1/21)
jum = jumble(score1)
scores = (score1, jum)
jum = jumble(score1)
scores = (scores,jum)
jum = jumble(score1)
scores = (scores,jum)
E = scores'*scores
F = cholesky(E/E[1,1])
T = scores*invsym(F)*C
st_matrix("r(T)",T)
end



*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index