1

Why is ets telling me there is no seasonality to my data?

> hwin <- ets(train.ts, model = "MAA")
Error in ets(train.ts, model = "MAA") : Nonseasonal data

Is there not a clear, daily seasonality to this data? Please note that the image below is of my full dataset, where as the sample code below only contains a partial dataset.

enter image description here

I know there isn't much trend, but seasonality should be there, right?

This is my code:

d <- structure(list(Date = structure(c(17349, 17350, 17351, 17352, 
                                       17353, 17354, 17355, 17356, 17357, 17358, 17359, 17360, 17361, 
                                       17362, 17363, 17364, 17365, 17366, 17367, 17368, 17369, 17370, 
                                       17371, 17372, 17373, 17374, 17375, 17376, 17377, 17378, 17379, 
                                       17380, 17381, 17382, 17383), class = "Date"), Ratio = c(67, 50, 
                                                                                               67, 50, 100, 50, 33, 67, 0, 0, 0, 0, 100, 75, 0, 0, 75, 100, 
                                                                                               67, 33, 33, 33, 50, 50, 67, 100, 67, 50, 25, 25, 33, 33, 100, 
                                                                                               33, 0)), .Names = c("Date", "Ratio"), row.names = 183:217, class = "data.frame")

library(xts)
dates = as.Date(d$Date,"%Y-%m-%d")
ratios.ts = xts(d$Ratio,dates)

library("forecast")
library("zoo")

plot(ratios.ts)

nValid <- 6
nTrain <- length(ratios.ts) - nValid
train.ts <- window(ratios.ts, start = as.Date("2017-07-02"), end = as.Date("2017-07-02") + nTrain)
valid.ts <- window(ratios.ts, start = as.Date("2017-07-02") + nTrain + 1, end = as.Date("2017-07-02") + nTrain + nValid)

hwin <- ets(train.ts, model = "MAA")
hwin.pred <- forecast(hwin, h = nValid, level = 0)

plot(hwin.pred, ylim = c(-20,120), ylab = "Ratio", xlab = "Date", bty = "l", xaxt = "n", main = "", flty = 2)
lines(hwin.pred$fitted, lwd = 2, col = "blue")

Thank you!

  • 1
    You didn't tell xts() that the data were periodic. The frequency argument allows you to do this. You need to tell it how many observations correspond to the unit time interval. If you had monthly data this would be 1/12 say. Do read ?xts though for details of the specific requirements here; I may have the input incorrect for xts as I use these things too infrequently for it to stick and there are several of these time-series data objects that have similar but not unique interfaces and arguments. This is also off topic for [stats.se] as it is about a programming problem not a stats one. – Gavin Simpson Aug 31 '17 at 18:37
  • Could you post your data? – kjetil b halvorsen Aug 31 '17 at 18:37
  • @GavinSimpson Thanks for that I'll try it out. Funnily enough, I first posted this question on stackoverflow.com where someone said, "this isn't a programming question, it's a stats question." Maybe there should be an intermediary site lol! – user1477388 Aug 31 '17 at 18:43
  • @GavinSimpson Upon changing my code to ratios.ts = xts(d$Ratio,dates,frequency = 365.25) addressing the frequency, I still get the same error. Any ideas? – user1477388 Aug 31 '17 at 18:45
  • @kjetilbhalvorsen Sorry, I can't post the full data set; however, the sample data should be sufficient as the error I am hoping to resolve occurs for both sets. Thanks. – user1477388 Aug 31 '17 at 18:45
  • 1
    Try ets(as.ts(train.ts), model = "MAA"), assuming train.ts is a zoo object by the time you come to model it. ?ets indicates that x is supposed to be a vector or 'ts' classed object. It may be ets() doesn't know how to handle what you are passing it. – Gavin Simpson Aug 31 '17 at 19:07
  • @GavinSimpson train.ts is not a zoo object; it's an xts object. ets(as.ts(train.ts), model = "MAA") seems to result in the same error but I think you are on the right track thinking that ets doesn't understand what I am passing it which is probably due to the fact that my data is daily, not weekly, monthly or yearly. – user1477388 Aug 31 '17 at 19:33
  • @user1477388 If it weren't coercible to zoo object then why load zoo, and the window method wouldn't have worked. I was unsure if the zoo method for window would preserve the xts- and zoo-ness of the underlying series or just return a zoo object. I'm also not sure you can specify a decimal for frequency, which may still be the cause of the problem... – Gavin Simpson Aug 31 '17 at 19:42
  • @GavinSimpson I think I had zoo in there from a previous attempt; it should be disregarded. Something interesting, though: It seems like xts understands that the data is daily. A quick call outputs that it determines the data to be daily. So, why isn't ets getting the memo? > periodicity(ratios.ts) Daily periodicity from 2017-01-01 to 2017-08-06 – user1477388 Aug 31 '17 at 19:58

1 Answers1

4

You have multiple issues.

  1. As Gavin Simpson points out, you don't tell R or ets() that your data have a seasonal cycle.

As a matter of fact, I'm not sure what seasonal cycle you believe to be obvious about your data: weekly (frequency=7), monthly (frequency=30 or some such) or yearly (frequency=365)?

  1. If we do specify a seasonal cycle, let's say weekly,

    hwin <- ets(ts(train.ts[,1],frequency=7), model = "MAA")
    

    then ets() quite understandably complains about specifying a multiplicative seasonality (the initial "M") for data that contains zeros. If you specify additive seasonality, ets() will fit it without errors:

    hwin <- ets(ts(train.ts[,1],frequency=7), model = "MAA")
    

    I'm a bit unsure whether this makes a lot of sense, though, because:

  2. Just descriptively, at least weekly seasonality is far from apparent in your data:

    seasonplot(ts(train.ts[,1],frequency=7))
    

    enter image description here

Stephan Kolassa
  • 123,354
  • Thanks for answering, that sheds some light. Let me clarify: The seasonality I was referring to was daily, not weekly. Do you have any ideas how to specify daily frequency? I have at least one years worth of data from 2016-08-11 to 2017-08-11. What would the ts configuration look like for something like that? – user1477388 Aug 31 '17 at 19:14
  • 1
    What's the seasonal cycle? Your data are daily. How many (daily) data points make one cycle? Is it 7 days (weekly seasonality), 14 (biweekly), ~30 (monthly) or 365 (yearly)? Any one, or even multiple ones, can make sense. Or do you actually have subdaily data that repeat day-over-day, e.g., hourly data with a cycle length of 24, or half-hourly data with a cycle length of 48? In that case, you'd need to also specify your subdaily timestamps. – Stephan Kolassa Sep 01 '17 at 07:31
  • I have 365 data points i.e. for each day in one year, a value is recorded between 0 and 100. I have set the frequency to seven based on my reading from an article I found and I've asked another question as I now have gotten the ets function to return predictions (using ANA, not MAA) here https://stats.stackexchange.com/questions/300869/how-can-i-get-more-accurate-predictions-with-extreme-values. The data is not sub-daily. It seems my predictions are off by 25 currently (MAE = 25). – user1477388 Sep 01 '17 at 11:53
  • 1
    If you have 365 data points, then you could potentially model weekly or monthly seasonality, but not yearly, since you haven't observed two full seasonal cycles. I'd suggest that you don't set your frequency based on some external article, but based on how often you expect seasonal cycles to recur. For instance, temperatures have yearly seasonality, not weekly or monthly. And supermarket sales have a strong weekly seasonality (higher sales on Friday and Saturday), which is far stronger than the yearly cycle (and the very weak monthly or biweekly paycheck cycle). – Stephan Kolassa Sep 01 '17 at 14:49
  • 1
    You may want to look at Forecasting: Principles and Practice by Hyndman and Athanasopoulos, a free online forecasting textbook. – Stephan Kolassa Sep 01 '17 at 14:50
  • I have been reading Practical TimeSeries Forecasting with R (http://www.forecastingbook.com/) but I will definitely check out the exponential smoothing chapter of the book you provided (https://www.otexts.org/fpp/7/1). I am at the point where I am trying to apply the things I've learned but am struggling a bit to fit my exact needs. I am only trying to predict the next 1-7 days based on the previous and I have 365 days of data to work with. – user1477388 Sep 01 '17 at 14:55