lag.formula {fixest} | R Documentation

## Lags a variable using a formula

### Description

Lags a variable using panel id+time identifiers in a formula.

### Usage

    ## S3 method for class 'formula'
    lag(x, k, data, time.step = "unitary", fill = NA,
      duplicate.method = c("none", "first"), ...)

### Arguments

`x` | A formula of the type `var ~ id + time` where `var` is the variable to be lagged, `id` is a variable representing the panel id, and `time` is the time variable of the panel.
`k` | An integer giving the number of lags. For leads, just use a negative number.
`data` | Optional, the data.frame in which to evaluate the formula.

`time.step`

The method to compute the lags. Can be equal to: `"unitary"` (default), `"consecutive"` or to a number. If `"unitary"`, then the largest common divisor between consecutive time periods is used (typically if the time variable represents years, it will be 1). This method can apply only to integer (or convertible to integer) variables. If `"consecutive"`, then the time variable can be of any type: two successive time periods represent a lag of 1. Finally, if the time variable is numeric, you can provide your own numeric time step.

`fill` | How to fill the observations without defined lead/lag values. Default is `NA`.
`duplicate.method` | If several observations have the same id and time values, then the notion of lag is not defined for them. If `duplicate.method = "none"` (default) and duplicate values are found, this leads to an error. You can use `duplicate.method = "first"` so that the first occurrence of identical id/time observations will be used as lag.
`...` | Not currently used.

### Value

It returns a vector of the same type and length as the variable to be lagged in the formula.

### Author(s)

Laurent Berge

### Examples

    # simple example with an unbalanced panel
    base = data.frame(id = rep(1:2, each = 4),
                      time = c(1, 2, 3, 4, 1, 4, 6, 9), x = 1:8)

    lag(x~id+time,  1, base) # lag 1
    lag(x~id+time, -1, base) # lead 1

    lag(x~id+time, 2, base, fill = 0)

    # with time.step = "consecutive"
    lag(x~id+time, 1, base, time.step = "cons")
    # => works for indiv. 2 because 9 (resp. 6) is consecutive to 6 (resp. 4)
    # mostly useful when the time variable is not a number:
    # e.g. c("1991q1", "1991q2", "1991q3") etc

    # with duplicates
    base_dup = data.frame(id = rep(1:2, each = 4),
                          time = c(1, 1, 1, 2, 1, 2, 2, 3), x = 1:8)

    # by default: error

    lag(x~id+time, 1, base_dup)

    # with duplicate.method = "first"
    lag(x~id+time, 1, base_dup, duplicate.method = "first")

    # You can create variables without specifying the data within data.table:
    library(data.table)
    base = data.table(id = rep(1:2, each = 3), year = 1990 + rep(1:3, 2), x = 1:6)
    base[, x.l1 := lag(x~id+year, 1)]

[Package fixest version 0.1.0 Index]