Create a function which takes two vectors of equal length and outputs \(\hat{\alpha}\), \(\hat{\beta}\) and \(\hat{\sigma^2}\) using the formulae from the STAT0002 notes.
Test the function using x = (1, 2, 3) and Y = (3, 5, 7) - you should be able to predict its outputs for that data.
Run the function against the Handspan (Y) Height (x) data you have downloaded. Update the function so that it also outputs \(R^2\), the coefficient of determination, which describes how much of the variation in Y is explained by x.
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))
}
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
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