R: Create, or interact variables with, factors

i {fixest} | R Documentation

## Create, or interact variables with, factors

### Description

Treat a variable as a factor, or interacts a variable with another treated as a factor. Values to be dropped/kept from the factor can be easily set. Note that to interact fixed-effects, this function should not be used: instead use directly the syntax `fe1^fe2`.

### Usage

    i(var, f, f2, ref, drop, keep, drop2, keep2)

    interact(var, f, f2, ref, drop, keep, drop2, keep2)

### Arguments

`var` | A vector to be interacted with `f`. If the other argument `f` is missing, then this vector will be treated as the argument `f`.
`f` | A vector (of any type) that will be treated as a factor. Must be of the same length as `var` if `var` is not missing.
`f2` | A vector (of any type) that will be treated as a factor. Must be of the same length as `f`.
`ref` | A single value that belongs to the interacted variable (`f`). Can be missing, can also be a logical: if `TRUE`, then the first value of `f` will be removed..
`drop` | A vector of regular expressions or integers (if `f` is integer). If provided, all values from `f` that match `drop` will be removed.
`keep` | A vector of regular expressions or integers (if `f` is integer). If provided, only the values from `f` that match `keep` will be kept.
`drop2` | A vector of regular expressions or integers (if `f2` is integer). If provided, all values from `f2` that match `drop2` will be removed.
`keep2` | A vector of regular expressions or integers (if `f2` is integer). If provided, only the values from `f2` that match `keep2` will be kept.

### Details

To interact fixed-effects, this function should not be used: instead use directly the syntax `fe1^fe2` in the fixed-effects part of the formula. Please see the details and examples in the help page of `feols`.

### Value

It returns a matrix with number of rows the length of `var`. The number of columns is equal to the number of cases contained in `f` minus the reference(s).

### Shorthand in `fixest` estimations

In `fixest` estimations, instead of using `i(var, f, ref)`, you can instead use the following writing `var::f(ref)`. Note that this way of doing interactions is not endorsed any more and will likely be deprecated in the future.

### Author(s)

Laurent Berge

### See Also

`coefplot` to plot interactions, `feols` for OLS estimation with multiple fixed-effects.

### Examples

    #
    # Simple illustration
    #

    x = 1:10
    y = rep(1:4, 3)[1:10]

    # interaction
    cbind(x, y, i(x, y, 1))

    # without interaction
    cbind(x, y, i(y, ref = 1))

    # You can interact factors too
    z = rep(c("a", "b", "c"), c(5, 3, 2))
    data.frame(z, y, i(z, y))

    #
    # In fixest estimations
    #

    data(base_did)
    # We interact the variable 'period' with the variable 'treat'
    est_did = feols(y ~ x1 + i(treat, period, 5) | id + period, base_did)

    # => special treatment in coefplot
    coefplot(est_did)

    # Using i() for factors
    est_bis = feols(y ~ x1 + i(period, keep = 3:6) + i(treat, period, 5) | id, base_did)

    coefplot(est_bis, only.inter = FALSE)

    # => special treatment in etable
    etable(est_bis, dict = c("6" = "six"))

    #
    # Interact two factors => f2
    #

    # To interact two factor, use the argument f2
    data(airquality)
    aq = airquality
    aq$week = aq$Day %/% 7 + 1

    # Interacting Month and week:
    res_2F = feols(Ozone ~ Solar.R + i(Month, f2 = week), aq)

    # Same but dropping the 5th Month and 1st week
    res_2F_bis = feols(Ozone ~ Solar.R + i(Month, f2 = week, drop = 5, drop2 = 1), aq)

    etable(res_2F, res_2F_bis)

[Package fixest version 0.8.4 Index]