Sunday, June 19, 2016

using cor() and sd() function customer built function and passing that function to rollapply()

Leave a Comment

using base, and xts libaries

i have constructed a function

    hedgeratio <- function(x,...){                    cor(x[,1],x[,2])*(sd(x[,1]/sd(x[,2])))                    } 

then passed a very simple xts object

rollapply(data, width = 3, FUN =hedgeratio, by.column = FALSE ) 

to data that looks like this

   dput(data) structure(c(0.52, 0.51, 0.51, 0.51, 0.51, 0.52, 0.53, 0.51, 0.51,  0.51, 0.51, 0.52, 0.52, 0.52, 0.52, 0.52, 0.52, 0.51, 0.52, 0.52,  0.52, 0.52, 0.51, 0.55, 0.51, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,  0.52, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.52, 0.5, 0.51, 0.51, 0.55,  0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.51, 0.51, 0.51, 0.52,  0.52, 0.51, 0.52, 0.52, 0.53, 0.52, 0.52, 0.52, 0.51, 0.53, 0.52,  0.51, 0.51, 0.52, 0.51, 0.51, 0.51, 0.52, 0.51, 0.53, 0.51, 0.54,  0.54, 0.54, 0.52, 0.52, 0.52, 0.53, 0.51, 0.51, 0.51, 0.51, 0.51,  0.52, 0.53, 0.53, 0.53, 0.52, 0.53, 0.52, 0.53, 0.53, 0.53, 0.53,  0.56, 0.54, 0.54, 0.52, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54,  0.54, 0.53, 0.53, 0.52, 0.54, 0.53, 0.54, 0.52, 0.53, 0.54, 0.53,  0.54, 0.53, 0.54, 0.53, 0.54, 0.53, 0.54, 0.53, 0.54, 0.53, 0.54,  0.53, 0.54, 0.53, 0.54, 0.53, 0.54, 0.53, 0.54, 0.53, 0.54, 0.53,  0.54, 0.53, 0.54, 0.53, 0.54, 0.53, 0.54, 0.52, 0.5, 0.5, 0.54,  0.54, 0.54, 0.52, 0.51, 0.52, 0.53, 0.52, 0.52, 0.52, 0.53, 0.52,  0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.53, 0.54, 0.52,  0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.52, 0.54, 0.54, 0.53,  0.53, 0.54, 0.52, 0.49, 0.5, 0.53, 0.5, 0.5, 0.5, 0.5, 0.52,  1.05, 1.05, 1.05, 1.05, 1.05, 1.04, 1.03, 1.05, 1.05, 1.05, 1.05,  1.04, 1.04, 1.04, 1.04, 1.04, 1.04, 1.05, 1.04, 1.04, 1.04, 1.04,  1.05, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.99, 1, 1, 1.03, 1.03, 1.03,  1.03, 1.04, 1.03, 1.03, 1.03, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98,  0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.97,  0.97, 0.97, 0.98, 0.98, 0.97, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98,  0.98, 0.98, 0.98, 0.96, 0.98, 0.96, 0.96, 0.96, 0.98, 0.98, 0.98,  0.97, 0.98, 0.98, 0.98, 0.98, 0.98, 0.97, 0.96, 0.96, 0.96, 0.97,  0.96, 0.97, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.98, 0.96,  0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.97, 0.97, 0.98, 0.96,  0.97, 0.96, 0.98, 0.97, 0.96, 0.97, 0.96, 0.97, 0.96, 0.97, 0.96,  0.97, 0.96, 0.97, 0.96, 0.97, 0.96, 0.97, 0.96, 0.97, 0.96, 0.97,  0.96, 0.97, 0.96, 0.97, 0.96, 0.97, 0.96, 0.97, 0.96, 0.97, 0.96,  0.97, 0.96, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.96, 0.97, 0.96,  0.95, 0.96, 0.96, 0.96, 0.96, 0.96, 0.98, 0.98, 0.98, 0.98, 0.98,  0.98, 0.98, 0.98, 0.98, 1.03, 1.02, 1.02, 1.04, 1.04, 1.04, 1.04,  1.04, 1.04, 1.04, 1.04, 1.04, 1.02, 1.02, 1.04, 1.04, 1.03, 1.05,  1.05, 1.04, 1.06, 1.04, 1.04, 1.04, 1.04, 1.06), .Dim = c(200L,  2L), .Dimnames = list(NULL, c("fg", "gk"))) 

then i get an error

Error in `[.xts`(x, , 2) : subscript out of bounds 

not sure what that even means. I've looked it up found different examples but I've na.omit(data). Just not sure whats going on here. Any help would be appreciated. Any problems or updates need with my post I will be quick to fix, just let me know.

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment