I want to work out a multiple regression example all the way through using matrix algebra to calculate the regression coefficients.
#create vectors -- these will be our columns
y <- c(3,3,2,4,4,5,2,3,5,3)
x1 <- c(2,2,4,3,4,4,5,3,3,5)
x2 <- c(3,3,4,4,3,3,4,2,4,4)
#create matrix from vectors
M <- cbind(y,x1,x2)
k <- ncol(M) #number of variables
n <- nrow(M) #number of subjects
#create means for each column
M_mean <- matrix(data=1, nrow=n) %*% cbind(mean(y),mean(x1),mean(x2)); M_mean
#creates a difference matrix which gives deviation scores
D <- M - M_mean; D
#creates the covariance matrix, the sum of squares are in the diagonal and the sum of cross products are in the off diagonals.
C <- t(D) %*% D; C
I can see what the final values should be (-.19, -.01) and what the matrices before this calculation look like.
E<-matrix(c(10.5,3,3,4.4),nrow=2,ncol=2)
F<-matrix(c(-2,-.6),nrow=2,ncol=1)
But I'm not sure how to create these from the variance-covariance matrix to get the coefficients using matrix algebra.
Hope you can help.