# Create data in terms of Y & X Y <- c(4,7,8) X1 <- c(1,2,3) X0 <- rep(1, times=length(X1)) # Create the design matrix X X <- cbind(X0,X1) # bind 2 column vectors together head(X) #-1 X' X matrix t(X) %*% X #-2 inverse(X' X) matrix inverse(t(X) %*% X) #-3 beta_hat = (X'X)^-1 X' Y bhat <- inverse(t(X) %*% X) %*% t(X) %*% Y bhat #-4 Yhat = H Y Yhat <- X %*% inverse(t(X) %*% X) %*% t(X) %*% Y c(Yhat) # Compare to estimates from lm() y.x1 <- lm(Y ~ X1) y.x1 y.x1$fit