0

Data: DOWNLOAD .TXT

Code:

data = read.table("DistrBdaily1yrs.txt", header = TRUE, sep = "", dec = ",")
data$DATE = as.Date(as.character(data$DATE),format="%Y%m%d") 
dataXts = xts(data$QUANTITY,data$DATE, frequency = 6)   

tseries = ts(dataXts, start = start(dataXts), end = end(dataXts), frequency = 6)

What I'm trying to do is to convert the xts dataXts object to a ts object with correct starting and ending date in order to use the decompose function. In this case start = start(dataXts) and end = end(dataXts) give me the right starting and ending date but tseries doesn't recognize the data column in dataXts and then think that all is data.

How can I fix this?

gmeroni
  • 571
  • 4
  • 16
  • Even if this somehow "works", your series is not strictly regular. So you will not be able to use the `decompose` function on it. See [How to analyse irregular time-series in R](http://stackoverflow.com/q/12623027/271616). – Joshua Ulrich Sep 08 '15 at 22:36
  • What do you mean with strictly regular? I've an observation for each day of the year, so is regularly spaced... – gmeroni Sep 09 '15 at 07:49

1 Answers1

3

I am not sure I was able to "FORCE" xts to ts but i got the decompose part to function:

library("data.table")
# I was unable to read-in using read.table() for some reason.... used fread() as it is much faster
data <- fread("DistrBdaily1yrs.txt", header = TRUE, sep="\t")

# Set column names to the ones I saw on dropbox, as i was unable to read-in header for some reason!
colnames(data) <- c("DATE", "QUANTITY")

# Keep as-is 
data$DATE = as.Date(as.character(data$DATE),format="%Y%m%d") 
dataXts = xts(data$QUANTITY,data$DATE, frequency = 6)   

# Not sure what the "QUANTITY" Column means but it must be turned into "numeric"
# You can see this post on how to do it if the following is unsatisfactory:
# http://stackoverflow.com/questions/3605807/how-to-convert-numbers-with-comma-inside-from-character-to-numeric-in-r
a<-as.numeric(gsub(",",".",dataXts))

dataXts <- reclass(a, match.to=dataXts); colnames(dataXts)<- "QUANTITY"


# Now convert it to timeSeries
timeseries <- ts(dataXts,frequency=6)

# decompose
decompose(timeseries)

Also, when I convert xts to ts I assume that it will use the first and last dates in order to construct the ts which is why i left out start = start(dataXts), end = end(dataXts) in the ts() function. Also see ?ts since you cannot pass Dates in the start or end criteria, rather:

Either a single number or a vector of two integers, which specify a natural time unit and a (1-based) number of samples into the time unit.

You can always convert back to xts using reclass:

# for example: Say you only want the trend
reclass(decompose(timeseries)$trend,match.to=dataXts)
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Rime
  • 912
  • 2
  • 11
  • 39