Search code examples
rtime-seriesxtswavelet

Convert multivariate XTS to TS in R


I wish to compute the wavelet transform of a multivariate time series dataset. I plan to use the wavethresh package and specifically the modwt() function. The help file for this function specifies that the object be either "A univariate or multivariate time series. Numeric vectors, matrices and data frames are also accepted."

Currently my dataset is in xts zoo format where the time is in 15 min intervals and I wish to convert it to ts but I am having great difficulty.

I have tried the following:

modwtCoeff <- modwt(as.ts(wideRawXTS,
+                           start = head(index(wideRawXTS), 1), 
+                           end = tail(index(wideRawXTS), 1),
+                           frequency = 1),
+                     filter = "la8",
+                     n.levels = "10",
+                     boundary = "periodic",
+                     fast = TRUE)

> class(wideRawXTS)
[1] "xts" "zoo"

where head(index(wideRawXTS,1),1) returns "2017-01-20 16:30:00 GMT" and tail(index(wideRawXTS,1),1) returns "2017-02-03 16:00:00 GMT"

I receive the following error as a result of the lines above:

Error in ts(coredata(x), frequency = frequency(x), ...) : 
  formal argument "frequency" matched by multiple actual arguments

The error lies in the xts to ts conversion as I removed the modwt wrapper function and I still get the same error. After further Googling I came across this article https://www.r-bloggers.com/preventing-argument-use-in-r/ but I don't fully get it. My guess is that I possibly need to decompose the conversion into individual steps to avoid errors from using some arguments in the as.ts function.

Can someone give me a bit of direction as to where I am going wrong in the conversion? In order to provide a reproducible example here is a link to a dput of the wideRawXTS object.


Solution

  • The general function to compute a frequency is:

    frequency = number_of_events / time_interval

    As your data have 1343 rows for a time interval of 14 days, the frequency depend on what is your time unit.

    Time unit: Day

    In this case, the frequency is:

     1343/14 = 95.93 => 96
    

    That's mean, you make 96 measurments per day.

    Time unit: Hour

    In this case, the frequency is:

     1343/(14*24) = 3.99 => 4
    

    That's mean, you make 4 measurments per hour.

    Time unit: 15 Minute

    In this case, the frequency is:

     1343/(14*24*4) = 0.999 => 1
    

    That's mean, you make one measurment every 15 minutes.