Import libraries

library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(astsa)
library(TSA)
## 
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
## 
##     acf, arima
## The following object is masked from 'package:utils':
## 
##     tar

Import Data

Sales = c(1,3,6,4,2,2,7,5,2,4,8,5,1,3,8,6)
Beer_ts <- ts(Sales, start = c(2016,1), frequency = 4, end=c(2019,4))

Check data if it is a time series.

ts.plot(Beer_ts, col="blue" ,main="Beer Sales in past 4 years", xlab="Years", ylab="Sales", type="b")
plot of chunk unnamed-chunk-3

ACF and PACF calculation

acf2(Beer_ts)
plot of chunk unnamed-chunk-4
##      [,1]  [,2]  [,3] [,4]  [,5]  [,6]
## ACF  0.09 -0.72 -0.05 0.66  0.07 -0.52
## PACF 0.09 -0.73  0.27 0.23 -0.07  0.09

It is observed that the time series is not stationary and that there is seasonality with an upward trend. Then the trend is displayed by Decompose our data.

decomp_data = decompose (Beer_ts, type = "multiplicative")
plot (decomp_data)
plot of chunk unnamed-chunk-6
plot (decomp_data$trend)
plot of chunk unnamed-chunk-7

Knowing that the time series has a frequency of 4 to eliminate seasonality we get the 4 differences. Then we eliminate the trend by taking the first difference after we have taken the 4.

diff4 = diff(Beer_ts, 4)
plot.ts(diff4)
plot of chunk unnamed-chunk-9
diff1and4 = diff(diff4,1)
plot.ts(diff1and4)
plot of chunk unnamed-chunk-11

ACF and PACF are recalculated.

acf2(diff1and4)
plot of chunk unnamed-chunk-12
##       [,1]  [,2] [,3]  [,4]  [,5]  [,6]
## ACF  -0.28 -0.17 0.17 -0.39  0.06  0.06
## PACF -0.28 -0.26 0.04 -0.42 -0.21 -0.26