orthogonalize: Residualize covariates.

Description Usage Arguments Details Value Author(s) Examples

View source: R/orthogonalize.R

Description

Provides functionality to create residual scores for a single outcome or a set of outcomes via ordinary least squares (OLS) regression more quickly and conveniently than lm. The 'response' vector produced by this function is "orthogonal" to the variables the predictor set.

Usage

1
2
orthogonalize(formula, data, intercept = FALSE,
              group = "", simplify = FALSE)

Arguments

formula

a "formula" object specifying the model 'response' variable to be residualized and a set of 'terms' to residualize the 'response' on, using linear regression. Can also be a "list" specifying several formulas.

data

a "data.frame" object containing the data set.

intercept

a "logical" vector indicating whether or not to add the intercept term estimated by the regression model to the extracted residuals in the return object. When specifying the "formula" argument as a list, the "intercept" vector can include the same number of elements as the list. Defaults to FALSE.

group

an optional "character" vector that specifies a grouping variable in the data that can be used for within-group residualization. When specifying the "formula" argument as a list, the "group" vector can include the same number of elements as the list. Defaults to "", which suggests no grouping variable will be used.

simplify

a "logical" value indicating whether or not to simplify the "matrix" result to a "numeric" vector.

Details

This function is based on symbolic model representation via a formula object, just like lm. The formula accepts a single 'response' separated by a "~" from a set of 'terms', which are themselves separated by a +. The formula is evaluated and the relevant data are provided to an OLS estimator where the 'response' is regressed on the 'terms'. The residuals of the 'response' are retained and returned by the function. If group is provided, the OLS estimator computes the 'response' as a set of within-group residuals.

When "formula" is specified as a "list", the user can either supply a single value to the arguments "intercept" and "group" or a vector of the same length as the "list" for a more modular approach to computing residuals.

Value

a numeric matrix of the same number of rows as the provided data and the same number of columns as the provided. If "simplify" is set to TRUE, a numeric vector will be returned.

Author(s)

Pavel Panko

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
## Load the data:
data(iris)

## Orthogonalize "Petal.Width" and add the intercept:
Petal.Width.Prime <- orthogonalize(
	formula   = Petal.Width ~ Petal.Length + Sepal.Length, 
	data      = iris, 
	intercept = TRUE
)

## Orthgonalize "Petal.Width" within "Species:
Petal.Width.Prime <- orthogonalize(
	formula = Petal.Width ~ ., 
	data    = iris, 
	group   = "Species" 
)

## Orthgonalize the "Width" variables, one within-group, one not:
Width.Prime <- orthogonalize(
	formula = list(
                    Petal.Width ~ .,
                    Sepal.Width ~ .
                  ),
	data    = iris, 
	group   = c("Species", "") 
)

## Orthgonalize the "Width" variables, adding the intercept to one
## but not the other:
Width.Prime <- orthogonalize(
	formula    = list(
                       Petal.Width ~ .,
                       Sepal.Width ~ .
                     ),
	data       = iris, 
	intercept  = c(TRUE, FALSE) 
)

ppanko/orthogonalize documentation built on July 17, 2020, 2:24 a.m.