library(fpp2)
## Warning: package 'fpp2' was built under R version 4.0.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## -- Attaching packages -------------- fpp2 2.4 --
## v ggplot2   3.3.0     v fma       2.4  
## v forecast  8.12      v expsmooth 2.3
## Warning: package 'forecast' was built under R version 4.0.2
## Warning: package 'fma' was built under R version 4.0.3
## Warning: package 'expsmooth' was built under R version 4.0.3
## 
AP = AirPassengers
str(AP)
##  Time-Series [1:144] from 1949 to 1961: 112 118 132 129 121 135 148 148 136 119 ...
sum(is.na(AP))
## [1] 0
#There are no missing values
autoplot(AP)

ggseasonplot(AP)

There appears to be seasonality. More people likely travel during the summer months, and over time, more and more people started to use air as a form of travel which explains the increasing trend.

There is a good amount of fluctuation from year to year, so we will use a log transformation and plot it.

AP = log(AP)
plot(AP)

We will now decompose.

#additive
dec1 = decompose(AP)
autoplot(dec1) 

#variance increases with time
checkresiduals(remainder(dec1))
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

This time series is non-stationary. There is a linear trend and seasonality - additive decomposition will likely be the choice, but we will do multiplicative decomposition anyway.

dec2 = decompose(AP, type="multiplicative")
autoplot(dec2)

checkresiduals(remainder(dec2))
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

This was pretty similar to additive.

Forecast

FAP = stl(AP, t.window = 13, s.window = "periodic", robust=TRUE)

FAP %>% seasadj() %>% naive() %>%
  autoplot() + ylab("Passengers") +
  ggtitle("Forecasts of AP")

FAP %>% forecast(method="naive") %>%
  autoplot() + ylab("Passengers")