[R] Unexpected interference between dplyr and plm

Constantin Weiser weiserc at hhu.de
Tue Nov 29 15:36:03 CET 2016


Hello,

I'm struggling with an unexpected interference between the two packages 
dplyr and plm, or to be more concrete with the "lag(x, ...)" function of 
both packages.

If dplyr is in the namespace the plm function uses no longer the 
appropriate lag()-function which accounts for the panel structure.

The following code demonstrates the unexpected behaviour:

## starting from a new R-Session (plm and dplyr unloaded) ##

   ## generate dataset
   set.seed(4711)
   df <- data.frame(
           i = rep(1:10, each = 4),
           t = rep(1:4, times = 10),
           y = rnorm(40),
           x = rnorm(40)
   )
   ## manually generated laged variable
   df$lagx <- c(NA, df$x[-40])
   df$lagx[df$t == 1] <- NA


require(plm)
summary(plm(y~lagx, data = df, index = c("i", "t")))
summary(plm(y~lag(x, 1), data = df, index = c("i", "t")))
# > this result is expected

require(dplyr)
summary(plm(y~lagx, data = df, index = c("i", "t")))
summary(plm(y~lag(x, 1), data = df, index = c("i", "t")))
# > this result is unexpected

Is there a way to force R to use the "correct" lag-function? (or at the 
devel-level to harmonise both functions)

Thank you very much in advance for your answer

Yours
Constantin

-- 
^
|                X
|               /eiser, Dr. Constantin (weiserc at hhu.de)
|              /Chair of Statistics and Econometrics
|             / Heinrich Heine-University of Düsseldorf
| *    /\    /  Universitätsstraße 1, 40225 Düsseldorf, Germany
|  \  /  \  /   Oeconomicum (Building 24.31), Room 01.22
|   \/    \/    Tel: 0049 211 81-15307
+----------------------------------------------------------->



More information about the R-help mailing list