Skip to content

Commit 7b7e118

Browse files
authored
align null_model() with other model types (#1085)
1 parent b01b623 commit 7b7e118

File tree

6 files changed

+66
-52
lines changed

6 files changed

+66
-52
lines changed

NEWS.md

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

3+
* Aligned `null_model()` with other model types; the model type now has an
4+
engine argument that defaults to `"parsnip"` and is checked with the same
5+
machinery that checks other model types in the package (#1083).
6+
37
* New `extract_fit_time()` method has been added that returns the time it took to train the model (#853).
48

59
# parsnip 1.2.1

R/nullmodel.R

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,11 @@ predict.nullmodel <- function (object, new_data = NULL, type = NULL, ...) {
130130
#' `null_model()` defines a simple, non-informative model. It doesn't have any
131131
#' main arguments. This function can fit classification and regression models.
132132
#'
133-
#' @inheritParams set_new_model
134-
#' @details The model can be created using the `fit()` function using the
135-
#' following _engines_:
136-
#' \itemize{
137-
#' \item \pkg{R}: `"parsnip"`
138-
#' }
133+
#' @param mode A single character string for the type of model. The only
134+
#' possible values for this model are `"regression"` and `"classification"`.
135+
#' @param engine A single character string specifying what computational engine
136+
#' to use for fitting. Possible engines are listed below. The default for this
137+
#' model is `"parsnip"`.
139138
#'
140139
#' @includeRmd man/rmd/null-model.md details
141140
#'
@@ -144,28 +143,20 @@ predict.nullmodel <- function (object, new_data = NULL, type = NULL, ...) {
144143
#' null_model(mode = "regression")
145144
#' @export
146145
null_model <-
147-
function(mode = "classification") {
148-
null_model_modes <- unique(get_model_env()$null_model$mode)
149-
# Check for correct mode
150-
if (!(mode %in% null_model_modes))
151-
rlang::abort(
152-
glue::glue(
153-
"`mode` should be one of: ",
154-
glue::glue_collapse(glue::glue("'{null_model_modes}'"), sep = ", ")
155-
)
156-
)
157-
158-
# Capture the arguments in quosures
146+
function(mode = "classification", engine = "parsnip") {
159147
args <- list()
160148

161-
# Save some empty slots for future parts of the specification
162-
out <- list(args = args, eng_args = NULL,
163-
mode = mode, method = NULL, engine = NULL)
164-
165-
# set classes in the correct order
166-
class(out) <- make_classes("null_model")
167-
out
168-
}
149+
new_model_spec(
150+
"null_model",
151+
args = args,
152+
eng_args = NULL,
153+
mode = mode,
154+
user_specified_mode = !missing(mode),
155+
method = NULL,
156+
engine = engine,
157+
user_specified_engine = !missing(engine)
158+
)
159+
}
169160

170161

171162

man/null_model.Rd

Lines changed: 7 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/model_basics.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@
235235
Output
236236
Null Model Specification (classification)
237237
238+
Computational engine: parsnip
239+
238240

239241
---
240242

tests/testthat/_snaps/nullmodel.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Output
66
Null Model Specification (classification)
77
8+
Computational engine: parsnip
9+
810

911
---
1012

tests/testthat/test_nullmodel.R

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,63 @@ test_that('bad input', {
1515

1616
num_pred <- names(hpc)[1:3]
1717
hpc_bad_form <- as.formula(class ~ term)
18-
hpc_basic <- null_model(mode = "regression") %>% set_engine("parsnip")
1918

2019
# ------------------------------------------------------------------------------
2120

2221
test_that('nullmodel execution', {
2322

24-
expect_error(
23+
expect_no_condition(
2524
res <- fit(
26-
hpc_basic,
25+
null_model(mode = "regression") %>% set_engine("parsnip"),
2726
compounds ~ log(input_fields) + class,
2827
data = hpc
29-
),
30-
regexp = NA
28+
)
3129
)
32-
expect_error(
30+
expect_no_condition(
31+
res <- fit(
32+
null_model(mode = "regression"),
33+
compounds ~ log(input_fields) + class,
34+
data = hpc
35+
)
36+
)
37+
expect_no_condition(
3338
res <- fit_xy(
34-
hpc_basic,
39+
null_model(mode = "regression") %>% set_engine("parsnip"),
3540
x = hpc[, num_pred],
3641
y = hpc$num_pending
37-
),
38-
regexp = NA
42+
)
43+
)
44+
expect_no_condition(
45+
res <- fit_xy(
46+
null_model(mode = "regression"),
47+
x = hpc[, num_pred],
48+
y = hpc$num_pending
49+
)
3950
)
4051

4152
expect_error(
4253
res <- fit(
43-
hpc_basic,
54+
null_model(mode = "regression") %>% set_engine("parsnip"),
4455
hpc_bad_form,
4556
data = hpc
4657
)
4758
)
4859

4960
## multivariate y
5061

51-
expect_error(
62+
expect_no_condition(
5263
res <- fit(
53-
hpc_basic,
64+
null_model(mode = "regression") %>% set_engine("parsnip"),
5465
cbind(compounds, input_fields) ~ .,
5566
data = hpc
56-
),
57-
regexp = NA
67+
)
68+
)
69+
expect_no_condition(
70+
res <- fit(
71+
null_model(mode = "regression"),
72+
cbind(compounds, input_fields) ~ .,
73+
data = hpc
74+
)
5875
)
5976

6077
})
@@ -67,7 +84,7 @@ test_that('nullmodel prediction', {
6784
carb = rep(2.8125, 5))
6885

6986
res_xy <- fit_xy(
70-
hpc_basic,
87+
null_model(mode = "regression") %>% set_engine("parsnip"),
7188
x = hpc[, num_pred],
7289
y = hpc$num_pending
7390
)
@@ -77,7 +94,7 @@ test_that('nullmodel prediction', {
7794
tolerance = .01)
7895

7996
res_form <- fit(
80-
hpc_basic,
97+
null_model(mode = "regression") %>% set_engine("parsnip"),
8198
num_pending ~ log(compounds) + class,
8299
data = hpc
83100
)
@@ -87,7 +104,7 @@ test_that('nullmodel prediction', {
87104

88105
# Multivariate y
89106
res <- fit(
90-
hpc_basic,
107+
null_model(mode = "regression") %>% set_engine("parsnip"),
91108
cbind(gear, carb) ~ .,
92109
data = mtcars
93110
)

0 commit comments

Comments
 (0)