Forecasting is a technique that is popularly used in the field of machine learning for making business predictions. Companies use past time series forecasts and make business decisions for the future. In this article, we will learn about Time Series Forecasting in detail.

• What is Time Series Forecasting?
• Time Series Forecasting Applications
• Time Series Components
• Time Series Methods
• Time Series Forecasting Using The ARIMA Model

## What is Time Series Forecasting?

Time series forecasting is the method of exploring and analyzing time-series data recorded or collected over a set period of time. This technique is used to forecast values and make future predictions. Not all data that have time values or date values as its features can be considered as a time series data. Any data fit for time series forecasting should consist of observations over a regular, continuous interval.

#### Post Graduate Program in AI and Machine Learning

In Partnership with Purdue University ## Time Series Forecasting Applications • Time series forecasting is used in stock price prediction to predict the closing price of the stock on each given day.
• E-Commerce and retail companies use forecasting to predict sales and units sold for different products.
• Weather prediction is another application that can be done using time series forecasting.
• It is used by government departments to predict a state's population, at any particular region, or the nation as a whole.

## Time Series Components

To use time-series data and develop a model, you need to understand the patterns in the data over time. These patterns are classified into four components, which are:

• ### Trend

It represents the gradual change in the time series data. The trend pattern depicts long-term growth or decline.

• ### Level

It refers to the baseline values for the series data if it were a straight line.

• ### Seasonality

It represents the short-term patterns that occur within a single unit of time and repeats indefinitely.

• ### Noise

It represents irregular variations and is purely random. These fluctuations are unforeseen, unpredictable, and cannot be explained by the model.

## Time Series Forecasting Methods

• ### ARIMA Model

ARIMA stands for Autoregressive Integrated Moving Average. It is a combination of the Autoregressive (AR) and Moving Average (MR) model. The AR model forecast corresponds to a linear combination of past values of the variable. The moving average model forecast corresponds to a linear combination of past forecast errors. The “I” represents the data values that are replaced by the difference between their values and the previous values.

• ### SARIMA Model

SARIMA stands for Seasonal Autoregressive Integrated Moving Average. It extends the ARIMA model by adding a linear combination of seasonal past values and forecast errors.

• ### VAR

The Vector Autoregression (VAR) method models the next step in each time series using an AR model. The VAR model is useful when you are interested in predicting multiple time series variables using a single model.

• ### LSTM

The Long Short Term Memory network or LSTM is a special kind of recurrent neural network that deals with long-term dependencies. It can remember information from past data and is capable of learning order dependence in sequence prediction problems.

## Time Series Forecasting Using the ARIMA Model

ARIMA models are classified by three factors:

p = Number of autoregressive terms (AR)

d = How many non-seasonal differences are needed to achieve stationarity (I)

q = Number of lagged forecast errors in the prediction equation (MA)

In this demo, we’ll use a dataset with information about air-ticket sales of the airline industry from 1949-1960. We’ll predict the Airline tickets’ sales of 1961 using the ARIMA model in R. The idea for this analysis is to identify the time series components which are:

• Trend
• Seasonality
• Random behavior of data

Then, we’ll forecast the values based on historical data.

• #### FREE Machine Learning Course

Master In-demand Machine Learning Skills & Tools ### 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 Let’s check on our date values

start(AirPassengers)

 1949    1

end(AirPassengers)

 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))

 0

• ### Check the Summary of the Dataset

summary(AirPassengers) • ### Plot the Dataset

plot(AirPassengers) • ### Decompose the Data Into Four Components

tsdata <- ts(AirPassengers, frequency = 12)

ddata <- decompose(tsdata, "multiplicative")

plot(ddata) • ### Plot the Different Components Individually

plot(ddata\$trend)

plot(ddata\$seasonal)

plot(ddata\$random) • ### Plot a Trendline on the Original Dataset

plot(AirPassengers)

abline(reg=lm(AirPassengers~time(AirPassengers))) ### Create a Box Plot by Cycle

boxplot(AirPassengers~cycle(AirPassengers, xlab="Date", ylab = "Passenger Numbers (1000's)", main = "Monthly air passengers boxplot from 1949-1960")) From the above plot, you can see that the number of ticket sales goes higher in June, July, and August as compared to the other months of the years.

• ### Build the ARIMA Model Using auto.arima() Function

mymodel <- auto.arima(AirPassengers)

mymodel • ### Plot the Residuals

plot.ts(mymodel\$residuals) • ### Forecast the Values for the Next 10 Years

myforecast <- forecast(mymodel, level=c(95), h=10*12)

plot(myforecast) • ### Validate the Model by Selecting Lag Values

Box.test(mymodel\$resid, lag=5, type="Ljung-Box") Box.test(mymodel\$resid, lag=10, type="Ljung-Box") Box.test(mymodel\$resid, lag=15, type="Ljung-Box") Looking at the lower p values, we can say that our model is relatively accurate, and we can conclude that from the ARIMA model, that the parameters (2, 1, 1) adequately fit the data.

Looking forward to becoming a Data Scientist? Check out the Data Science Certification Courses and get certified today

## Conclusion

After reading this article, you would have learned what time series is, its various applications, and its different components. Finally, we looked into creating a time series forecasting model using the ARIMA model in R to predict the sale of airline tickets. Avijeet Biswal
{"ft_lang_label":"__label__en","ft_lang_prob":0.8728523,"math_prob":0.815015,"size":5228,"snap":"2021-21-2021-25","text_gpt3_token_len":1178,"char_repetition_ratio":0.13667688,"word_repetition_ratio":0.018134715,"special_character_ratio":0.21078807,"punctuation_ratio":0.09967846,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97505087,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44],"im_url_duplicate_count":[null,10,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-05-13T06:34:43Z\",\"WARC-Record-ID\":\"<urn:uuid:10b57516-98c2-4393-a449-e1b568c7dac8>\",\"Content-Length\":\"275385\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:21d27075-3249-4400-b538-0012591214e6>\",\"WARC-Concurrent-To\":\"<urn:uuid:b51ba2bf-099a-4b00-b68f-15021f07d434>\",\"WARC-IP-Address\":\"52.85.61.26\",\"WARC-Target-URI\":\"https://www.simplilearn.com/tutorials/data-science-tutorial/time-series-forecasting-in-r?source=sl_frs_nav_playlist_video_clicked\",\"WARC-Payload-Digest\":\"sha1:A7SCX3BTRXRTQTZL7OXXPSMDRB7ZD2M3\",\"WARC-Block-Digest\":\"sha1:NR7OZQFL5IG27VMERYLHJ2TZPA4JYUGN\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-21/CC-MAIN-2021-21_segments_1620243991537.32_warc_CC-MAIN-20210513045934-20210513075934-00307.warc.gz\"}"}