Skip to content

Commit 33fdfe9

Browse files
committed
fix: format
1 parent f3405be commit 33fdfe9

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

R/layer_yeo_johnson.R

+11-5
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,29 @@ slather.layer_epi_YeoJohnson <- function(object, components, workflow, new_data,
136136
# Wish I could suggest `all_outcomes()` here, but currently it's the same as
137137
# not specifying any terms. I don't want to spend time with dealing with
138138
# this case until someone asks for it.
139-
cli::cli_abort("Not specifying columns to layer Yeo-Johnson is not implemented.
139+
cli::cli_abort(
140+
"Not specifying columns to layer Yeo-Johnson is not implemented.
140141
If you had a single outcome, you can use `.pred` as a column name.
141142
If you had multiple outcomes, you'll need to specify them like
142143
`.pred_ahead_1_<outcome_col>`, `.pred_ahead_7_<outcome_col>`, etc.
143-
", call = rlang::caller_env())
144+
",
145+
call = rlang::caller_env()
146+
)
144147
} else {
145148
# In this case, we assume that the user has specified the columns they want
146149
# transformed here. We then need to determine the lambda columns for each of
147150
# these columns. That is, we need to convert a vector of column names like
148151
# c(".pred_ahead_1_case_rate", ".pred_ahead_7_case_rate") to
149152
# c("lambda_ahead_1_case_rate", "lambda_ahead_7_case_rate").
150153
original_outcome_cols <- stringr::str_match(col_names, ".pred_ahead_\\d+_(.*)")[, 2]
151-
outcomes_wout_ahead <- stringr::str_match(names(components$mold$outcomes), "ahead_\\d+_(.*)")[,2]
154+
outcomes_wout_ahead <- stringr::str_match(names(components$mold$outcomes), "ahead_\\d+_(.*)")[, 2]
152155
if (any(original_outcome_cols %nin% outcomes_wout_ahead)) {
153-
cli_abort("All columns specified in `...` must be outcome columns.
156+
cli_abort(
157+
"All columns specified in `...` must be outcome columns.
154158
They must be of the form `.pred_ahead_1_<outcome_col>`, `.pred_ahead_7_<outcome_col>`, etc.
155-
", call = rlang::caller_env())
159+
",
160+
call = rlang::caller_env()
161+
)
156162
}
157163

158164
for (i in seq_along(col_names)) {

R/step_yeo_johnson.R

+12-10
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ bake.step_epi_YeoJohnson <- function(object, new_data, ...) {
189189
}
190190
# Check that the keys match.
191191
keys <- key_colnames(new_data, exclude = "time_value")
192-
old_keys <- object$lambdas %>% select(-starts_with(".lambda_")) %>% colnames()
192+
old_keys <- object$lambdas %>%
193+
select(-starts_with(".lambda_")) %>%
194+
colnames()
193195
if (!all(keys %in% old_keys)) {
194196
cli::cli_abort(
195197
"The keys of the new data do not match the keys of the training data.",
@@ -348,15 +350,15 @@ estimate_yj <- function(dat, limits = c(-5, 5), num_unique = 5, na_rm = TRUE, ca
348350
const <- sum(sign(dat) * log(abs(dat) + 1))
349351

350352
suppressWarnings(
351-
res <- optimize(
352-
yj_obj,
353-
interval = limits,
354-
maximum = TRUE,
355-
dat = dat,
356-
ind_neg = ind_neg,
357-
const = const,
358-
tol = .0001
359-
)
353+
res <- optimize(
354+
yj_obj,
355+
interval = limits,
356+
maximum = TRUE,
357+
dat = dat,
358+
ind_neg = ind_neg,
359+
const = const,
360+
tol = .0001
361+
)
360362
)
361363
lam <- res$maximum
362364
if (abs(limits[1] - lam) <= eps | abs(limits[2] - lam) <= eps) {

tests/testthat/test-yeo-johnson.R

+15-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ test_that("Yeo-Johnson steps and layers invert each other", {
3939
wf <- epi_workflow(r, linear_reg()) %>%
4040
fit(filtered_data) %>%
4141
add_frosting(f)
42-
out1 <- filtered_data %>% as_tibble() %>% slice_max(time_value, by = geo_value)
42+
out1 <- filtered_data %>%
43+
as_tibble() %>%
44+
slice_max(time_value, by = geo_value)
4345
out2 <- forecast(wf) %>% rename(cases = .pred)
4446
expect_equal(out1, out2)
4547

@@ -68,7 +70,9 @@ test_that("Yeo-Johnson steps and layers invert each other", {
6870
wf <- epi_workflow(r, linear_reg()) %>%
6971
fit(filtered_data) %>%
7072
add_frosting(f)
71-
out1 <- filtered_data %>% as_tibble() %>% slice_max(time_value, by = geo_value)
73+
out1 <- filtered_data %>%
74+
as_tibble() %>%
75+
slice_max(time_value, by = geo_value)
7276
# debugonce(slather.layer_epi_YeoJohnson)
7377
out2 <- forecast(wf) %>% rename(case_rate = .pred_ahead_0_case_rate, death_rate = .pred_ahead_0_death_rate)
7478
expect_equal(out1, out2)
@@ -124,7 +128,14 @@ test_that("Yeo-Johnson steps and layers invert each other when other_keys are pr
124128
wf <- epi_workflow(r, linear_reg()) %>%
125129
fit(filtered_data) %>%
126130
add_frosting(f)
127-
out1 <- filtered_data %>% as_tibble() %>% slice_max(time_value, by = geo_value) %>% select(geo_value, age_group, time_value, med_income_2y) %>% arrange(geo_value, age_group, time_value)
128-
out2 <- forecast(wf) %>% rename(med_income_2y = .pred) %>% select(geo_value, age_group, time_value, med_income_2y) %>% arrange(geo_value, age_group, time_value)
131+
out1 <- filtered_data %>%
132+
as_tibble() %>%
133+
slice_max(time_value, by = geo_value) %>%
134+
select(geo_value, age_group, time_value, med_income_2y) %>%
135+
arrange(geo_value, age_group, time_value)
136+
out2 <- forecast(wf) %>%
137+
rename(med_income_2y = .pred) %>%
138+
select(geo_value, age_group, time_value, med_income_2y) %>%
139+
arrange(geo_value, age_group, time_value)
129140
expect_equal(out1, out2)
130141
})

0 commit comments

Comments
 (0)