Utility to extract the coefficients of multiple estimations and rearrange them into a matrix.
Usage
# S3 method for fixest_multi
coef(
object,
keep,
drop,
order,
collin = FALSE,
long = FALSE,
na.rm = TRUE,
...
)
# S3 method for fixest_multi
coefficients(
object,
keep,
drop,
order,
collin = FALSE,
long = FALSE,
na.rm = TRUE,
...
)
Arguments
- object
A
fixest_multi
object. Obtained from a multiple estimation.- keep
Character vector. This element is used to display only a subset of variables. This should be a vector of regular expressions (see
base::regex
help for more info). Each variable satisfying any of the regular expressions will be kept. This argument is applied post aliasing (see argumentdict
). Example: you have the variablex1
tox55
and want to display onlyx1
tox9
, then you could usekeep = "x[[:digit:]]$"
. If the first character is an exclamation mark, the effect is reversed (e.g. keep = "!Intercept" means: every variable that does not contain “Intercept” is kept). See details.- drop
Character vector. This element is used if some variables are not to be displayed. This should be a vector of regular expressions (see
base::regex
help for more info). Each variable satisfying any of the regular expressions will be discarded. This argument is applied post aliasing (see argumentdict
). Example: you have the variablex1
tox55
and want to display onlyx1
tox9
, then you could usedrop = "x[[:digit:]]{2}
". If the first character is an exclamation mark, the effect is reversed (e.g. drop = "!Intercept" means: every variable that does not contain “Intercept” is dropped). See details.- order
Character vector. This element is used if the user wants the variables to be ordered in a certain way. This should be a vector of regular expressions (see
base::regex
help for more info). The variables satisfying the first regular expression will be placed first, then the order follows the sequence of regular expressions. This argument is applied post aliasing (see argumentdict
). Example: you have the following variables:month1
tomonth6
, thenx1
tox5
, thenyear1
toyear6
. If you want to display first the x's, then the years, then the months you could use:order = c("x", "year")
. If the first character is an exclamation mark, the effect is reversed (e.g. order = "!Intercept" means: every variable that does not contain “Intercept” goes first). See details.- collin
Logical, default is
FALSE
. Whether the coefficients removed because of collinearity should be also returned asNA
. It cannot be used when coefficients aggregation is also used.- long
Logical, default is
FALSE
. Whether the results should be displayed in a long format.- na.rm
Logical, default is
TRUE
. Only applies whenlong = TRUE
: whether to remove the coefficients withNA
values.- ...
Not currently used.
Examples
base = iris
names(base) = c("y", "x1", "x2", "x3", "species")
# A multiple estimation
est = feols(y ~ x1 + csw0(x2, x3), base)
# Getting all the coefficients at once,
# each row is a model
coef(est)
#> id rhs (Intercept) x1 x2 x3
#> 1 1 x1 6.526223 -0.2233611 NA NA
#> 2 2 x1 + x2 2.249140 0.5955247 0.471920 NA
#> 3 3 x1 + x2 + x3 1.855997 0.6508372 0.709132 -0.5564827
# Example of keep/drop/order
coef(est, keep = "Int|x1", order = "x1")
#> id rhs x1 (Intercept)
#> 1 1 x1 -0.2233611 6.526223
#> 2 2 x1 + x2 0.5955247 2.249140
#> 3 3 x1 + x2 + x3 0.6508372 1.855997
# To change the order of the model, use fixest_multi
# extraction tools:
coef(est[rhs = .N:1])
#> id rhs (Intercept) x1 x2 x3
#> 3 1 x1 + x2 + x3 1.855997 0.6508372 0.709132 -0.5564827
#> 2 2 x1 + x2 2.249140 0.5955247 0.471920 NA
#> 1 3 x1 6.526223 -0.2233611 NA NA
# collin + long + na.rm
base$x1_bis = base$x1 # => collinear
est = feols(y ~ x1_bis + csw0(x1, x2, x3), base, split = ~species)
#> Error: in message_magic(..., .sep = .sep, .end = .end, .wid...:
#> In `string_magic`, the operator `width` must take a numeric argument.
#> PROBLEM: `min(100, .sw)` is not numeric.
# does not display x1 since it is always collinear
coef(est)
#> id rhs (Intercept) x1 x2 x3
#> 1 1 x1 6.526223 -0.2233611 NA NA
#> 2 2 x1 + x2 2.249140 0.5955247 0.471920 NA
#> 3 3 x1 + x2 + x3 1.855997 0.6508372 0.709132 -0.5564827
# now it does
coef(est, collin = TRUE)
#> id rhs (Intercept) x1 x2 x3
#> 1 1 x1 6.526223 -0.2233611 NA NA
#> 2 2 x1 + x2 2.249140 0.5955247 0.471920 NA
#> 3 3 x1 + x2 + x3 1.855997 0.6508372 0.709132 -0.5564827
# long
coef(est, long = TRUE)
#> id rhs coefficient estimate
#> 1 1 x1 (Intercept) 6.5262226
#> 2 1 x1 x1 -0.2233611
#> 5 2 x1 + x2 (Intercept) 2.2491402
#> 6 2 x1 + x2 x1 0.5955247
#> 7 2 x1 + x2 x2 0.4719200
#> 9 3 x1 + x2 + x3 (Intercept) 1.8559975
#> 10 3 x1 + x2 + x3 x1 0.6508372
#> 11 3 x1 + x2 + x3 x2 0.7091320
#> 12 3 x1 + x2 + x3 x3 -0.5564827
# long but balanced (with NAs then)
coef(est, long = TRUE, na.rm = FALSE)
#> id rhs coefficient estimate
#> 1 1 x1 (Intercept) 6.5262226
#> 2 1 x1 x1 -0.2233611
#> 3 1 x1 x2 NA
#> 4 1 x1 x3 NA
#> 5 2 x1 + x2 (Intercept) 2.2491402
#> 6 2 x1 + x2 x1 0.5955247
#> 7 2 x1 + x2 x2 0.4719200
#> 8 2 x1 + x2 x3 NA
#> 9 3 x1 + x2 + x3 (Intercept) 1.8559975
#> 10 3 x1 + x2 + x3 x1 0.6508372
#> 11 3 x1 + x2 + x3 x2 0.7091320
#> 12 3 x1 + x2 + x3 x3 -0.5564827