投影正交化与先前列的线性回归残差

Orthogonalization by projections versus residuals of linear regression on previous columns

根据this示例,通过投影进行正交化类似于对前几列进行线性回归的残差。但是,当我尝试他们的示例时,我没有获得预期的结果。这里发生了什么?为什么最后三行代码没有得到TRUE

library(matlib)
data(class)

class$male <- as.numeric(class$sex=="M")   
X <- as.matrix(class[,c(3,4,2,5)])

Z <- cbind(X[,1], 0, 0, 0)
Z[,2] <- X[,2] - Proj(X[,2], Z[,1])
Z[,3] <- X[,3] - Proj(X[,3], Z[,1]) - Proj(X[,3], Z[,2]) 
Z[,4] <- X[,4] - Proj(X[,4], Z[,1]) - Proj(X[,4], Z[,2]) - Proj(X[,4], Z[,3])

z2 <- residuals(lm(X[,2] ~ X[,1]), type="response")
z3 <- residuals(lm(X[,3] ~ X[,1:2]), type="response")
z4 <- residuals(lm(X[,4] ~ X[,1:3]), type="response")

我本来期望获得Z[,2] = z2Z[,3] = z3Z[,4] = z4,但事实并非如此

> all(Z[,2]==z2) [1] FALSE
> all(Z[,3]==z3) [1] FALSE
> all(Z[,4]==z4) [1] FALSE

这是因为 lm 自动添加了 "intercept"。去掉它(0 + ...)就有相等性:

z2 <- residuals(lm(X[,2] ~ 0 + X[,1]), type="response")
z3 <- residuals(lm(X[,3] ~ 0 + X[,1:2]), type="response")
z4 <- residuals(lm(X[,4] ~ 0 + X[,1:3]), type="response")

all.equal(z2, Z[,2])
# TRUE
all.equal(z3, Z[,3])
# TRUE
all.equal(z4, Z[,4])
# TRUE