Skip to content

Commit b462f76

Browse files
authored
improve error when outcome is missing (#1016)
1 parent 1afcd2f commit b462f76

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# parsnip (development version)
22

3+
* Improved errors in cases where the outcome column is mis-specified. (#1003)
4+
35
# parsnip 1.1.1
46

57
* Fixed bug where prediction on rank deficient `lm()` models produced `.pred_res` instead of `.pred`. (#985)

R/misc.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,16 @@ check_outcome <- function(y, spec) {
354354
return(invisible(NULL))
355355
}
356356

357+
has_no_outcome <- if (is.atomic(y)) {is.null(y)} else {length(y) == 0}
358+
if (isTRUE(has_no_outcome)) {
359+
cli::cli_abort(
360+
c("!" = "{.fun {class(spec)[1]}} was unable to find an outcome.",
361+
"i" = "Ensure that you have specified an outcome column and that it \\
362+
hasn't been removed in pre-processing."),
363+
call = NULL
364+
)
365+
}
366+
357367
if (spec$mode == "regression") {
358368
outcome_is_numeric <- if (is.atomic(y)) {is.numeric(y)} else {all(map_lgl(y, is.numeric))}
359369
if (!outcome_is_numeric) {

tests/testthat/_snaps/misc.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,33 @@
140140
Error in `check_outcome()`:
141141
! For a regression model, the outcome should be `numeric`, not a `factor`.
142142

143+
---
144+
145+
Code
146+
check_outcome(NULL, reg_spec)
147+
Condition
148+
Error:
149+
! `linear_reg()` was unable to find an outcome.
150+
i Ensure that you have specified an outcome column and that it hasn't been removed in pre-processing.
151+
152+
---
153+
154+
Code
155+
check_outcome(tibble::new_tibble(list(), nrow = 10), reg_spec)
156+
Condition
157+
Error:
158+
! `linear_reg()` was unable to find an outcome.
159+
i Ensure that you have specified an outcome column and that it hasn't been removed in pre-processing.
160+
161+
---
162+
163+
Code
164+
fit(reg_spec, ~mpg, mtcars)
165+
Condition
166+
Error:
167+
! `linear_reg()` was unable to find an outcome.
168+
i Ensure that you have specified an outcome column and that it hasn't been removed in pre-processing.
169+
143170
---
144171

145172
Code
@@ -148,6 +175,33 @@
148175
Error in `check_outcome()`:
149176
! For a classification model, the outcome should be a `factor`, not a `integer`.
150177

178+
---
179+
180+
Code
181+
check_outcome(NULL, class_spec)
182+
Condition
183+
Error:
184+
! `logistic_reg()` was unable to find an outcome.
185+
i Ensure that you have specified an outcome column and that it hasn't been removed in pre-processing.
186+
187+
---
188+
189+
Code
190+
check_outcome(tibble::new_tibble(list(), nrow = 10), class_spec)
191+
Condition
192+
Error:
193+
! `logistic_reg()` was unable to find an outcome.
194+
i Ensure that you have specified an outcome column and that it hasn't been removed in pre-processing.
195+
196+
---
197+
198+
Code
199+
fit(class_spec, ~mpg, mtcars)
200+
Condition
201+
Error:
202+
! `logistic_reg()` was unable to find an outcome.
203+
i Ensure that you have specified an outcome column and that it hasn't been removed in pre-processing.
204+
151205
---
152206

153207
Code

tests/testthat/test_misc.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ test_that('check_outcome works as expected', {
205205
check_outcome(factor(1:2), reg_spec)
206206
)
207207

208+
expect_snapshot(
209+
error = TRUE,
210+
check_outcome(NULL, reg_spec)
211+
)
212+
213+
expect_snapshot(
214+
error = TRUE,
215+
check_outcome(tibble::new_tibble(list(), nrow = 10), reg_spec)
216+
)
217+
218+
expect_snapshot(
219+
error = TRUE,
220+
fit(reg_spec, ~ mpg, mtcars)
221+
)
222+
208223
class_spec <- logistic_reg()
209224

210225
expect_no_error(
@@ -220,6 +235,21 @@ test_that('check_outcome works as expected', {
220235
check_outcome(1:2, class_spec)
221236
)
222237

238+
expect_snapshot(
239+
error = TRUE,
240+
check_outcome(NULL, class_spec)
241+
)
242+
243+
expect_snapshot(
244+
error = TRUE,
245+
check_outcome(tibble::new_tibble(list(), nrow = 10), class_spec)
246+
)
247+
248+
expect_snapshot(
249+
error = TRUE,
250+
fit(class_spec, ~ mpg, mtcars)
251+
)
252+
223253
# Fake specification to avoid having to load {censored}
224254
cens_spec <- logistic_reg()
225255
cens_spec$mode <- "censored regression"

0 commit comments

Comments
 (0)