fixest
estimationProduce lags or leads in the formulas of fixest
estimations or when creating variables in a data.table
. The data must be set as a panel beforehand (either with the function panel
or with the argument panel.id
in the estimation).
f(x, lead = 1, fill = NA)l(x, lag = 1, fill = NA)
The variable.
A vector of integers giving the number of leads. Negative values lead to lags. This argument can be a vector when using it in fixest estimations. When creating variables in a data.table
, it **must** be of length one.
A scalar, default is NA
. How to fill the missing values due to the lag/lead? Note that in a fixest
estimation, 'fill' must be numeric (not required when creating new variables).
A vector of integers giving the number of lags. Negative values lead to leads. This argument can be a vector when using it in fixest estimations. When creating variables in a data.table
, it **must** be of length one.
These functions can only be used i) in a formula of a fixest
estimation, or ii) when creating variables within a fixest_panel
object (obtained with function panel
) which is alaos a data.table
.
f
: Forwards a variable (inverse of lagging) in a fixest
estimation
The function panel
changes data.frames
into a panel from which the functions l
and f
can be called. Otherwise you can set the panel 'live' during the estimation using the argument panel.id
(see for example in the function feols
).
# NOT RUN {
data(base_did)
# Setting a data set as a panel...
pdat = panel(base_did, ~id+period)
# ...then using the functions l and f
est1 = feols(y~l(x1, 0:1), pdat)
est2 = feols(f(y)~l(x1, -1:1), pdat)
est3 = feols(l(y)~l(x1, 0:3), pdat)
etable(est1, est2, est3, order = c("f", "^x"), drop="Int")
# or using the argument panel.id
feols(f(y)~l(x1, -1:1), base_did, panel.id = ~id+period)
# l() and f() can also be used within a data.table:
if(require("data.table")){
pdat_dt = panel(as.data.table(base_did), ~id+period)
# Now since pdat_dt is also a data.table
# you can create lags/leads directly
pdat_dt[, x1_l1 := l(x1)]
pdat_dt[, c("x1_l1_fill0", "y_f2") := .(l(x1, fill = 0), f(y, 2))]
}
# }
Run the code above in your browser using DataLab