Skip to content

Commit db9702f

Browse files
topepohfrick
andauthored
Fix metric checking for quantile regression (#1187)
* changes for #1186 * news file entry * fix test name * Update R/metric-selection.R Co-authored-by: Hannah Frick <hfrick@users.noreply.github.com> * changes based on reviewer comments --------- Co-authored-by: Hannah Frick <hfrick@users.noreply.github.com>
1 parent 6ab523c commit db9702f

7 files changed

Lines changed: 103 additions & 45 deletions

File tree

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ _cache$
2121
^\.vscode$
2222
^[.]?air[.]toml$
2323
^\.claude$
24+
^\.positai$

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ tests/testthat/Rplots.pdf
1313
docs
1414
revdep
1515
tests/testthat/_snaps/notes.new.md
16+
.positai

NEWS.md

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

3+
## Bug Fixes
4+
5+
* Resampling and tuning would fail for quantile regression models if they passed a quantile regression metric (#1186)
6+
37
# tune 2.1.0
48

59
* Model tuning has been enabled for quantile regression models. (#1125)

R/metric-selection.R

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -330,43 +330,46 @@ check_metrics_arg <- function(mtr_set, wflow, ..., call = rlang::caller_env()) {
330330
return(mtr_set)
331331
}
332332

333+
if (!inherits(mtr_set, "metric_set")) {
334+
cli::cli_abort(
335+
"The {.arg metrics} argument should have class {.cls metric_set}, not {.cls {class(mtr_set)}}.",
336+
call = call
337+
)
338+
}
339+
333340
is_numeric_metric_set <- inherits(mtr_set, "numeric_metric_set")
334341
is_class_prob_metric_set <- inherits(mtr_set, "class_prob_metric_set")
335342
is_surv_metric_set <- inherits(mtr_set, c("survival_metric_set"))
343+
is_qnt_metric_set <- inherits(mtr_set, c("quantile_metric_set"))
336344

337-
if (
338-
!is_numeric_metric_set && !is_class_prob_metric_set && !is_surv_metric_set
339-
) {
345+
if (mode == "regression" && !is_numeric_metric_set) {
340346
cli::cli_abort(
341-
"The {.arg metrics} argument should be the results of
342-
{.fn yardstick::metric_set}.",
347+
"The parsnip model has {.code mode} value of {.val {mode}},
348+
but the metric set for a different model mode.",
343349
call = call
344350
)
345351
}
346352

347-
if (mode == "regression" && !is_numeric_metric_set) {
353+
if (mode == "classification" && !is_class_prob_metric_set) {
348354
cli::cli_abort(
349355
"The parsnip model has {.code mode} value of {.val {mode}},
350-
but the {.arg metrics} is a metric set for a
351-
different model mode.",
356+
but the metric set for a different model mode.",
352357
call = call
353358
)
354359
}
355360

356-
if (mode == "classification" && !is_class_prob_metric_set) {
361+
if (mode == "censored regression" && !is_surv_metric_set) {
357362
cli::cli_abort(
358363
"The parsnip model has {.code mode} value of {.val {mode}},
359-
but the {.arg metrics} is a metric set for a
360-
different model mode.",
364+
but the metric set for a different model mode.",
361365
call = call
362366
)
363367
}
364368

365-
if (mode == "censored regression" && !is_surv_metric_set) {
369+
if (mode == "quantile regression" && !is_qnt_metric_set) {
366370
cli::cli_abort(
367371
"The parsnip model has {.code mode} value of {.val {mode}},
368-
but the {.arg metrics} is a metric set for a
369-
different model mode.",
372+
but the metric set for a different model mode.",
370373
call = call
371374
)
372375
}

tests/testthat/_snaps/censored-reg.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
tune_grid(spec, Surv(time, status) ~ ., resamples = rs, metrics = reg_mtr)
1313
Condition
1414
Error in `tune_grid()`:
15-
! The parsnip model has `mode` value of "censored regression", but the `metrics` is a metric set for a different model mode.
15+
! The parsnip model has `mode` value of "censored regression", but the metric set for a different model mode.
1616

1717
---
1818

tests/testthat/_snaps/metric-args.md

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
- `rmse()`, a numeric metric | direction: minimize
88
- `rsq()`, a numeric metric | direction: maximize
99

10+
---
11+
12+
Code
13+
check_metrics_arg(rmse, wflow)
14+
Condition
15+
Error:
16+
! The `metrics` argument should have class <metric_set>, not <numeric_metric/metric/function>.
17+
1018
---
1119

1220
Code
@@ -21,79 +29,79 @@
2129
check_metrics_arg(met_cls, wflow)
2230
Condition
2331
Error:
24-
! The parsnip model has `mode` value of "regression", but the `metrics` is a metric set for a different model mode.
32+
! The parsnip model has `mode` value of "regression", but the metric set for a different model mode.
2533

2634
---
2735

2836
Code
2937
check_metrics_arg(met_mix_int, wflow)
3038
Condition
3139
Error:
32-
! The parsnip model has `mode` value of "regression", but the `metrics` is a metric set for a different model mode.
40+
! The parsnip model has `mode` value of "regression", but the metric set for a different model mode.
3341

3442
---
3543

3644
Code
3745
fit_resamples(wflow, rs, metrics = met_cls)
3846
Condition
3947
Error in `fit_resamples()`:
40-
! The parsnip model has `mode` value of "regression", but the `metrics` is a metric set for a different model mode.
48+
! The parsnip model has `mode` value of "regression", but the metric set for a different model mode.
4149

4250
---
4351

4452
Code
4553
fit_resamples(wflow, rs, metrics = met_mix_int)
4654
Condition
4755
Error in `fit_resamples()`:
48-
! The parsnip model has `mode` value of "regression", but the `metrics` is a metric set for a different model mode.
56+
! The parsnip model has `mode` value of "regression", but the metric set for a different model mode.
4957

5058
---
5159

5260
Code
5361
tune_grid(wflow_tune, rs, metrics = met_cls)
5462
Condition
5563
Error in `tune_grid()`:
56-
! The parsnip model has `mode` value of "regression", but the `metrics` is a metric set for a different model mode.
64+
! The parsnip model has `mode` value of "regression", but the metric set for a different model mode.
5765

5866
---
5967

6068
Code
6169
tune_grid(wflow_tune, rs, metrics = met_mix_int)
6270
Condition
6371
Error in `tune_grid()`:
64-
! The parsnip model has `mode` value of "regression", but the `metrics` is a metric set for a different model mode.
72+
! The parsnip model has `mode` value of "regression", but the metric set for a different model mode.
6573

6674
---
6775

6876
Code
6977
tune_bayes(wflow_tune, rs, metrics = met_cls)
7078
Condition
7179
Error in `tune_bayes()`:
72-
! The parsnip model has `mode` value of "regression", but the `metrics` is a metric set for a different model mode.
80+
! The parsnip model has `mode` value of "regression", but the metric set for a different model mode.
7381

7482
---
7583

7684
Code
7785
tune_bayes(wflow_tune, rs, metrics = met_mix_int)
7886
Condition
7987
Error in `tune_bayes()`:
80-
! The parsnip model has `mode` value of "regression", but the `metrics` is a metric set for a different model mode.
88+
! The parsnip model has `mode` value of "regression", but the metric set for a different model mode.
8189

8290
---
8391

8492
Code
8593
last_fit(wflow, split, metrics = met_cls)
8694
Condition
8795
Error in `last_fit()`:
88-
! The parsnip model has `mode` value of "regression", but the `metrics` is a metric set for a different model mode.
96+
! The parsnip model has `mode` value of "regression", but the metric set for a different model mode.
8997

9098
---
9199

92100
Code
93101
last_fit(wflow, split, metrics = met_mix_int)
94102
Condition
95103
Error in `last_fit()`:
96-
! The parsnip model has `mode` value of "regression", but the `metrics` is a metric set for a different model mode.
104+
! The parsnip model has `mode` value of "regression", but the metric set for a different model mode.
97105

98106
# metric inputs are checked for classification models
99107

@@ -111,7 +119,7 @@
111119
check_metrics_arg(met_reg, wflow)
112120
Condition
113121
Error:
114-
! The parsnip model has `mode` value of "classification", but the `metrics` is a metric set for a different model mode.
122+
! The parsnip model has `mode` value of "classification", but the metric set for a different model mode.
115123

116124
---
117125

@@ -127,71 +135,71 @@
127135
check_metrics_arg(met_mix_int, wflow)
128136
Condition
129137
Error:
130-
! The parsnip model has `mode` value of "classification", but the `metrics` is a metric set for a different model mode.
138+
! The parsnip model has `mode` value of "classification", but the metric set for a different model mode.
131139

132140
---
133141

134142
Code
135143
fit_resamples(wflow, rs, metrics = met_reg)
136144
Condition
137145
Error in `fit_resamples()`:
138-
! The parsnip model has `mode` value of "classification", but the `metrics` is a metric set for a different model mode.
146+
! The parsnip model has `mode` value of "classification", but the metric set for a different model mode.
139147

140148
---
141149

142150
Code
143151
fit_resamples(wflow, rs, metrics = met_mix_int)
144152
Condition
145153
Error in `fit_resamples()`:
146-
! The parsnip model has `mode` value of "classification", but the `metrics` is a metric set for a different model mode.
154+
! The parsnip model has `mode` value of "classification", but the metric set for a different model mode.
147155

148156
---
149157

150158
Code
151159
tune_grid(wflow_tune, rs, metrics = met_reg)
152160
Condition
153161
Error in `tune_grid()`:
154-
! The parsnip model has `mode` value of "classification", but the `metrics` is a metric set for a different model mode.
162+
! The parsnip model has `mode` value of "classification", but the metric set for a different model mode.
155163

156164
---
157165

158166
Code
159167
tune_grid(wflow_tune, rs, metrics = met_mix_int)
160168
Condition
161169
Error in `tune_grid()`:
162-
! The parsnip model has `mode` value of "classification", but the `metrics` is a metric set for a different model mode.
170+
! The parsnip model has `mode` value of "classification", but the metric set for a different model mode.
163171

164172
---
165173

166174
Code
167175
tune_bayes(wflow_tune, rs, metrics = met_reg)
168176
Condition
169177
Error in `tune_bayes()`:
170-
! The parsnip model has `mode` value of "classification", but the `metrics` is a metric set for a different model mode.
178+
! The parsnip model has `mode` value of "classification", but the metric set for a different model mode.
171179

172180
---
173181

174182
Code
175183
tune_bayes(wflow_tune, rs, metrics = met_mix_int)
176184
Condition
177185
Error in `tune_bayes()`:
178-
! The parsnip model has `mode` value of "classification", but the `metrics` is a metric set for a different model mode.
186+
! The parsnip model has `mode` value of "classification", but the metric set for a different model mode.
179187

180188
---
181189

182190
Code
183191
last_fit(wflow, split, metrics = met_reg)
184192
Condition
185193
Error in `last_fit()`:
186-
! The parsnip model has `mode` value of "classification", but the `metrics` is a metric set for a different model mode.
194+
! The parsnip model has `mode` value of "classification", but the metric set for a different model mode.
187195

188196
---
189197

190198
Code
191199
last_fit(wflow, split, metrics = met_mix_int)
192200
Condition
193201
Error in `last_fit()`:
194-
! The parsnip model has `mode` value of "classification", but the `metrics` is a metric set for a different model mode.
202+
! The parsnip model has `mode` value of "classification", but the metric set for a different model mode.
195203

196204
# metric inputs are checked for censored regression models
197205

@@ -207,15 +215,15 @@
207215
check_metrics_arg(met_reg, wflow)
208216
Condition
209217
Error:
210-
! The parsnip model has `mode` value of "censored regression", but the `metrics` is a metric set for a different model mode.
218+
! The parsnip model has `mode` value of "censored regression", but the metric set for a different model mode.
211219

212220
---
213221

214222
Code
215223
check_metrics_arg(met_cls, wflow)
216224
Condition
217225
Error:
218-
! The parsnip model has `mode` value of "censored regression", but the `metrics` is a metric set for a different model mode.
226+
! The parsnip model has `mode` value of "censored regression", but the metric set for a different model mode.
219227

220228
---
221229

@@ -231,61 +239,85 @@
231239
fit_resamples(wflow, rs, metrics = met_cls)
232240
Condition
233241
Error in `fit_resamples()`:
234-
! The parsnip model has `mode` value of "censored regression", but the `metrics` is a metric set for a different model mode.
242+
! The parsnip model has `mode` value of "censored regression", but the metric set for a different model mode.
235243

236244
---
237245

238246
Code
239247
fit_resamples(wflow, rs, metrics = met_reg)
240248
Condition
241249
Error in `fit_resamples()`:
242-
! The parsnip model has `mode` value of "censored regression", but the `metrics` is a metric set for a different model mode.
250+
! The parsnip model has `mode` value of "censored regression", but the metric set for a different model mode.
243251

244252
---
245253

246254
Code
247255
tune_grid(wflow_tune, rs, metrics = met_cls)
248256
Condition
249257
Error in `tune_grid()`:
250-
! The parsnip model has `mode` value of "censored regression", but the `metrics` is a metric set for a different model mode.
258+
! The parsnip model has `mode` value of "censored regression", but the metric set for a different model mode.
251259

252260
---
253261

254262
Code
255263
tune_grid(wflow_tune, rs, metrics = met_reg)
256264
Condition
257265
Error in `tune_grid()`:
258-
! The parsnip model has `mode` value of "censored regression", but the `metrics` is a metric set for a different model mode.
266+
! The parsnip model has `mode` value of "censored regression", but the metric set for a different model mode.
259267

260268
---
261269

262270
Code
263271
tune_bayes(wflow_tune, rs, metrics = met_cls)
264272
Condition
265273
Error in `tune_bayes()`:
266-
! The parsnip model has `mode` value of "censored regression", but the `metrics` is a metric set for a different model mode.
274+
! The parsnip model has `mode` value of "censored regression", but the metric set for a different model mode.
267275

268276
---
269277

270278
Code
271279
tune_bayes(wflow_tune, rs, metrics = met_reg)
272280
Condition
273281
Error in `tune_bayes()`:
274-
! The parsnip model has `mode` value of "censored regression", but the `metrics` is a metric set for a different model mode.
282+
! The parsnip model has `mode` value of "censored regression", but the metric set for a different model mode.
275283

276284
---
277285

278286
Code
279287
last_fit(wflow, split, metrics = met_cls)
280288
Condition
281289
Error in `last_fit()`:
282-
! The parsnip model has `mode` value of "censored regression", but the `metrics` is a metric set for a different model mode.
290+
! The parsnip model has `mode` value of "censored regression", but the metric set for a different model mode.
283291

284292
---
285293

286294
Code
287295
last_fit(wflow, split, metrics = met_reg)
288296
Condition
289297
Error in `last_fit()`:
290-
! The parsnip model has `mode` value of "censored regression", but the `metrics` is a metric set for a different model mode.
298+
! The parsnip model has `mode` value of "censored regression", but the metric set for a different model mode.
299+
300+
# metric inputs are checked for quantile regression models
301+
302+
Code
303+
check_metrics_arg(NULL, wflow)
304+
Output
305+
A metric set, consisting of:
306+
- `weighted_interval_score()`, a quantile metric | direction: minimize
307+
308+
---
309+
310+
Code
311+
check_metrics_arg(metric_set(rmse), wflow)
312+
Condition
313+
Error:
314+
! The parsnip model has `mode` value of "quantile regression", but the metric set for a different model mode.
315+
316+
---
317+
318+
Code
319+
check_metrics_arg(metric_set(weighted_interval_score), wflow)
320+
Output
321+
A metric set, consisting of:
322+
- `weighted_interval_score()`, a quantile metric | direction: minimize
291323

0 commit comments

Comments
 (0)