Timestamp in moving-average time series missing

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?

The code of my attempt is as follows:

daysales <- c(NA, 140, 150, 100, 150, 220, NA,
NA, 155, 170, 105, 220, 300, NA,
NA, 175, 190, 130, 225, 325, NA)
day <- seq(as.Date("2024/1/7"), # 2024/1/7 is a Sunday
length = 21,
by = "day")
daysales_xts <- xts(daysales, day)
daysales_xts <- daysales_xts[.indexwday(daysales_xts) %in% 1:5]
forecast::ma(daysales_xts, 5)

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.

library(xts)
library(ggplot2)

daysales <- c(NA, 140, 150, 100, 150, 220, NA,
              NA, 155, 170, 105, 220, 300, NA,
              NA, 175, 190, 130, 225, 325, NA)
day <- seq(as.Date("2024/1/7"), # 2024/1/7 is a Sunday
           length = 21, by = "day")
daysales_xts <- xts(daysales, day)
daysales_xts <- daysales_xts[.indexwday(daysales_xts) %in% 1:5]
daysales_ma <- rollmean(daysales_xts, 5)  

autoplot(daysales_xts) +
  geom_line(aes(y = daysales_ma), 
    color = "red", data = fortify(daysales_ma))

Created on 2024-01-23 with reprex v2.0.2

Thanks for the quick reply. It solves my problem. Out of curiosity, I check the output of both forecast::ma() and zoo::rollmean().

daysales_ma <- forecast::ma(daysales_xts, 5)
class(daysales_ma)
#> [1] "ts"

daysales_ma
#> Time Series:
#> Start = 1
#> End = 15
#> Frequency = 1
#> [1] NA NA 152 155 159 160 174 190 194 198 203 204 209 NA NA

time(daysales_ma)
#> Time Series:
#> Start = 1
#> End = 15
#> Frequency = 1
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

daysales_rm <- zoo::rollmean(daysales_xts, 5)
class(daysales_rm)
#> [1] "xts" "zoo"

daysales_rm
#> [,1]
#> 2024-01-10 152
#> 2024-01-11 155
#> 2024-01-12 159

time(daysales_rm)
#> [1] "2024-01-10" "2024-01-11" "2024-01-12" "2024-01-15" "2024-01-16" "2024-01-17"
#> [7] "2024-01-18" "2024-01-19" "2024-01-22" "2024-01-23" "2024-01-24"

They are indeed different!

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.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.