detrend: Detrend data by individual slopes

View source: R/utility_functions.R

detrendR Documentation

Detrend data by individual slopes

Description

Detrends the input data by the predicted values based on the slope parameters within each group specified by id. The result is equal to the transformed data used for estimation in feis.

Usage

detrend(
  data,
  slopes,
  id = NULL,
  intercept = TRUE,
  na.action = c("na.exlude", "na.omit"),
  tol = .Machine$double.eps,
  predicted = FALSE,
  ...
)

Arguments

data

a data.frame, matrix, or vector of data to be detrended. If id and / or slopes are given as character (see below), must contain id and / or slopes as variable(s). Otherwise must be excluded.

slopes

a data.frame, matrix, or vector of slopes to be used for detrending, not containing an intercept. Optionally, a character vector of the names of slope variables in data. For pure de-meaning use "slopes = 1".

id

a vector of a unique group / person identifier. Optionally, a character of the name of the unique group / person identifier in data. For overall detrending, use "id = 1".

intercept

logical. If TRUE the slopes will contain an individual intercept (default is TRUE). For "id = 1", this is an overall intercept. Ignored if "slopes = 1".

na.action

character, either na.exclude (default) or na.omit indicates the use of NAs. na.exclude passes NAs through to the output (same length as input). na.omit drops NA rows (list-wise).

tol

the tolerance for detecting linear dependencies in the residual maker transformation (see solve).

predicted

logical. If TRUE returns the predicted values instead of the detrended data (default is FALSE).

...

further arguments.

Details

detrend performs within-group "residual maker" transformation on the origin data. Within each group, the predicted values of the columns in data are computed based on the slope columns plus an individual intercept if intercept = TRUE (the default). Subsequently the predicted values are subtracted from the origin data. The transformed data can, for instance, be used to obtain coefficients of a fixed effects individual slopes estimator via lm \insertCiteBruderl.2015.387,Ruttenauer.2020,Wooldridge.2010.384feisr.

Estimation requires at least q+1 observations per unit, where q is the number of slope parameters (including a constant). detrend automatically selects only those groups from the current data set which have at least q+1 observations, and returns NA for all groups with n_i < q+1.

NA values in the input data are handled by list-wise deletion based on the data to be detrended and the slopes.

Value

An object of class "data.frame" or "numeric (if only one data column), containing the detrended data with row.names equal to the row.names of the origin data. If input is an unnamed vector, names are 1:length.

References

\insertAllCited

See Also

feis

Examples

data("mwp", package = "feisr")

# Detrend entire data.frame
mwp_det <- detrend(data = mwp, slopes = c("exp", "expq"), id = "id")

# Detrend single variable
lnw_det <- detrend(data = mwp$lnw, slopes = mwp[, c("exp", "expq")], id = mwp$id)


ruettenauer/feisr documentation built on April 5, 2022, 5:43 p.m.