i | R Documentation |
Treat a variable as a factor, or interacts a variable with 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
.
i(factor_var, var, ref, keep, bin, ref2, keep2, bin2, ...)
factor_var |
A vector (of any type) that will be treated as a factor.
You can set references (i.e. exclude values for which to create dummies) with
the |
var |
A variable of the same length as |
ref |
A vector of values to be taken as references from |
keep |
A vector of values to be kept from |
bin |
A list of values to be grouped, a vector, a formula, or the special
values |
ref2 |
A vector of values to be dropped from |
keep2 |
A vector of values to be kept from |
bin2 |
A list or vector defining the binning of the second variable.
See help for the argument |
... |
Not currently used. |
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
.
It returns a matrix with number of rows the length of factor_var
. If there is no interacted
variable or it is interacted with a numeric variable, the number of columns is equal to the
number of cases contained in factor_var
minus the reference(s). If the interacted variable is
a factor, the number of columns is the number of combined cases between factor_var
and var
.
Laurent Berge
iplot
to plot interactions or factors created with i()
, feols
for
OLS estimation with multiple fixed-effects.
See the function bin
for binning variables.
#
# Simple illustration
#
x = rep(letters[1:4], 3)[1:10]
y = rep(1:4, c(1, 2, 3, 4))
# interaction
data.frame(x, y, i(x, y, ref = TRUE))
# without interaction
data.frame(x, i(x, "b"))
# you can interact factors too
z = rep(c("e", "f", "g"), c(5, 3, 2))
data.frame(x, z, i(x, z))
# to force a numeric variable to be treated as a factor: use i.
data.frame(x, y, i(x, i.y))
# Binning
data.frame(x, i(x, bin = list(ab = c("a", "b"))))
# Same as before but using .() for list() and a regular expression
# note that to trigger a regex, you need to use an @ first
data.frame(x, i(x, bin = .(ab = "@a|b")))
#
# In fixest estimations
#
data(base_did)
# We interact the variable 'period' with the variable 'treat'
est_did = feols(y ~ x1 + i(period, treat, 5) | id + period, base_did)
# => plot only interactions with iplot
iplot(est_did)
# Using i() for factors
est_bis = feols(y ~ x1 + i(period, keep = 3:6) + i(period, treat, 5) | id, base_did)
# we plot the second set of variables created with i()
# => we need to use keep (otherwise only the first one is represented)
coefplot(est_bis, keep = "trea")
# => special treatment in etable
etable(est_bis, dict = c("6" = "six"))
#
# Interact two factors
#
# We use the i. prefix to consider week as a factor
data(airquality)
aq = airquality
aq$week = aq$Day %/% 7 + 1
# Interacting Month and week:
res_2F = feols(Ozone ~ Solar.R + i(Month, i.week), aq)
# Same but dropping the 5th Month and 1st week
res_2F_bis = feols(Ozone ~ Solar.R + i(Month, i.week, ref = 5, ref2 = 1), aq)
etable(res_2F, res_2F_bis)
#
# Binning
#
data(airquality)
feols(Ozone ~ i(Month, bin = "bin::2"), airquality)
feols(Ozone ~ i(Month, bin = list(summer = 7:9)), airquality)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.