orthonormalizeCovariates: Orthonormalize Covariates

Description Usage Arguments Details Value Note Author(s) Examples

View source: R/ramwas.r

Description

Takes a matrix of data frame with covariates, adds a constant covariate (optional), and orthonormalizes the set.

Usage

1
orthonormalizeCovariates(cvrt, modelhasconstant)

Arguments

cvrt

A matrix or data frame with covariates (one column per covariate).

modelhasconstant

Set to TRUE to add a constant covariate into the set before normalization.

Details

Factor variables are split into dummy variables before orthonormalization.
The operation is performed via QR decomposition (qr).

Value

Returns a matrix with orthogonal columns with unit length, whose columns spans the same space as the covariates plus a constant (if modelhasconstant is TRUE).

Note

This function is used in several parts of the pipeline.

Author(s)

Andrey A Shabalin andrey.shabalin@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Sample matrix of covariates
covariates = data.frame(a = 1:12, b = 12:1)

# Orthonormalizing Covariates
cvrtqr = orthonormalizeCovariates(covariates, modelhasconstant = TRUE)

# Checking the results (round to ignore rounding errors)
print( round(crossprod(cvrtqr),15) )

# Stop if not orthonormal
stopifnot(all.equal( crossprod(cvrtqr), diag(ncol(cvrtqr)) ))

# Example with a factor variable
groups = data.frame(gr = c("a","a","a","b","b","b","c","c","c"))
orthonormalizeCovariates(groups)

ramwas documentation built on Nov. 8, 2020, 8:24 p.m.