4

I have xts time-series object for 10 days of data. The data is sampled at minutes frequency. Therefore, for each day, I have 1440 observations. I need to coerce xts to ts object so that I can use stl function as used in the example. But, On coercion, R generates error as

ts(min_data,start=start(min_data),end = end(min_data),frequency=10)
Error in ts(min_data, start = start(min_data), end = end(min_data), frequency = 10) : 
  invalid time series parameters specified

I set frequency to 10, since I am using data of 10 days. I am not sure whether it should be 10 or 1440. Can anyone help me to fix this error.

MWE is as

library(xts)
timevalues="20150101 0000/20150110 2359"
timesequence<- timeBasedSeq(timevalues)
min_data<-xts(rnorm(14400),timesequence)
ts_data<- ts(min_data,start=start(min_data), end = end(min_data),frequency=10)

UPDATE Although I am able to plot the graph using stl function as suggested by @Pascal, but still I am missing the time component in the x-axis of the graph. Any help will be greatly appreciated.

6
  • Not sure what you want to do with a ts object, but you should keep your xts to manage your 10-minute data. Or if you really need a ts object, I guess the closest you will get is with as.ts(min_data).
    – user3710546
    Feb 29, 2016 at 9:55
  • I want to use stl() function of the basic stats package. I tried as.ts() as well Feb 29, 2016 at 10:06
  • 1
    Then try ts_data <- ts(as.numeric(min_data), frequency = 1440); plot(stl(ts_data, s.window = "per")).
    – user3710546
    Feb 29, 2016 at 10:15
  • Thanks, it solves one part of the problem. But, important part of information is lost, i.e., timestamp. I am following example from /www.otexts.org/fpp/6/1. Anyways I need to work on the timestamp part Feb 29, 2016 at 10:30
  • So please make your question specific next time. You didn't say neither you want to use stl nor you want to work on timestamp in your OP. However, your time stamp are timesequence, as NROW(stl(ts_data, s.window = "per")$time.series) is 14400.
    – user3710546
    Feb 29, 2016 at 10:34

5 Answers 5

12

I recently have discovered a package called "tsbox".

It promises easy conversion between time series types. (here a tutorial: https://cran.r-project.org/web/packages/tsbox/vignettes/tsbox.html)

Might be useful in cases like this one.

Here an example:

library(tsbox)
nowTS <-ts_ts(formerXTS)

or the other way round if you want to convert the ts back to an xts series

library(tsbox)
nowXTS <-ts_xts(nowTS)
1
  • Handy package. Thanks!
    – Jason
    Dec 24, 2020 at 11:59
4

An xts-only solution, based on an idea I had from looking at Pascal's answer.

library(xts)
set.seed(42)
timevalues = "20150101 0000/20150110 2359"
timesequence <- timeBasedSeq(timevalues)
min_data <- xts(rnorm(14400),timesequence)

ts_data <- ts(as.numeric(min_data), frequency = 1440)
out <- stl(ts_data, s.window = "per")

ts_out <- merge(min_data, out$time.series)
plot.zoo(ts_out)

enter image description here

2
  • Should I infer from your answer that it is impossible to retain both data and index in ts object while coercing from xts object Mar 2, 2016 at 5:05
  • 1
    @HaroonRashid: ts objects do not have an index like xts/zoo objects do. They don't need one, because ts objects can only contain strictly regular data. So they just have an attribute with the start, end, and interval. print.ts can print the times nicely for yearly, quarterly, and monthly data, but I'm not sure they can print intraday times nicely. Mar 2, 2016 at 12:01
3
library(xts)
library(ggplot2)
library(reshape2)

set.seed(42)
timevalues = "20150101 0000/20150110 2359"
timesequence <- timeBasedSeq(timevalues)
min_data <- xts(rnorm(14400),timesequence)

ts_data <- ts(as.numeric(min_data), frequency = 1440)
out <- stl(ts_data, s.window = "per")
time.series <- as.data.frame(cbind(ts_data, out$time.series))
colnames(time.series) <- c("Data", "Seasonal", "Trend", "Remainder")
time.series$Date <- timesequence
time.series <- melt(time.series, 'Date')

ggplot(time.series, aes(x=Date, y=value)) + 
  geom_line() +
  facet_free(variable~.)

enter image description here

1

If you hav a xts data in monthly, quarterly and yearly frequency it maby be useful (use packages: xts, stats, data.table, zoo)

xts_ts <- function(xts_data) {
  freq_list <-
    data.table::data.table(
      freq = c('month', 'quarter', 'year'),
      freq_n = c(12L, 4L, 1L),
      freq_format = c('%Y, %m', '%Y, %q', '%Y')
    )

  d_ferq <- xts::periodicity(xts_data)[["label"]]
  freq_n <- freq_list[freq == d_ferq, freq_n]
  freq_format <- freq_list[freq == d_ferq, freq_format]

  # Put NA if missing date
  empty <-
    zoo::zoo(order.by = seq.Date(zoo::index(xts_data)[1], zoo::index(xts_data)[nrow(xts_data)], by = d_ferq))
  no_misssing <- merge(xts_data, empty)

  if (d_ferq == 'quarter') {
    start_date <-
      format(zoo::as.yearqtr(xts::periodicity(xts_data)[["start"]]), freq_format)

  } else {
    start_date <-
      format(zoo::as.Date(xts::periodicity(xts_data)[["start"]]), freq_format)
  }

  stats::ts(zoo::coredata(no_misssing),
     start = as.integer(strsplit(start_date, split = ',')[[1]]),
     frequency = freq_n)
}
0

i think what you are looking for is the below:

xts2ts <- function(XD) {
             maxRow <- nrow(XD)
             startYM <- c(.indexyear(XD[1]) + 1900, .indexmon(XD[1]) + 1L)
             endYM <- c(.indexyear(XD[maxRow]) + 1900, .indexmon(XD[maxRow]) + 1L)
             ts(as.numeric(XD), start = startYM, end = endYM, frequency = 12L)
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.