Search code examples
rdatetimetime-seriesforecastingtimeserieschart

Time Series R with duplicate Items for daily forecast


I would like a guidance no how to plot daily data and use forecasting in R.

There are low purchase for Sunday and Saturday in this data. And there are certain weekdays that have no purchase at all. So its the obstacles for the analysis. I have around 300 rows with various item name which the items are duplicated inside the column, but with different dates. Example, I bought exactly 1 soap 3 times a week, at monday, wednesday and also sunday. This is the example data table :

Example Data

My trouble so far is that it took me a long time to forecast manually using other statistical software, so I try to learn R from the start and see how it could save the time. The table above have been put into R, the date also have been converted from factor into date class by using the function as.Date(data$Date)

Usually i used exponential smoothing method, since the purchase are still low and sometimes out of stock, so not much of pattern are shown from the historical data. The output of this analysis is that i could provide a forecast for the purchase of the item daily in order to give instruction when should we demand an item.


Solution

  • First please consider adding a reproducible example for a more substantial answer. Look at the most upvoted question with tag R for a how-to.

    EDIT: I think this is what you want before creating the ts:

    data.agg <- aggregate(data$purchase, by = list(data$date, data$item), FUN = sum)
    

    If your data is not yet of class 'ts' you can create a time-series object with the ts() command. From the ?ts page:

       ts(data = NA, start = 1, end = numeric(), frequency = 1,
       deltat = 1, ts.eps = getOption("ts.eps"), class = , names = )
    as.ts(x, ...)
    

    Generally you could use the HoltWinters function for exponential smoothing like so:

    data.hw <- HotlWinters(data)
    data.predict <- predict(data.hw, n.ahead = x) # for x = units of time ahead you would like to predict
    

    See also ?HoltWinters for more info on the function

    Reproducible Example for aggregate:

    data <- data.frame(date = c(1, 2, 1, 2, 1, 1), item = c('b','b','a','a', 'a', 'a'), purchase = c(5,15, 23, 7, 12, 11))
    
    data.agg <- aggregate(data$purchase, by = list(data$date, data$item), FUN = sum)
    

    Reproducible Example for HoltWinters:

    library(AER)
    data("UKNonDurables")
    
    nd <- window((log(UKNonDurables)), end = c(1970, 4))
    tsp(nd)
    hw <- HoltWinters(nd)
    pred <- predict(hw, n.ahead = 35)
    pred
    
    plot(hw, pred, ylim = range(log(UKNonDurables)))
    lines(log(UKNonDurables))