qrxcenter: qrxcenter

View source: R/qrxcenter.r

qrxcenterR Documentation

qrxcenter

Description

Mean-center a data matrix and QR transform it

Usage

qrxcenter(x)

Arguments

x

a numeric matrix or vector with at least 2 rows

Details

For a numeric matrix x (or a numeric vector that is automatically changed to a one-column matrix), computes column means and subtracts them from x columns, and passes this matrix to base::qr() to orthogonalize columns. Columns of the transformed x are negated as needed so that original directions are preserved (which are arbitrary with QR decomposition). Instead of the default qr operation for which sums of squares of column values are 1.0, qrxcenter makes all the transformed columns have standard deviation of 1.0.

Value

a list with components x (transformed data matrix), R (the matrix that can be used to transform raw x and to transform regression coefficients computed on transformed x back to the original space), Ri (transforms transformed x back to original scale except for xbar), and xbar (vector of means of original x columns')

Examples

set.seed(1)
age <- 1:10
country <- sample(c('Slovenia', 'Italy', 'France'), 10, TRUE)
x <- model.matrix(~ age + country)[, -1]
x
w <- qrxcenter(x)
w
# Reproduce w$x
sweep(x, 2, w$xbar) %*% w$R
# Reproduce x from w$x
sweep(w$x %*% w$Ri, 2, w$xbar, FUN='+')

harrelfe/Hmisc documentation built on Nov. 21, 2024, 3:47 p.m.