library(MASS)
x <-c(22,52,60,42,47,65)
y<-c(41, 49,69,55,60,62)

Y Vector:

Y <- matrix(c(41,49,69,55,60,62), nrow = 6,
            ncol = 1)
Y
##      [,1]
## [1,]   41
## [2,]   49
## [3,]   69
## [4,]   55
## [5,]   60
## [6,]   62

Design Matrix

X <- matrix(c(1,22,1,52,1,60,1,42,1,47, 1,65),
            nrow = 6,
            ncol = 2,
            byrow = TRUE)
X
##      [,1] [,2]
## [1,]    1   22
## [2,]    1   52
## [3,]    1   60
## [4,]    1   42
## [5,]    1   47
## [6,]    1   65

X^T * X

t(X)%*%X
##      [,1]  [,2]
## [1,]    6   288
## [2,]  288 14986

X^T * Y

t(X)%*%Y
##       [,1]
## [1,]   336
## [2,] 16750

(X^T * X)^-1

fractions(solve(t(X)%*%X))
##      [,1]      [,2]     
## [1,] 7493/3486   -24/581
## [2,]   -24/581    1/1162

B_hat, the vector of coefficient estimates

fractions((solve(t(X)%*%X))%*%(t(X)%*%Y))
##      [,1]     
## [1,] 17608/581
## [2,]   311/581

Y_hat, the fitted value vector

fractions(X%*%(solve(t(X)%*%X))%*%(t(X)%*%Y))
##      [,1]     
## [1,] 24450/581
## [2,] 33780/581
## [3,] 36268/581
## [4,] 11719/222
## [5,] 32225/581
## [6,] 37823/581

e_mat, the vector of residuals

hMat<-X%*%solve(t(X)%*%X)%*%t(X)
fractions(hMat%*%Y)
##      [,1]     
## [1,] 24450/581
## [2,] 33780/581
## [3,] 36268/581
## [4,] 11719/222
## [5,] 32225/581
## [6,] 37823/581

Checking with Y-Yhat:

fractions(Y-(X%*%(solve(t(X)%*%X))%*%(t(X)%*%Y)))
##      [,1]     
## [1,]  -629/581
## [2,] -5311/581
## [3,]  3821/581
## [4,]  1285/581
## [5,]  2635/581
## [6,] -1801/581

Why are these two different?

Part Two: Input the data into R and create a scatter plot with the regression line.

mod<-lm(y~x)

plot(x,y, pch=16)
abline(coefficients(mod))