# load in needed library
library(forecast)

# start by exploring the data
AirPassengers
View(AirPassengers)
class(AirPassengers)

# get the first and last timestamp
start(AirPassengers)
end(AirPassengers)

# check for any missing values
sum(is.na(AirPassengers))

# get a summary of the data
summary(AirPassengers)

# plot the time series
plot(AirPassengers)

# AirPassengers is already a 'ts' object, but here's how you'd create one
tsdata <- ts(AirPassengers, frequency = 12, start = c(1949, 1))

# get the individual components of the time series
data <- decompose(tsdata, "multiplicative")

# plot the individual components
plot(data$trend)
plot(data$seasonal)
plot(data$random)
plot(data)

# Alternative: STL decomposition (more robust)
stl_data <- stl(log(AirPassengers), s.window = "periodic")
plot(stl_data)

# get the timestamps for each data point
time(AirPassengers)
time(tsdata)

# Add a linear trend line to visualize general growth
plot(AirPassengers)
abline(reg = lm(AirPassengers ~ time(AirPassengers)), col = "red")

# get the statistics for each cycle
boxplot(
  AirPassengers~cycle(AirPassengers),
  xlab = "Data",
  ylab = "Passenger Numbers (1000s)",
  main = "Monthly Air Passenger Boxplot from 1949-1960"
)

# build an ARIMA model
mymodel <- auto.arima(AirPassengers)
mymodel

# plot something from the ARIMA model
plot(mymodel$residuals)
checkresiduals(mymodel)

# use the model to forecast 10 years into the future
myforecast <- forecast(mymodel, level = c(95), h = 10*12)
plot(myforecast)
myforecast
