feols | R Documentation |
Estimates OLS with any number of fixed-effects.
feols(
fml,
data,
vcov,
weights,
offset,
subset,
split,
fsplit,
split.keep,
split.drop,
cluster,
se,
ssc,
panel.id,
fixef,
fixef.rm = "none",
fixef.tol = 1e-06,
fixef.iter = 10000,
fixef.algo = NULL,
collin.tol = 1e-10,
nthreads = getFixest_nthreads(),
lean = FALSE,
verbose = 0,
warn = TRUE,
notes = getFixest_notes(),
only.coef = FALSE,
data.save = FALSE,
combine.quick,
demeaned = FALSE,
mem.clean = FALSE,
only.env = FALSE,
env,
...
)
feols.fit(
y,
X,
fixef_df,
vcov,
offset,
split,
fsplit,
split.keep,
split.drop,
cluster,
se,
ssc,
weights,
subset,
fixef.rm = "perfect",
fixef.tol = 1e-06,
fixef.iter = 10000,
fixef.algo = NULL,
collin.tol = 1e-10,
nthreads = getFixest_nthreads(),
lean = FALSE,
warn = TRUE,
notes = getFixest_notes(),
mem.clean = FALSE,
verbose = 0,
only.env = FALSE,
only.coef = FALSE,
env,
...
)
fml |
A formula representing the relation to be estimated. For example: |
data |
A data.frame containing the necessary variables to run the model.
The variables of the non-linear right hand side of the formula are identified
with this |
vcov |
Versatile argument to specify the VCOV. In general, it is either a character
scalar equal to a VCOV type, either a formula of the form: |
weights |
A formula or a numeric vector. Each observation can be weighted,
the weights must be greater than 0. If equal to a formula, it should be one-sided:
for example |
offset |
A formula or a numeric vector. An offset can be added to the estimation.
If equal to a formula, it should be of the form (for example) |
subset |
A vector (logical or numeric) or a one-sided formula. If provided, then the estimation will be performed only on the observations defined by this argument. |
split |
A one sided formula representing a variable (eg |
fsplit |
A one sided formula representing a variable (eg |
split.keep |
A character vector. Only used when |
split.drop |
A character vector. Only used when |
cluster |
Tells how to cluster the standard-errors (if clustering is requested).
Can be either a list of vectors, a character vector of variable names, a formula or
an integer vector. Assume we want to perform 2-way clustering over |
se |
Character scalar. Which kind of standard error should be computed:
“standard”, “hetero”, “cluster”, “twoway”, “threeway”
or “fourway”? By default if there are clusters in the estimation:
|
ssc |
An object of class |
panel.id |
The panel identifiers. Can either be: i) a one sided formula
(e.g. |
fixef |
Character vector. The names of variables to be used as fixed-effects. These variables should contain the identifier of each observation (e.g., think of it as a panel identifier). Note that the recommended way to include fixed-effects is to insert them directly in the formula. |
fixef.rm |
Can be equal to "perfect" (default), "singleton", "both" or "none". Controls which observations are to be removed. If "perfect", then observations having a fixed-effect with perfect fit (e.g. only 0 outcomes in Poisson estimations) will be removed. If "singleton", all observations for which a fixed-effect appears only once will be removed. Note, importantly, that singletons are removed in just one pass, there is no recursivity implemented. The meaning of "both" and "none" is direct. |
fixef.tol |
Precision used to obtain the fixed-effects. Defaults to |
fixef.iter |
Maximum number of iterations in fixed-effects algorithm (only in use for 2+ fixed-effects). Default is 10000. |
fixef.algo |
|
collin.tol |
Numeric scalar, default is |
nthreads |
The number of threads. Can be: a) an integer lower than, or equal to,
the maximum number of threads; b) 0: meaning all available threads will be used;
c) a number strictly between 0 and 1 which represents the fraction of all threads to use.
The default is to use 50% of all threads. You can set permanently the number
of threads used within this package using the function |
lean |
Logical, default is |
verbose |
Integer. Higher values give more information. In particular, it can detail the number of iterations in the demeaning algorithm (the first number is the left-hand-side, the other numbers are the right-hand-side variables). |
warn |
Logical, default is |
notes |
Logical. By default, two notes are displayed: when NAs are removed
(to show additional information) and when some observations are removed because
of collinearity. To avoid displaying these messages, you can set |
only.coef |
Logical, default is |
data.save |
Logical scalar, default is |
combine.quick |
Logical. When you combine different variables to transform them
into a single fixed-effects you can do e.g. |
demeaned |
Logical, default is |
mem.clean |
Logical, default is |
only.env |
(Advanced users.) Logical, default is |
env |
(Advanced users.) A |
... |
Not currently used. |
y |
Numeric vector/matrix/data.frame of the dependent variable(s). Multiple dependent
variables will return a |
X |
Numeric matrix of the regressors. |
fixef_df |
Matrix/data.frame of the fixed-effects. |
The method used to demean each variable along the fixed-effects is based on Berge (2018), since this is the same problem to solve as for the Gaussian case in a ML setup.
A fixest
object. Note that fixest
objects contain many elements and most of them are
for internal use, they are presented here only for information. To access them, it is safer
to use the user-level methods (e.g. vcov.fixest
, resid.fixest
, etc) or functions
(like for instance fitstat
to access any fit statistic).
nobs |
The number of observations. |
fml |
The linear formula of the call. |
call |
The call of the function. |
method |
The method used to estimate the model. |
data |
The original data set used when calling the function. Only available when
the estimation was called with |
fml_all |
A list containing different parts of the formula. Always contain the linear formula. Then depending on the cases: |
fixef_vars |
The names of each fixed-effect dimension. |
fixef_id |
The list (of length the number of fixed-effects) of the fixed-effects identifiers for each observation. |
fixef_sizes |
The size of each fixed-effect (i.e. the number of unique identifierfor each fixed-effect dimension). |
coefficients |
The named vector of estimated coefficients. |
multicol |
Logical, if multicollinearity was found. |
coeftable |
The table of the coefficients with their standard errors, z-values and p-values. |
loglik |
The loglikelihood. |
ssr_null |
Sum of the squared residuals of the null model (containing only with the intercept). |
ssr_fe_only |
Sum of the squared residuals of the model estimated with fixed-effects only. |
ll_null |
The log-likelihood of the null model (containing only with the intercept). |
ll_fe_only |
The log-likelihood of the model estimated with fixed-effects only. |
fitted.values |
The fitted values. |
linear.predictors |
The linear predictors. |
residuals |
The residuals (y minus the fitted values). |
sq.cor |
Squared correlation between the dependent variable and the expected predictor (i.e. fitted.values) obtained by the estimation. |
hessian |
The Hessian of the parameters. |
cov.iid |
The variance-covariance matrix of the parameters. |
se |
The standard-error of the parameters. |
scores |
The matrix of the scores (first derivative for each observation). |
residuals |
The difference between the dependent variable and the expected predictor. |
sumFE |
The sum of the fixed-effects coefficients for each observation. |
offset |
(When relevant.) The offset formula. |
weights |
(When relevant.) The weights formula. |
obs_selection |
(When relevant.) List containing vectors of integers. It represents the sequential selection of observation vis a vis the original data set. |
collin.var |
(When relevant.) Vector containing the variables removed because of collinearity. |
collin.coef |
(When relevant.) Vector of coefficients, where the values of the variables removed because of collinearity are NA. |
collin.min_norm |
The minimal diagonal value of the Cholesky decomposition. Small values indicate possible presence collinearity. |
y_demeaned |
Only when |
X_demeaned |
Only when |
You can combine two variables to make it a new fixed-effect using ^
.
The syntax is as follows: fe_1^fe_2
. Here you created a new variable which is the combination
of the two variables fe_1 and fe_2. This is identical to doing paste0(fe_1, "_", fe_2)
but more convenient.
Note that pasting is a costly operation, especially for large data sets.
Thus, the internal algorithm uses a numerical trick which is fast, but the drawback is
that the identity of each observation is lost (i.e. they are now equal to a meaningless
number instead of being equal to paste0(fe_1, "_", fe_2)
). These “identities”
are useful only if you're interested in the value of the fixed-effects (that you can
extract with fixef.fixest
). If you're only interested in coefficients of the variables,
it doesn't matter. Anyway, you can use combine.quick = FALSE
to tell the internal
algorithm to use paste
instead of the numerical trick. By default, the numerical
trick is performed only for large data sets.
You can add variables with varying slopes in the fixed-effect part of the formula.
The syntax is as follows: fixef_var[var1, var2]
. Here the variables var1 and var2 will
be with varying slopes (one slope per value in fixef_var) and the fixed-effect
fixef_var will also be added.
To add only the variables with varying slopes and not the fixed-effect,
use double square brackets: fixef_var[[var1, var2]]
.
In other words:
fixef_var[var1, var2]
is equivalent to fixef_var + fixef_var[[var1]] + fixef_var[[var2]]
fixef_var[[var1, var2]]
is equivalent to fixef_var[[var1]] + fixef_var[[var2]]
In general, for convergence reasons, it is recommended to always add the fixed-effect and avoid using only the variable with varying slope (i.e. use single square brackets).
To use leads/lags of variables in the estimation, you can: i) either provide the argument
panel.id
, ii) either set your data set as a panel with the function
panel
, f
and d
.
You can provide several leads/lags/differences at once: e.g. if your formula is equal to
f(y) ~ l(x, -1:1)
, it means that the dependent variable is equal to the lead of y
,
and you will have as explanatory variables the lead of x1
, x1
and the lag of x1
.
See the examples in function l
for more details.
You can interact a numeric variable with a "factor-like" variable by using
i(factor_var, continuous_var, ref)
, where continuous_var
will be interacted with
each value of factor_var
and the argument ref
is a value of factor_var
taken as a reference (optional).
Using this specific way to create interactions leads to a different display of the
interacted values in etable
. See examples.
It is important to note that if you do not care about the standard-errors of
the interactions, then you can add interactions in the fixed-effects part of the formula,
it will be incomparably faster (using the syntax factor_var[continuous_var]
, as explained
in the section “Varying slopes”).
The function i
has in fact more arguments, please see details in its associated help page.
Standard-errors can be computed in different ways, you can use the arguments se
and ssc
in summary.fixest
to define how to compute them. By default, in the presence
of fixed-effects, standard-errors are automatically clustered.
The following vignette: On standard-errors describes in details how the standard-errors are computed in
fixest
and how you can replicate standard-errors from other software.
You can use the functions setFixest_vcov
and setFixest_ssc
to
permanently set the way the standard-errors are computed.
To estimate two stage least square regressions, insert the relationship between the endogenous regressor(s) and the instruments in a formula, after a pipe.
For example, fml = y ~ x1 | x_endo ~ x_inst
will use the variables x1
and x_inst
in
the first stage to explain x_endo
. Then will use the fitted value of x_endo
(which will be named fit_x_endo
) and x1
to explain y
.
To include several endogenous regressors, just use "+",
like in: fml = y ~ x1 | x_endo1 + x_end2 ~ x_inst1 + x_inst2
.
Of course you can still add the fixed-effects, but the IV formula must always come last,
like in fml = y ~ x1 | fe1 + fe2 | x_endo ~ x_inst
.
If you want to estimate a model without exogenous variables, use "1"
as a
placeholder: e.g. fml = y ~ 1 | x_endo ~ x_inst
.
By default, the second stage regression is returned. You can access the first stage(s)
regressions either directly in the slot iv_first_stage
(not recommended),
or using the argument stage = 1
from the function summary.fixest
.
For example summary(iv_est, stage = 1)
will give the first stage(s).
Note that using summary you can display both the second and first stages at
the same time using, e.g., stage = 1:2
(using 2:1
would reverse the order).
Multiple estimations can be performed at once, they just have to be specified in the formula.
Multiple estimations yield a fixest_multi
object which is ‘kind of’ a list of
all the results but includes specific methods to access the results in a handy way.
Please have a look at the dedicated vignette:
Multiple estimations.
To include multiple dependent variables, wrap them in c()
(list()
also works).
For instance fml = c(y1, y2) ~ x1
would estimate the model fml = y1 ~ x1
and
then the model fml = y2 ~ x1
.
To include multiple independent variables, you need to use the stepwise functions.
There are 4 stepwise functions: sw
, sw0
, csw
, csw0
, and mvsw
. Of course sw
stands for stepwise, and csw
for cumulative stepwise. Finally mvsw
is a bit special,
it stands for multiverse stepwise. Let's explain that.
Assume you have the following formula: fml = y ~ x1 + sw(x2, x3)
.
The stepwise function sw
will estimate the following two models: y ~ x1 + x2
and
y ~ x1 + x3
. That is, each element in sw()
is sequentially, and separately,
added to the formula. Would have you used sw0
in lieu of sw
, then the model
y ~ x1
would also have been estimated. The 0
in the name means that the model
without any stepwise element also needs to be estimated.
The prefix c
means cumulative: each stepwise element is added to the next. That is,
fml = y ~ x1 + csw(x2, x3)
would lead to the following models y ~ x1 + x2
and
y ~ x1 + x2 + x3
. The 0
has the same meaning and would also lead to the model without
the stepwise elements to be estimated: in other words, fml = y ~ x1 + csw0(x2, x3)
leads to the following three models: y ~ x1
, y ~ x1 + x2
and y ~ x1 + x2 + x3
.
Finally mvsw
will add, in a stepwise fashion all possible combinations of the variables
in its arguments. For example mvsw(x1, x2, x3)
is equivalent to
sw0(x1, x2, x3, x1 + x2, x1 + x3, x2 + x3, x1 + x2 + x3)
. The number of models
to estimate grows at a factorial rate: so be cautious!
Multiple independent variables can be combined with multiple dependent variables, as in
fml = c(y1, y2) ~ cw(x1, x2, x3)
which would lead to 6 estimations. Multiple
estimations can also be combined to split samples (with the arguments split
, fsplit
).
You can also add fixed-effects in a stepwise fashion. Note that you cannot perform
stepwise estimations on the IV part of the formula (feols
only).
If NAs are present in the sample, to avoid too many messages, only NA removal concerning the variables common to all estimations is reported.
A note on performance. The feature of multiple estimations has been highly optimized for
feols
, in particular in the presence of fixed-effects. It is faster to estimate
multiple models using the formula rather than with a loop. For non-feols
models using
the formula is roughly similar to using a loop performance-wise.
To use multiple dependent variables in fixest
estimations, you need to include them
in a vector: like in c(y1, y2, y3)
.
First, if names are stored in a vector, they can readily be inserted in a formula to
perform multiple estimations using the dot square bracket operator. For instance if
my_lhs = c("y1", "y2")
, calling fixest
with, say feols(.[my_lhs] ~ x1, etc)
is
equivalent to using feols(c(y1, y2) ~ x1, etc)
. Beware that this is a special feature
unique to the left-hand-side of fixest
estimations (the default behavior of the DSB
operator is to aggregate with sums, see xpd
).
Second, you can use a regular expression to grep the left-hand-sides on the fly. When the
..("regex")
feature is used naked on the LHS, the variables grepped are inserted into
c()
. For example ..("Pe") ~ Sepal.Length, iris
is equivalent to
c(Petal.Length, Petal.Width) ~ Sepal.Length, iris
. Beware that this is a
special feature unique to the left-hand-side of fixest
estimations
(the default behavior of ..("regex")
is to aggregate with sums, see xpd
).
When the data set has been set up globally using
setFixest_estimation
(data = data_set)
, the argument vcov
can be used implicitly.
This means that calls such as feols(y ~ x, "HC1")
, or feols(y ~ x, ~id)
, are valid:
i) the data is automatically deduced from the global settings, and ii) the vcov
is deduced to be the second argument.
Although the argument 'data' is placed in second position, the data can be piped to the
estimation functions. For example, with R >= 4.1, mtcars |> feols(mpg ~ cyl)
works as
feols(mpg ~ cyl, mtcars)
.
In a formula, the dot square bracket (DSB) operator can: i) create manifold variables at once, or ii) capture values from the current environment and put them verbatim in the formula.
Say you want to include the variables x1
to x3
in your formula. You can use
xpd(y ~ x.[1:3])
and you'll get y ~ x1 + x2 + x3
.
To summon values from the environment, simply put the variable in square brackets. For example:
for(i in 1:3) xpd(y.[i] ~ x)
will create the formulas y1 ~ x
to y3 ~ x
depending on the
value of i
.
You can include a full variable from the environment in the same way:
for(y in c("a", "b")) xpd(.[y] ~ x)
will create the two formulas a ~ x
and b ~ x
.
The DSB can even be used within variable names, but then the variable must be nested in
character form. For example y ~ .["x.[1:2]_sq"]
will create y ~ x1_sq + x2_sq
. Using the
character form is important to avoid a formula parsing error. Double quotes must be used. Note
that the character string that is nested will be parsed with the function dsb
, and thus it
will return a vector.
By default, the DSB operator expands vectors into sums. You can add a comma, like in .[, x]
,
to expand with commas–the content can then be used within functions. For instance:
c(x.[, 1:2])
will create c(x1, x2)
(and not c(x1 + x2)
).
In all fixest
estimations, this special parsing is enabled, so you don't need to use xpd
.
One-sided formulas can be expanded with the DSB operator: let x = ~sepal + petal
, then
xpd(y ~ .[x])
leads to color ~ sepal + petal
.
You can even use multiple square brackets within a single variable, but then the use of nesting
is required. For example, the following xpd(y ~ .[".[letters[1:2]]_.[1:2]"])
will create
y ~ a_1 + b_2
. Remember that the nested character string is parsed with dsb
,
which explains this behavior.
When the element to be expanded i) is equal to the empty string or, ii) is of length 0, it is
replaced with a neutral element, namely 1
. For example, x = "" ; xpd(y ~ .[x])
leads to
y ~ 1
.
Laurent Berge
Berge, Laurent, 2018, "Efficient estimation of maximum likelihood models with multiple fixed-effects: the R package FENmlm." CREA Discussion Papers, 13 ().
For models with multiple fixed-effects:
Gaure, Simen, 2013, "OLS with multiple high dimensional category variables", Computational Statistics & Data Analysis 66 pp. 8–18
See also summary.fixest
to see the results with the appropriate standard-errors,
fixef.fixest
to extract the fixed-effects coefficients, and the function etable
to visualize the results of multiple estimations. For plotting coefficients: see coefplot
.
And other estimation methods: femlm
, feglm
, fepois
, fenegbin
, feNmlm
.
#
# Basic estimation
#
res = feols(Sepal.Length ~ Sepal.Width + Petal.Length, iris)
# You can specify clustered standard-errors in summary:
summary(res, cluster = ~Species)
#
# Just one set of fixed-effects:
#
res = feols(Sepal.Length ~ Sepal.Width + Petal.Length | Species, iris)
# By default, the SEs are clustered according to the first fixed-effect
summary(res)
#
# Varying slopes:
#
res = feols(Sepal.Length ~ Petal.Length | Species[Sepal.Width], iris)
summary(res)
#
# Combining the FEs:
#
base = iris
base$fe_2 = rep(1:10, 15)
res_comb = feols(Sepal.Length ~ Petal.Length | Species^fe_2, base)
summary(res_comb)
fixef(res_comb)[[1]]
#
# Using leads/lags:
#
data(base_did)
# We need to set up the panel with the arg. panel.id
est1 = feols(y ~ l(x1, 0:1), base_did, panel.id = ~id+period)
est2 = feols(f(y) ~ l(x1, -1:1), base_did, panel.id = ~id+period)
etable(est1, est2, order = "f", drop="Int")
#
# Using interactions:
#
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)
# Now we can plot the result of the interaction with coefplot
coefplot(est_did)
# You have many more example in coefplot help
#
# Instrumental variables
#
# To estimate Two stage least squares,
# insert a formula describing the endo. vars./instr. relation after a pipe:
base = iris
names(base) = c("y", "x1", "x2", "x3", "fe1")
base$x_inst1 = 0.2 * base$x1 + 0.7 * base$x2 + rpois(150, 2)
base$x_inst2 = 0.2 * base$x2 + 0.7 * base$x3 + rpois(150, 3)
base$x_endo1 = 0.5 * base$y + 0.5 * base$x3 + rnorm(150, sd = 2)
base$x_endo2 = 1.5 * base$y + 0.5 * base$x3 + 3 * base$x_inst1 + rnorm(150, sd = 5)
# Using 2 controls, 1 endogenous var. and 1 instrument
res_iv = feols(y ~ x1 + x2 | x_endo1 ~ x_inst1, base)
# The second stage is the default
summary(res_iv)
# To show the first stage:
summary(res_iv, stage = 1)
# To show both the first and second stages:
summary(res_iv, stage = 1:2)
# Adding a fixed-effect => IV formula always last!
res_iv_fe = feols(y ~ x1 + x2 | fe1 | x_endo1 ~ x_inst1, base)
# With two endogenous regressors
res_iv2 = feols(y ~ x1 + x2 | x_endo1 + x_endo2 ~ x_inst1 + x_inst2, base)
# Now there's two first stages => a fixest_multi object is returned
sum_res_iv2 = summary(res_iv2, stage = 1)
# You can navigate through it by subsetting:
sum_res_iv2[iv = 1]
# The stage argument also works in etable:
etable(res_iv, res_iv_fe, res_iv2, order = "endo")
etable(res_iv, res_iv_fe, res_iv2, stage = 1:2, order = c("endo", "inst"),
group = list(control = "!endo|inst"))
#
# Multiple estimations:
#
# 6 estimations
est_mult = feols(c(Ozone, Solar.R) ~ Wind + Temp + csw0(Wind:Temp, Day), airquality)
# We can display the results for the first lhs:
etable(est_mult[lhs = 1])
# And now the second (access can be made by name)
etable(est_mult[lhs = "Solar.R"])
# Now we focus on the two last right hand sides
# (note that .N can be used to specify the last item)
etable(est_mult[rhs = 2:.N])
# Combining with split
est_split = feols(c(Ozone, Solar.R) ~ sw(poly(Wind, 2), poly(Temp, 2)),
airquality, split = ~ Month)
# You can display everything at once with the print method
est_split
# Different way of displaying the results with "compact"
summary(est_split, "compact")
# You can still select which sample/LHS/RHS to display
est_split[sample = 1:2, lhs = 1, rhs = 1]
#
# Split sample estimations
#
base = setNames(iris, c("y", "x1", "x2", "x3", "species"))
est = feols(y ~ x.[1:3], base, split = ~species)
etable(est)
# You can select specific values with the %keep% and %drop% operators
# By default, partial matching is enabled. It should refer to a single variable.
est = feols(y ~ x.[1:3], base, split = ~species %keep% c("set", "vers"))
etable(est)
# You can supply regular expression by using an @ first.
# regex can match several values.
est = feols(y ~ x.[1:3], base, split = ~species %keep% c("@set|vers"))
etable(est)
#
# Argument sliding
#
# When the data set is set up globally, you can use the vcov argument implicitly
base = setNames(iris, c("y", "x1", "x2", "x3", "species"))
no_sliding = feols(y ~ x1 + x2, base, ~species)
# With sliding
setFixest_estimation(data = base)
# ~species is implicitly deduced to be equal to 'vcov'
sliding = feols(y ~ x1 + x2, ~species)
etable(no_sliding, sliding)
# Resetting the global options
setFixest_estimation(data = NULL)
#
# Formula expansions
#
# By default, the features of the xpd function are enabled in
# all fixest estimations
# Here's a few examples
base = setNames(iris, c("y", "x1", "x2", "x3", "species"))
# dot square bracket operator
feols(y ~ x.[1:3], base)
# fetching variables via regular expressions: ..("regex")
feols(y ~ ..("1|2"), base)
# NOTA: it also works for multiple LHS
mult1 = feols(x.[1:2] ~ y + species, base)
mult2 = feols(..("y|3") ~ x.[1:2] + species, base)
etable(mult1, mult2)
# Use .[, stuff] to include variables in functions:
feols(y ~ csw(x.[, 1:3]), base)
# Same for ..(, "regex")
feols(y ~ csw(..(,"x")), base)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.