Linear Regression using matrix method.
Defining matrix of Predictor Variable, X,
<- matrix(c(3,4,5),nrow = 3)
X X
## [,1]
## [1,] 3
## [2,] 4
## [3,] 5
Defining matrix of Outcome Variable, Y
<- matrix(c(5,9,13),nrow = 3)
Y Y
## [,1]
## [1,] 5
## [2,] 9
## [3,] 13
Defining X matrix with an extra column of 1’s
<- rep(1,3)
one_col <- matrix(rbind(one_col,c(3,4,5)),nrow = 3,byrow = TRUE)
X_1 X_1
## [,1] [,2]
## [1,] 1 3
## [2,] 1 4
## [3,] 1 5
Calculating Beta matrix with the equation - B=((X’X)^-1)(X’Y)
<-solve(t(X_1)%*%(X_1))%*%t(X_1)%*%Y
B B
## [,1]
## [1,] -7
## [2,] 4
Coefficients for linear regression, Slope-B1, Intercept-B0
= B[1]
B0 = B[2]
B1 B0
## [1] -7
B1
## [1] 4
Finding residuals of the linear regression model, Res = Y-(B1*X+B0) which equals zero.
<-(B1*X+B0)
Y_Pred = Y-Y_Pred
Res Res
## [,1]
## [1,] -1.776357e-14
## [2,] -1.776357e-14
## [3,] -1.776357e-14
Linear Regression using lm() function
<- lm(formula = Y ~ X)
l_reg summary(l_reg)
## Warning in summary.lm(l_reg): essentially perfect fit: summary may be unreliable
##
## Call:
## lm(formula = Y ~ X)
##
## Residuals:
## 1 2 3
## 0 0 0
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -7 0 -Inf <2e-16 ***
## X 4 0 Inf <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0 on 1 degrees of freedom
## Multiple R-squared: 1, Adjusted R-squared: 1
## F-statistic: Inf on 1 and 1 DF, p-value: < 2.2e-16
From the summary, R square value is 1 and residual error 0 which implies that model is a perfect fit but highly dependent on perdictor variable and is unrealistic.
Plotting X,Y, predicted values and linear model.