Skip to content

Commit 8590444

Browse files
authored
allow sparse data in nullmodel (#1300)
1 parent 952e07b commit 8590444

File tree

3 files changed

+132
-2
lines changed

3 files changed

+132
-2
lines changed

R/nullmodel_data.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ set_encoding(
2929
predictor_indicators = "traditional",
3030
compute_intercept = FALSE,
3131
remove_intercept = FALSE,
32-
allow_sparse_x = FALSE
32+
allow_sparse_x = TRUE
3333
)
3434
)
3535

@@ -53,7 +53,7 @@ set_encoding(
5353
predictor_indicators = "traditional",
5454
compute_intercept = FALSE,
5555
remove_intercept = FALSE,
56-
allow_sparse_x = FALSE
56+
allow_sparse_x = TRUE
5757
)
5858
)
5959

tests/testthat/_snaps/nullmodel.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# bad input
2+
3+
Code
4+
translate(set_engine(null_model(mode = "regression")))
5+
Condition
6+
Error in `set_engine()`:
7+
! Missing engine. Possible mode/engine combinations are: classification {parsnip} and regression {parsnip}.
8+
9+
---
10+
11+
Code
12+
translate(set_engine(null_model(), "wat?"))
13+
Condition
14+
Error in `set_engine()`:
15+
x Engine "wat?" is not supported for `null_model()`
16+
i See `show_engines("null_model")`.
17+
18+
# nullmodel execution
19+
20+
Code
21+
res <- fit(set_engine(null_model(mode = "regression"), "parsnip"), hpc_bad_form,
22+
data = hpc)
23+
Condition
24+
Error:
25+
! object 'term' not found
26+
27+
# null_model printing
28+
29+
Code
30+
print(null_model(mode = "classification"))
31+
Output
32+
Null Model Specification (classification)
33+
34+
Computational engine: parsnip
35+
36+
37+
---
38+
39+
Code
40+
print(translate(set_engine(null_model(mode = "classification"), "parsnip")))
41+
Output
42+
Null Model Specification (classification)
43+
44+
Computational engine: parsnip
45+
46+
Model fit template:
47+
parsnip::nullmodel(x = missing_arg(), y = missing_arg())
48+

tests/testthat/test-nullmodel.R

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,85 @@ test_that("check_args() works", {
147147
# Here for completeness, no checking is done
148148
expect_true(TRUE)
149149
})
150+
151+
# ------------------------------------------------------------------------------
152+
153+
test_that("null_model works with sparse matrix data - regression", {
154+
skip_if_not_installed("sparsevctrs")
155+
156+
# Make materialization of sparse vectors throw an error
157+
withr::local_options("sparsevctrs.verbose_materialize" = 3)
158+
159+
hotel_data <- sparse_hotel_rates()
160+
161+
spec <- null_model(mode = "regression") |>
162+
set_engine("parsnip")
163+
164+
expect_no_error(
165+
null_fit <- fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1])
166+
)
167+
168+
expect_no_error(
169+
preds <- predict(null_fit, hotel_data)
170+
)
171+
172+
# All predictions should be the mean of the outcome
173+
expect_true(all(preds$.pred == preds$.pred[1]))
174+
})
175+
176+
test_that("null_model works with sparse matrix data - classification", {
177+
skip_if_not_installed("sparsevctrs")
178+
179+
# Make materialization of sparse vectors throw an error
180+
withr::local_options("sparsevctrs.verbose_materialize" = 3)
181+
182+
hotel_data <- sparse_hotel_rates()
183+
184+
# Create a factor outcome for classification
185+
y_class <- factor(ifelse(hotel_data[, 1] > median(hotel_data[, 1]), "high", "low"))
186+
187+
spec <- null_model(mode = "classification") |>
188+
set_engine("parsnip")
189+
190+
expect_no_error(
191+
null_fit <- fit_xy(spec, x = hotel_data[, -1], y = y_class)
192+
)
193+
194+
expect_no_error(
195+
preds <- predict(null_fit, hotel_data)
196+
)
197+
198+
# All predictions should be the same (most prevalent class)
199+
expect_true(all(preds$.pred_class == preds$.pred_class[1]))
200+
201+
expect_no_error(
202+
probs <- predict(null_fit, hotel_data, type = "prob")
203+
)
204+
205+
# All probability predictions should be identical
206+
expect_true(all(probs$.pred_high == probs$.pred_high[1]))
207+
expect_true(all(probs$.pred_low == probs$.pred_low[1]))
208+
})
209+
210+
test_that("null_model works with sparse tibble data - regression", {
211+
skip_if_not_installed("sparsevctrs")
212+
213+
# Make materialization of sparse vectors throw an error
214+
withr::local_options("sparsevctrs.verbose_materialize" = 3)
215+
216+
hotel_data <- sparse_hotel_rates(tibble = TRUE)
217+
218+
spec <- null_model(mode = "regression") |>
219+
set_engine("parsnip")
220+
221+
expect_no_error(
222+
null_fit <- fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1])
223+
)
224+
225+
expect_no_error(
226+
preds <- predict(null_fit, hotel_data)
227+
)
228+
229+
# All predictions should be the mean of the outcome
230+
expect_true(all(preds$.pred == preds$.pred[1]))
231+
})

0 commit comments

Comments
 (0)