Skip to content
Prev 361051 / 398506 Next

Matrix multiplications

Mistake is that (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X))) is NOT a scalar, but a 1 x 1 matrix. 

This works:

as.vector((t(y-X %*% b)%*%(y-X %*% b))/(length(y)-ncol(X)))*solve(t(X)%*% X)

as does recognizing the first term as a sum of squares:

sum((y-X %*% b)^2)/(length(y)-ncol(X))*solve(t(X)%*% X)

(And, for the Illuminati, a Kronecker product works too:

(t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X))) %x% solve(t(X)%*% X)

This could be useful for multivariate y.)