Skip to content

align null_model() with other model types #1085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# parsnip (development version)

* Aligned `null_model()` with other model types; the model type now has an
engine argument that defaults to `"parsnip"` and is checked with the same
machinery that checks other model types in the package (#1083).

* New `extract_fit_time()` method has been added that returns the time it took to train the model (#853).

# parsnip 1.2.1
Expand Down
43 changes: 17 additions & 26 deletions R/nullmodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,11 @@ predict.nullmodel <- function (object, new_data = NULL, type = NULL, ...) {
#' `null_model()` defines a simple, non-informative model. It doesn't have any
#' main arguments. This function can fit classification and regression models.
#'
#' @inheritParams set_new_model
#' @details The model can be created using the `fit()` function using the
#' following _engines_:
#' \itemize{
#' \item \pkg{R}: `"parsnip"`
#' }
#' @param mode A single character string for the type of model. The only
#' possible values for this model are `"regression"` and `"classification"`.
#' @param engine A single character string specifying what computational engine
#' to use for fitting. Possible engines are listed below. The default for this
#' model is `"parsnip"`.
#'
#' @includeRmd man/rmd/null-model.md details
#'
Expand All @@ -144,28 +143,20 @@ predict.nullmodel <- function (object, new_data = NULL, type = NULL, ...) {
#' null_model(mode = "regression")
#' @export
null_model <-
function(mode = "classification") {
null_model_modes <- unique(get_model_env()$null_model$mode)
# Check for correct mode
if (!(mode %in% null_model_modes))
rlang::abort(
glue::glue(
"`mode` should be one of: ",
glue::glue_collapse(glue::glue("'{null_model_modes}'"), sep = ", ")
)
)

# Capture the arguments in quosures
function(mode = "classification", engine = "parsnip") {
args <- list()

# Save some empty slots for future parts of the specification
out <- list(args = args, eng_args = NULL,
mode = mode, method = NULL, engine = NULL)

# set classes in the correct order
class(out) <- make_classes("null_model")
out
}
new_model_spec(
"null_model",
args = args,
eng_args = NULL,
mode = mode,
user_specified_mode = !missing(mode),
method = NULL,
engine = engine,
user_specified_engine = !missing(engine)
)
}



Expand Down
16 changes: 7 additions & 9 deletions man/null_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/testthat/_snaps/model_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@
Output
Null Model Specification (classification)

Computational engine: parsnip


---

Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/_snaps/nullmodel.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Output
Null Model Specification (classification)

Computational engine: parsnip


---

Expand Down
51 changes: 34 additions & 17 deletions tests/testthat/test_nullmodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,63 @@ test_that('bad input', {

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

# ------------------------------------------------------------------------------

test_that('nullmodel execution', {

expect_error(
expect_no_condition(
res <- fit(
hpc_basic,
null_model(mode = "regression") %>% set_engine("parsnip"),
compounds ~ log(input_fields) + class,
data = hpc
),
regexp = NA
)
)
expect_error(
expect_no_condition(
res <- fit(
null_model(mode = "regression"),
compounds ~ log(input_fields) + class,
data = hpc
)
)
expect_no_condition(
res <- fit_xy(
hpc_basic,
null_model(mode = "regression") %>% set_engine("parsnip"),
x = hpc[, num_pred],
y = hpc$num_pending
),
regexp = NA
)
)
expect_no_condition(
res <- fit_xy(
null_model(mode = "regression"),
x = hpc[, num_pred],
y = hpc$num_pending
)
)

expect_error(
res <- fit(
hpc_basic,
null_model(mode = "regression") %>% set_engine("parsnip"),
hpc_bad_form,
data = hpc
)
)

## multivariate y

expect_error(
expect_no_condition(
res <- fit(
hpc_basic,
null_model(mode = "regression") %>% set_engine("parsnip"),
cbind(compounds, input_fields) ~ .,
data = hpc
),
regexp = NA
)
)
expect_no_condition(
res <- fit(
null_model(mode = "regression"),
cbind(compounds, input_fields) ~ .,
data = hpc
)
)

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

res_xy <- fit_xy(
hpc_basic,
null_model(mode = "regression") %>% set_engine("parsnip"),
x = hpc[, num_pred],
y = hpc$num_pending
)
Expand All @@ -77,7 +94,7 @@ test_that('nullmodel prediction', {
tolerance = .01)

res_form <- fit(
hpc_basic,
null_model(mode = "regression") %>% set_engine("parsnip"),
num_pending ~ log(compounds) + class,
data = hpc
)
Expand All @@ -87,7 +104,7 @@ test_that('nullmodel prediction', {

# Multivariate y
res <- fit(
hpc_basic,
null_model(mode = "regression") %>% set_engine("parsnip"),
cbind(gear, carb) ~ .,
data = mtcars
)
Expand Down