Explain MA modelling of time series in R

This recipe explains what MA modelling of time series in R

Recipe Objective

Explain the MA 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. In the moving average method, the relation of dependency between the observations and the residuals from a moving average model applied to the lagged observations is used. Use the arima () to fit the AR model of time series. This recipe demonstrates an example of MA modelling of time series.

Apply Machine Learning to Demand Forecasting Data Science Problems

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(0,0,1))
model
model2 <- arima(rain_ts, order = c(0,0,2))
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 - (0,0,1)

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

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy