Estimates OLS with any number of fixed-effects.

feols(
  fml,
  data,
  weights,
  offset,
  subset,
  split,
  fsplit,
  cluster,
  se,
  dof,
  panel.id,
  fixef,
  fixef.rm = "none",
  fixef.tol = 1e-06,
  fixef.iter = 10000,
  collin.tol = 1e-10,
  nthreads = getFixest_nthreads(),
  lean = FALSE,
  verbose = 0,
  warn = TRUE,
  notes = getFixest_notes(),
  combine.quick,
  demeaned = FALSE,
  mem.clean = FALSE,
  only.env = FALSE,
  env,
  ...
)

Arguments

fml

A formula representing the relation to be estimated. For example: fml = z~x+y. To include fixed-effects, insert them in this formula using a pipe: e.g. fml = z~x+y | fe_1+fe_2. You can combine two fixed-effects with ^: e.g. fml = z~x+y|fe_1^fe_2, see details. You can also use variables with varying slopes using square brackets: e.g. in fml = z~y|fe_1[x] + fe_2, see details. To add IVs, insert the endogenous vars./instruments after a pipe, like in y ~ x | c(x_endo1, x_endo2) ~ x_inst1 + x_inst2. Note that it should always be the last element, see details. Multiple estimations can be performed at once: for multiple dep. vars, wrap them in c(): ex c(y1, y2). For multiple indep. vars, use the stepwise functions: ex x1 + csw(x2, x3). The formula fml = c(y1, y2) ~ x1 + cw0(x2, x3) leads to 6 estimation, see details.

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 data.frame names. Can also be a matrix.

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 ~ var_weight.

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) ~0.5*x**2. This offset is linearly added to the elements of the main formula 'fml'.

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 split = ~var) or a vector. If provided, the sample is split according to the variable and one estimation is performed for each value of that variable. If you also want to include the estimation for the full sample, use the argument fsplit instead.

fsplit

A one sided formula representing a variable (eg split = ~var) or a vector. If provided, the sample is split according to the variable and one estimation is performed for each value of that variable. This argument is the same as split but also includes the full sample as the first estimation.

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 var1 and var2 contained in the data.frame base used for the estimation. All the following cluster arguments are valid and do the same thing: cluster = base[, c("var1", "var2")], cluster = c("var1", "var2"), cluster = ~var1+var2. If the two variables were used as clusters in the estimation, you could further use cluster = 1:2 or leave it blank with se = "twoway" (assuming var1 [resp. var2] was the 1st [res. 2nd] cluster). You can interact two variables using ^ with the following syntax: cluster = ~var1^var2 or cluster = "var1^var2".

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: se = "cluster", otherwise se = "standard". Note that this argument can be implicitly deduced from the argument cluster.

dof

An object of class dof.type obtained with the function dof. Represents how the degree of freedom correction should be done.You must use the function dof for this argument. The arguments and defaults of the function dof are: adj = TRUE, fixef.K="nested", cluster.adj = TRUE, cluster.df = "conventional", t.df = "conventional", fixef.force_exact=FALSE). See the help of the function dof for details.

panel.id

The panel identifiers. Can either be: i) a one sided formula (e.g. panel.id = ~id+time), ii) a character vector of length 2 (e.g. panel.id=c('id', 'time'), or iii) a character scalar of two variables separated by a comma (e.g. panel.id='id,time'). Note that you can combine variables with ^ only inside formulas (see the dedicated section in feols).

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. The meaning of "both" and "none" is direct.

fixef.tol

Precision used to obtain the fixed-effects. Defaults to 1e-5. It corresponds to the maximum absolute difference allowed between two coefficients of successive iterations. Argument fixef.tol cannot be lower than 10000*.Machine$double.eps. Note that this parameter is dynamically controlled by the algorithm.

fixef.iter

Maximum number of iterations in fixed-effects algorithm (only in use for 2+ fixed-effects). Default is 10000.

collin.tol

Numeric scalar, default is 1e-10. Threshold deciding when variables should be considered collinear and subsequently removed from the estimation. Higher values means more variables will be removed (if there is presence of collinearity). One signal of presence of collinearity is t-stats that are extremely low (for instance when t-stats < 1e-3).

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 setFixest_nthreads.

lean

Logical, default is FALSE. If TRUE then all large objects are removed from the returned result: this will save memory but will block the possibility to use many methods. It is recommended to use the arguments se or cluster to obtain the appropriate standard-errors at estimation time, since obtaining different SEs won't be possible afterwards.

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 TRUE. Whether warnings should be displayed (concerns warnings relating to convergence state).

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 notes = FALSE. You can remove these messages permanently by using setFixest_notes(FALSE).

combine.quick

Logical. When you combine different variables to transform them into a single fixed-effects you can do e.g. y ~ x | paste(var1, var2). The algorithm provides a shorthand to do the same operation: y ~ x | var1^var2. Because pasting variables is a costly operation, the internal algorithm may use a numerical trick to hasten the process. The cost of doing so is that you lose the labels. If you are interested in getting the value of the fixed-effects coefficients after the estimation, you should use combine.quick = FALSE. By default it is equal to FALSE if the number of observations is lower than 50,000, and to TRUE otherwise.

demeaned

Logical, default is FALSE. Only used in the presence of fixed-effects: should the centered variables be returned? If TRUE, it creates the items y_demeaned and X_demeaned.

mem.clean

Logical, default is FALSE. Only to be used if the data set is large compared to the available RAM. If TRUE then intermediary objects are removed as much as possible and gc is run before each substantial C++ section in the internal code to avoid memory issues.

only.env

(Advanced users.) Logical, default is FALSE. If TRUE, then only the environment used to make the estimation is returned.

env

(Advanced users.) A fixest environment created by a fixest estimation with only.env = TRUE. Default is missing. If provided, the data from this environment will be used to perform the estimation.

...

Not currently used.

Value

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.

family

The family used to estimate the model.

fml_all

A list containing different parts of the formula. Always contain the linear formula. Then depending on the cases: fixef: the fixed-effects, iv: the IV part of the formula.

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.unscaled

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.

obsRemoved

(When relevant.) Vector of observations that were removed because of NA values.

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 demeaned = TRUE: the centered dependent variable.

X_demeaned

Only when demeaned = TRUE: the centered explanatory variable.

Details

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.

Combining the fixed-effects

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.

Varying slopes

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

Lagging variables

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. Doing either of the two will give you acceess to the lagging functions l, 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.

Interactions

You can interact a numeric variable with a "factor-like" variable by using interact(var, fe, ref), where fe is the variable to be interacted with and the argument ref is a value of fe taken as a reference (optional). Instead of using the function interact, you can use the alias i(var, fe, ref).

Using this specific way to create interactions leads to a different display of the interacted values in etable and offers a special representation of the interacted coefficients in the function coefplot. 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 (using the syntax fe[[var]], as explained in the section “Varying slopes”).

The function interact has in fact more arguments, please see details in its associated help page.

On standard-errors

Standard-errors can be computed in different ways, you can use the arguments se and dof 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_se and setFixest_dof to permanently set the way the standard-errors are computed.

Instrumental variables

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.

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

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.

To include mutliple 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 associated to 4 short aliases. These are a) stepwise, stepwise0, cstepwise, cstepwise0, and b) sw, sw0, csw, csw0. 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 wihtout any stepwise element also needs to be estimated. Finally, 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.

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

Fixed-effects cannot be included in a stepwise fashion: they are there or not and stay the same for all estimations.

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.

References

Berge, Laurent, 2018, "Efficient estimation of maximum likelihood models with multiple fixed-effects: the R package FENmlm." CREA Discussion Papers, 13 (https://wwwen.uni.lu/content/download/110162/1299525/file/2018_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

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.

Author

Laurent Berge

Examples

# # Basic estimation # res = feols(Sepal.Length ~ Sepal.Width + Petal.Length, iris) # You can specify clustered standard-errors in summary: summary(res, cluster = ~Species)
#> OLS estimation, Dep. Var.: Sepal.Length #> Observations: 150 #> Standard-errors: Clustered (Species) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 2.249100 0.162626 13.830 0.005187 ** #> Sepal.Width 0.595525 0.051733 11.511 0.007462 ** #> Petal.Length 0.471920 0.006873 68.667 0.000212 *** #> --- #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 #> RMSE: 0.329937 Adj. R2: 0.838003
# # 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)
#> OLS estimation, Dep. Var.: Sepal.Length #> Observations: 150 #> Fixed-effects: Species: 3 #> Standard-errors: Clustered (Species) #> Estimate Std. Error t value Pr(>|t|) #> Sepal.Width 0.432217 0.161308 2.6794 0.115623 #> Petal.Length 0.775629 0.126546 6.1293 0.025601 * #> --- #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 #> RMSE: 0.305129 Adj. R2: 0.859538 #> Within R2: 0.641507
# # Varying slopes: # res = feols(Sepal.Length ~ Petal.Length | Species[Sepal.Width], iris) summary(res)
#> OLS estimation, Dep. Var.: Sepal.Length #> Observations: 150 #> Fixed-effects: Species: 3 #> Varying slopes: Sepal.Width (Species: 3) #> Standard-errors: Clustered (Species) #> Estimate Std. Error t value Pr(>|t|) #> Petal.Length 0.822045 0.100265 8.1987 0.014553 * #> --- #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 #> RMSE: 0.298903 Adj. R2: 0.863326 #> Within R2: 0.518738
# # 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)
#> OLS estimation, Dep. Var.: Sepal.Length #> Observations: 150 #> Fixed-effects: Species^fe_2: 30 #> Standard-errors: Clustered (Species^fe_2) #> Estimate Std. Error t value Pr(>|t|) #> Petal.Length 0.875613 0.062395 14.033 1.85e-14 *** #> --- #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 #> RMSE: 0.309865 Adj. R2: 0.823494 #> Within R2: 0.548794
fixef(res_comb)[[1]]
#> setosa_1 setosa_10 setosa_2 setosa_3 setosa_4 #> 3.826581 3.646581 3.661605 3.601703 3.621605 #> setosa_5 setosa_6 setosa_7 setosa_8 setosa_9 #> 3.736532 3.884093 3.859118 3.699118 3.721605 #> versicolor_1 versicolor_10 versicolor_2 versicolor_3 versicolor_4 #> 2.067402 2.142623 2.322426 2.359841 1.922328 #> versicolor_5 versicolor_6 versicolor_7 versicolor_8 versicolor_9 #> 2.187402 2.287304 2.209694 2.284914 2.274963 #> virginica_1 virginica_10 virginica_2 virginica_3 virginica_4 #> 1.769007 1.841642 1.826716 1.716471 1.639252 #> virginica_5 virginica_6 virginica_7 virginica_8 virginica_9 #> 1.474081 2.006422 1.591887 1.791495 1.609007
# # 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)
#> NOTE: 108 observations removed because of NA values (RHS: 108).
est2 = feols(f(y) ~ l(x1, -1:1), base_did, panel.id = ~id+period)
#> NOTE: 216 observations removed because of NA values (LHS: 108, RHS: 216).
etable(est1, est2, order = "f", drop="Int")
#> est1 est2 #> Dependent Var.: y f(y,1) #> #> f(x1,1) 0.9940*** (0.0579) #> x1 0.9948*** (0.0532) 0.0081 (0.0584) #> l(x1,1) 0.0410 (0.0540) 0.0157 (0.0585) #> _______________ __________________ __________________ #> S.E. type Standard Standard #> Observations 972 864 #> R2 0.26558 0.25697 #> Adj. R2 0.26406 0.25438
# # Using interactions: # 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) # 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)
#> TSLS estimation, Dep. Var.: y, Endo.: x_endo1, Instr.: x_inst1 #> Second stage: Dep. Var.: y #> Observations: 150 #> Standard-errors: Standard #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) -5.444100 82.9070 -0.065665 0.947735 #> fit_x_endo1 4.156900 44.6790 0.093038 0.926001 #> x1 0.533483 1.8094 0.294843 0.768532 #> x2 -1.358100 19.6740 -0.069030 0.945060 #> --- #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 #> RMSE: 7.9776 Adj. R2: -94.4 #> F-test (1st stage): stat = 0.008704, p = 0.925795, on 1 and 146 DoF. #> Wu-Hausman: stat = 5.2387 , p = 0.023535, on 1 and 145 DoF.
# To show the first stage: summary(res_iv, stage = 1)
#> TSLS estimation, Dep. Var.: x_endo1, Endo.: x_endo1, Instr.: x_inst1 #> First stage: Dep. Var.: x_endo1 #> Observations: 150 #> Standard-errors: Standard #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 1.867800 1.454900 1.283900 0.201226 #> x_inst1 -0.011136 0.119357 -0.093298 0.925795 #> x1 0.018677 0.405512 0.046059 0.963326 #> x2 0.447831 0.128635 3.481400 0.000658 *** #> --- #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 #> RMSE: 1.9138 Adj. R2: 0.122258 #> F-test (1st stage): stat = 0.008704, p = 0.925795, on 1 and 146 DoF.
# To show both the first and second stages: summary(res_iv, stage = 1:2)
#> IV: First stage: x_endo1 #> TSLS estimation, Dep. Var.: x_endo1, Endo.: x_endo1, Instr.: x_inst1 #> First stage: Dep. Var.: x_endo1 #> Observations: 150 #> Standard-errors: Standard #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 1.867800 1.454900 1.283900 0.201226 #> x_inst1 -0.011136 0.119357 -0.093298 0.925795 #> x1 0.018677 0.405512 0.046059 0.963326 #> x2 0.447831 0.128635 3.481400 0.000658 *** #> --- #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 #> RMSE: 1.9138 Adj. R2: 0.122258 #> F-test (1st stage): stat = 0.008704, p = 0.925795, on 1 and 146 DoF. #> #> IV: Second stage #> TSLS estimation, Dep. Var.: y, Endo.: x_endo1, Instr.: x_inst1 #> Second stage: Dep. Var.: y #> Observations: 150 #> Standard-errors: Standard #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) -5.444100 82.9070 -0.065665 0.947735 #> fit_x_endo1 4.156900 44.6790 0.093038 0.926001 #> x1 0.533483 1.8094 0.294843 0.768532 #> x2 -1.358100 19.6740 -0.069030 0.945060 #> --- #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 #> RMSE: 7.9776 Adj. R2: -94.4 #> F-test (1st stage): stat = 0.008704, p = 0.925795, on 1 and 146 DoF. #> Wu-Hausman: stat = 5.2387 , p = 0.023535, on 1 and 145 DoF.
# 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]
#> TSLS estimation, Dep. Var.: x_endo1, Endo.: x_endo1, x_endo2, Instr.: x_inst1, x_inst2 #> First stage: Dep. Var.: x_endo1 #> Observations: 150 #> Standard-errors: Standard #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 1.850700 1.516300 1.220500 0.224240 #> x_inst1 -0.011391 0.119922 -0.094984 0.924459 #> x_inst2 0.003804 0.091032 0.041784 0.966728 #> x1 0.020822 0.410131 0.050770 0.959578 #> x2 0.446540 0.132727 3.364300 0.000982 *** #> --- #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 #> RMSE: 1.9138 Adj. R2: 0.116216 #> F-test (1st stage): stat = 0.005195, p = 0.994818, on 2 and 145 DoF.
# The stage argument also works in etable: etable(res_iv, res_iv_fe, res_iv2, order = "endo")
#> res_iv res_iv_fe res_iv2 #> Dependent Var.: y y y #> #> x_endo1 4.157 (44.68) 3.414 (15.83) -1.912 (56.41) #> x_endo2 -0.0206 (0.2092) #> (Intercept) -5.444 (82.91) 5.959 (106.1) #> x1 0.5335 (1.809) 0.6988 (0.9094) 0.6664 (1.476) #> x2 -1.358 (19.67) -0.6848 (6.072) 1.368 (25.36) #> Fixed-Effects: -------------- --------------- ---------------- #> fe1 No Yes No #> _______________ ______________ _______________ ________________ #> S.E. type Standard by: fe1 Standard #> Observations 150 150 150 #> R2 -92.436 -61.946 -18.724 #> Within R2 -- -164.08 --
etable(res_iv, res_iv_fe, res_iv2, stage = 1:2, order = c("endo", "inst"), group = list(control = "!endo|inst"))
#> res_iv.1 res_iv.2 res_iv_fe.1 res_iv_fe.2 #> IV stages First Second First Second #> Dependent Var.: x_endo1 y x_endo1 y #> #> x_endo1 4.157 (44.68) 3.414 (15.83) #> x_endo2 #> x_inst1 -0.0111 (0.1194) -0.0127 (0.0508) #> x_inst2 #> control Yes Yes Yes Yes #> Fixed-Effects: ---------------- ------------- ---------------- ------------- #> fe1 No No Yes Yes #> _______________ ________________ _____________ ________________ _____________ #> S.E. type Standard Standard by: fe1 by: fe1 #> Observations 150 150 150 150 #> R2 0.13993 -92.436 0.14157 -61.946 #> Within R2 -- -- 0.00829 -164.08 #> res_iv2.1 res_iv2.2 res_iv2.3 #> IV stages First First Second #> Dependent Var.: x_endo1 x_endo2 y #> #> x_endo1 -1.912 (56.41) #> x_endo2 -0.0206 (0.2092) #> x_inst1 -0.0114 (0.1199) 3.295*** (0.3310) #> x_inst2 0.0038 (0.0910) -0.2070 (0.2513) #> control Yes Yes Yes #> Fixed-Effects: ---------------- ----------------- ---------------- #> fe1 No No No #> _______________ ________________ _________________ ________________ #> S.E. type Standard Standard Standard #> Observations 150 150 150 #> R2 0.13994 0.57761 -18.724 #> Within R2 -- -- --
# # 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])
#> model 1 model 2 model 3 #> Dependent Var.: Ozone Ozone Ozone #> #> (Intercept) -71.03** (23.58) -248.5*** (48.14) -257.7*** (48.45) #> Wind -3.055*** (0.6633) 14.34*** (4.239) 14.58*** (4.228) #> Temp 1.840*** (0.2500) 4.076*** (0.5875) 4.136*** (0.5871) #> Wind x Temp -0.2239*** (0.0540) -0.2273*** (0.0539) #> Day 0.2940 (0.2185) #> _______________ __________________ ___________________ ___________________ #> S.E. type Standard Standard Standard #> Observations 116 116 116 #> R2 0.56871 0.62613 0.63213 #> Adj. R2 0.56108 0.61611 0.61887
# And now the second (access can be made by name) etable(est_mult[lhs = "Solar.R"])
#> model 1 model 2 model 3 #> Dependent Var.: Solar.R Solar.R Solar.R #> #> (Intercept) -76.36 (82.00) 10.01 (189.6) 45.19 (190.6) #> Wind 2.211 (2.308) -5.893 (16.20) -6.214 (16.15) #> Temp 3.075*** (0.8778) 1.982 (2.333) 1.788 (2.329) #> Wind x Temp 0.1044 (0.2064) 0.1069 (0.2057) #> Day -1.161 (0.8277) #> _______________ _________________ _______________ _______________ #> S.E. type Standard Standard Standard #> Observations 146 146 146 #> R2 0.08198 0.08363 0.09624 #> Adj. R2 0.06914 0.06427 0.07061
# 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])
#> model 1 model 2 model 3 #> Dependent Var.: Ozone Solar.R Ozone #> #> (Intercept) -248.5*** (48.14) 10.01 (189.6) -257.7*** (48.45) #> Wind 14.34*** (4.239) -5.893 (16.20) 14.58*** (4.228) #> Temp 4.076*** (0.5875) 1.982 (2.333) 4.136*** (0.5871) #> Wind x Temp -0.2239*** (0.0540) 0.1044 (0.2064) -0.2273*** (0.0539) #> Day 0.2940 (0.2185) #> _______________ ___________________ _______________ ___________________ #> S.E. type Standard Standard Standard #> Observations 116 146 116 #> R2 0.62613 0.08363 0.63213 #> Adj. R2 0.61611 0.06427 0.61887 #> model 4 #> Dependent Var.: Solar.R #> #> (Intercept) 45.19 (190.6) #> Wind -6.214 (16.15) #> Temp 1.788 (2.329) #> Wind x Temp 0.1069 (0.2057) #> Day -1.161 (0.8277) #> _______________ _______________ #> S.E. type Standard #> Observations 146 #> R2 0.09624 #> Adj. R2 0.07061
# 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
#> Standard-errors: Standard #> #> #> # SAMPLE: 5 #> #> #> ### Dep. var.: Ozone #> #> Expl. vars.: poly(Wind, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 29.851 4.6248 6.4545 0.00000138 *** #> poly(Wind, 2)1 -180.150 68.1460 -2.6436 0.01451600 * #> poly(Wind, 2)2 93.319 55.6890 1.6757 0.10734000 #> --- #> Expl. vars.: poly(Temp, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 60.885 13.331 4.5670 0.000137 *** #> poly(Temp, 2)1 436.080 162.610 2.6817 0.013321 * #> poly(Temp, 2)2 156.570 107.680 1.4540 0.159455 #> #> ### Dep. var.: Solar.R #> #> Expl. vars.: poly(Wind, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 185.98 27.217 6.833200 0.000000456 *** #> poly(Wind, 2)1 -101.51 403.970 -0.251277 0.803739000 #> poly(Wind, 2)2 -248.61 327.310 -0.759552 0.454919000 #> --- #> Expl. vars.: poly(Temp, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 184.74 72.527 2.547100 0.017690 * #> poly(Temp, 2)1 -306.20 881.230 -0.347473 0.731265 #> poly(Temp, 2)2 -822.99 547.850 -1.502200 0.146087 #> #> #> # SAMPLE: 6 #> #> #> ### Dep. var.: Ozone #> #> Expl. vars.: poly(Wind, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 21.821 9.489 2.299600 0.061137 . #> poly(Wind, 2)1 166.960 161.900 1.031300 0.342179 #> poly(Wind, 2)2 -60.631 92.541 -0.655171 0.536652 #> --- #> Expl. vars.: poly(Temp, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 34.296 4.1531 8.2579 0.000171 *** #> poly(Temp, 2)1 114.920 62.2060 1.8474 0.114205 #> poly(Temp, 2)2 206.490 79.9990 2.5811 0.041707 * #> #> ### Dep. var.: Solar.R #> #> Expl. vars.: poly(Wind, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 187.780 16.466 11.404000 7.89000e-12 *** #> poly(Wind, 2)1 389.560 194.900 1.998800 5.57870e-02 . #> poly(Wind, 2)2 -72.818 156.400 -0.465574 6.45254e-01 #> --- #> Expl. vars.: poly(Temp, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 188.34 21.41 8.796900 2.06000e-09 *** #> poly(Temp, 2)1 575.59 374.37 1.537500 1.35815e-01 #> poly(Temp, 2)2 133.70 363.96 0.367352 7.16220e-01 #> #> #> # SAMPLE: 7 #> #> #> ### Dep. var.: Ozone #> #> Expl. vars.: poly(Wind, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 49.812 5.5194 9.024800 5.11000e-09 *** #> poly(Wind, 2)1 -280.640 84.2530 -3.330900 2.90600e-03 ** #> poly(Wind, 2)2 58.003 107.6300 0.538924 5.95118e-01 #> --- #> Expl. vars.: poly(Temp, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 29.211 16.069 1.817800 0.082138 . #> poly(Temp, 2)1 584.100 265.920 2.196500 0.038399 * #> poly(Temp, 2)2 14.189 198.960 0.071314 0.943764 #> #> ### Dep. var.: Solar.R #> #> Expl. vars.: poly(Wind, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 213.1600 16.335 13.049000 2.01000e-13 *** #> poly(Wind, 2)1 -144.0800 246.760 -0.583909 5.63956e-01 #> poly(Wind, 2)2 8.7091 324.820 0.026812 9.78800e-01 #> --- #> Expl. vars.: poly(Temp, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 172.30 54.609 3.155200 0.003813 ** #> poly(Temp, 2)1 835.02 904.970 0.922697 0.364048 #> poly(Temp, 2)2 -107.28 645.970 -0.166070 0.869295 #> #> #> # SAMPLE: 8 #> #> #> ### Dep. var.: Ozone #> #> Expl. vars.: poly(Wind, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 49.732 6.1498 8.0867 3.56000e-08 *** #> poly(Wind, 2)1 -289.250 97.1100 -2.9786 6.71900e-03 ** #> poly(Wind, 2)2 116.300 90.0030 1.2922 2.09134e-01 #> --- #> Expl. vars.: poly(Temp, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 26.129 16.648 1.569500 0.130195 #> poly(Temp, 2)1 678.140 325.990 2.080200 0.048834 * #> poly(Temp, 2)2 -162.120 188.330 -0.860822 0.398222 #> #> ### Dep. var.: Solar.R #> #> Expl. vars.: poly(Wind, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 170.340 15.836 10.757000 7.21000e-11 *** #> poly(Wind, 2)1 -78.922 265.260 -0.297520 7.68528e-01 #> poly(Wind, 2)2 130.240 250.900 0.519111 6.08249e-01 #> --- #> Expl. vars.: poly(Temp, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 141.740 34.927 4.058100 0.000427 *** #> poly(Temp, 2)1 610.440 708.890 0.861125 0.397354 #> poly(Temp, 2)2 -62.816 420.060 -0.149540 0.882327 #> #> #> # SAMPLE: 9 #> #> #> ### Dep. var.: Ozone #> #> Expl. vars.: poly(Wind, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 32.49 3.0073 10.8040 4.140e-11 *** #> poly(Wind, 2)1 -151.11 39.2660 -3.8484 6.930e-04 *** #> poly(Wind, 2)2 166.75 45.9360 3.6301 1.217e-03 ** #> --- #> Expl. vars.: poly(Temp, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 35.773 1.9254 18.5800 < 2.2e-16 *** #> poly(Temp, 2)1 198.970 30.2130 6.5854 5.53e-07 *** #> poly(Temp, 2)2 160.480 32.3630 4.9588 3.7e-05 *** #> #> ### Dep. var.: Solar.R #> #> Expl. vars.: poly(Wind, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 168.050 14.943 11.246000 1.08000e-11 *** #> poly(Wind, 2)1 -94.963 195.710 -0.485211 6.31440e-01 #> poly(Wind, 2)2 28.000 231.670 0.120859 9.04697e-01 #> --- #> Expl. vars.: poly(Temp, 2) #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 166.48 15.317 10.869000 2.31000e-11 *** #> poly(Temp, 2)1 204.78 241.870 0.846686 4.04613e-01 #> poly(Temp, 2)2 -146.65 256.410 -0.571947 5.72090e-01
# Different way of displaying the results with "compact" summary(est_split, "compact")
#> sample lhs rhs (Intercept) poly(Wind, 2)1 poly(Wind, 2)2 #> 1 5 Ozone poly(Wind, 2) 29.9*** (4.62) -180* (68.1) 93.3 (55.7) #> 2 5 Ozone poly(Temp, 2) 60.9*** (13.3) #> 3 5 Solar.R poly(Wind, 2) 186*** (27.2) -102 (404) -249 (327) #> 4 5 Solar.R poly(Temp, 2) 185* (72.5) #> 5 6 Ozone poly(Wind, 2) 21.8. (9.49) 167 (162) -60.6 (92.5) #> 6 6 Ozone poly(Temp, 2) 34.3*** (4.15) #> 7 6 Solar.R poly(Wind, 2) 188*** (16.5) 390. (195) -72.8 (156) #> 8 6 Solar.R poly(Temp, 2) 188*** (21.4) #> 9 7 Ozone poly(Wind, 2) 49.8*** (5.52) -281** (84.3) 58 (108) #> 10 7 Ozone poly(Temp, 2) 29.2. (16.1) #> 11 7 Solar.R poly(Wind, 2) 213*** (16.3) -144 (247) 8.71 (325) #> 12 7 Solar.R poly(Temp, 2) 172** (54.6) #> 13 8 Ozone poly(Wind, 2) 49.7*** (6.15) -289** (97.1) 116 (90) #> 14 8 Ozone poly(Temp, 2) 26.1 (16.6) #> 15 8 Solar.R poly(Wind, 2) 170*** (15.8) -78.9 (265) 130 (251) #> 16 8 Solar.R poly(Temp, 2) 142*** (34.9) #> 17 9 Ozone poly(Wind, 2) 32.5*** (3.01) -151*** (39.3) 167** (45.9) #> 18 9 Ozone poly(Temp, 2) 35.8*** (1.93) #> 19 9 Solar.R poly(Wind, 2) 168*** (14.9) -95 (196) 28 (232) #> 20 9 Solar.R poly(Temp, 2) 166*** (15.3) #> poly(Temp, 2)1 poly(Temp, 2)2 #> 1 #> 2 436* (163) 157 (108) #> 3 #> 4 -306 (881) -823 (548) #> 5 #> 6 115 (62.2) 206* (80) #> 7 #> 8 576 (374) 134 (364) #> 9 #> 10 584* (266) 14.2 (199) #> 11 #> 12 835 (905) -107 (646) #> 13 #> 14 678* (326) -162 (188) #> 15 #> 16 610 (709) -62.8 (420) #> 17 #> 18 199*** (30.2) 160*** (32.4) #> 19 #> 20 205 (242) -147 (256)
# You can still select which sample/LHS/RHS to display est_split[sample = 1:2, lhs = 1, rhs = 1]
#> Standard-errors: Standard #> Dep. var.: Ozone #> Expl. vars.: poly(Wind, 2) #> Sample: 5 #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 29.851 4.6248 6.4545 0.00000138 *** #> poly(Wind, 2)1 -180.150 68.1460 -2.6436 0.01451600 * #> poly(Wind, 2)2 93.319 55.6890 1.6757 0.10734000 #> --- #> Sample: 6 #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 21.821 9.489 2.299600 0.061137 . #> poly(Wind, 2)1 166.960 161.900 1.031300 0.342179 #> poly(Wind, 2)2 -60.631 92.541 -0.655171 0.536652