
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")
> 
> 
> # Baltagi/Wu (1999), p. 822, Table 1: test data construction
> a <- Grunfeld[["year"]][c(9, 10)]
> b <- Grunfeld[["year"]][c(17, 18)]
> c <- Grunfeld[["year"]][c(3, 4, 5)]
> d <- Grunfeld[["year"]][c(7, 8, 9)]
> e <- Grunfeld[["year"]][c(13, 14, 15)]
> f <- Grunfeld[["year"]][c(3, 4, 5, 6)]
> g <- Grunfeld[["year"]][c(12, 13, 14, 15)]
> h <- Grunfeld[["year"]][c(2, 4, 5, 14)]
> i <- Grunfeld[["year"]][c(8, 9, 16, 17, 19)]
> j <- Grunfeld[["year"]][c(2, 3, 15, 16, 17, 19)]
> k <- Grunfeld[["year"]][c(2, 3, 15, 18, 19, 20)]
> l <- Grunfeld[["year"]][c(2, 3, 5, 7, 15, 20)]
> m <- Grunfeld[["year"]][c(3, 5, 8, 9, 16, 17, 19)]
> n <- Grunfeld[["year"]][c(2, 4, 5, 14, 15, 16, 19)]
> o <- Grunfeld[["year"]][c(2, 3, 4, 8, 9, 16, 17, 19)]
> p <- Grunfeld[["year"]][c(2, 3, 5, 7, 15, 18, 19, 20)]
> q <- Grunfeld[["year"]][c(2, 4, 5, 8, 14, 15, 16, 19)]
> 
> models_fe <- lapply(letters[1:17], function(let) plm(inv ~ value + capital, data = Grunfeld[!Grunfeld[["year"]] %in% get(let), ], model = "within"))
> 
> results_modbnf <- lapply(models_fe, pbnftest)
> names(results_modbnf) <- letters[1:17]
> print(unlist(lapply(results_modbnf, function(i) i$statistic)))
     a.DW      b.DW      c.DW      d.DW      e.DW      f.DW      g.DW      h.DW 
0.7057889 0.8065231 0.7375006 0.7014472 0.6742988 0.7332151 0.6122722 0.6938174 
     i.DW      j.DW      k.DW      l.DW      m.DW      n.DW      o.DW      p.DW 
0.9676265 0.9105938 0.8134087 0.6891568 1.0313051 0.9010460 1.0045470 0.8662283 
     q.DW 
0.8734696 
> 
> results_lbi <- lapply(models_fe, function(mod) pbnftest(mod, test = "lbi"))
> names(results_lbi) <- letters[1:17]
> print(unlist(lapply(results_lbi, function(i) i$statistic)))
    a.LBI     b.LBI     c.LBI     d.LBI     e.LBI     f.LBI     g.LBI     h.LBI 
1.0218979 1.1394027 1.1616861 1.0127364 0.9816259 1.1875789 0.9203234 1.2372585 
    i.LBI     j.LBI     k.LBI     l.LBI     m.LBI     n.LBI     o.LBI     p.LBI 
1.4988555 1.5799163 1.1743453 1.3301079 1.8065594 1.6413748 1.7091607 1.5889458 
    q.LBI 
1.6558149 
> 
> # formula interface
> pbnftest(inv ~ value + capital, data = Grunfeld[!Grunfeld$year %in% c(1943, 1944), ], model = "within")

	modified Bhargava/Franzini/Narendranathan Panel Durbin-Watson Test

data:  inv ~ value + capital
DW = 0.70579
alternative hypothesis: serial correlation in idiosyncratic errors

> pbnftest(inv ~ value + capital, data = Grunfeld[!Grunfeld$year %in% c(1943, 1944), ], test = "lbi", model = "within")

	Baltagi/Wu LBI Test for Serial Correlation in Panel Models

data:  inv ~ value + capital
LBI = 1.0219
alternative hypothesis: serial correlation in idiosyncratic errors

> 
> # x <- plm(inv ~ value + capital, data = Grunfeld[!Grunfeld$year %in% c(1943, 1944), ], model = "within")
> # x <- plm(inv ~ value + capital, data = Grunfeld[!Grunfeld$year %in% c(1951, 1952), ], model = "within")
> # x <- plm(inv ~ value + capital, data = Grunfeld, model = "within")
> # x <- plm(inv ~ value + capital, data = Grunfeld, model = "pooling")
> # x <- plm(inv ~ value + capital, data = Grunfeld[!Grunfeld$year %in% c(1943, 1944), ], model = "pooling")
> 
> 
> 
> 
> proc.time()
   user  system elapsed 
  0.813   0.064   0.864 
