Search code examples
rplm

Error in `colnames<-`(`*tmp*`, value = c("x", "y", "x_1", "x_2", "y_1", : length of 'dimnames' [2] not equal to array extent for Panel Data


I encounter the above error while attempting to run the Granger causality test for panel data using the pgrangertest function from the plm package. I read several questions by users facing a similar issue and tried the suggestions given there, however, none of them could solve my problem.

Essentially, I have a panel data which looks something lime this:

>head(granger_data)
       panel_id     time_id close_close_ret log_volume
25-2        25       2     0.004307257       4.753590
25-3        25       3    -0.001912046       8.249836
25-4        25       4     0.011417821       8.628377
25-5        25       5     0.018744691       9.134754
25-6        25       6    -0.024913157       8.920122
25-7        25       7    -0.008604260       8.724370


str(granger_data)
'data.frame':   105209 obs. of  4 variables:
 $ panel_id       : Factor w/ 938 levels "25","26","27",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ time_id        : Factor w/ 323 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ close_close_ret: num  NA 0.00431 -0.00191 0.01142 0.01874 ...
 $ log_volume : num  4.88 4.75 8.25 8.63 9.13 ...

Now, I want to run the granger causality test for panel data using the pgrangertest function from the plm package and while doing so, I encounter the following problem:

> vol_ret <- pgrangertest(log_volume ~ close_close_ret,data = granger_data)
Error in `colnames<-`(`*tmp*`, value = c("x", "y", "x_1", "y_1")) : 
length of 'dimnames' [2] not equal to array extent

I even read the source code of the function and tried to understand where the error came, from, but I couldn't figure it out.


Solution

  • The panel granger test requires to have times series of length 5+3*order per individual otherwise the second order moments of the individual Wald statistics do not exist. pgrangertest in package plm has a check for that since version 1.7-0 of the package. From its NEWS file:

    • pgrangertest: better detection of infeasibility of test due to lacking data.

    It gives an informative error message in case you supply too short a time series for an individual, like in the case you encountered, e.g.:

    Error in pgrangertest(inv ~ value, data = pG, order = 1) : Condition for test = "Ztilde" not met for all individuals: length of time series must be larger than 5+3*order (>5+3*1=8)