Skip to content

Commit

Permalink
Merge branch 'main' into fit-sparse-matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt authored Sep 24, 2024
2 parents daa4294 + acf34db commit 43926ca
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 24 deletions.
8 changes: 4 additions & 4 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ fit.workflow <- function(object, data, ..., control = control_workflow()) {
data <- sparsevctrs::coerce_to_sparse_tibble(data)
}

# If `potato` is not overwritten in the following `if` statement, then the
# If `calibration` is not overwritten in the following `if` statement, then the
# the postprocessor doesn't actually require training and the dataset
# passed to `.fit_post()` will have no effect.
potato <- data
calibration <- data
if (.should_inner_split(object)) {
inner_split <- make_inner_split(object, data)

data <- rsample::analysis(inner_split)
potato <- rsample::assessment(inner_split)
calibration <- rsample::assessment(inner_split)
}

workflow <- object
workflow <- .fit_pre(workflow, data)
workflow <- .fit_model(workflow, control)
if (has_postprocessor(workflow)) {
workflow <- .fit_post(workflow, potato)
workflow <- .fit_post(workflow, calibration)
}
workflow <- .fit_finalize(workflow)

Expand Down
6 changes: 3 additions & 3 deletions R/post-action-tailor.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
#' predictions. Predictions on data that a model was trained on likely follow
#' different distributions than predictions on unseen data; thus, workflows must
#' split up the supplied `data` into two training sets, where the first is used to
#' train the preprocessor and model and the second is passed to that trained
#' processor and model to generate predictions, which then form the training data
#' for the post-processor.
#' train the preprocessor and model and the second, called the "calibration set,"
#' is passed to that trained postprocessor and model to generate predictions,
#' which then form the training data for the postprocessor.
#'
#' The arguments `prop` and `method` parameterize how that data is split up.
#' `prop` determines the proportion of rows in `fit.workflow(data)` that are
Expand Down
11 changes: 2 additions & 9 deletions man/add_model.Rd

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

6 changes: 3 additions & 3 deletions man/add_tailor.Rd

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

4 changes: 2 additions & 2 deletions man/fit-workflow.Rd

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

4 changes: 2 additions & 2 deletions man/workflow.Rd

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

34 changes: 34 additions & 0 deletions tests/testthat/test-fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,40 @@ test_that(".should_inner_split works", {
))
})

test_that("`make_inner_split()` works", {
tlr <-
tailor::tailor() %>%
tailor::adjust_numeric_calibration() %>%
tailor::adjust_numeric_range(lower_limit = 1)

wflow <-
workflow() %>%
add_formula(mpg ~ .) %>%
add_model(parsnip::linear_reg()) %>%
add_tailor(tlr)

# defaults to 1/3 allotted to calibration via `mc_split`s
inner_splt <- make_inner_split(wflow, data.frame(x = 1:36))
expect_s3_class(inner_splt, c("mc_split_inner", "mc_split"))
expect_equal(nrow(rsample::analysis(inner_splt)), 24)
expect_equal(nrow(rsample::assessment(inner_splt)), 12)

# respects `add_tailor(prop)`
wflow <- wflow %>% remove_tailor() %>% add_tailor(tlr, prop = 1/2)
inner_splt <- make_inner_split(wflow, data.frame(x = 1:36))
expect_s3_class(inner_splt, c("mc_split_inner", "mc_split"))
expect_equal(nrow(rsample::analysis(inner_splt)), 18)
expect_equal(nrow(rsample::assessment(inner_splt)), 18)

# respects `add_tailor(method)`
skip("currently errors re: passing `prop` to `bootstraps()`")
wflow <- wflow %>% remove_tailor() %>% add_tailor(tlr, method = "boot_split")
inner_splt <- make_inner_split(wflow, data.frame(x = 1:36))
expect_s3_class(inner_splt, "boot_split")
expect_equal(nrow(rsample::analysis(inner_splt)), 18)
expect_equal(nrow(rsample::assessment(inner_splt)), 18)
})

# ------------------------------------------------------------------------------
# .fit_finalize()

Expand Down
48 changes: 47 additions & 1 deletion tests/testthat/test-post-action-tailor.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,53 @@ test_that("postprocessor fit aligns with manually fitted version (with calibrati
wf_post_fit <- fit(wflow_post, dat)

# ...verify predictions are the same as training the post-proc separately.
# note that this test naughtily re-predicts on the potato set.
# note that this test naughtily re-predicts on the calibration set.
wflow_simple_preds <- augment(wf_simple_fit, rsample::assessment(inner_split))
post_trained <- fit(post, wflow_simple_preds, y, .pred)
wflow_manual_preds <- predict(post_trained, wflow_simple_preds)

wflow_post_preds <- predict(wf_post_fit, rsample::assessment(inner_split))

expect_equal(wflow_manual_preds[".pred"], wflow_post_preds)
expect_false(all(wflow_simple_preds[".pred"] == wflow_manual_preds[".pred"]))
})

test_that("postprocessor fit uses correct data (with calibration, non-default `prop`)", {
skip_if_not_installed("modeldata")
skip_if_not_installed("rsample")

# create example data
y <- seq(0, 7, .1)
dat <- data.frame(y = y, x = y + (y-3)^2)

# construct workflows
post <- tailor::tailor()
post <- tailor::adjust_numeric_calibration(post, "linear")

wflow_simple <- workflow(y ~ ., parsnip::linear_reg())
wflow_post <- add_tailor(wflow_simple, post, prop = 1/2)

# train workflow

# first, separate out the same data that workflows ought to internally
# when training with a postprocessor that needs estimation
mocked_split <-
rsample::make_splits(
list(analysis = seq_len(nrow(dat)), assessment = integer()),
data = dat,
class = "mc_split"
)
set.seed(1)
inner_split <- rsample::inner_split(mocked_split, list(prop = 1/2))

wf_simple_fit <- fit(wflow_simple, rsample::analysis(inner_split))

# the following fit will do all of this internally
set.seed(1)
wf_post_fit <- fit(wflow_post, dat)

# ...verify predictions are the same as training the post-proc separately.
# note that this test naughtily re-predicts on the calibration set.
wflow_simple_preds <- augment(wf_simple_fit, rsample::assessment(inner_split))
post_trained <- fit(post, wflow_simple_preds, y, .pred)
wflow_manual_preds <- predict(post_trained, wflow_simple_preds)
Expand Down

0 comments on commit 43926ca

Please sign in to comment.