Skip to content

add an eval_time argument to augment() #200

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 5 commits into from
May 5, 2023
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: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Imports:
hardhat (>= 1.2.0),
lifecycle (>= 1.0.3),
modelenv (>= 0.1.0),
parsnip (>= 1.0.3),
parsnip (>= 1.1.0.9001),
rlang (>= 1.0.3),
tidyselect (>= 1.2.0),
vctrs (>= 0.4.1)
Expand All @@ -46,6 +46,8 @@ Config/Needs/website:
tidyr,
tidyverse/tidytemplate,
yardstick
Remotes:
tidymodels/parsnip
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

* Each of the `pull_*()` functions soft-deprecated in workflows v0.2.3 now warn on every usage.

* `augment.workflow()` gained an `eval_time` argument, enabling augmenting
censored regression models (#200).

* The prediction columns are now appended to the LHS rather than RHS of
`new_data` in `augment.workflow()`, following analogous changes in parsnip (#200).

# workflows 1.1.3

* The workflows methods for `generics::tune_args()` and `generics::tunable()`
Expand Down
10 changes: 7 additions & 3 deletions R/broom.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ glance.workflow <- function(x, ...) {
#'
#' @return `new_data` with new prediction specific columns.
#'
#' @param eval_time For censored regression models, a vector of time points at
#' which the survival probability is estimated. See
#' [parsnip::augment.model_fit()] for more details.
#'
#' @export
#' @examples
#' if (rlang::is_installed("broom")) {
Expand All @@ -133,13 +137,13 @@ glance.workflow <- function(x, ...) {
#' augment(wf_fit, attrition)
#'
#' }
augment.workflow <- function(x, new_data, ...) {
augment.workflow <- function(x, new_data, eval_time = NULL, ...) {
fit <- extract_fit_parsnip(x)

# `augment.model_fit()` requires the pre-processed `new_data`
predictors <- forge_predictors(new_data, x)
predictors <- prepare_augment_predictors(predictors)
predictors_and_predictions <- augment(fit, predictors, ...)
predictors_and_predictions <- augment(fit, predictors, eval_time = eval_time, ...)

prediction_columns <- setdiff(
names(predictors_and_predictions),
Expand All @@ -149,7 +153,7 @@ augment.workflow <- function(x, new_data, ...) {
predictions <- predictors_and_predictions[prediction_columns]

# Return original `new_data` with new prediction columns
out <- vctrs::vec_cbind(new_data, predictions)
out <- vctrs::vec_cbind(predictions, new_data)

out
}
Expand Down
6 changes: 5 additions & 1 deletion man/augment.workflow.Rd

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

2 changes: 1 addition & 1 deletion man/predict-workflow.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/test-broom.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ test_that("can augment using a fitted workflow's model", {

# at least 1 prediction specific column should be added
expect_true(ncol(x) > ncol(df))

expect_named(x, c(".pred", "y", "x"))
})

test_that("augment returns `new_data`, not the pre-processed version of `new_data`", {
Expand Down