Bogaso
Bogaso

Reputation: 3318

Some functions in PerformanceAnalytics package are failing when use with zoo::rollapply

I am trying to use the zoo::rollapply function to chronologically calculate Sterling Ratio using the package PerformanceAnalytics. Below is my calculation -

library(zoo)
library(PerformanceAnalytics)

Orig_Data = structure(c(12.05, 12.75, 12.75, 12.55, 13.05, 14.15, 16.2, 16.5, 
15.95, 16.9, 17.3, 17.5, 16.2, 16.7, 16.4, 15.95, 17.15, 16.2, 
16.5, 15.35, 15.8, 16.1, 16.35, 18.3, 18.2, 17.35, 17.95, 18.35, 
18.3, 17.9, 18.1, 20.65, 20.45, 20.3, 20.65, 18.7, 18.95, 18.3, 
16.65, 17.05, 17.15, 16.9, 17.15, 16.55, 16.2, 16, 16.8, 16.8, 
16.9, 16.75, 16.4, 15.8, 15.2, 15.15, 16.5, 16.75, 16.5, 15.85, 
15.75, 15.15, 15.2, 15.45, 16.15, 16.1, 16.05, 15.7, 15.75, 15.85, 
15.9, 15.5, 15.5, 15.7, 15.55, 15.45, 15.2, 14.75, 14.7, 14.1, 
14.2, 14.1, 14.25, 14.35, 14, 13.4, 13.4, 13.05, 12.9, 12.8, 
12.5, 12.45, 12.45, 12.6, 12.15, 12, 12.1, 12.4, 13, 12.9, 12.9, 
12.7, 13.15, 11.75, 11.25, 11.05, 10.95, 11.4, 11.4, 11.45, 11.2, 
10.95, 10.65, 9.9, 9.65, 9.65, 9.05, 8.6, 8.75, 8.8, 9.3, 9.85, 
10.15, 9.7, 9.8, 9.85, 10.25, 11.15, 12.25, 12.75, 12.55, 12, 
12.85, 12.5, 12.3, 12.6, 12.25, 11.65, 11.4, 11.2, 11.15, 10.75
), index = structure(c(15341, 15342, 15343, 15344, 15345, 15348, 
15349, 15350, 15351, 15352, 15355, 15356, 15357, 15358, 15359, 
15362, 15363, 15364, 15366, 15369, 15370, 15371, 15372, 15373, 
15376, 15377, 15378, 15379, 15380, 15383, 15384, 15385, 15386, 
15387, 15391, 15392, 15393, 15394, 15397, 15398, 15399, 15400, 
15401, 15404, 15405, 15406, 15408, 15411, 15412, 15413, 15414, 
15415, 15418, 15419, 15420, 15421, 15422, 15425, 15426, 15427, 
15428, 15429, 15432, 15433, 15434, 15439, 15440, 15441, 15442, 
15443, 15446, 15447, 15448, 15449, 15450, 15453, 15454, 15455, 
15456, 15457, 15460, 15462, 15463, 15464, 15467, 15468, 15469, 
15470, 15471, 15474, 15475, 15476, 15477, 15478, 15481, 15482, 
15483, 15484, 15485, 15488, 15489, 15490, 15491, 15492, 15495, 
15496, 15497, 15498, 15499, 15502, 15503, 15504, 15505, 15506, 
15509, 15510, 15511, 15512, 15513, 15516, 15517, 15518, 15519, 
15520, 15523, 15524, 15525, 15526, 15527, 15530, 15531, 15532, 
15533, 15534, 15537, 15538, 15539, 15540, 15541, 15544), class = "Date"), class = "zoo")

rollapply(Orig_Data,
            width = 132,
            align = 'right',
            function(Data) {
                    Start = as.character(index(Data)[1])
                    End   = as.character(last(index(Data)))

                    SterlingRatio = as.vector(SterlingRatio(R = diff(log(Data))))

                    return(data.frame('Start' = Start,
                                        'End' = End,
                                        'SterlingRatio' = SterlingRatio, check.names = FALSE))

                })

This code is failing with an error -

Error in try.xts(x, error = "'x' needs to be timeBased or xtsible") : 
  'x' needs to be timeBased or xtsible

Couple of issues with the result -

  1. Even if I dont use the function SterlingRatio() above code is not retuning correct values for Start and End
  2. Above error is with SterlingRatio() and SharpeRatio() functions. If I use some other function e.g. SortinoRatio() it works fine.

Any idea what is going wrong would be highly helpful.

Thanks and regards,

Upvotes: 1

Views: 136

Answers (1)

phiver
phiver

Reputation: 23608

Both SterlingRatio and SharpeRatio have some internal checks that use of the function xtsible from the xts package. This just checks if the data is in an xts format. Which in this case it is not, it is a zoo object. Example below would return data.

rollapply(xts::as.xts(Orig_Data),
          width = 132,
          align = 'right',
          SterlingRatio)

But using as.xts inside the function you have created above, would return an error because you would be trying to merge a data.frame with an xts object. So first you need to know how you want your output to look like.

Upvotes: 1

Related Questions