Explain AR modelling of time series in R

This recipe explains what AR modelling of time series in R

Recipe Objective

Explain the AR modelling of time series in R.

Time series is a statistical technique that deals with time series data or trend analysis. Time series data means the data is collected over a period of time/ intervals. Time series data helps us with making forecasting based on the previously collected data. ARIMA is a time series forecasting method where AR stands for Autoregressive and MA stands for Moving Average. The AR model is based on the assumption that the data collected is stationary and univariate, and the forecast corresponds to a linear combination of previous data of the variables. Use the arima () to fit the AR model of time series. This recipe demonstrates an example of AR modelling of time series.

Step 1 - Install required package

install.packages('forecast')
library(forecast)

Step 2 - Generate random time series data

# Get the data points in form of a R vector.
rain <- c(987,1025,978,774,1563,569,1456,789,1479,566,1563,1698)
# Convert it to a time series object.
rain_ts <- ts(rain,start = c(2020,1),frequency = 12)
# Print the timeseries data.
print(rain_ts)
plot(rain_ts, main = "Time series data")
summary(rain_ts)

Step 3 - Plot a trend line

plot(rain_ts)
abline(reg=lm(rain_ts~time(rain_ts)))

Step 4 - Build a model using arima()

model <- arima(rain_ts , order = c(1, 0, 0))
model
model2 <- arima(rain_ts, order = c(2,0,0))
model2

We can try to fit different 'ar models' by changing the order/ The lower the aic term , the better the model. Hence we choose the a model with order - (1,0,0)

Step 5 - Plot the residuals

residuals <- residuals(model)
data_fitted <- rain_ts - residuals
ts.plot(rain_ts)
points(data_fitted, type = "l", col = 2, lty = 2)

Step 6 - Forescast the future values

predict <- predict(model, n.ahead = 6) # predict for next 6 periods
predict
predict_val<- forecast(model, level=c(95), h=6) # forecast for 2021.0 till 2021.6 i.e 6 months
plot(predict_val)

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.