|
| 1 | +test_that("sparse matrices can be passed to `fit_xy()", { |
| 2 | + hotel_data <- sparse_hotel_rates() |
| 3 | + |
| 4 | + lm_spec <- linear_reg(penalty = 0) %>% |
| 5 | + set_engine("glmnet") |
| 6 | + |
| 7 | + expect_no_error( |
| 8 | + lm_fit <- fit_xy(lm_spec, x = hotel_data[, -1], y = hotel_data[, 1]) |
| 9 | + ) |
| 10 | +}) |
| 11 | + |
| 12 | +test_that("to_sparse_data_frame() is used correctly", { |
| 13 | + local_mocked_bindings( |
| 14 | + to_sparse_data_frame = function(x, object) { |
| 15 | + if (methods::is(x, "sparseMatrix")) { |
| 16 | + if (allow_sparse(object)) { |
| 17 | + stop("x is spare, and sparse is allowed") |
| 18 | + } else { |
| 19 | + stop("x is spare, and sparse is not allowed") |
| 20 | + } |
| 21 | + } |
| 22 | + stop("x is not sparse") |
| 23 | + } |
| 24 | + ) |
| 25 | + |
| 26 | + hotel_data <- sparse_hotel_rates() |
| 27 | + |
| 28 | + lm_spec <- linear_reg(penalty = 0) %>% |
| 29 | + set_engine("lm") |
| 30 | + |
| 31 | + expect_snapshot( |
| 32 | + error = TRUE, |
| 33 | + fit_xy(lm_spec, x = mtcars[, -1], y = mtcars[, 1]) |
| 34 | + ) |
| 35 | + expect_snapshot( |
| 36 | + error = TRUE, |
| 37 | + fit_xy(lm_spec, x = hotel_data[, -1], y = hotel_data[, 1]) |
| 38 | + ) |
| 39 | + |
| 40 | + lm_spec <- linear_reg(penalty = 0) %>% |
| 41 | + set_engine("glmnet") |
| 42 | + |
| 43 | + expect_snapshot( |
| 44 | + error = TRUE, |
| 45 | + fit_xy(lm_spec, x = hotel_data[, -1], y = hotel_data[, 1]) |
| 46 | + ) |
| 47 | +}) |
| 48 | + |
| 49 | +test_that("maybe_sparse_matrix() is used correctly", { |
| 50 | + local_mocked_bindings( |
| 51 | + maybe_sparse_matrix = function(x) { |
| 52 | + if (any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))) { |
| 53 | + stop("sparse vectors detected") |
| 54 | + } else { |
| 55 | + stop("no sparse vectors detected") |
| 56 | + } |
| 57 | + } |
| 58 | + ) |
| 59 | + |
| 60 | + hotel_data <- sparse_hotel_rates() |
| 61 | + |
| 62 | + lm_spec <- linear_reg(penalty = 0) %>% |
| 63 | + set_engine("glmnet") |
| 64 | + |
| 65 | + expect_snapshot( |
| 66 | + error = TRUE, |
| 67 | + fit_xy(lm_spec, x = hotel_data[, -1], y = hotel_data[, 1]) |
| 68 | + ) |
| 69 | + expect_snapshot( |
| 70 | + error = TRUE, |
| 71 | + fit_xy(lm_spec, x = mtcars[, -1], y = mtcars[, 1]) |
| 72 | + ) |
| 73 | + expect_snapshot( |
| 74 | + error = TRUE, |
| 75 | + fit_xy(lm_spec, x = as.data.frame(mtcars)[, -1], y = as.data.frame(mtcars)[, 1]) |
| 76 | + ) |
| 77 | + expect_snapshot( |
| 78 | + error = TRUE, |
| 79 | + fit_xy(lm_spec, x = tibble::as_tibble(mtcars)[, -1], y = tibble::as_tibble(mtcars)[, 1]) |
| 80 | + ) |
| 81 | +}) |
0 commit comments