orthog: Orthogonalization of a matrix to another matrix

View source: R/orthog.R

orthogR Documentation

Orthogonalization of a matrix to another matrix

Description

Function orthog orthogonalizes a matrix Y to a matrix X. The row observations can be weighted.

The function uses function lm.

Usage


orthog(X, Y, weights = NULL)

Arguments

X

A n x p matrix or data frame.

Y

A n x q matrix or data frame to orthogonalize to X.

weights

A vector of length n defining a priori weights to apply to the observations. Internally, weights are "normalized" to sum to 1. Default to NULL (weights are set to 1 / n).

Value

Y

The Y matrix orthogonalized to X.

b

The regression coefficients used for orthogonalization.

Examples


n <- 8 ; p <- 3 ; q <- 2
set.seed(1)
X <- matrix(rnorm(n * p), ncol = p)
Y <- matrix(rnorm(n * q), ncol = q)
colnames(Y) <- c("y1", "y2")
set.seed(NULL)
X
Y

res <- orthog(X, Y)
res$Y
crossprod(res$Y, X)     # ===> orthogonal
res$b

# Same as:
fm <- lm(Y ~ X - 1)
Y - fm$fitted.values
fm$coef

#### WITH WEIGHTS

w <- 1:n
fm <- lm(Y ~ X - 1, weights = w)
Y - fm$fitted.values
fm$coef

res <- orthog(X, Y, weights = w)
res$Y
crossprod(res$Y, w * X)       # ===> orthogonal in metric w
res$b


mlesnoff/rnirs documentation built on April 24, 2023, 4:17 a.m.