Skip to contents

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 argument dict). Example: you have the variable x1 to x55 and want to display only x1 to x9, then you could use keep = "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 argument dict). Example: you have the variable x1 to x55 and want to display only x1 to x9, then you could use drop = "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 argument dict). Example: you have the following variables: month1 to month6, then x1 to x5, then year1 to year6. 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 as NA. 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 when long = TRUE: whether to remove the coefficients with NA 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)
#> Notes from the estimations:
#> [x 9] The variable 'x1' has been removed because of collinearity (see $collin.var).

# does not display x1 since it is always collinear
coef(est)
#>    id sample.var     sample                   rhs (Intercept)    x1_bis
#> 1   1    species     setosa                x1_bis   2.6390012 0.6904897
#> 2   2    species     setosa           x1_bis + x1   2.6390012 0.6904897
#> 3   3    species     setosa      x1_bis + x1 + x2   2.3037382 0.6674162
#> 4   4    species     setosa x1_bis + x1 + x2 + x3   2.3518898 0.6548350
#> 5   5    species versicolor                x1_bis   3.5397347 0.8650777
#> 6   6    species versicolor           x1_bis + x1   3.5397347 0.8650777
#> 7   7    species versicolor      x1_bis + x1 + x2   2.1164314 0.2476422
#> 8   8    species versicolor x1_bis + x1 + x2 + x3   1.8955395 0.3868576
#> 9   9    species  virginica                x1_bis   3.9068365 0.9015345
#> 10 10    species  virginica           x1_bis + x1   3.9068365 0.9015345
#> 11 11    species  virginica      x1_bis + x1 + x2   0.6247824 0.2599540
#> 12 12    species  virginica x1_bis + x1 + x2 + x3   0.6998830 0.3303370
#>           x2         x3
#> 1         NA         NA
#> 2         NA         NA
#> 3  0.2834193         NA
#> 4  0.2375602  0.2521257
#> 5         NA         NA
#> 6         NA         NA
#> 7  0.7355868         NA
#> 8  0.9083370 -0.6792238
#> 9         NA         NA
#> 10        NA         NA
#> 11 0.9348189         NA
#> 12 0.9455356 -0.1697527
# now it does
coef(est, collin = TRUE)
#>    id sample.var     sample                   rhs (Intercept)    x1_bis x1
#> 1   1    species     setosa                x1_bis   2.6390012 0.6904897 NA
#> 2   2    species     setosa           x1_bis + x1   2.6390012 0.6904897 NA
#> 3   3    species     setosa      x1_bis + x1 + x2   2.3037382 0.6674162 NA
#> 4   4    species     setosa x1_bis + x1 + x2 + x3   2.3518898 0.6548350 NA
#> 5   5    species versicolor                x1_bis   3.5397347 0.8650777 NA
#> 6   6    species versicolor           x1_bis + x1   3.5397347 0.8650777 NA
#> 7   7    species versicolor      x1_bis + x1 + x2   2.1164314 0.2476422 NA
#> 8   8    species versicolor x1_bis + x1 + x2 + x3   1.8955395 0.3868576 NA
#> 9   9    species  virginica                x1_bis   3.9068365 0.9015345 NA
#> 10 10    species  virginica           x1_bis + x1   3.9068365 0.9015345 NA
#> 11 11    species  virginica      x1_bis + x1 + x2   0.6247824 0.2599540 NA
#> 12 12    species  virginica x1_bis + x1 + x2 + x3   0.6998830 0.3303370 NA
#>           x2         x3
#> 1         NA         NA
#> 2         NA         NA
#> 3  0.2834193         NA
#> 4  0.2375602  0.2521257
#> 5         NA         NA
#> 6         NA         NA
#> 7  0.7355868         NA
#> 8  0.9083370 -0.6792238
#> 9         NA         NA
#> 10        NA         NA
#> 11 0.9348189         NA
#> 12 0.9455356 -0.1697527

# long
coef(est, long = TRUE)
#>    id sample.var     sample                   rhs coefficient   estimate
#> 1   1    species     setosa                x1_bis (Intercept)  2.6390012
#> 2   1    species     setosa                x1_bis      x1_bis  0.6904897
#> 5   2    species     setosa           x1_bis + x1 (Intercept)  2.6390012
#> 6   2    species     setosa           x1_bis + x1      x1_bis  0.6904897
#> 9   3    species     setosa      x1_bis + x1 + x2 (Intercept)  2.3037382
#> 10  3    species     setosa      x1_bis + x1 + x2      x1_bis  0.6674162
#> 11  3    species     setosa      x1_bis + x1 + x2          x2  0.2834193
#> 13  4    species     setosa x1_bis + x1 + x2 + x3 (Intercept)  2.3518898
#> 14  4    species     setosa x1_bis + x1 + x2 + x3      x1_bis  0.6548350
#> 15  4    species     setosa x1_bis + x1 + x2 + x3          x2  0.2375602
#> 16  4    species     setosa x1_bis + x1 + x2 + x3          x3  0.2521257
#> 17  5    species versicolor                x1_bis (Intercept)  3.5397347
#> 18  5    species versicolor                x1_bis      x1_bis  0.8650777
#> 21  6    species versicolor           x1_bis + x1 (Intercept)  3.5397347
#> 22  6    species versicolor           x1_bis + x1      x1_bis  0.8650777
#> 25  7    species versicolor      x1_bis + x1 + x2 (Intercept)  2.1164314
#> 26  7    species versicolor      x1_bis + x1 + x2      x1_bis  0.2476422
#> 27  7    species versicolor      x1_bis + x1 + x2          x2  0.7355868
#> 29  8    species versicolor x1_bis + x1 + x2 + x3 (Intercept)  1.8955395
#> 30  8    species versicolor x1_bis + x1 + x2 + x3      x1_bis  0.3868576
#> 31  8    species versicolor x1_bis + x1 + x2 + x3          x2  0.9083370
#> 32  8    species versicolor x1_bis + x1 + x2 + x3          x3 -0.6792238
#> 33  9    species  virginica                x1_bis (Intercept)  3.9068365
#> 34  9    species  virginica                x1_bis      x1_bis  0.9015345
#> 37 10    species  virginica           x1_bis + x1 (Intercept)  3.9068365
#> 38 10    species  virginica           x1_bis + x1      x1_bis  0.9015345
#> 41 11    species  virginica      x1_bis + x1 + x2 (Intercept)  0.6247824
#> 42 11    species  virginica      x1_bis + x1 + x2      x1_bis  0.2599540
#> 43 11    species  virginica      x1_bis + x1 + x2          x2  0.9348189
#> 45 12    species  virginica x1_bis + x1 + x2 + x3 (Intercept)  0.6998830
#> 46 12    species  virginica x1_bis + x1 + x2 + x3      x1_bis  0.3303370
#> 47 12    species  virginica x1_bis + x1 + x2 + x3          x2  0.9455356
#> 48 12    species  virginica x1_bis + x1 + x2 + x3          x3 -0.1697527

# long but balanced (with NAs then)
coef(est, long = TRUE, na.rm = FALSE)
#>    id sample.var     sample                   rhs coefficient   estimate
#> 1   1    species     setosa                x1_bis (Intercept)  2.6390012
#> 2   1    species     setosa                x1_bis      x1_bis  0.6904897
#> 3   1    species     setosa                x1_bis          x2         NA
#> 4   1    species     setosa                x1_bis          x3         NA
#> 5   2    species     setosa           x1_bis + x1 (Intercept)  2.6390012
#> 6   2    species     setosa           x1_bis + x1      x1_bis  0.6904897
#> 7   2    species     setosa           x1_bis + x1          x2         NA
#> 8   2    species     setosa           x1_bis + x1          x3         NA
#> 9   3    species     setosa      x1_bis + x1 + x2 (Intercept)  2.3037382
#> 10  3    species     setosa      x1_bis + x1 + x2      x1_bis  0.6674162
#> 11  3    species     setosa      x1_bis + x1 + x2          x2  0.2834193
#> 12  3    species     setosa      x1_bis + x1 + x2          x3         NA
#> 13  4    species     setosa x1_bis + x1 + x2 + x3 (Intercept)  2.3518898
#> 14  4    species     setosa x1_bis + x1 + x2 + x3      x1_bis  0.6548350
#> 15  4    species     setosa x1_bis + x1 + x2 + x3          x2  0.2375602
#> 16  4    species     setosa x1_bis + x1 + x2 + x3          x3  0.2521257
#> 17  5    species versicolor                x1_bis (Intercept)  3.5397347
#> 18  5    species versicolor                x1_bis      x1_bis  0.8650777
#> 19  5    species versicolor                x1_bis          x2         NA
#> 20  5    species versicolor                x1_bis          x3         NA
#> 21  6    species versicolor           x1_bis + x1 (Intercept)  3.5397347
#> 22  6    species versicolor           x1_bis + x1      x1_bis  0.8650777
#> 23  6    species versicolor           x1_bis + x1          x2         NA
#> 24  6    species versicolor           x1_bis + x1          x3         NA
#> 25  7    species versicolor      x1_bis + x1 + x2 (Intercept)  2.1164314
#> 26  7    species versicolor      x1_bis + x1 + x2      x1_bis  0.2476422
#> 27  7    species versicolor      x1_bis + x1 + x2          x2  0.7355868
#> 28  7    species versicolor      x1_bis + x1 + x2          x3         NA
#> 29  8    species versicolor x1_bis + x1 + x2 + x3 (Intercept)  1.8955395
#> 30  8    species versicolor x1_bis + x1 + x2 + x3      x1_bis  0.3868576
#> 31  8    species versicolor x1_bis + x1 + x2 + x3          x2  0.9083370
#> 32  8    species versicolor x1_bis + x1 + x2 + x3          x3 -0.6792238
#> 33  9    species  virginica                x1_bis (Intercept)  3.9068365
#> 34  9    species  virginica                x1_bis      x1_bis  0.9015345
#> 35  9    species  virginica                x1_bis          x2         NA
#> 36  9    species  virginica                x1_bis          x3         NA
#> 37 10    species  virginica           x1_bis + x1 (Intercept)  3.9068365
#> 38 10    species  virginica           x1_bis + x1      x1_bis  0.9015345
#> 39 10    species  virginica           x1_bis + x1          x2         NA
#> 40 10    species  virginica           x1_bis + x1          x3         NA
#> 41 11    species  virginica      x1_bis + x1 + x2 (Intercept)  0.6247824
#> 42 11    species  virginica      x1_bis + x1 + x2      x1_bis  0.2599540
#> 43 11    species  virginica      x1_bis + x1 + x2          x2  0.9348189
#> 44 11    species  virginica      x1_bis + x1 + x2          x3         NA
#> 45 12    species  virginica x1_bis + x1 + x2 + x3 (Intercept)  0.6998830
#> 46 12    species  virginica x1_bis + x1 + x2 + x3      x1_bis  0.3303370
#> 47 12    species  virginica x1_bis + x1 + x2 + x3          x2  0.9455356
#> 48 12    species  virginica x1_bis + x1 + x2 + x3          x3 -0.1697527