vignettes/tests/test-linreg-estimation-residuals.Rmd
test-linreg-estimation-residuals.Rmd
# The Linear Regression Model: Residuals {#linreg-estimation-residuals-example}
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
result_yminusyhat1 <- .yminusyhat(
y = y,
yhat = yhat
)
result_yminusyhat2 <- .yminusyhat(
y = y,
X = X
)
result_yminusyhat3 <- yminusyhat(
X = X,
y = y
)
result_epsilonhat <- epsilonhat(
X = X,
y = y
)
lm()
function
lmobj <- lm(
wages ~ gender + race + union + education + experience,
data = jeksterslabRdatarepo::wages
)
lm_epsilonhat <- as.vector(residuals(lmobj))
context("Test linreg-estimation-projection")
test_that("Py = yhat.", {
expect_equivalent(
length(result_My1),
length(result_My2),
length(result_My3),
length(result_yminusyhat1),
length(result_yminusyhat2),
length(result_yminusyhat3),
length(result_epsilonhat)
)
for (i in seq_along(result_My1)) {
expect_equivalent(
result_My1[i],
lm_epsilonhat[i]
)
}
for (i in seq_along(result_My2)) {
expect_equivalent(
result_My2[i],
lm_epsilonhat[i]
)
}
for (i in seq_along(result_My3)) {
expect_equivalent(
result_My3[i],
lm_epsilonhat[i]
)
}
for (i in seq_along(result_yminusyhat1)) {
expect_equivalent(
result_yminusyhat1[i],
lm_epsilonhat[i]
)
}
for (i in seq_along(result_yminusyhat2)) {
expect_equivalent(
result_yminusyhat2[i],
lm_epsilonhat[i]
)
}
for (i in seq_along(result_yminusyhat3)) {
expect_equivalent(
result_yminusyhat3[i],
lm_epsilonhat[i]
)
}
for (i in seq_along(result_epsilonhat)) {
expect_equivalent(
result_epsilonhat[i],
lm_epsilonhat[i]
)
}
})
#> Test passed 😸
test_that("error.", {
expect_error(
.My(
y = y
)
)
expect_error(
.yminusyhat(
y = y
)
)
})
#> Test passed 🥳