| 1 |
skip_if_not_installed("modeltests") |
| 2 |
library(modeltests) |
| 3 |
|
| 4 |
test_that("lm tidier arguments", { |
| 5 |
check_arguments(tidy.lm) |
| 6 |
check_arguments(glance.lm) |
| 7 |
# errors with "Tidiers with `conf.level` argument must have `conf.int` argument." |
| 8 |
# check_arguments(augment.lm) |
| 9 |
}) |
| 10 |
|
| 11 |
fit <- lm(mpg ~ wt, mtcars) |
| 12 |
fit2 <- lm(mpg ~ wt + log(disp), mtcars) |
| 13 |
fit3 <- lm(mpg ~ 1, mtcars) |
| 14 |
fit4 <- lm(mpg ~ 0 + cyl, mtcars) |
| 15 |
|
| 16 |
# zero weights used to break influence columns in augment.lm |
| 17 |
wts <- c(0, rep(1, nrow(mtcars) - 1)) |
| 18 |
fit_0wts <- lm(mpg ~ 1, weights = wts, data = mtcars) |
| 19 |
|
| 20 |
# the cyl:qsec term isn't defined for this fit |
| 21 |
na_row_data <- mtcars[c(6, 9, 13:15, 22), ] |
| 22 |
fit_na_row <- lm(mpg ~ cyl * qsec + gear, data = na_row_data) |
| 23 |
|
| 24 |
# rank-deficient fit |
| 25 |
rd_data <- data.frame(y = rnorm(10), x = letters[seq_len(10)]) |
| 26 |
fit_rd <- lm(y ~ x - 1, data = rd_data) |
| 27 |
|
| 28 |
test_that("tidy.lm works", { |
| 29 |
td <- tidy(fit) |
| 30 |
td2 <- tidy(fit2) |
| 31 |
td3 <- tidy(fit3) |
| 32 |
td_na_row <- tidy(fit_na_row) |
| 33 |
|
| 34 |
# conf.int = TRUE works for rank deficient fits |
| 35 |
# should get a "NaNs produced" warning |
| 36 |
expect_snapshot(td_rd <- tidy(fit_rd, conf.int = TRUE)) |
| 37 |
|
| 38 |
check_tidy_output(td) |
| 39 |
check_tidy_output(td2) |
| 40 |
check_tidy_output(td3) |
| 41 |
check_tidy_output(td_rd) |
| 42 |
check_tidy_output(td_na_row) |
| 43 |
|
| 44 |
check_dims(td, expected_rows = 2) |
| 45 |
check_dims(td2, expected_rows = 3) |
| 46 |
check_dims(td3, expected_rows = 1) |
| 47 |
check_dims(td_na_row, expected_rows = 5) |
| 48 |
|
| 49 |
expect_equal(td$term, c("(Intercept)", "wt")) |
| 50 |
expect_equal(td2$term, c("(Intercept)", "wt", "log(disp)")) |
| 51 |
expect_equal(td3$term, c("(Intercept)")) |
| 52 |
|
| 53 |
# shouldn't error. regression test for issues #166 and #241. |
| 54 |
# rows for confidence intervals of undefined terms should be dropped |
| 55 |
expect_no_error(tidy(fit_na_row, conf.int = TRUE)) |
| 56 |
}) |
| 57 |
|
| 58 |
test_that("glance.lm", { |
| 59 |
gl <- glance(fit) |
| 60 |
gl2 <- glance(fit2) |
| 61 |
gl3 <- glance(fit3) |
| 62 |
gl4 <- glance(fit4) |
| 63 |
|
| 64 |
check_glance_outputs(gl, gl2, gl3, gl4) |
| 65 |
}) |
| 66 |
|
| 67 |
test_that("augment.lm", { |
| 68 |
check_augment_function( |
| 69 |
aug = augment.lm, |
| 70 |
model = fit, |
| 71 |
data = mtcars, |
| 72 |
newdata = mtcars |
| 73 |
) |
| 74 |
|
| 75 |
check_augment_function( |
| 76 |
aug = augment.lm, |
| 77 |
model = fit2, |
| 78 |
data = mtcars, |
| 79 |
newdata = mtcars |
| 80 |
) |
| 81 |
|
| 82 |
check_augment_function( |
| 83 |
aug = augment.lm, |
| 84 |
model = fit3, |
| 85 |
data = mtcars, |
| 86 |
newdata = mtcars |
| 87 |
) |
| 88 |
|
| 89 |
if (paste(R.version$major, R.version$minor, sep = ".") <= "4.2.2") { |
| 90 |
expect_warning( |
| 91 |
check_augment_function( |
| 92 |
aug = augment.lm, |
| 93 |
model = fit_na_row, |
| 94 |
data = na_row_data, |
| 95 |
newdata = na_row_data |
| 96 |
), |
| 97 |
"prediction from a rank-deficient fit may be misleading" |
| 98 |
) |
| 99 |
} |
| 100 |
|
| 101 |
check_augment_function( |
| 102 |
aug = augment.lm, |
| 103 |
model = fit_rd, |
| 104 |
data = rd_data, |
| 105 |
newdata = rd_data |
| 106 |
) |
| 107 |
|
| 108 |
check_augment_function( |
| 109 |
aug = augment.lm, |
| 110 |
model = fit_0wts, |
| 111 |
data = mtcars, |
| 112 |
newdata = mtcars |
| 113 |
) |
| 114 |
|
| 115 |
# conf.level defaults to 0.95 |
| 116 |
aug <- augment(fit, newdata = mtcars, interval = "confidence") |
| 117 |
pred <- predict(fit, newdata = mtcars, interval = "confidence", level = 0.95) |
| 118 |
expect_equal(aug$.lower, pred[, "lwr"]) |
| 119 |
expect_equal(aug$.upper, pred[, "upr"]) |
| 120 |
|
| 121 |
# conf.level is respected |
| 122 |
aug <- augment( |
| 123 |
fit, |
| 124 |
newdata = mtcars, |
| 125 |
interval = "confidence", |
| 126 |
conf.level = 0.75 |
| 127 |
) |
| 128 |
pred <- predict(fit, newdata = mtcars, interval = "confidence", level = 0.75) |
| 129 |
expect_equal(aug$.lower, pred[, "lwr"]) |
| 130 |
expect_equal(aug$.upper, pred[, "upr"]) |
| 131 |
|
| 132 |
# conf.level works for prediction intervals as well |
| 133 |
aug <- augment( |
| 134 |
fit, |
| 135 |
newdata = mtcars, |
| 136 |
interval = "prediction", |
| 137 |
conf.level = 0.25 |
| 138 |
) |
| 139 |
pred <- predict(fit, newdata = mtcars, interval = "prediction", level = 0.25) |
| 140 |
expect_equal(aug$.lower, pred[, "lwr"]) |
| 141 |
expect_equal(aug$.upper, pred[, "upr"]) |
| 142 |
|
| 143 |
# conf.level is ignored when interval = "none" |
| 144 |
aug <- augment(fit, newdata = mtcars, interval = "none", conf.level = 0.25) |
| 145 |
expect_false(any(names(aug) %in% c(".lower", ".upper"))) |
| 146 |
|
| 147 |
# warns when passed as level rather than conf.level |
| 148 |
expect_snapshot( |
| 149 |
augment(fit, newdata = mtcars, interval = "confidence", level = 0.95) |
| 150 |
) |
| 151 |
}) |
| 152 |
|
| 153 |
test_that("glance.lm returns non-NA entries with 0-intercept model (#1209)", { |
| 154 |
fit <- lm(mpg ~ 0 + cyl, mtcars) |
| 155 |
fit_glance <- glance(fit) |
| 156 |
expect_false(is.na(fit_glance[["statistic"]])) |
| 157 |
expect_false(is.na(fit_glance[["p.value"]])) |
| 158 |
expect_false(is.na(fit_glance[["df"]])) |
| 159 |
}) |