Categories
2. You Should Know about Time Series Forecasting in R

Load the Forecast Package into RStudio

install.packages(‘forecast’)

library(forecast)

  • Load the Air Passengers’ Dataset and View Its Class

data(“AirPassengers”)

class(AirPassengers

Here, ts represents that it’s a time series dataset.

  • Display the Dataset
Time_Series-3

Let’s check on our date values

start(AirPassengers)

[1] 1949    1

end(AirPassengers)

[1] 1960   12

So, our start date is January 1949, while the end date is December 1960.

  • Find out If There Are Any Missing Values

sum(is.na(AirPassengers))

[1] 0

  • Check the Summary of the Dataset

summary(AirPassengers)

/Time_Series-4
  • Plot the Dataset

plot(AirPassengers)

Time_Series-5
  • Decompose the Data Into Four Components

tsdata <- ts(AirPassengers, frequency = 12) 

ddata <- decompose(tsdata, “multiplicative”)

plot(ddata)

Time_Series-6.
  • Plot the Different Components Individually

plot(ddata$trend)

plot(ddata$seasonal)

plot(ddata$random)

Time_Series-7
  • Plot a Trendline on the Original Dataset

plot(AirPassengers)

abline(reg=lm(AirPassengers~time(AirPassengers)))

time series 10

Leave a Reply

Your email address will not be published. Required fields are marked *