chopin_is_the_best
chopin_is_the_best

Reputation: 2111

Time Series Analysis and R Holt Winters

I have a seasonal (7 days interval) time series, daily data for 30 days. What is the best approach for a reasonable forecast? The time series contains orders made with a app, it shows a seasonality of 1 week (lower sales at the beginning of the week). I try the holt winters approach with this code:

(m <- HoltWinters(ts,seasonal = "mult"))
 plot(m)
 plot(fitted(m))

but it gives me an error like: Error in decompose(ts(x[1L:wind], start = start(x), frequency = f),seasonal) : time series has no or less than 2 periods

What do you suggest?

EDIT: data here

Upvotes: 1

Views: 2719

Answers (2)

josep maria porr&#224;
josep maria porr&#224;

Reputation: 1396

You can use df$data to keep the dates that correspond to each day in the ts series.

ts_series <- ts(df$install, frequency = 7)
ts_dates <- as.Date(df$data, format = "%d/%m/%Y")

In a similar way, dates for the forecasted values can be kept in another sequence

m <- HoltWinters(ts_series, seasonal = "mult")
predict_values <- predict(m, 10)
predict_dates <- seq.Date(tail(ts_dates, 1) + 1, length.out = 10, by = "day")

With the dates sequence, the daily series can be plot with dates in x axis with the right format. More control on the x axis ticks can be obtained with the axis.Date function

plot(ts_dates, ts_series, typ = "o"
  , ylim = c(0, 4000)
  , xlim = c(ts_dates[1], tail(predict_dates, 1))
  , xlab = "Date", ylab = "install", las = 1)
lines(predict_dates, predict_values, lty = 2, col = "blue", lwd = 2)
grid()

enter image description here

Upvotes: 0

DatamineR
DatamineR

Reputation: 9628

You must first determine a ts object. Assuming your data is called df:

ts <- ts(df$install, frequency = 7)
(m <- HoltWinters(ts,seasonal = "mult"))
 plot(m)
 plot(fitted(m))

enter image description here

Then you can make prediction like (10 steps-ahead):

predict(m, n = 10)
Time Series:
Start = c(4, 5) 
End = c(5, 7) 
Frequency = 7 
            fit
 [1,] 1028.8874
 [2,] 1178.4244
 [3,] 1372.5466
 [4,] 1165.2337
 [5,]  866.6185
 [6,]  711.6965
 [7,]  482.2550
 [8,]  719.0593
 [9,]  807.6147
[10,]  920.3250

The question about the best method is too difficult to answer. Usually one compares the performance of different models considering their out-of-sample accuracy and chooses the one whith the best result.

Upvotes: 1

Related Questions