Skip to contents

Produce 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).

Usage

f(x, k = 1, fill = NA)

d(x, k = 1, fill = NA)

l(x, k = 1, fill = NA)

Arguments

x

The variable.

k

A vector of integers giving the number of lags (for l() and d()) or leads (for f()). For l() and d() negative values lead to leads. For f() negative values lead to lags. This argument can be a vector when using it in fixest estimations. When creating variables in a data.table::data.table, it must be of length one.

fill

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).

Value

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.

Functions

  • f(): Forwards a variable (inverse of lagging) in a fixest estimation

  • d(): Creates differences (i.e. x - lag(x)) in a fixest estimation

See also

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).

Examples


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)
#> NOTE: 108 observations removed because of NA values (RHS: 108).
est2 = feols(f(y) ~ l(x1, -1:1), pdat)
#> NOTE: 216 observations removed because of NA values (LHS: 108, RHS: 216).
est3 = feols(l(y) ~ l(x1, 0:3), pdat)
#> NOTE: 324 observations removed because of NA values (LHS: 108, RHS: 324).
etable(est1, est2, est3, order = c("f", "^x"), drop = "Int")
#>                               est1               est2               est3
#> Dependent Var.:                  y             f(y,1)             l(y,1)
#>                                                                         
#> f(x1,1)                            0.9940*** (0.0542)                   
#> x1              0.9948*** (0.0487)    0.0081 (0.0592)   -0.0534 (0.0545)
#> Constant         2.235*** (0.2032)  2.464*** (0.2233)  2.196*** (0.2110)
#> l(x1,1)            0.0410 (0.0558)    0.0157 (0.0640) 0.9871*** (0.0551)
#> l(x1,2)                                                  0.0220 (0.0580)
#> l(x1,3)                                                  0.0102 (0.0639)
#> _______________ __________________ __________________ __________________
#> S.E.: Clustered             by: id             by: id             by: id
#> Observations                   972                864                756
#> R2                         0.26558            0.25697            0.25875
#> Adj. R2                    0.26406            0.25438            0.25480
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# or using the argument panel.id
feols(f(y) ~ l(x1, -1:1), base_did, panel.id = ~id + period)
#> NOTE: 216 observations removed because of NA values (LHS: 108, RHS: 216).
#> OLS estimation, Dep. Var.: f(y, 1)
#> Observations: 864
#> Standard-errors: Clustered (id) 
#>             Estimate Std. Error   t value  Pr(>|t|)    
#> (Intercept) 2.464313   0.223277 11.037009 < 2.2e-16 ***
#> f(x1, 1)    0.994018   0.054216 18.334504 < 2.2e-16 ***
#> x1          0.008072   0.059247  0.136241   0.89189    
#> l(x1, 1)    0.015693   0.063958  0.245360   0.80665    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 4.97418   Adj. R2: 0.254377
feols(d(y) ~ d(x1), base_did, panel.id = ~id + period)
#> NOTE: 108 observations removed because of NA values (LHS: 108, RHS: 108).
#> OLS estimation, Dep. Var.: d(y, 1)
#> Observations: 972
#> Standard-errors: Clustered (id) 
#>             Estimate Std. Error  t value   Pr(>|t|)    
#> (Intercept) 0.510685   0.072720  7.02262 2.0948e-10 ***
#> d(x1, 1)    0.972664   0.053437 18.20224  < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 5.83885   Adj. R2: 0.329253

# 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))]
}
#> Loading required package: data.table