I am trying to calculate rolling daily correlations on 2 stock prices (type xts), AGL and BIL (OHLC data below):
library(RODBC)
library(quantmod)
library(xts)
library(TTR)
dput(my.AGL)
structure(c(28500, 27800, 28699, 28440, 28569, 28600, 26650,
27250, 26910, 27450, 28814, 27950, 28950, 28740, 29250, 28765,
27429, 27584, 27534, 28072, 27122, 27050, 28406, 28030, 28211,
27349, 26618, 26509, 26560, 27200, 27203, 27900, 28665, 28694,
28836, 27698, 27090, 26600, 27079, 27206), .Dim = c(10L, 4L), .Dimnames = list(
NULL, c("days.Open", "days.High", "days.Low", "days.Close"
)), index = structure(c(1312988460, 1313074860, 1313420460,
1313506860, 1313593260, 1313679660, 1314025260, 1314111660, 1314198060,
1314284460), tzone = "", tclass = c("POSIXt", "POSIXct")), class = c("xts",
"zoo"), .indexCLASS = c("POSIXt", "POSIXct"), .indexTZ = "", tclass = c("POSIXct",
"POSIXt"))
my.AGL
days.Open days.High days.Low days.Close
2011-08-10 17:01:00 28500 28814 27122 27203
2011-08-11 17:01:00 27800 27950 开发者_JS百科27050 27900
2011-08-15 17:01:00 28699 28950 28406 28665
2011-08-16 17:01:00 28440 28740 28030 28694
2011-08-17 17:01:00 28569 29250 28211 28836
2011-08-18 17:01:00 28600 28765 27349 27698
2011-08-22 17:01:00 26650 27429 26618 27090
2011-08-23 17:01:00 27250 27584 26509 26600
2011-08-24 17:01:00 26910 27534 26560 27079
2011-08-25 17:01:00 27450 28072 27200 27206
I then create a series using ROC:
my.AGL.roc <- ROC(my.AGL[,4])
From the feedback below, I gathered that ROC is not compatible with 2.13.1, so, to create log returns I replaced the ROC function with:
my.AGL.lret <- log(my.AGL[,4]) - log(lag(my.AGL[,4], 1)
replacing the first NA observation with:
my.AGL.lret[ is.na(my.AGL.lret) ] <- 0
my.AGL.lret
days.Close
2011-08-10 17:01:00 0.000000000
2011-08-11 17:01:00 0.025299427
2011-08-15 17:01:00 0.027050178
2011-08-16 17:01:00 0.001011175
2011-08-17 17:01:00 0.004936565
2011-08-18 17:01:00 -0.040264398
2011-08-22 17:01:00 -0.022195552
2011-08-23 17:01:00 -0.018253440
2011-08-24 17:01:00 0.017847304
2011-08-25 17:01:00 0.004679017
However, both suggestions yield the same result in terms of the error. The reason I am using xts, is that I want to merge my resulting rolling correlation with my original price series.
> rollapply(my.AGL.lret, 30, mean)
Error in `colnames<-`(`*tmp*`, value = "days.Close") :
attempt to set colnames on object with less than two dimensions
> rollmean(my.AGL.lret, 30)
Error in `colnames<-`(`*tmp*`, value = "days.Close") :
attempt to set colnames on object with less than two dimensions
I am sure I am doing something silly, but I would appreciate it if someone could please explain how the dimensions are handled? With my limited knowledge I have created a return series, which is still a time series.
dim(my.AGL.roc)
[1] 406 1
Thanks in advance Ed
There's nothing wrong with TTR::ROC
under R-2.13.1. You can use TTR::runCor
to calculate the rolling correlation between your two price series.
library(quantmod)
my.AGL <-
structure(c(32020L, 32810L, 33000L, 33394L, 33650L, 34205L, 34140L,
33400L, 34300L, 32975L, 33179L, 33450L, 33700L, 34180L, 35000L,
34140L, 33600L, 34300L, 32020L, 32460L, 32811L, 33157L, 33599L,
34205L, 33299L, 33155L, 33106L, 32850L, 33020L, 33400L, 33539L,
34000L, 34461L, 33480L, 33400L, 33250L), .Dim = c(9L, 4L), .Dimnames = list(
NULL, c("days.Open", "days.High", "days.Low", "days.Close"
)), index = structure(c(1262646025, 1262732404, 1262818810,
1262905220, 1262991623, 1263250801, 1263337207, 1263423608, 1263510020
), tzone = "", tclass = c("POSIXct", "POSIXt")), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "", class = c("xts", "zoo"))
my.AGL.roc <- ROC(Cl(my.AGL))
my.BIL.roc <- ROC(Op(my.AGL)) # since OP didn't provide BIL data
runCor(x=my.AGL.roc,y=my.BIL.roc,n=3)
# [,1]
# 2010-01-04 17:00:25 NA
# 2010-01-05 17:00:04 NA
# 2010-01-06 17:00:10 NA
# 2010-01-07 17:00:20 -0.6614544157
# 2010-01-08 17:00:23 -0.8643698058
# 2010-01-11 17:00:01 0.0001661546
# 2010-01-12 17:00:07 0.8768496736
# 2010-01-13 17:00:08 0.3459987310
# 2010-01-14 17:00:20 0.0289108044
UPDATE:
zoo's rollapply
doesn't work with xts objects in this case because of a fundamental design difference between xts and zoo objects. xts objects always have a dim
attribute, whereas zoo objects can be a vector. The rollapply
calculation "drops" to the lowest dimension, which reduces the input xts object to a vector and you can't set column names on a vector.
Adding xts rollapply
methods has been on my to-do list and this won't be a problem once those are available.
The first observation in my.AGL.roc
is probably NA
.
From the ?rollmean
:
The default method of rollmean does not handle inputs that contain NAs. In such cases, use rollapply instead.
rollapply(my.AGL.roc, 30, mean)
精彩评论