R: Lags a variable in a 'fixest' estimation

f {fixest} | R Documentation

## Lags a variable in a `fixest` estimation

### Description

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

### Usage

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

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

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

### Arguments

`x` | The variable.
`lead` | 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.
`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).
`lag` | 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.

### 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`.

### 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)
    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))]
    }

[Package fixest version 0.10.0 Index]