I am trying to solve an introductory statistics textbook exercise on time series using R. The data was given as daily sales from Monday to Friday for three weeks. No date was given. My brave attempt is just give a fictitious starting date (which is a Sunday, the beginning of a week) and set it as the start time for my concocted time series, and assign NA to sales on Saturday and Sunday. Then I find the raw time series using xts() and moving-average time series (of order 5) using forecast::ma(). The raw time series appeared fine but the timestamps of the moving-average was not, which was a sequence of 1 to 15. (The plot of the moving-averages by itself seemed fine with time as 1 to 15). How can I preserve the timestamps of the ma time series to date as in the raw time series?
Because of the different timestamps in the raw and ma time series, I can't plot them together on the same graph using {ggplot2}.
I'm at the end of my wits ;).
You are trying to mix different time series classes which do not work well together. Since your data is in xts class, you would be better off using zoo::rollmean rather than forecast::ma. The following code does what you want.
The book "Forecasting: Principles and Practice" is very informative and helpful. Thanks.
Just curious. Can I use forecast::ma() to plot the moving-average trend and the raw time series on the same graph by just using {forecast} without resorting to zoo::rollmean()? Perhaps by using a differently defined time series object?
Also, what is the general approach to deal with workdays ? Is my use of assigning NA's to Sat and Sun correct?
I hope my questions aren't beyond the topic discussed.