|
| 1 | + |
| 2 | +context("augment") |
| 3 | + |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | + |
| 6 | +test_that('regression models', { |
| 7 | + x <- linear_reg() %>% set_engine("lm") |
| 8 | + |
| 9 | + reg_form <- x %>% fit(mpg ~ ., data = mtcars) |
| 10 | + reg_xy <- x %>% fit_xy(mtcars[, -1], mtcars$mpg) |
| 11 | + |
| 12 | + expect_equal( |
| 13 | + colnames(augment(reg_form, head(mtcars))), |
| 14 | + c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", |
| 15 | + "gear", "carb", ".pred", ".resid") |
| 16 | + ) |
| 17 | + expect_equal(nrow(augment(reg_form, head(mtcars))), 6) |
| 18 | + expect_equal( |
| 19 | + colnames(augment(reg_form, head(mtcars[, -1]))), |
| 20 | + c("cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", |
| 21 | + "gear", "carb", ".pred") |
| 22 | + ) |
| 23 | + expect_equal(nrow(augment(reg_form, head(mtcars[, -1]))), 6) |
| 24 | + |
| 25 | + expect_equal( |
| 26 | + colnames(augment(reg_xy, head(mtcars))), |
| 27 | + c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", |
| 28 | + "gear", "carb", ".pred") |
| 29 | + ) |
| 30 | + expect_equal(nrow(augment(reg_xy, head(mtcars))), 6) |
| 31 | + expect_equal( |
| 32 | + colnames(augment(reg_xy, head(mtcars[, -1]))), |
| 33 | + c("cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", |
| 34 | + "gear", "carb", ".pred") |
| 35 | + ) |
| 36 | + expect_equal(nrow(augment(reg_xy, head(mtcars[, -1]))), 6) |
| 37 | + |
| 38 | + reg_form$spec$mode <- "depeche" |
| 39 | + |
| 40 | + expect_error(augment(reg_form, head(mtcars[, -1])), "Unknown mode: depeche") |
| 41 | + |
| 42 | +}) |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +test_that('classification models', { |
| 47 | + data(two_class_dat, package = "modeldata") |
| 48 | + x <- logistic_reg() %>% set_engine("glm") |
| 49 | + |
| 50 | + cls_form <- x %>% fit(Class ~ ., data = two_class_dat) |
| 51 | + cls_xy <- x %>% fit_xy(two_class_dat[, -3], two_class_dat$Class) |
| 52 | + |
| 53 | + expect_equal( |
| 54 | + colnames(augment(cls_form, head(two_class_dat))), |
| 55 | + c("A", "B", "Class", ".pred_class", ".pred_Class1", ".pred_Class2") |
| 56 | + ) |
| 57 | + expect_equal(nrow(augment(cls_form, head(two_class_dat))), 6) |
| 58 | + expect_equal( |
| 59 | + colnames(augment(cls_form, head(two_class_dat[, -3]))), |
| 60 | + c("A", "B", ".pred_class", ".pred_Class1", ".pred_Class2") |
| 61 | + ) |
| 62 | + expect_equal(nrow(augment(cls_form, head(two_class_dat[, -3]))), 6) |
| 63 | + |
| 64 | + expect_equal( |
| 65 | + colnames(augment(cls_xy, head(two_class_dat))), |
| 66 | + c("A", "B", "Class", ".pred_class", ".pred_Class1", ".pred_Class2") |
| 67 | + ) |
| 68 | + expect_equal(nrow(augment(cls_xy, head(two_class_dat))), 6) |
| 69 | + expect_equal( |
| 70 | + colnames(augment(cls_xy, head(two_class_dat[, -3]))), |
| 71 | + c("A", "B", ".pred_class", ".pred_Class1", ".pred_Class2") |
| 72 | + ) |
| 73 | + expect_equal(nrow(augment(cls_xy, head(two_class_dat[, -3]))), 6) |
| 74 | + |
| 75 | +}) |
| 76 | + |
0 commit comments