vignettes/tests/test-linreg-estimation-predicted.Rmd
test-linreg-estimation-predicted.Rmd
# The Linear Regression Model: Predicted Values {#linreg-estimation-predicted-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_yhat <- yhat(
X = X,
y = y
)
lm()
function
lmobj <- lm(
wages ~ gender + race + union + education + experience,
data = jeksterslabRdatarepo::wages
)
lm_yhat <- as.vector(predict(lmobj))
context("Test linreg-estimation-projection")
test_that("Py = yhat.", {
expect_equivalent(
length(result_Py1),
length(result_Py2),
length(result_Py3),
length(result_Xbetahat1),
length(result_Xbetahat2),
length(result_Xbetahat3),
length(result_yhat),
length(lm_yhat)
)
for (i in seq_along(result_Py1)) {
expect_equivalent(
result_Py1[i],
lm_yhat[i]
)
}
for (i in seq_along(result_Py2)) {
expect_equivalent(
result_Py2[i],
lm_yhat[i]
)
}
for (i in seq_along(result_Py3)) {
expect_equivalent(
result_Py3[i],
lm_yhat[i]
)
}
for (i in seq_along(result_Xbetahat1)) {
expect_equivalent(
result_Xbetahat1[i],
lm_yhat[i]
)
}
for (i in seq_along(result_Xbetahat2)) {
expect_equivalent(
result_Xbetahat2[i],
lm_yhat[i]
)
}
for (i in seq_along(result_Xbetahat3)) {
expect_equivalent(
result_Xbetahat3[i],
lm_yhat[i]
)
}
for (i in seq_along(result_yhat)) {
expect_equivalent(
result_yhat[i],
lm_yhat[i]
)
}
})
#> Test passed 🌈
test_that("error.", {
expect_error(
.Py(
y = y
)
)
expect_error(
.Xbetahat(
X = X
)
)
})
#> Test passed 🌈