@@ -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