Search code examples
rcsvexportxtszoo

R: Export specific value class from xts objects


I have several xts output objects that can contain different "value" classes. All xts objects contain the value class datetime and spot with same length. Some xts objects contain another value class par

E.g. for a xts object with par value: (Values are: "datetime", "spot" and "par")

$datetime           $spot
2017-10-02 09:05:00 4.503936e-04
2017-10-02 09:10:00 4.799895e-04
2017-10-02 09:15:00 5.181447e-04
2017-10-02 09:20:00 5.734970e-04
2017-10-02 09:25:00 5.637900e-04
2017-10-02 09:30:00 4.684099e-04
2017-10-02 09:35:00 5.149570e-04
2017-10-02 09:40:00 5.459784e-04

$par
        sigma      sigma_mu       sigma_h       sigma_k           phi           rho           mu1           mu2      delta_c1 
 0.0001963601  0.1727417926  0.0070247195  1.2313740300  0.1940041110  0.2426273212  0.6050628200  0.1732629813  0.3489579734 
     delta_c2      delta_c3      delta_c4      delta_c5      delta_s1      delta_s2      delta_s3      delta_s4      delta_s5 
-1.5338494995  1.0146674063  0.8648589185  0.2488922309 -1.3362789351  1.1684672029  2.0240062847  0.2421184159 -0.4020884885

I want to export the values for datetime and spot.
I use the following method to save the xts objects as csv.

for (n in c("vol1", "vol2", "vol3", "vol4", "vol5", "vol6", "vol7", "vol8")) {
  v = get(n)
  myFile <- paste0("Vola_Est", "_", n, ".csv")
  write.zoo(as.xts(do.call(rbind, unname(v))), file=myFile, sep=",")
}

Where vol{i} is the xts object. This works fine for those objects not containing the value class par. I guess this because of the "structural break" in the way the data is presented in a xts object.

Find below a sample code that produces the input data

library("highfrequency")
library("xts")
library("forecast")

time_index <- seq(from = as.POSIXct("2012-05-15 00:00:00"), 
                  to = as.POSIXct("2012-06-03 23:59:00"), by = "min")
t <- 1:length(time_index)

set.seed(1234)
value <- ts(15 + 0.001*t + 10*sin(2*pi*t/(length(t)/5)) + rnorm(length(t)),  freq=length(time_index)/5)

sample_file <- xts(value, order.by = time_index)
plot(value)

vol1 <- spotvol(sample_file)

# Compare to stochastic periodicity

init = list(sigma = 0.03, sigma_mu = 0.005, sigma_h = 0.007,
            sigma_k = 0.06, phi = 0.194, rho = 0.986, mu = c(1.87,-0.42),
            delta_c = c(0.25, -0.05, -0.2, 0.13, 0.02), delta_s = c(-1.2,
                                                                    0.11, 0.26, -0.03, 0.08))

# next method will take around 110 iterations
vol2 <- spotvol(sample_file, method = "stochper", init = init, marketopen = "00:00:00", marketclose = "23:59:00", tz = "GMT")

for (n in c("vol1", "vol2")) {
  v = get(n)
  myFile <- paste0("Vola_Est", "_", n, ".csv")
  write.zoo(as.xts(do.call(rbind, unname(v))), file=myFile, sep=",")
}

Solution

  • Found a solution. Didn't recognize that value classes in xts objects can be called like columns in a data frame.

    Used the following loop to export the specific content from xts to csv.

    for (n in c("vol1", "vol2", "vol3", "vol4", "vol5", "vol6", "vol7", "vol8")) {
      v = get(n)
      extr <- v[j = 'spot']
      vola <- as.data.frame(extr)
      myfile <- paste("Vola_Est_1Min_",n,".csv", sep="")
      write.csv(vola, myfile)
    }