# The Linear Regression Model: Mean Square Error {#linreg-estimation-MSE-example}

Data

See jeksterslabRdatarepo::wages.matrix() for the data set used in this example.

X <- jeksterslabRdatarepo::wages.matrix[["X"]]
# age is removed
X <- X[, -ncol(X)]
y <- jeksterslabRdatarepo::wages.matrix[["y"]]
head(X)
#>      constant gender race union education experience
#> [1,]        1      1    0     0        12         20
#> [2,]        1      0    0     0         9          9
#> [3,]        1      0    0     0        16         15
#> [4,]        1      0    1     1        14         38
#> [5,]        1      1    1     0        16         19
#> [6,]        1      1    0     0        12          4
head(y)
#>      wages
#> [1,] 11.55
#> [2,]  5.00
#> [3,] 12.00
#> [4,]  7.00
#> [5,] 21.15
#> [6,]  6.92

\(MSE\)

RSS <- RSS(
  X = X,
  y = y
)
n <- nrow(X)
result_MSE1 <- .MSE(
  RSS = RSS,
  n = n
)
result_MSE2 <- .MSE(
  X = X,
  y = y
)
result_MSE3 <- MSE(
  X = X,
  y = y
)

\(RMSE\)

MSE <- MSE(
  X = X,
  y = y
)
result_RMSE1 <- .RMSE(
  MSE = MSE
)
result_RMSE2 <- .RMSE(
  X = X,
  y = y
)
result_RMSE3 <- RMSE(
  X = X,
  y = y
)

lm() function

lmobj <- lm(
  wages ~ gender + race + union + education + experience,
  data = jeksterslabRdatarepo::wages
)
lm_MSE <- mean(lmobj$residuals^2)
lm_RMSE <- sqrt(lm_MSE)
result_MSE <- c(
  result_MSE1, result_MSE2, result_MSE3
)
result_RMSE <- c(
  result_RMSE1, result_RMSE2, result_RMSE3
)
context("Test linreg-estimation-MSE.")
test_that("MSE", {
  for (i in seq_along(result_MSE)) {
    expect_equivalent(
      lm_MSE,
      result_MSE[i]
    )
  }
})
#> Test passed 🎉
test_that("RMSE", {
  for (i in seq_along(result_RMSE)) {
    expect_equivalent(
      lm_RMSE,
      result_RMSE[i]
    )
  }
})
#> Test passed 😀