Skip to contents

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

Usage

# S3 method for fixest
model.matrix(
  object,
  data,
  type = "rhs",
  na.rm = TRUE,
  subset = FALSE,
  as.matrix = FALSE,
  as.df = FALSE,
  collin.rm = 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?

subset

Logical or character vector. Default is FALSE. If TRUE, then the matrix created will be restricted only to the variables contained in the argument data, which can then contain a subset of the variables used in the estimation. If a character vector, then only the variables matching the elements of the vector via regular expressions will be created.

as.matrix

Logical scalar, default is FALSE. Whether to coerce the result to a matrix.

as.df

Logical scalar, default is FALSE. Whether to coerce the result to a data.frame.

collin.rm

Logical scalar, default is TRUE. Whether to remove variables that were found to be collinear during the estimation. Beware: it does not perform a collinearity check.

...

Not currently used.

Value

It returns either a vector, a matrix or a data.frame. It returns a vector for the dependent variable ("lhs"), a data.frame for the fixed-effects ("fixef") and a matrix for any other type.

See also

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

Author

Laurent Berge

Examples


base = iris
names(base) = c("y", "x1", "x2", "x3", "species")

est = feols(y ~ poly(x1, 2) + x2, base)
head(model.matrix(est))
#>      (Intercept) poly(x1, 2)1 poly(x1, 2)2  x2
#> [1,]           1  0.083201357 -0.016039377 1.4
#> [2,]           1 -0.010776079 -0.053252127 1.4
#> [3,]           1  0.026814895 -0.056361540 1.3
#> [4,]           1  0.008019408 -0.057805919 1.5
#> [5,]           1  0.101996844  0.009397687 1.4
#> [6,]           1  0.158383306  0.121697905 1.7

# Illustration of subset

# subset => character vector
head(model.matrix(est, subset = "x1"))
#>      poly(x1, 2)1 poly(x1, 2)2
#> [1,]  0.083201357 -0.016039377
#> [2,] -0.010776079 -0.053252127
#> [3,]  0.026814895 -0.056361540
#> [4,]  0.008019408 -0.057805919
#> [5,]  0.101996844  0.009397687
#> [6,]  0.158383306  0.121697905

# subset => TRUE, only works with data argument!!
head(model.matrix(est, data = base[, "x1", drop = FALSE], subset = TRUE))
#>   poly(x1, 2)1 poly(x1, 2)2
#> 1  0.083201357 -0.016039377
#> 2 -0.010776079 -0.053252127
#> 3  0.026814895 -0.056361540
#> 4  0.008019408 -0.057805919
#> 5  0.101996844  0.009397687
#> 6  0.158383306  0.121697905