orthonormalizeCovariates {ramwas} | R Documentation |
Takes a matrix of data frame with covariates, adds a constant covariate (optional), and orthonormalizes the set.
orthonormalizeCovariates(cvrt, modelhasconstant)
cvrt |
A matrix or data frame with covariates (one column per covariate). |
modelhasconstant |
Set to |
Factor variables are split into dummy variables before orthonormalization.
The operation is performed via QR decomposition (qr).
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
).
This function is used in several parts of the pipeline.
Andrey A Shabalin andrey.shabalin@gmail.com
# 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)