I want to create lags of a variable. In a panel data setting, obviously lags are only considered within each panel.
How come that plm
's lag()
does not respect the panel structure (by default) and is there a way to change that (without manually dplyr it)?
# Load example data
data("EmplUK", package = "plm")
Em <- pdata.frame(EmplUK, index=c('firm', 'year'))
# how I think it should have worked
Em$lwage_incorrect = lag(Em$wage)
# what actually works
Em= Em %>% group_by(firm) %>% mutate(lwage_correct = lag(wage))