Proj: Projection of Vector y on columns of X

View source: R/proj.R

ProjR Documentation

Projection of Vector y on columns of X

Description

Fitting a linear model, lm(y ~ X), by least squares can be thought of geometrically as the orthogonal projection of y on the column space of X. This function is designed to allow exploration of projections and orthogonality.

Usage

Proj(y, X, list = FALSE)

Arguments

y

a vector, treated as a one-column matrix

X

a vector or matrix. Number of rows of y and X must match

list

logical; if FALSE, return just the projected vector; otherwise returns a list

Details

The projection is defined as P y where P = X (X'X)^- X' and X^- is a generalized inverse.

Value

the projection of y on X (if list=FALSE) or a list with elements y and P

Author(s)

Michael Friendly

See Also

Other vector diagrams: arc(), arrows3d(), circle3d(), corner(), plot.regvec3d(), pointOnLine(), regvec3d(), vectors(), vectors3d()

Examples

X <- matrix( c(1, 1, 1, 1, 1, -1, 1, -1), 4,2, byrow=TRUE)
y <- 1:4
Proj(y, X[,1])  # project y on unit vector
Proj(y, X[,2])
Proj(y, X)

# project unit vector on line between two points
y <- c(1,1)
p1 <- c(0,0)
p2 <- c(1,0)
Proj(y, cbind(p1, p2))

# orthogonal complements
y <- 1:4
yp <-Proj(y, X, list=TRUE)
yp$y
P <- yp$P
IP <- diag(4) - P
yc <- c(IP %*% y)
crossprod(yp$y, yc)

# P is idempotent:  P P = P
P %*% P
all.equal(P, P %*% P)

matlib documentation built on Oct. 3, 2024, 1:09 a.m.