This article illustrates how to perform time-series analysis and forecasting using the R programming language. Time series analysis refers to an important statistical technique for studying the trends and characteristics of collecting data points indexed in chronological order. On the other hand, time series forecasting involves the task of getting insights from recorded time series data and making future predictions based on them.

If you are unfamiliar with R and its basic concepts, check out this article before proceeding. We have already covered several articles on time series analysis and time series forecasting but with Pythonic code.

Deep Learning DevCon 2021 | 23-24th Sep | Register>>

**Practical implementation** using R

The code here has been implemented using **RStudio** IDE (version 1.2.1335). You can download RStudio from here. Step-wise explanation of the code is as follows:

**Time-Series Analysis**

- Install rmeta R package.

`install.packages("rmeta")`

Check if the package has been installed by displaying the whole list of packages

Looking for a job change? Let us help you.

installed in the library.

`installed.packages()`

**Output: **

package ‘rmeta’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\Lenovo\AppData\Local\Temp\RtmpUJ2vKU\downloaded_packages

- Read the data containing age of various kings when they died. The dataset is available here.

kings_data <- scan("http://robjhyndman.com/tsdldata/misc/kings.dat",skip=3) kings_data

**Output:**

- Store the kings’ data into a time series object for performing time series analysis.

kings_ts_data <- ts(kings_data) kings_ts_data

**Output:**

- Similarly, read the New York City (NYC)’s data containing a monthly record of a number of births in the city. (Raw data is available here).

NYC_data <- scan("http://robjhyndman.com/tsdldata/data/nybirths.dat") NYC_data

**Output:**

- Store NYC’s data into an R time series object. ‘frequency’ parameter of the ts() function should be set to 12 for month-wise data. Also, we specify start=c(1950,1)) as a parameter to ts() indicating that the data should start from the year 1950 and there should 1 sample per year for each month.

NYC_ts_data <- ts(NYC_data, frequency=12, start=c(1950,1)) NYC_ts_data

**Sample condensed output:**

- Read the data containing monthly records of a souvenir shop in Australia. Find the data here.

shop_data <- scan("http://robjhyndman.com/tsdldata/data/fancy.dat") shop_data

**Output:**

- Convert the souvenir shop’s data into a time series object;

shop_ts_data <- ts(shop_data, frequency=12, start=c(1980,1)) shop_ts_data

**Output:**

- Plot the time-series version of all the three datasets (kings’ data, NYC’s data and souvenir shop’s data).

`plot.ts(kings_ts_data)`

**Output:**

`plot.ts(NYC_ts_data)`

**Output:**

`plot.ts(shop_ts_data)`

**Output:**

**Time series decomposition**is a process of decomposing the time series data into components viz. A trend component and an irregular component. A seasonal data additionally has a seasonal component.

We first smoothen the kings’ time series data using SMA() function of the TTR package (where SMA stands for ‘simple moving average’) for getting the trend component.

install.packages("TTR") library("TTR") kings_ts_SMA2 <- SMA(kings_ts_data,n=2)

(‘n’ parameter here specifies the order of SMA)

`plot.ts(kings_ts_SMA2)`

**Output:**

Similarly, we can change the order to say 8 and observe the change in the trend.

kings_ts_SMA8 <- SMA(kings_ts_data,n=8) plot.ts(kings_ts_SMA8)

**Output:**

It can be observed that incrementing the order of SMA smoothens the plot more i.e. reduces fluctuations in the trend.

- For seasonal data such as that of births in NYC, the decomposition can be carried out using the decompose() function since it also has a seasonal component apart from the trend and irregular ones.

`NYC_ts_comp <- decompose(NYC_ts_data)`

Now, we can separate obtain each of the three components of the decomposed NYC data.

Seasonal component:

`NYC_ts_comp$seasonal`

**Sample condensed output:**

Trend component:

`NYC_ts_comp$trend`

**Sample condensed output:**

Irregular component:

`NYC_ts_comp$random`

**Sample condensed output:**

Plot all the components of the NYC time series in a single plot.

`plot(NYC_ts_comp)`

**Sample condensed output:**

- The seasonal time series data of NYC can be seasonally adjusted by subtracting its seasonal component from the original time series.

NYC_ts_adjusted <- NYC_ts_data - NYC_ts_comp$seasonal plot(NYC_ts_adjusted)

**Output:**

**Time-Series Forecasting**

- Read the data containing records of annual precepitation (in inches) in London (data is available here).

rain_data <- scan("http://robjhyndman.com/tsdldata/hurst/precip1.dat",skip=1) rain_data

**Output:**

- Create a time series object of the precipitation data for the year starting from 1815 and plot that time-series.

rain_ts_data <- ts(rain_data, start=c(1815)) plot.ts(rain_ts_data)

**Output:**

- We can forecast the rainfall in future using simple exponential smoothing technique since the rain data has no seasonality and is an additive time-series.

The HolWinters() functions with ‘beta’ and ‘gamma’ parameters set to False can be used for simple exponential smoothing of the data. It estimates the value of smoothing factor ‘alpha’ whose value lies between 0 and 1. Larger the value of alpha, more is the importance/weightage given to the historic data for making predictions.

rain_fc <- HoltWinters(rain_ts_data, beta=FALSE, gamma=FALSE) rain_fc

**Output:**

The forecasts made by the HoltWinters() function can be obtained from the named variable called ‘filleted’ of the rain_fc variable. It is in this variable that the HoltWinters() function stores its output.

`rain_fc$fitted`

**Output:**

The complete output will show that HolWinters() makes predictions for that period only which is covered by our original time-series.

- Plot the forecasted as well as original time series in a single plot.

`plot(rain_fc)`

**Output:**

The ‘red’ line in the above plot shows the forecasted time series which is, as can be

seen, has much less fluctuations than the original time series (shown in black).

- Code source
**.R file**of the above implemented code is available here.