How to perform a durbin watson test in r

The Durbin-Watson test possesses the null hypothesis which the autocorrelation of the disruptions is 0. It is feasible to test against the substitute that it is larger than, not equivalent to, or lower than 0, appropriately. This can be detailed by the substitute argument.**

Concerning the assumption of commonly distributed disturbances, the null circulation of the Durbin-Watson statistic is the circulation of a linear consolidation of chi-squared variables. The p-value is computed utilizing a Fortran adaptation of the Applied Statistics Algorithm AS 153 from Farebrother (1980, 1984). That algorithm is named “pan” or “gradsol”. For big sample sizes the algorithm may fail to calculate the p value; in this case a caution is printed and an exact p value may be provided; this p value is computed utilizing a standard approximation with variance and mean of the Durbin-Watson test data.

One of the main assumptions in linear relapse is that there is no interaction between the residuals, e.g. the remains are autonomous.**

One way to decide if this acceptance is met is to execute a Durbin-Watson test, which is utilized to locate the existence of autocorrelation in the remains of a regression. This test utilizes the following explanation:**

H0 (null hypothesis): There is no interaction among the remains.

HA (alternative hypothesis): The remains are autocorrelated.

The following explains how to execute a Durbin-Watson test in R.**

#load mtcars dataset

#view beginning six rows of dataset

mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

#fit relapse model
model : lm(mpg ~ disp+wt, data=mtcars)

Following, we can execute a Durbin-Watson test utilizing the durbinWatsonTest() action from the car package:

#load car package

#execute Durbin-Watson test

Storing required package: carData
lag Autocorrelation D-W Statistic p-value
1 0.341622 1.276569 0.034
Substitute hypothesis: rho != 0**

From the result we can view that the test data is 1.276569 and the comparable p-value is 0.034. Because this p-value is lower than 0.05, we will reject the null hypothesis and complete that the remains in this relapse model are autocorrelated.**

What to Perform if Autocorrelation is Detected

If you deny the null hypothesis and close that autocorrelation is current in the remains, then you have a few different choices to correct this issue if you allow it to be severe enough:**

For positive serial interaction, consider summing lags of the independent and/or dependent variable to the case.
For negative serial interaction, check to make certain that any of your integers are overdifferenced.
For seasonal interaction, acknowledge applying seasonal dummy variables to the example.

Example: Durbin-Watson Test in R

To execute a Durbin-Watson test, we first require to fit a linear relapse model. We may utilize the built-in R dataset mtcars and place a relapse case utilizing mpg as the predictor integer and disp and wt as informative integers.**

How to execute the Durbin Watson test in R?

Linear Relapse is a managed learning algorithm utilized for continuous variables. The simple Linear Regression explains the relation amongst 2 variables, a dependent variable (y) and an independent variable (x). The linear relapse creates an assumption that there is no interaction amongst the residuals, i.e., the remains are independent. In order to identify if this assumption is accurate, the Durbin Watson Test is utilized, which finds the presence of autocorrelation in the remains of a regression. This test speculates the next hypothesis: H0 — null hypothesis: There is no correlation amongst the remains. HA — alternative hypothesis: The remains are autocorrelated. This recipe tells how to execute a Durbin Watson test on relapse in R.**

Procedure 1 – Include the required libraries

install.packages(“caTools”) # For Linear relapse


Procedure 2 – Read a csv file and perform EDA :

Exploratory Data Investigation

The dataset connected includes the data of 160 distinct bags corresponding with ABC industries. The bags possess particular attributes which are explained 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 may hold 5. Weight1 – Weight the bag may hold after expansion The company now wants to forecast the price they should establish for a new variant of these types 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

Procedure 3 – Design a linear relapse model

Here, a plain linear relapse model is designed with, y(dependent variable) – Price

model : lm(Price ~., data=data)

summary yields the summary output of training model , the achievement metrics r2 and rmse gathered helps us to identify how well our metrics is operating**


Step 4 – Execute the Durbin Watson Test

durbin_test: durbinWatsonTest(model)

From the result we can view that the test statistic is 0.4779257 and the comparable p-value is 0. Because this p-value is 0, we can deny the null hypothesis and close that the remains in this relapse model are entirely positive autocorrelated.


Scroll to top
Privacy Policy