Learn R Programming

fixest (version 0.7.1)

demean: Centers a set of variables around a set of factors

Description

User-level access to internal demeaning algorithm of fixest.

Usage

demean(
  X,
  f,
  weights,
  nthreads = getFixest_nthreads(),
  notes = getFixest_notes(),
  iter = 2000,
  tol = 1e-06,
  im_confident = FALSE
)

Arguments

X

A matrix, vector or a list. The vectors to be centered. There must be the same number of observations as in the factors used for centering (argument f).

f

A matrix, vector or list. The factors used to center the variables in argument X.

weights

Vector, can be missing or NULL. If present, it must contain the same number of observations as in X.

nthreads

Number of threads to be used. By default it is equal to getFixest_nthreads().

notes

Logical, whether to display a message when NA values are removed. By default it is equal to getFixest_notes().

iter

Number of iterations, default is 2000.

tol

Stopping criterion of the algorithm. Default is 1e-6. The algorithm stops when the maximum absolute increase in the coefficients values is lower than tol.

im_confident

Logical, default is FALSE. FOR EXPERT USERS ONLY! This argument allows to skip some of the preprocessing of the arguments given in input. If TRUE, then X MUST be a numeric matrix (not integer, numeric), f MUST be a list and weights, if given, MUST be numeric (not integer!). Further the three MUST NOT contain any NA values and MUST have the same number of observations. Non compliance to these rules may simply lead your R session to break.

Value

It returns a matrix of the same number of columns as X in input. The number of rows is equal to the number of rows of X minus the number of NA values (contained in X, f or weights).

Examples

Run this code
# NOT RUN {
# Illustration of the FWL theorem
data(trade)

base = trade
base$ln_dist = log(base$dist_km)
base$ln_euros = log(base$Euros)

# We center the two variables ln_dist and ln_euros
#  on the factors Origin and Destination
X_demean = demean(X = base[, c("ln_dist", "ln_euros")],
                  f = base[, c("Origin", "Destination")])
base[, c("ln_dist_dm", "ln_euros_dm")] = X_demean

est = feols(ln_euros_dm ~ ln_dist_dm, base)
est_fe = feols(ln_euros ~ ln_dist | Origin + Destination, base)

# The results are the same as if we used the two factors
# as fixed-effects
etable(est, est_fe, se = "st")




# }

Run the code above in your browser using DataLab