Granger Causality Test in R (with Example)

[This article was first published on Methods – finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Granger-Causality Test in R, The Granger Causality test is used to examine if one time series may be used to forecast another.

Null Hypothesis (H0):

Time series X does not cause time series Y to Granger-cause itself.

Alternative Hypothesis (H1):

Time series X  cause time series Y to Granger-cause itself.

Knowing the value of a time series X at a given lag is valuable for forecasting the value of a time series Y at a later time period is referred to as “Granger-causes.”

How to run R code in PyCharm? » R & PyCharm »

Granger Causality Test in R

This test generates an F test statistic along with a p-value.

We can reject the null hypothesis and infer that time series X Granger causes time series Y if the p-value is less than a particular significance level (e.g. =.05).

In R, we may use the grangertest() function from the lmtest package to perform a Granger-Causality test, which has the following syntax:

15 Essential packages in R for Data Science »

grangertest(X, Y, order = 1)

where:

X: This is the very first time series.

Y: The second set of the time series

order: In the first time series, the number of lags to utilize. The default value is 1.

The step-by-step example below demonstrates how to utilize this function in practice.

Step 1: Load Data

For this example, we’ll use the EuStockMarkets dataset. This dataset contains values for the DAX, SMI, CAC, and FTSE of values but we are utilizing only DAX and SMI values.

Only for illustration purposes randomly taking DAX and SMI values here.

tsData <- EuStockMarkets[, 1:2]

view first six rows of dataset

head(tsData)
       DAX    SMI
[1,] 1628.75 1678.1
[2,] 1613.63 1688.5
[3,] 1606.51 1678.6
[4,] 1621.04 1684.1
[5,] 1618.16 1686.6
[6,] 1610.61 1671.6

Step 2: Perform the Granger-causality Test

Next, we’ll use the grangertest() function to run a Granger-Causality test to examine if the values of SMI predict the values of DAX in the future. We’ll conduct the test with three different lags:

tidyverse in r – Complete Tutorial » Unknown Techniques »

perform Granger-Causality test

library(lmtest)
grangertest(DAX ~ SMI, order = 3, data = tsData)
Granger causality test

Model 1: DAX ~ Lags(DAX, 1:3) + Lags(SMI, 1:3)
Model 2: DAX ~ Lags(DAX, 1:3)
  Res.Df Df      F    Pr(>F)    
1   1850                        
2   1853 -3 8.4968 1.322e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

The F test statistic is denoted by the letter F equal to 8.4968 and the p-value that corresponds to the F test statistic is Pr(>F) 1.322e-05.

We may reject the null hypothesis of the test because the p-value is smaller than 0.05, and infer that knowing the values of SMI is valuable for forecasting the future values of DAX.

Step 3: Perform the Granger-causality Test in Reverse

Despite the fact that the null hypothesis of the test was rejected, it’s possible that reverse causation is occurring. That example, it’s probable that changes in the values of DAX are affecting changes in the values of SMI.

Bubble Chart in R-ggplot & Plotly » (Code & Tutorial) »

To rule out this possibility, we must use DAX as the predictor variable and SMI as the responder variable in the Granger-Causality test in reverse:

perform Granger-Causality test in reverse

grangertest(SMI ~ DAX, order = 3, data = tsData)
Granger causality test

Model 1: SMI ~ Lags(SMI, 1:3) + Lags(DAX, 1:3)
Model 2: SMI ~ Lags(SMI, 1:3)
  Res.Df Df      F  Pr(>F)  
1   1850                    
2   1853 -3 2.6576 0.04689 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

The test’s p-value is 0.04689. We reject the null hypothesis because this value is less than 0.05. That is, the values of DAX predict the values of SMI in the future.

As a result, we can conclude that knowing the values of SMI is helpful in projecting the values of DAX in the future.

ggpairs in R- A Brief Introduction to ggpairs »

The post Granger Causality Test in R (with Example) appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)