getting an error while trying Moving average

View(d)
library(ggplot2)
ggplot(d, aes(y=Sales, x=Year))+geom_line(col="blue")+theme_bw()+geom_point()
c <- ma(d, 5) #moving average #Mcomp package
c
autoplot(d, series="Data")+autolayer(c, series="5-MA")+xlab("Year")+ylab("Sales")+ggtitle("Sales")

image

Since you have not shown any data, I am guessing at the cause of your problem. The variable d seems to be a data frame, since you use it in ggplot(). The function ma(), which comes from the package forecast, takes a univariate time series as its first argument. Here is a quote from the documentation.

ma {forecast} R Documentation

Moving-average smoothing

Description

ma computes a simple moving average smoother of a given time series.

Usage

ma(x, order, centre = TRUE)

Arguments

x Univariate time series
order Order of moving average smoother
centre If TRUE, then the moving average is centred for even orders.

I would expect ma() to throw an error if you pass it a data frame (edit: if it has more than one column). If my answer has not helped you solve your problem, please tells us what d is and what error your are getting. The best way to post is question is with a Reproducible Example.

Thanks for your reply

d was my dataframe.

this is the error which i am getting---
"Error: Objects of type data.frame not supported by autoplot."

I am unable to attach the file.
Kindly find the data below.

year sales
1 23
2 40
3 25
4 27
5 32
6 48
7 33
8 37
9 37
10 50
11 40

my objective was to calculate the moving average.
while plotting i am getting an error.

regards
rahul

Unless you're constrained to use ggplot2 and forecast, you can simply do something like this:

dataset <- data.frame(year = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
                      sales = c(23, 40, 25, 27, 32, 48, 33, 37, 37, 50, 40))

with(data = dataset,
     expr = {
       original_time_series <- ts(data = sales)
       moving_averages <- filter(x = original_time_series,
                                 filter = rep(x = (1 / 5),
                                              times = 5))
       matplot(x = year,
               y = cbind(original_time_series, moving_averages),
               type = "o",
               lty = c(1, 3),
               lwd = c(1, 2),
               pch = c(1, 19),
               col = c(1, 2),
               xlab = "year",
               ylab = "sales",
               main = "Annual vs 5-Year Moving Average")
       legend(x = 7,
              y = 30,
              legend = c("Annual", "5-Year Moving Average"),
              col = c(1, 2),
              lty = c(1, 3),
              lwd = c(1, 2),
              pch = c(1, 19))
     })

Created on 2019-06-30 by the reprex package (v0.3.0)

thankyou for your reply.

I was trying to plot Moving average using MA function because I have to perform this with function. I am getting the values of moving average but while performing in ggplot i am getting an error.
image

if you will use ma it will calculate the values. while plotting the ma values together with annual value i am getting an error.

I want to attach my csv file but i cannot attach the any files because I am new user.

Best regards
rahul

Here is an example of plotting your data using ggplot and the ma() function.

library(forecast)
#> Warning: package 'forecast' was built under R version 3.5.3
library(ggplot2)
d <- data.frame(Year = seq(1,11),
                Sales = c(23, 40, 25, 27, 32, 48, 33, 37, 37, 50, 40))
ggplot(d, aes(y=Sales, x=Year))+geom_line(col="blue")+theme_bw()+geom_point()

d$MovingAvg <- ma(d$Sales, 5)
ggplot(data = d)+geom_line(aes(y=Sales, x=Year), col="blue")+
  theme_bw()+ geom_point(aes(y=Sales, x=Year)) +
  geom_line(aes(y=MovingAvg, x=Year), color = "red") +
  geom_point(aes(y=MovingAvg, x=Year))
#> Warning: Removed 4 rows containing missing values (geom_path).
#> Warning: Removed 4 rows containing missing values (geom_point).

Created on 2019-06-30 by the reprex package (v0.2.1)

thank you very much. it really helped me.

i appreciated. thanks alot.

best regards
rahul:grinning:

A little help with the styling

library(zoo)
library(tidyverse)

d <- data.frame(
    year = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
    sales = c(23, 40, 25, 27, 32, 48, 33, 37, 37, 50, 40)
)

d %>% 
    mutate(ma=rollapply(sales,5,mean,align='center',fill=NA)) %>% 
    gather(series, sales, -year) %>% 
    ggplot(aes(x=year, y=sales, color = series)) + 
    geom_line(aes(linetype = series), key_glyph = "timeseries") + 
    geom_point() +
    labs(title = "Annual vs 5-Year Moving Average",
         color = "", 
         linetype = "") +
    scale_color_discrete(labels = c("5-Year Moving Average", "Anual")) +
    scale_linetype_discrete(labels = c("5-Year Moving Average", "Anual")) +
    theme_bw() + 
    theme(legend.position = "bottom") 

1 Like

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