knitr::opts_chunk$set(
  error = TRUE,
  collapse = TRUE,
  comment = "#>",
  out.width = "100%"
)
# The Linear Regression Model: Mean Square Error {#linreg-estimation-MSE-example}
library(testthat)
library(jeksterslabRlinreg)

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)
head(y)

$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_that("RMSE", {
  for (i in seq_along(result_RMSE)) {
    expect_equivalent(
      lm_RMSE,
      result_RMSE[i]
    )
  }
})


jeksterslabds/jeksterslabRlinreg documentation built on Jan. 7, 2021, 8:30 a.m.