vignettes/tests/test-linreg-estimation-SS.Rmd
test-linreg-estimation-SS.Rmd
# The Linear Regression Model: Sum of Squares {#linreg-estimation-SS-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
yhat <- yhat(
X = X,
y = y
)
ybar <- mean(y)
result_ESS1 <- .ESS(
yhat = yhat,
ybar = ybar
)
result_ESS2 <- .ESS(
yhat = yhat,
y = y
)
result_ESS3 <- .ESS(
ybar = ybar,
X = X,
y = y,
betahat = NULL
)
result_ESS4 <- .ESS(
ybar = ybar,
X = X,
y = y,
betahat = betahat
)
result_ESS5 <- .ESS(
X = X,
y = y,
betahat = betahat
)
result_ESS6 <- .ESS(
X = X,
y = y
)
result_ESS7 <- ESS(
X = X,
y = y
)
result_TSS <- TSS(
y = y
)
lm()
function
lmobj <- lm(
wages ~ gender + race + union + education + experience,
data = jeksterslabRdatarepo::wages
)
lm_anova <- anova(lmobj)
lm_RSS <- lm_anova["Residuals", "Sum Sq"]
lm_TSS <- sum(lm_anova[["Sum Sq"]])
lm_ESS <- lm_TSS - lm_RSS
result_RSS <- c(
result_RSS1, result_RSS2, result_RSS3, result_RSS4
)
result_ESS <- c(
result_ESS1, result_ESS2, result_ESS3, result_ESS4, result_ESS5, result_ESS6, result_ESS7
)
context("Test linreg-estimation-SS.")
test_that("RSS", {
for (i in seq_along(result_RSS)) {
expect_equivalent(
lm_RSS,
result_RSS[i]
)
}
})
#> Test passed 🥳
test_that("ESS", {
for (i in seq_along(result_RSS)) {
expect_equivalent(
lm_ESS,
result_ESS[i]
)
}
})
#> Test passed 🥇
test_that("TSS", {
expect_equivalent(
lm_TSS,
result_TSS
)
})
#> Test passed 🌈