qfd
qfd

Reputation: 788

sapply with Performance Analytics data

I have a data frame consisting of returns time series which has the following columns

date x1 x3 x8 x11

x.R is my data frame consisting of returns

I would like to use the method findDrawdowns in the Performance Analytics tools and apply it to each time series. I would like to store the result in a list so that I can access all the output from findDrawdowns

sapply(x.R,  sortDrawdowns(findDrawdowns))

The above command produces the below. Not sure how I can access the values.. any help is greatly apppreciated!

             x1          x3         x8         x11      
return       Numeric,47 Numeric,47 Numeric,47 Numeric,49
from         Numeric,47 Numeric,47 Numeric,47 Numeric,49
trough       Numeric,47 Numeric,47 Numeric,47 Numeric,49 
to           Numeric,47 Numeric,47 Numeric,47 Numeric,49 
length       Numeric,47 Numeric,47 Numeric,47 Numeric,49 
peaktotrough Numeric,47 Numeric,47 Numeric,47 Numeric,49
recovery     Numeric,47 Numeric,47 Numeric,47 Numeric,49 

Upvotes: 0

Views: 110

Answers (1)

BrodieG
BrodieG

Reputation: 52647

You need a nested sapply because sortDrawdowns returns ALL your drawdowns which can't be displayed in 2 dimensions (i.e. each cell in the table you have above contains all the drawdowns. Here is a solution with made up data:

# Make up data
len=29
data <- data.frame(
  x1=rnorm(len, 0, .05),
  x3=rnorm(len, 0, .05),
  x8=rnorm(len, 0, .05),
  x11=rnorm(len, 0, .05)
)    
rownames(data) <- as.character(as.Date("2013-12-01") - (len:1))

# Get worst drawdowns    
sapply(
  names(data),    # for each entry in df
  function(x) {   
    sapply(       # cycle through each item in a drawDown object
      sortDrawdowns(findDrawdowns(data[, x, drop=F])),  # get drawdows sorted
      `[[`, 1                                           # extract 1st item
    )
  } 
)
#                      x1         x3         x8        x11
# return       -0.1887651 -0.3425831 -0.1592202 -0.2928802
# from         17.0000000  5.0000000 16.0000000  1.0000000
# trough       20.0000000 24.0000000 27.0000000 16.0000000
# to           25.0000000 30.0000000 30.0000000 30.0000000
# length        9.0000000 26.0000000 15.0000000 30.0000000
# peaktotrough  4.0000000 20.0000000 12.0000000 16.0000000
# recovery      5.0000000  6.0000000  3.0000000 14.0000000

Upvotes: 0

Related Questions