0
votes

I have a daily time series about the sales of a product, my series start from 01/01/2016 until 31/08/2017, my problem is that I do not know what value of frequency I should use, considering that it is a six-day week (my week starts on Monday and ends Saturday) and there is no data for Sundays.

Should be it like this ?

myts <- ts(sales, start=c(2016, 1), frequency=6)

Thanks for your help !!

2

2 Answers

1
votes

ts expects you to have values for each element of the time-series, i.e., it would expect you to have the seventh day values in the data.

One option is to expand the date index to include your missing observations. You could fill those missing observations with na.approx or na, but you can't give ts a six day week and expect it to comprehend it as a seven day cycle.

A good way to do this is to look at zoo, which has specific functions for dealing with these sorts of situations.

0
votes

It really depends on what you want to do with the data.

1) plot for example, if your objective is simply to plot it then "ts" class is not a good fit since it is not good at representing dates. Try this instead where we have defined test vector for sales and tt in the Note at the end.

library(zoo)
z <- zoo(sales, tt)
plot(z)

2) acf If you want to compute the autocorrelation function then using the plain vector sales or ts(sales) would be fine:

acf(sales)

3) StructTS If you want to fit a structural time series using StructTS then you will need to decide on the length of a cycle, i.e. does it repeat every week? quarter? year?. Typically an annual cycle is appropriate for sales but, in general, you will need two complete cycles to do anything so you don't really have enough data for that.

4) monthly/quarterly If you are willing to reduce it to monthly or quarterly data then you could use ts but you only have 20 points for monthly or 7 for quarterly. Here we have used the last point in each month:

library(zoo)

z <- zoo(sales, tt)
zm <- aggregate(z, as.yearmon, tail, 1) 
tsm <- as.ts(zm)
tsm

giving:

          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
2016 3.258097 3.931826 4.356709 4.644391 4.867534 5.049856 5.204007 5.342334
2017 5.828946 5.897154 5.968708 6.030685 6.093570 6.150603 6.204558 6.257668
          Sep      Oct      Nov      Dec
2016 5.459586 5.564520 5.659482 5.749393
2017                                    

5) weekly Another thing you could consider would be to use weekly series by just using Saturday, for example:

library(zoo)
z <- zoo(sales, tt)
zw <- z[weekdays(time(z)) == "Saturday"]

Note: We used this dummy data:

set.seed(123)
tt <- seq(as.Date("2016-01-01"), as.Date("2017-08-31"), "day")
tt <- tt[! weekdays(tt) == "Sunday"]
n <- length(tt)
sales <- log(1:n)