
R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(plm)
> data("Grunfeld", package = "plm")
> 
> pgrangertest(inv ~ value, data = Grunfeld)

	Panel Granger (Non-)Causality Test (Dumitrescu/Hurlin (2012))

data:  inv ~ value
Ztilde = 3.2896, p-value = 0.001003
alternative hypothesis: Granger causality for at least one individual

> pgrangertest(inv ~ value, data = Grunfeld, order = 2L)

	Panel Granger (Non-)Causality Test (Dumitrescu/Hurlin (2012))

data:  inv ~ value
Ztilde = 1.6832, p-value = 0.09234
alternative hypothesis: Granger causality for at least one individual

> pgrangertest(inv ~ value, data = Grunfeld, order = 2L, test = "Zbar")

	Panel Granger (Non-)Causality Test (Dumitrescu/Hurlin (2012))

data:  inv ~ value
Zbar = 2.9657, p-value = 0.00302
alternative hypothesis: Granger causality for at least one individual

> 
> 
> # unbalanced
> unbal <- pgrangertest(inv ~ value, data = Grunfeld[1:199, ], order = 2L)
> unbal$indgranger
   firm      Chisq     p-value df lag
1     1  1.8255237 0.401414049  2   2
2     2  4.3694800 0.112506980  2   2
3     3  0.7983334 0.670878856  2   2
4     4  3.3069760 0.191381208  2   2
5     5 11.0631807 0.003959687  2   2
6     6 10.8343468 0.004439678  2   2
7     7  1.3410752 0.511433558  2   2
8     8  0.2900525 0.864999607  2   2
9     9  4.4068769 0.110422824  2   2
10   10  0.2960011 0.862430626  2   2
> 
> # varying lag order
> bal_varorder <- pgrangertest(inv ~ value, data = Grunfeld[1:199, ], order = c(rep(2L, 9), 3L))
> bal_varorder$indgranger
   firm      Chisq     p-value df lag
1     1  1.8255237 0.401414049  2   2
2     2  4.3694800 0.112506980  2   2
3     3  0.7983334 0.670878856  2   2
4     4  3.3069760 0.191381208  2   2
5     5 11.0631807 0.003959687  2   2
6     6 10.8343468 0.004439678  2   2
7     7  1.3410752 0.511433558  2   2
8     8  0.2900525 0.864999607  2   2
9     9  4.4068769 0.110422824  2   2
10   10  2.9874921 0.393557671  3   3
> unbal_varorder <- pgrangertest(inv ~ value, data = Grunfeld[1:199, ], order = c(rep(2L, 9), 3L))
> unbal_varorder$indgranger
   firm      Chisq     p-value df lag
1     1  1.8255237 0.401414049  2   2
2     2  4.3694800 0.112506980  2   2
3     3  0.7983334 0.670878856  2   2
4     4  3.3069760 0.191381208  2   2
5     5 11.0631807 0.003959687  2   2
6     6 10.8343468 0.004439678  2   2
7     7  1.3410752 0.511433558  2   2
8     8  0.2900525 0.864999607  2   2
9     9  4.4068769 0.110422824  2   2
10   10  2.9874921 0.393557671  3   3
> 
> 
> ## Demo data from Dumitrescu/Hurlin (2012) supplement:
> ## http://www.runmycode.org/companion/view/42
> ## The data are in the following format: 20 x 20
> ## First 20 columns are the x series for the 10 individual
> ## next 20 columns are the y series for the 10 individuals
> ## -> need to convert to 'long' format first
> 
> # demodat <- readxl::read_excel("data/Granger_Data_demo_long.xls")
> # demodat <- data.frame(demodat)
> # pdemodat <- pdata.frame(demodat)
> 
> # pgrangertest(y ~ x, data = pdemodat, order = 1L)
> # pgrangertest(y ~ x, data = pdemodat, order = 1L, test = "Zbar")
> # 
> # pgrangertest(y ~ x, data = pdemodat, order = 2L)
> # pgrangertest(y ~ x, data = pdemodat, order = 2L, test = "Zbar")
> 
> proc.time()
   user  system elapsed 
  1.154   0.067   1.209 
