0

hey does anyone know how to loop through the MedRV function in the RTAQ package? I have

##find days in data
ddx.f = endpoints(full, on="days");
days.full = format(index(full)[ddx.f], "%Y-%m-%d");

for (day in days.full) {
  x = full[day]

}

to extract each day but don't know what to add to loop through the MedRV function per each day. Any ideas? And also does anyone know is there a significance test in R to test for jumps?

Thanks in advance.

number8
  • 161
  • 8
  • Please do read the [link I gave you](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in your last question – GSee Jun 16 '12 at 00:38

2 Answers2

0

The idea is the same as your last question

Split the data into days, and apply a function to each day. Below split(dxts, "days") will create a list where each element is 1 day of data. lapply will apply a function to each day.

set.seed(123)
full <- .xts(rnorm(2880), 1:2880*5*60)
mrv <- lapply(split(full, "days"), function(x) {
    #return an xts-object, which requires a timeBased index
    xts(MedRV(x), end(x)) #use the last timestamp of the day
})

Then rbind the results into a single xts object

do.call(rbind, mrv)
#                         [,1]
# 1969-12-31 23:55:00  58.2340
# 1970-01-01 23:55:00 268.5672
# 1970-01-02 23:55:00 260.3016
# 1970-01-03 23:55:00 310.5664
# 1970-01-04 23:55:00 302.1562
# 1970-01-05 23:55:00 272.9567
# 1970-01-06 23:55:00 291.0333
# 1970-01-07 23:55:00 309.7571
# 1970-01-08 23:55:00 229.9853
# 1970-01-09 23:55:00 298.3878
# 1970-01-10 18:00:00 215.6014

Edit/Alternate syntax

mrv <- lapply(split(full, "days"), MedRV)
names(mrv) <- index(full)[endpoints(full, on="days")]
as.xts(do.call(rbind, mrv))
Community
  • 1
  • 1
GSee
  • 48,880
  • 13
  • 125
  • 145
  • really? thanks for your help again, sorry I have so much to learn in such little time. – number8 Jun 16 '12 at 00:44
  • Are you familiar with the package at all? I am trying to test for jumps for a dissertation, but there does not seem to be a significance test – number8 Jun 16 '12 at 00:47
  • I'm sorry. I am not very familiar with that package. You could pose that as a new question, OR you might have more luck asking about this on the [r-sig-finance mailing list](https://stat.ethz.ch/mailman/listinfo/r-sig-finance). (but don't crosspost to both) – GSee Jun 16 '12 at 00:59
  • I run: ##Realized Variance; lapply(split(r.full, "days"), rRealizedVariance) -> full.obs.realized; and when I try to plot object, I get error: "Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' is a list, but does not have components 'x' and 'y'" How do I convert it to an object with 'x' and 'y'? And do you know is it possible to take ,lapply(split(r.full, "days"), MedRV) -> full.obs.medrv from full.obs.realized so as to see the differences each day? **I have 'r.full' because i forgot to take returns** – number8 Jun 16 '12 at 01:49
  • That's because you were trying to plot a list. You need to convert it to something printable (e.g. `unlist`). I edited this answer to return an `xts` that you can plot – GSee Jun 16 '12 at 02:10
  • to answer your question about calculating differences, I think you can just subtract. If you've tried and it doesn't work then create a question showing what you tried and what you expected to see. And see that post about providing a reproducible example :-) – GSee Jun 16 '12 at 02:23
  • Hi yeah the plot worked like a dream as did the `unlist()` function, though is it true that switching to and from `xts` objects may cause your data to distort? I think the subtraction wouldn't work the first time because they were both `as.xts` but they both subtracted fine either after `unlist()` or by running your second suggested code. Thank you. – number8 Jun 16 '12 at 02:50
  • 1
    P.s sorry only beginning to understand what you had meant by reproducible example – number8 Jun 16 '12 at 02:52
  • yeah, I don't understand the question. Maybe you want to `merge` 2 xts objects and subtract one column from another. If you're having trouble, put together a question showing what you tried and what you want. – GSee Jun 16 '12 at 02:56
  • You don't understand my question? Basically before the significance test is conducted I need to see if there was any times that Median RV doesn't converge to realized variance so I needed to construct a time series of the two and take their differences and then to test if there are differences statistically different from zero. but so far so good after the code you sent helped me with. Now for the "easy" part and trying to compute the test statistic! – number8 Jun 16 '12 at 03:18
0

Another approach is to use the period.apply function, or its apply.daily wrapper. I've gotten some unexpected results in the past due to the fact that period.apply uses sapply instead of lapply (at least I think that's why), so, I usually do it the way I did in my other answer.

set.seed(123)
full <- .xts(rnorm(2880), 1:2880*5*60)
ddx.f = endpoints(full, on="days")
period.apply(full, ddx.f, MedRV)

apply.daily(full, MedRV) # same as period.apply but, endpoints are created for you
#                         [,1]
# 1969-12-31 23:55:00  58.2340
# 1970-01-01 23:55:00 268.5672
# 1970-01-02 23:55:00 260.3016
# 1970-01-03 23:55:00 310.5664
# 1970-01-04 23:55:00 302.1562
# 1970-01-05 23:55:00 272.9567
# 1970-01-06 23:55:00 291.0333
# 1970-01-07 23:55:00 309.7571
# 1970-01-08 23:55:00 229.9853
# 1970-01-09 23:55:00 298.3878
# 1970-01-10 18:00:00 215.6014
GSee
  • 48,880
  • 13
  • 125
  • 145