Design matrix of a fixest
object returned in sparse format
Source: R/sparse_model_matrix.R
sparse_model_matrix.Rd
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",
sample = "estimation",
na.rm = FALSE,
collin.rm = NULL,
combine = TRUE,
...
)
Arguments
- object
A
fixest
object. Obtained using the functionsfemlm
,feols
orfeglm
.- data
If missing (default) then the original data is obtained by evaluating the
call
. Otherwise, it should be adata.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).
- sample
Character scalar equal to "estimation" (default) or "original". Only used when
data=NULL
(i.e. the original data is requested). By default, only the observations effectively used in the estimation are returned (it includes the observations with NA values or the fully explained by the fixed-effects (FE), or due to NAs in the weights).If
sample="original"
, all the observations are returned. In that case, if you usena.rm=TRUE
(which is not the default), you can withdraw the observations with NA values (and keep the ones fully explained by the FEs).- na.rm
Default is
FALSE
. 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 afixest
object, orFALSE
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
.
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