Skip to content

Commit 5aa7828

Browse files
authored
Merge pull request #328 from cmu-delphi/325-workflow-adj
325 workflow adj
2 parents 0c3236c + f043918 commit 5aa7828

File tree

6 files changed

+217
-149
lines changed

6 files changed

+217
-149
lines changed

NAMESPACE

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(Add_model,epi_workflow)
4+
S3method(Add_model,workflow)
35
S3method(Math,dist_quantiles)
46
S3method(Ops,dist_quantiles)
5-
S3method(add_model,epi_workflow)
7+
S3method(Remove_model,epi_workflow)
8+
S3method(Remove_model,workflow)
9+
S3method(Update_model,epi_workflow)
10+
S3method(Update_model,workflow)
611
S3method(adjust_epi_recipe,epi_recipe)
712
S3method(adjust_epi_recipe,epi_workflow)
813
S3method(adjust_frosting,epi_workflow)
@@ -92,7 +97,6 @@ S3method(print,step_population_scaling)
9297
S3method(print,step_training_window)
9398
S3method(quantile,dist_quantiles)
9499
S3method(refresh_blueprint,default_epi_recipe_blueprint)
95-
S3method(remove_model,epi_workflow)
96100
S3method(residuals,flatline)
97101
S3method(run_mold,default_epi_recipe_blueprint)
98102
S3method(slather,layer_add_forecast_date)
@@ -115,10 +119,12 @@ S3method(tidy,check_enough_train_data)
115119
S3method(tidy,frosting)
116120
S3method(tidy,layer)
117121
S3method(update,layer)
118-
S3method(update_model,epi_workflow)
119122
S3method(vec_ptype_abbr,dist_quantiles)
120123
S3method(vec_ptype_full,dist_quantiles)
121124
export("%>%")
125+
export(Add_model)
126+
export(Remove_model)
127+
export(Update_model)
122128
export(add_epi_recipe)
123129
export(add_frosting)
124130
export(add_layer)

R/epi_workflow.R

-101
Original file line numberDiff line numberDiff line change
@@ -59,107 +59,6 @@ is_epi_workflow <- function(x) {
5959
}
6060

6161

62-
#' Add a model to an `epi_workflow`
63-
#'
64-
#' @seealso [workflows::add_model()]
65-
#' - `add_model()` adds a parsnip model to the `epi_workflow`.
66-
#'
67-
#' - `remove_model()` removes the model specification as well as any fitted
68-
#' model object. Any extra formulas are also removed.
69-
#'
70-
#' - `update_model()` first removes the model then adds the new
71-
#' specification to the workflow.
72-
#'
73-
#' @details
74-
#' Has the same behaviour as [workflows::add_model()] but also ensures
75-
#' that the returned object is an `epi_workflow`.
76-
#'
77-
#' @inheritParams workflows::add_model
78-
#'
79-
#' @param x An `epi_workflow`.
80-
#'
81-
#' @param spec A parsnip model specification.
82-
#'
83-
#' @param ... Not used.
84-
#'
85-
#' @return
86-
#' `x`, updated with a new, updated, or removed model.
87-
#'
88-
#' @export
89-
#' @examples
90-
#' jhu <- case_death_rate_subset %>%
91-
#' dplyr::filter(
92-
#' time_value > "2021-11-01",
93-
#' geo_value %in% c("ak", "ca", "ny")
94-
#' )
95-
#'
96-
#' r <- epi_recipe(jhu) %>%
97-
#' step_epi_lag(death_rate, lag = c(0, 7, 14)) %>%
98-
#' step_epi_ahead(death_rate, ahead = 7)
99-
#'
100-
#' rf_model <- rand_forest(mode = "regression")
101-
#'
102-
#' wf <- epi_workflow(r)
103-
#'
104-
#' wf <- wf %>% add_model(rf_model)
105-
#' wf
106-
#'
107-
#' lm_model <- parsnip::linear_reg()
108-
#'
109-
#' wf <- update_model(wf, lm_model)
110-
#' wf
111-
#'
112-
#' wf <- remove_model(wf)
113-
#' wf
114-
#' @export
115-
add_model <- function(x, spec, ..., formula = NULL) {
116-
UseMethod("add_model")
117-
}
118-
119-
#' @rdname add_model
120-
#' @export
121-
remove_model <- function(x) {
122-
UseMethod("remove_model")
123-
}
124-
125-
#' @rdname add_model
126-
#' @export
127-
update_model <- function(x, spec, ..., formula = NULL) {
128-
UseMethod("update_model")
129-
}
130-
131-
#' @rdname add_model
132-
#' @export
133-
add_model.epi_workflow <- function(x, spec, ..., formula = NULL) {
134-
workflows::add_model(x, spec, ..., formula = formula)
135-
}
136-
137-
#' @rdname add_model
138-
#' @export
139-
remove_model.epi_workflow <- function(x) {
140-
workflows:::validate_is_workflow(x)
141-
142-
if (!workflows:::has_spec(x)) {
143-
rlang::warn("The workflow has no model to remove.")
144-
}
145-
146-
new_epi_workflow(
147-
pre = x$pre,
148-
fit = workflows:::new_stage_fit(),
149-
post = x$post,
150-
trained = FALSE
151-
)
152-
}
153-
154-
#' @rdname add_model
155-
#' @export
156-
update_model.epi_workflow <- function(x, spec, ..., formula = NULL) {
157-
rlang::check_dots_empty()
158-
x <- remove_model(x)
159-
workflows::add_model(x, spec, ..., formula = formula)
160-
}
161-
162-
16362
#' Fit an `epi_workflow` object
16463
#'
16564
#' @description

R/model-methods.R

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#' Add a model to an `epi_workflow`
2+
#'
3+
#' @seealso [workflows::add_model()]
4+
#' - `Add_model()` adds a parsnip model to the `epi_workflow`.
5+
#'
6+
#' - `Remove_model()` removes the model specification as well as any fitted
7+
#' model object. Any extra formulas are also removed.
8+
#'
9+
#' - `Update_model()` first removes the model then adds the new
10+
#' specification to the workflow.
11+
#'
12+
#' @details
13+
#' Has the same behaviour as [workflows::add_model()] but also ensures
14+
#' that the returned object is an `epi_workflow`.
15+
#'
16+
#' This family is called `Add_*` / `Update_*` / `Remove_*` to avoid
17+
#' masking the related functions in `{workflows}`. We also provide
18+
#' aliases with the lower-case names. However, in the event that
19+
#' `{workflows}` is loaded after `{epipredict}`, these may fail to function
20+
#' properly.
21+
#'
22+
#' @inheritParams workflows::add_model
23+
#'
24+
#' @param x An `epi_workflow`.
25+
#'
26+
#' @param spec A parsnip model specification.
27+
#'
28+
#' @param ... Not used.
29+
#'
30+
#' @return
31+
#' `x`, updated with a new, updated, or removed model.
32+
#'
33+
#' @export
34+
#' @examples
35+
#' jhu <- case_death_rate_subset %>%
36+
#' dplyr::filter(
37+
#' time_value > "2021-11-01",
38+
#' geo_value %in% c("ak", "ca", "ny")
39+
#' )
40+
#'
41+
#' r <- epi_recipe(jhu) %>%
42+
#' step_epi_lag(death_rate, lag = c(0, 7, 14)) %>%
43+
#' step_epi_ahead(death_rate, ahead = 7)
44+
#'
45+
#' rf_model <- rand_forest(mode = "regression")
46+
#'
47+
#' wf <- epi_workflow(r)
48+
#'
49+
#' wf <- wf %>% Add_model(rf_model)
50+
#' wf
51+
#'
52+
#' lm_model <- parsnip::linear_reg()
53+
#'
54+
#' wf <- Update_model(wf, lm_model)
55+
#' wf
56+
#'
57+
#' wf <- Remove_model(wf)
58+
#' wf
59+
#' @export
60+
Add_model <- function(x, spec, ..., formula = NULL) {
61+
UseMethod("Add_model")
62+
}
63+
64+
#' @rdname Add_model
65+
#' @export
66+
Remove_model <- function(x) {
67+
UseMethod("Remove_model")
68+
}
69+
70+
#' @rdname Add_model
71+
#' @export
72+
Update_model <- function(x, spec, ..., formula = NULL) {
73+
UseMethod("Update_model")
74+
}
75+
76+
#' @rdname Add_model
77+
#' @export
78+
Add_model.epi_workflow <- function(x, spec, ..., formula = NULL) {
79+
workflows::add_model(x, spec, ..., formula = formula)
80+
}
81+
82+
#' @rdname Add_model
83+
#' @export
84+
Remove_model.epi_workflow <- function(x) {
85+
workflows:::validate_is_workflow(x)
86+
87+
if (!workflows:::has_spec(x)) {
88+
rlang::warn("The workflow has no model to remove.")
89+
}
90+
91+
new_epi_workflow(
92+
pre = x$pre,
93+
fit = workflows:::new_stage_fit(),
94+
post = x$post,
95+
trained = FALSE
96+
)
97+
}
98+
99+
#' @rdname Add_model
100+
#' @export
101+
Update_model.epi_workflow <- function(x, spec, ..., formula = NULL) {
102+
rlang::check_dots_empty()
103+
x <- Remove_model(x)
104+
Add_model(x, spec, ..., formula = formula)
105+
}
106+
107+
108+
#' @rdname Add_model
109+
#' @export
110+
Add_model.workflow <- workflows::add_model
111+
112+
#' @rdname Add_model
113+
#' @export
114+
Remove_model.workflow <- workflows::remove_model
115+
116+
#' @rdname Add_model
117+
#' @export
118+
Update_model.workflow <- workflows::update_model
119+
120+
121+
# Aliases -----------------------------------------------------------------
122+
123+
#' @rdname Add_model
124+
#' @export
125+
add_model <- Add_model
126+
127+
#' @rdname Add_model
128+
#' @export
129+
remove_model <- Remove_model
130+
131+
#' @rdname Add_model
132+
#' @export
133+
update_model <- Update_model

_pkgdown.yml

+23-22
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ navbar:
2222
type: light
2323

2424
articles:
25-
- title: Get started
26-
navbar: ~
27-
contents:
28-
- epipredict
29-
- preprocessing-and-models
30-
- arx-classifier
31-
- articles/update
25+
- title: Get started
26+
navbar: ~
27+
contents:
28+
- epipredict
29+
- preprocessing-and-models
30+
- arx-classifier
31+
- update
3232

33-
- title: Advanced methods
34-
contents:
35-
- articles/sliding
36-
- articles/smooth-qr
37-
- articles/symptom-surveys
38-
- panel-data
33+
- title: Advanced methods
34+
contents:
35+
- articles/sliding
36+
- articles/smooth-qr
37+
- articles/symptom-surveys
38+
- panel-data
3939

4040
repo:
4141
url:
@@ -78,15 +78,16 @@ reference:
7878
- smooth_quantile_reg
7979
- title: Custom panel data forecasting workflows
8080
contents:
81-
- epi_recipe
82-
- epi_workflow
83-
- add_epi_recipe
84-
- adjust_epi_recipe
85-
- add_model
86-
- predict.epi_workflow
87-
- fit.epi_workflow
88-
- augment.epi_workflow
89-
- forecast.epi_workflow
81+
- epi_recipe
82+
- epi_workflow
83+
- add_epi_recipe
84+
- adjust_epi_recipe
85+
- Add_model
86+
- predict.epi_workflow
87+
- fit.epi_workflow
88+
- augment.epi_workflow
89+
- forecast.epi_workflow
90+
9091
- title: Epi recipe preprocessing steps
9192
contents:
9293
- starts_with("step_")

0 commit comments

Comments
 (0)