ranking problem with @ranks

For questions regarding the import, export and manipulation of data in EViews, including graphing and basic statistics.

Moderators: EViews Gareth, EViews Steve, EViews Moderator, EViews Jason

Liu
Posts: 90
Joined: Tue Jun 08, 2010 6:22 am

ranking problem with @ranks

Postby Liu » Thu Jul 19, 2012 11:29 am

Hi,

I want to convert a whole bunch of series into ranked series. Assuming I have gdp growth of three
countries like this:

Year ca_gdp jp_gdp uk_gdp
1995 NA NA 20
1996 20 NA 40
1997 -20 NA 30
1998 10 15 20


I combined the three series and used eviews @ranks and it eventually gave me a ranked matrix like this:
Year ca_gdp_rank jp_gdp_rank uk_gdp_rank
1995 2 2 1
1996 2 3 1
1997 3 2 1
1998 3 2 1


The above ranking matrix is wrong in the way that NA in the matrix was deemed as zero and receive
a rank. Is there a way to let eviews rank among only those who have data?

(( the correct ranking I was expecting is:

Year ca_gdp_rank jp_gdp_rank uk_gdp_rank
1995 NA NA 1
1996 2 NA 1
1997 2 NA 1
1998 3 2 1 ))


The key command I use is
rowvector r5=@ranks(r3, "d", "f" )

this command take NA as zero instead of ignoring it.

Is there a way I can let eviews to rank with NA in?



Thank you in advance.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13600
Joined: Tue Sep 16, 2008 5:38 pm

Re: ranking problem with @ranks

Postby EViews Gareth » Thu Jul 19, 2012 11:58 am

I can't think of a good way to do that, no.

Liu
Posts: 90
Joined: Tue Jun 08, 2010 6:22 am

Re: ranking problem with @ranks

Postby Liu » Thu Jul 19, 2012 1:17 pm

Thanks Gareth,

does @ranks have a counterpart similar to "stom"-"stomna" commands, which allows users to ignore nas when doing the ranking?

or alternatively, is there other command besides @rank that can do ranking? [my current strategy is to combine series into a matrix, extract each row of the matrix and do the ranking (using @rank), and finally assign the rank value back to the series. ]

or maybe I can use @rank with a different strategy?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13600
Joined: Tue Sep 16, 2008 5:38 pm

Re: ranking problem with @ranks

Postby EViews Gareth » Thu Jul 19, 2012 1:22 pm

There is no @ranks that will handle NAs. The only strategy I can think of is to loop through the numbers yourself and rank them.

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: ranking problem with @ranks

Postby EViews Glenn » Thu Jul 19, 2012 1:39 pm

I should point out that we are talking about the @ranks on vectors here. The @ranks on series propagate NAs.

Liu
Posts: 90
Joined: Tue Jun 08, 2010 6:22 am

Re: ranking problem with @ranks

Postby Liu » Thu Jul 19, 2012 2:08 pm

There is no @ranks that will handle NAs. The only strategy I can think of is to loop through the numbers yourself and rank them.
do you mean loop for each date(index) to rank each row vector? like this:
for !i= 1 to @obssmpl
rowvector r1=@rowextract(matrix, !i)
rowvector r2=@ranks(r1, "d", "f" )
next

or you mean loop within a row vector to rank them? I guess what you mean is the latter.
(a row vector here is like 10 15 20 in the first example matrix.)

I should point out that we are talking about the @ranks on vectors here. The @ranks on series propagate NAs.
I think @ranks working on series will give ranks among observations within a series right?? so instead of using @ranks on series directly, I can do by this:

(1) combine series s1 s2 s3 into matrix m1,
(2) transpose the matrix into mt.
(3) convert matrix mt into series s1t, s2t, s3t ...
(4) rank these series (the goodness is that as you pointed out, @ranks can propagate NA for series.)
(5) combine these series back to a matrix and transpose back to another matrix, then convert it to series again to obtain rank series.

would this be a solution? it sounds like massive calculation.

dagfinnrime
Posts: 54
Joined: Sat Oct 11, 2008 9:37 am
Location: Oslo
Contact:

Re: ranking problem with @ranks

Postby dagfinnrime » Thu Sep 17, 2015 6:26 am

Hi

a request: it would be very useful if @ranks could control how NAs are treated. Useful example is eg. for constructing portfolios in empirical asset pricing.

However, a solution is:

Code: Select all

group x x_* !n = @obsrange !m = x.@count stomna(x,_tmp_mx) matrix(!n,!m) m_Ranks for !r = 1 to !n rowvector _tmp_rvx!r = @rowextract(_tmp_mx,!r) !nas = @nas(_tmp_rvx!r) if !nas>0 then !na = 0 vector _tmp = @nan(_tmp_rvx!r,@max(_tmp_rvx!r)+1) vector y!r = @ranks(_tmp, "a", "i") for !i = 1 to !m if _tmp_rvx!r(!i)=na then y!r(!i) = na !na = !na+1 endif if !na=!nas then exitloop endif next else vector y!r = @ranks(_tmp_rvx!r, "a", "i") endif rowplace(m_ranks,y!r,!r) next
Dagfinn Rime


Return to “Data Manipulation”

Who is online

Users browsing this forum: No registered users and 2 guests