Simple empirical formuals of the SLR Model
Model:
# Define the data
x <- dataset$predictor
y <- dataset$target
n <- lenght(x)
We can obtain the optimal result through the following:
s2x <- sum((x-mean(x))^2)/n
s2y <- sum((y-mean(y))^2)/n
covxy <- cov(x,y)
rxy <- cor(x,y)
mx <- mean(x)
my <- mean(y)
# Parameters
(beta1 <- rxy * sqrt(s2y/s2x))
(beta0 <- my - beta1 *mx)
# Estimated Values
yhat <- beta0 + beta1 * x
# Empirical MSE
mse_hat <- sum((y-yhat)^2)
The lm class follows exactly the same approach we used above but it automates it.
# Define model
fit <- lm(formula = y ~ x, data = dataset)
coef(fit)
summary(fit)
fitted(fit)
residuals(fit)