What is Durbin Watson test How to perform it in R

This recipe explains what is Durbin Watson test This recipe helps you perform it in R

Recipe Objective

What is the Durbin Watson test? How to perform it in R?

Linear Regression is a supervised learning algorithm used for continuous variables. The simple Linear Regression describes the relation between 2 variables, an independent variable (x) and a dependent variable (y). The linear regression makes an assumption that there is no correlation between the residuals, i.e., the residuals are independent. In order to check if this assumption is correct, the Durbin Watson Test is used, which detects the presence of autocorrelation in the residuals of a regression. This test assumes the following hypothesis: H0 — null hypothesis: There is no correlation between the residuals. HA — alternative hypothesis: The residuals are autocorrelated. This recipe explains how to perform a Durbin Watson test on regression in R.

Learn How to do Exploratory Data Analysis

Step 1 - Install the necessary libraries

install.packages("caTools") # For Linear regression library(caTools) install.packages('car') library(car)

Step 2 - Read a csv file and do EDA : Exploratory Data Analysis

The dataset attached contains the data of 160 different bags associated with ABC industries. The bags have certain attributes which are described below: 1. Height – The height of the bag 2. Width – The width of the bag 3. Length – The length of the bag 4. Weight – The weight the bag can carry 5. Weight1 – Weight the bag can carry after expansion The company now wants to predict the cost they should set for a new variant of these kinds of bags.

data <- read.csv("R_333_Data_1.csv") dim(data) # returns the shape of the data, i.e the total number of rows,columns print(head(data)) # head() returns the top 6 rows of the dataframe summary(data) # returns the statistical summary of the data columns

Step 3 - Create a linear regression model

Here, a simple linear regression model is created with, y(dependent variable) - Cost

model <- lm(Cost ~., data=data) summary gives the summary result of training model , the performance metrics r2 and rmse obtained helps us to check how well our metrics is performing summary(model)

Step 4 - Perform the Durbin Watson Test

durbin_test<- durbinWatsonTest(model) durbin_test

From the output we can see that the test statistic is 0.4779257 and the corresponding p-value is 0. Since this p-value is 0, we can reject the null hypothesis and conclude that the residuals in this regression model are perfectly positive autocorrelated.

{"mode":"full","isActive":false}

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

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.

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

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.