qrxcenter | R Documentation |
Mean-center a data matrix and QR transform it
qrxcenter(x)
x |
a numeric matrix or vector with at least 2 rows |
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.
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')
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='+')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.