f | R Documentation |
fixest
estimationProduce lags or leads in the formulas of fixest
estimations or when creating variables in
a data.table::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, k = 1, fill = NA)
d(x, k = 1, fill = NA)
l(x, k = 1, fill = NA)
x |
The variable. |
k |
A vector of integers giving the number of lags (for |
fill |
A scalar, default is |
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::data.table
.
f()
: Forwards a variable (inverse of lagging) in a fixest
estimation
d()
: Creates differences (i.e. x - lag(x)) 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
).
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)
feols(d(y) ~ d(x1), 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[, x1_d1 := d(x1)]
pdat_dt[, c("x1_l1_fill0", "y_f2") := .(l(x1, fill = 0), f(y, 2))]
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.