Exercise 4.2.

Define the Function

para <- function (X, Y) {
  
  Ybar <- (1 / length(Y)) * sum(Y)
  xbar <- (1 / length(X)) * sum(X)
  
  bhat <- (sum( (X - xbar)* (Y - Ybar)) 
           / 
             sum( (X - xbar)^2)
           )
  
  ahat <- Ybar - bhat * xbar
  
  RSS <- sum((Y - ahat - bhat * X)^2)
  Rsquared <- 1 - RSS / sum((Y - Ybar)^2)
    
  return (list (ahat = ahat,
                bhat = bhat,
                Rsquared = Rsquared))
}

try with x = 123, y = 357

try1 <- para(X = c (1, 2, 3),
             Y = c (3, 5, 7))
try1
## $ahat
## [1] 1
## 
## $bhat
## [1] 2
## 
## $Rsquared
## [1] 1
try1_check <- lm(c (3, 5, 7) ~ c (1, 2, 3))
try1_check
## 
## Call:
## lm(formula = c(3, 5, 7) ~ c(1, 2, 3))
## 
## Coefficients:
## (Intercept)   c(1, 2, 3)  
##           1            2

try with x = height, y = handspan

try2 <- para (X = qanda$Height,
              Y = qanda$Handspan)
try2
## $ahat
## [1] -0.03742753
## 
## $bhat
## [1] 0.1034013
## 
## $Rsquared
## [1] 0.05169853
try2_check <- lm (qanda$Handspan ~
                    qanda$Height)
try2_check
## 
## Call:
## lm(formula = qanda$Handspan ~ qanda$Height)
## 
## Coefficients:
##  (Intercept)  qanda$Height  
##     -0.03743       0.10340