1

For the purposes of this question, I would like to construct a data frame or similar to be able to "stack rank" and sort various metrics that are generated from functions.

Let's take an example from the Performance Analytics package:

  • I have the close-close returns of 3 indices from 2001: SPX, NASDAQ (CCMP) and EuroStoxx (SX5E).
  • I'd like to get the 95% 1day VaR for each of these, place them into a table and then sort them from high to low (or low to high etc).

To illustrate my question, I will use the table.DownsideRisk function in the Performance Analytics package rather than just calling VaR().

So, I can get these results individually:

                              SPX.cc
Semi Deviation                 0.0095
Gain Deviation                 0.0096
Loss Deviation                 0.0102
Downside Deviation (MAR=210%)  0.0142
Downside Deviation (Rf=0%)     0.0094
Downside Deviation (0%)        0.0094
Maximum Drawdown               0.5678
Historical VaR (95%)          -0.0203
Historical ES (95%)           -0.0317
Modified VaR (95%)            -0.0193
Modified ES (95%)             -0.0273

Or I can place them all in an xts object together and then run table.DownsideRisk:

                           SPX.cc CCMP.cc SX5E.cc
Semi Deviation                 0.0095  0.0114  0.0111
Gain Deviation                 0.0096  0.0117  0.0114
Loss Deviation                 0.0102  0.0116  0.0113
Downside Deviation (MAR=210%)  0.0142  0.0161  0.0161
Downside Deviation (Rf=0%)     0.0094  0.0113  0.0112
Downside Deviation (0%)        0.0094  0.0113  0.0112
Maximum Drawdown               0.5678  0.6103  0.6219
Historical VaR (95%)          -0.0203 -0.0260 -0.0249
Historical ES (95%)           -0.0317 -0.0370 -0.0372
Modified VaR (95%)            -0.0193 -0.0231 -0.0237
Modified ES (95%)             -0.0273 -0.0293 -0.0330

but, let's say I ran the individual example either as an lapply or for loop as part of a broader analytical program - what I would like to do is extract each of the Historical VaR (95%) figures from the output of the table.DownsideRisk function as the loop / apply function is running on each element and place those extracted values in a table in the .GlobalEnv() and then sort that table along the lines of (Low to High)

           Historical VaR (95%)
SPX.ccl    -.0203
SX5E.ccl   -.0249
CCMP.ccl   -.0260

I know this will seem rather basic to data frame / table users but any assistance is much appreciated.

Cheers.

n.e.w
  • 1,128
  • 10
  • 23
  • 1
    I downvoted this question because after reading it I still don't know what your exact goal is and how it relates to the Performance Analytics package. A bounty doesn't fix that. – Roland Jul 29 '13 at 12:18
  • Roland, it's not about the PA package; it's just using PA as an illustration that I thought would be generally applicable to the community. All I'm trying to do is to 'loop over' a set of returns and - as each set of results is returned - extract a particular value from that set of results and then place the extracted result into a data.frame / table so that I can then view / sort those extracted results after the loop has finished running. I hope that clarifies. – n.e.w Jul 29 '13 at 22:46
  • I also have no clue what it is you are trying to accomplish or what the need here is. Perhaps clarifying the question would get you more answers – Ricardo Saporta Aug 02 '13 at 22:54

1 Answers1

2

I'm not sure what your intent is with the GlobalEnv, but this might be of help:

swapped = data.frame(t(xts))
ordered = swapped[with(swapped, order(Historical.VaR..95..)),]
result = subset(ordered, select=Historical.VaR..95..)
Aert
  • 1,989
  • 2
  • 15
  • 17
  • I think this works in the instance where the securities are *all* in an xts...my question was more if you are returning the output *by security*... ie, in this case SPX, followd by CCMP, followed by SX5E...and as each set of results is generated, the VaR is extracted and placed into a table with a new row being added for each new security parsed. – n.e.w Jul 25 '13 at 04:56
  • You can just use the above, and `rbind()` the resulting dataframes together. – Aert Jul 25 '13 at 06:00
  • Yeh, that's still too static; needs to be dynamically generating the table after the 'main' program has pulled out each VaR figure. I like the approach you've taken -- and it's certainly useful on an interactive basis -- but I think there's something that will work better programmatically and be more computationally efficient. – n.e.w Jul 25 '13 at 06:14
  • 1
    @n.e.w What you say you want (dynamically adding rows to a table as they are computed) will be the _most inefficient way possible_ of doing this in R. Growing objects will always be very, very slow. – joran Jul 29 '13 at 14:39
  • Thanks, Joran. So what would you suggest as an alternative? – n.e.w Jul 29 '13 at 22:46
  • 3
    @n.e.w I can't offer anything because (a) I don't know anything about finance, and (b) you haven't provided enough of a clear, reproducible example for me to understand what you're trying to do. – joran Jul 30 '13 at 17:20
  • Hi n.e.w., I'm also confused - you can use the code I suggested to get the results one by one, and then put them together as part of a loop. You say that's not efficient, which I agree with. But not because the solution isn't efficient, but the way you intend to use it. – Aert Aug 01 '13 at 23:43