Skip to contents

This function creates the left-hand-side or the right-hand-side(s) of a femlm, feols or feglm estimation.

Usage

sparse_model_matrix(
  object,
  data,
  type = "rhs",
  na.rm = TRUE,
  collin.rm = NULL,
  combine = TRUE,
  ...
)

Arguments

object

A fixest object. Obtained using the functions femlm, feols or feglm.

data

If missing (default) then the original data is obtained by evaluating the call. Otherwise, it should be a data.frame.

type

Character vector or one sided formula, default is "rhs". Contains the type of matrix/data.frame to be returned. Possible values are: "lhs", "rhs", "fixef", "iv.rhs1" (1st stage RHS), "iv.rhs2" (2nd stage RHS), "iv.endo" (endogenous vars.), "iv.exo" (exogenous vars), "iv.inst" (instruments).

na.rm

Default is TRUE. Should observations with NAs be removed from the matrix?

collin.rm

Logical scalar. Whether to remove variables that were found to be collinear during the estimation. Beware: it does not perform a collinearity check and bases on the coef(object). Default is TRUE if object is a fixest object, or FALSE if object is a formula.

combine

Logical scalar, default is TRUE. Whether to combine each resulting sparse matrix

...

Not currently used.

Value

It returns either a single sparse matrix a list of matrices, depending whether combine is TRUE or FALSE. The sparse matrix is of class dgCMatrix from the Matrix package.

See also

See also the main estimation functions femlm, feols or feglm. formula.fixest, update.fixest, summary.fixest, vcov.fixest.

Author

Laurent Berge, Kyle Butts

Examples


est = feols(wt ~ i(vs) + hp | cyl, mtcars)
sparse_model_matrix(est)
#> 32 x 2 sparse Matrix of class "dgCMatrix"
#>       vs::1  hp
#>  [1,]     . 110
#>  [2,]     . 110
#>  [3,]     1  93
#>  [4,]     1 110
#>  [5,]     . 175
#>  [6,]     1 105
#>  [7,]     . 245
#>  [8,]     1  62
#>  [9,]     1  95
#> [10,]     1 123
#> [11,]     1 123
#> [12,]     . 180
#> [13,]     . 180
#> [14,]     . 180
#> [15,]     . 205
#> [16,]     . 215
#> [17,]     . 230
#> [18,]     1  66
#> [19,]     1  52
#> [20,]     1  65
#> [21,]     1  97
#> [22,]     . 150
#> [23,]     . 150
#> [24,]     . 245
#> [25,]     . 175
#> [26,]     1  66
#> [27,]     .  91
#> [28,]     1 113
#> [29,]     . 264
#> [30,]     . 175
#> [31,]     . 335
#> [32,]     1 109
sparse_model_matrix(wt ~ i(vs) + hp | cyl, mtcars)
#> 32 x 3 sparse Matrix of class "dgCMatrix"
#>       vs::0 vs::1  hp
#>  [1,]     1     . 110
#>  [2,]     1     . 110
#>  [3,]     .     1  93
#>  [4,]     .     1 110
#>  [5,]     1     . 175
#>  [6,]     .     1 105
#>  [7,]     1     . 245
#>  [8,]     .     1  62
#>  [9,]     .     1  95
#> [10,]     .     1 123
#> [11,]     .     1 123
#> [12,]     1     . 180
#> [13,]     1     . 180
#> [14,]     1     . 180
#> [15,]     1     . 205
#> [16,]     1     . 215
#> [17,]     1     . 230
#> [18,]     .     1  66
#> [19,]     .     1  52
#> [20,]     .     1  65
#> [21,]     .     1  97
#> [22,]     1     . 150
#> [23,]     1     . 150
#> [24,]     1     . 245
#> [25,]     1     . 175
#> [26,]     .     1  66
#> [27,]     1     .  91
#> [28,]     .     1 113
#> [29,]     1     . 264
#> [30,]     1     . 175
#> [31,]     1     . 335
#> [32,]     .     1 109