From 3d4ec19cfdad739413af1e69bc18514537621f67 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 20:54:15 -0400 Subject: [PATCH 01/16] Minor refactoring. --- DESCRIPTION | 1 - R/dev-xregs.R | 16 ++++------------ R/modeltime-forecast.R | 3 +-- R/modeltime-residuals.R | 2 +- R/nested-modeltime_data_prep.R | 3 +-- R/zzz.R | 2 +- man/prep_nested.Rd | 3 +-- tests/testthat/test-conf_by_id.R | 3 +-- tests/testthat/test-modeltime_residuals.R | 3 +-- vignettes/modeling-panel-data.Rmd | 3 +-- vignettes/modeltime-conformal-prediction.Rmd | 2 +- vignettes/modeltime-spark.Rmd | 8 +++----- vignettes/nested-forecasting.Rmd | 5 ++--- 13 files changed, 18 insertions(+), 36 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e90c393c..7c402739 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -79,4 +79,3 @@ Suggests: Roxygen: list(markdown = TRUE) VignetteBuilder: knitr RoxygenNote: 7.2.3 - diff --git a/R/dev-xregs.R b/R/dev-xregs.R index 1eed6b85..2b218a3f 100644 --- a/R/dev-xregs.R +++ b/R/dev-xregs.R @@ -210,9 +210,7 @@ prepare_xreg_recipe_from_predictors <- function(data, prepare = TRUE, } # Convert any ordered factors to factors - names_ordered <- data_copy %>% - dplyr::select_if(is.ordered) %>% - names() + names_ordered <- names(data_copy)[purrr::map_lgl(data_copy, is.ordered)] if (length(names_ordered) > 0) { recipe_spec <- recipe_spec %>% @@ -221,13 +219,9 @@ prepare_xreg_recipe_from_predictors <- function(data, prepare = TRUE, } # Convert factors to dummies - names_factor <- data_copy %>% - dplyr::select_if(is.factor)%>% - names() + names_factor <- names(data_copy)[purrr::map_lgl(data_copy, is.factor)] - names_character <- data_copy %>% - dplyr::select_if(is.character)%>% - names() + names_character <- names(data_copy)[purrr::map_lgl(data_copy, is.character)] if (length(c(names_factor, names_character)) > 0 && dummy_encode) { recipe_spec <- recipe_spec %>% @@ -235,9 +229,7 @@ prepare_xreg_recipe_from_predictors <- function(data, prepare = TRUE, } # Drop any date features - names_date <- data_copy %>% - dplyr::select_if(timetk::is_date_class) %>% - names() + names_date <- names(data_copy)[purrr::map_lgl(data_copy, timetk::is_date_class)] if (length(c(names_date)) > 0) { recipe_spec <- recipe_spec %>% diff --git a/R/modeltime-forecast.R b/R/modeltime-forecast.R index 7272959b..6c73862f 100644 --- a/R/modeltime-forecast.R +++ b/R/modeltime-forecast.R @@ -1173,8 +1173,7 @@ mdl_time_forecast.workflow <- function(object, calibration_data, new_data = NULL } ret <- data_formatted %>% - dplyr::rename(.value = .pred) %>% - dplyr::select(.key, .index, .value) %>% + dplyr::select(.key, .index, .value = .pred) %>% dplyr::mutate(.key = factor(.key, levels = c("actual", "prediction"))) # Keep Data diff --git a/R/modeltime-residuals.R b/R/modeltime-residuals.R index c01f5930..5e6ed5cf 100644 --- a/R/modeltime-residuals.R +++ b/R/modeltime-residuals.R @@ -90,7 +90,7 @@ modeltime_residuals.mdl_time_tbl <- function(object, new_data = NULL, quiet = TR # Residuals Extraction ---- ret <- data %>% dplyr::ungroup() %>% - dplyr::select(-.model) %>% + dplyr::select(-".model") %>% tidyr::unnest(.calibration_data) %>% dplyr::rename(.index = 4) diff --git a/R/nested-modeltime_data_prep.R b/R/nested-modeltime_data_prep.R index 42621784..5d26abcf 100644 --- a/R/nested-modeltime_data_prep.R +++ b/R/nested-modeltime_data_prep.R @@ -75,8 +75,7 @@ #' #' #' nested_data_tbl <- walmart_sales_weekly %>% -#' select(id, Date, Weekly_Sales) %>% -#' set_names(c("id", "date", "value")) %>% +#' select(id, date = Date, value = Weekly_Sales) #' #' # Step 1: Extends the time series by id #' extend_timeseries( diff --git a/R/zzz.R b/R/zzz.R index b194dc59..e7b1ede1 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -2,7 +2,7 @@ # StanHeaders - Used to prevent issues with Prophet dynload error #' @import StanHeaders - +NULL # ON LOAD ---- diff --git a/man/prep_nested.Rd b/man/prep_nested.Rd index 8a105617..970d30fd 100644 --- a/man/prep_nested.Rd +++ b/man/prep_nested.Rd @@ -97,8 +97,7 @@ library(timetk) nested_data_tbl <- walmart_sales_weekly \%>\% - select(id, Date, Weekly_Sales) \%>\% - set_names(c("id", "date", "value")) \%>\% + select(id, date = Date, value = Weekly_Sales) # Step 1: Extends the time series by id extend_timeseries( diff --git a/tests/testthat/test-conf_by_id.R b/tests/testthat/test-conf_by_id.R index 73ac4195..0f19ad6a 100644 --- a/tests/testthat/test-conf_by_id.R +++ b/tests/testthat/test-conf_by_id.R @@ -11,8 +11,7 @@ test_that("Confidence and Accuracy by ID", { # Data data <- walmart_sales_weekly %>% - select(id, Date, Weekly_Sales) %>% - set_names(c("ID", "date", "value")) + select(ID = id, date = Date, value = Weekly_Sales) splits <- data %>% time_series_split(assess = "3 months", cumulative = TRUE) diff --git a/tests/testthat/test-modeltime_residuals.R b/tests/testthat/test-modeltime_residuals.R index bc054ea2..0667e05d 100644 --- a/tests/testthat/test-modeltime_residuals.R +++ b/tests/testthat/test-modeltime_residuals.R @@ -11,8 +11,7 @@ test_that("modeltime_residuals(): Returns correct order", { data <- walmart_sales_weekly %>% - select(id, Date, Weekly_Sales) %>% - set_names(c("ID", "date", "value")) + select(ID = id, date = Date, value = Weekly_Sales) splits <- data %>% time_series_split(assess = "3 months", cumulative = TRUE) diff --git a/vignettes/modeling-panel-data.Rmd b/vignettes/modeling-panel-data.Rmd index 4cee7d54..1eb29fc8 100644 --- a/vignettes/modeling-panel-data.Rmd +++ b/vignettes/modeling-panel-data.Rmd @@ -103,8 +103,7 @@ Next, let's use the `walmart_sales_weekly` dataset that comes with `timetk`. ```{r} data_tbl <- walmart_sales_weekly %>% - select(id, Date, Weekly_Sales) %>% - set_names(c("id", "date", "value")) + select(id, date = Date, value = Weekly_Sales) data_tbl ``` diff --git a/vignettes/modeltime-conformal-prediction.Rmd b/vignettes/modeltime-conformal-prediction.Rmd index c0f2e18e..74274325 100644 --- a/vignettes/modeltime-conformal-prediction.Rmd +++ b/vignettes/modeltime-conformal-prediction.Rmd @@ -60,7 +60,7 @@ We'll start with the Walmart Sales data set from `timetk`. walmart_sales_tbl <- timetk::walmart_sales_weekly %>% select(id, Date, Weekly_Sales) %>% - mutate(id = as_factor(id)) + mutate(id = forcats::as_factor(id)) ``` We can visualize the data set. diff --git a/vignettes/modeltime-spark.Rmd b/vignettes/modeltime-spark.Rmd index 2ab49e54..be5713fc 100644 --- a/vignettes/modeltime-spark.Rmd +++ b/vignettes/modeltime-spark.Rmd @@ -88,18 +88,16 @@ The dataset we'll be forecasting is the `walmart_sales_weekly`, which we modify ```{r} walmart_sales_weekly %>% - select(id, Date, Weekly_Sales) %>% - set_names(c("id", "date", "value")) %>% + select(id, date = Date, value = Weekly_Sales) %>% group_by(id) %>% - plot_time_series(date, value, .facet_ncol = 2, .interactive = F) + plot_time_series(date, value, .facet_ncol = 2, .interactive = FALSE) ``` We prepare as nested data using the Nested Forecasting preparation functions. ```{r} nested_data_tbl <- walmart_sales_weekly %>% - select(id, Date, Weekly_Sales) %>% - set_names(c("id", "date", "value")) %>% + select(id, date = Date, value = Weekly_Sales) %>% extend_timeseries( .id_var = id, .date_var = date, diff --git a/vignettes/nested-forecasting.Rmd b/vignettes/nested-forecasting.Rmd index cb62c18a..21486359 100644 --- a/vignettes/nested-forecasting.Rmd +++ b/vignettes/nested-forecasting.Rmd @@ -77,8 +77,7 @@ Next, let's use the `walmart_sales_weekly` dataset that comes with `timetk`. ```{r} data_tbl <- walmart_sales_weekly %>% - select(id, Date, Weekly_Sales) %>% - set_names(c("id", "date", "value")) + select(id, date = Date, value = Weekly_Sales) data_tbl ``` @@ -97,7 +96,7 @@ We can visualize this by time series group to expose the differences in sales by data_tbl %>% group_by(id) %>% plot_time_series( - date, value, .interactive = F, .facet_ncol = 2 + date, value, .interactive = FALSE, .facet_ncol = 2 ) ``` From 743e3641eb04b8430792955ae05c507d34135cdc Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 20:55:48 -0400 Subject: [PATCH 02/16] More function :: in tests. --- tests/testthat/test-algo-window_reg.R | 2 +- tests/testthat/test-conf_by_id.R | 4 +- tests/testthat/test-panel-data.R | 8 ++-- tests/testthat/test-recursive-chunk-uneven.R | 43 ++++++++++---------- tests/testthat/test-recursive-chunk.R | 43 ++++++++++---------- tests/testthat/test-recursive.R | 16 ++++---- 6 files changed, 57 insertions(+), 59 deletions(-) diff --git a/tests/testthat/test-algo-window_reg.R b/tests/testthat/test-algo-window_reg.R index c7cfc9b1..50eea66f 100644 --- a/tests/testthat/test-algo-window_reg.R +++ b/tests/testthat/test-algo-window_reg.R @@ -8,7 +8,7 @@ splits <- rsample::initial_time_split(m750, prop = 0.8) # Data - Multiple Time Series (Panel) full_data_tbl <- timetk::m4_monthly %>% dplyr::group_by(id) %>% - future_frame(date, .length_out = 60, .bind_data = TRUE) %>% + timetk::future_frame(date, .length_out = 60, .bind_data = TRUE) %>% dplyr::ungroup() future_tbl <- full_data_tbl %>% dplyr::filter(is.na(value)) diff --git a/tests/testthat/test-conf_by_id.R b/tests/testthat/test-conf_by_id.R index 0f19ad6a..4d191c83 100644 --- a/tests/testthat/test-conf_by_id.R +++ b/tests/testthat/test-conf_by_id.R @@ -38,7 +38,7 @@ test_that("Confidence and Accuracy by ID", { # CALIBRATION BY ID ---- - test_data <- rsample::testing(splits) %>% arrange(ID, date) + test_data <- rsample::testing(splits) %>% dplyr::arrange(ID, date) calib_tbl <- model_tbl %>% modeltime_calibrate(new_data = test_data, id = "ID") @@ -49,7 +49,7 @@ test_that("Confidence and Accuracy by ID", { expect_equal(names(df)[5], "ID") expect_equal( - df %>% select(ID, date, .actual) %>% rename(value = .actual), + df %>% dplyr::select(ID, date, value = .actual), test_data ) diff --git a/tests/testthat/test-panel-data.R b/tests/testthat/test-panel-data.R index e4d3aeb6..3d7b3b9d 100644 --- a/tests/testthat/test-panel-data.R +++ b/tests/testthat/test-panel-data.R @@ -10,7 +10,7 @@ test_that("Panel Data - Forecast Jumbled", { # m4_monthly_jumbled <- timetk::m4_monthly %>% - arrange(desc(date)) + dplyr::arrange(dplyr::desc(date)) data_set <- m4_monthly_jumbled @@ -23,9 +23,9 @@ test_that("Panel Data - Forecast Jumbled", { wflw_fit_prophet <- workflows::workflow() %>% workflows::add_model( prophet_boost( - seasonality_yearly = F, - seasonality_weekly = F, - seasonality_daily = F + seasonality_yearly = FALSE, + seasonality_weekly = FALSE, + seasonality_daily = FALSE ) %>% parsnip::set_engine( "prophet_xgboost" diff --git a/tests/testthat/test-recursive-chunk-uneven.R b/tests/testthat/test-recursive-chunk-uneven.R index 75206c3a..b7172314 100644 --- a/tests/testthat/test-recursive-chunk-uneven.R +++ b/tests/testthat/test-recursive-chunk-uneven.R @@ -164,8 +164,8 @@ test_that("Chunked Recursive Tests - Uneven ", { # * Modeltime Refit ---- - retrain_tbl <- train_data %>% slice(1:200) - future_tbl <- train_data %>% slice(201:224) + retrain_tbl <- train_data %>% dplyr::slice_head(n = 200) + future_tbl <- train_data %>% dplyr::slice(201:224) # wflw_fit_lm_recursive %>% mdl_time_refit(retrain_tbl) @@ -200,34 +200,34 @@ test_that("Chunked Recursive Tests - Uneven ", { # Jumble the data to make sure it forecasts properly m4_monthly_updated <- timetk::m4_monthly %>% - arrange(desc(id), date) %>% - mutate(id = as_factor(as.character(id))) + dplyr::arrange(desc(id), date) %>% + dplyr::mutate(id = forcats::as_factor(as.character(id))) m4_extended <- m4_monthly_updated %>% - group_by(id) %>% - future_frame( + dplyr::group_by(id) %>% + timetk::future_frame( .length_out = FORECAST_HORIZON, .bind_data = TRUE ) %>% - ungroup() + dplyr::ungroup() # Transformation Function lag_transformer_grouped <- function(data){ data %>% - group_by(id) %>% + dplyr::group_by(id) %>% # Lags tk_augment_lags(value, .lags = c(3,6,9,12)) %>% - ungroup() + dplyr::ungroup() } m4_lags <- m4_extended %>% lag_transformer_grouped() train_data <- m4_lags %>% - drop_na() + tidyr::drop_na() future_data <- m4_lags %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- @@ -525,34 +525,33 @@ test_that("Chunked Recursive Tests - Single ", { # Jumble the data to make sure it forecasts properly m4_monthly_updated <- timetk::m4_monthly %>% - arrange(desc(id), date) %>% - mutate(id = as_factor(as.character(id))) + dplyr::arrange(desc(id), date) %>% + dplyr::mutate(id = forcats::as_factor(as.character(id))) m4_extended <- m4_monthly_updated %>% - group_by(id) %>% - future_frame( + dplyr::group_by(id) %>% + timetk::future_frame( .length_out = FORECAST_HORIZON, .bind_data = TRUE ) %>% - ungroup() + dplyr::ungroup() # Transformation Function lag_transformer_grouped <- function(data){ data %>% - group_by(id) %>% + dplyr::group_by(id) %>% # Lags - tk_augment_lags(value, .lags = c(3,6,9,12)) %>% - ungroup() + timetk::tk_augment_lags(value, .lags = c(3,6,9,12)) %>% + dplyr::ungroup() } m4_lags <- m4_extended %>% lag_transformer_grouped() - train_data <- m4_lags %>% - drop_na() + train_data <- tidyr::drop_na(m4_lags) future_data <- m4_lags %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- diff --git a/tests/testthat/test-recursive-chunk.R b/tests/testthat/test-recursive-chunk.R index 7d742515..d86f9ec9 100644 --- a/tests/testthat/test-recursive-chunk.R +++ b/tests/testthat/test-recursive-chunk.R @@ -11,27 +11,27 @@ test_that("Chunked Recursive Tests ", { FORECAST_HORIZON <- 24 m750_extended <- m750 %>% - group_by(id) %>% - future_frame( + dplyr::group_by(id) %>% + timetk::future_frame( .length_out = FORECAST_HORIZON, .bind_data = TRUE ) %>% - ungroup() + dplyr::ungroup() # recursive 1 - single / recipe / parsnip ---- # Lag Recipe recipe_lag <- recipes::recipe(value ~ date, m750_extended) %>% - step_lag(value, lag = c(3,6,9,12)) + recipes::step_lag(value, lag = c(3,6,9,12)) # Data Transformation - m750_lagged <- recipe_lag %>% prep() %>% juice() + m750_lagged <- recipe_lag %>% recipes::prep() %>% recipes::juice() train_data <- m750_lagged %>% - drop_na() + tidyr::drop_na() future_data <- m750_lagged %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- @@ -113,19 +113,18 @@ test_that("Chunked Recursive Tests ", { lag_transformer <- function(data){ data %>% # Lags - tk_augment_lags(value, .lags = c(3,6,9,12)) + timetk::tk_augment_lags(value, .lags = c(3,6,9,12)) } # Data Preparation m750_lagged <- m750_extended %>% lag_transformer() %>% - select(-id) + dplyr::select(-id) - train_data <- m750_lagged %>% - drop_na() + train_data <- tidyr::drop_na(m750_lagged) future_data <- m750_lagged %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- wflw_fit_lm <- workflows::workflow() %>% @@ -169,8 +168,8 @@ test_that("Chunked Recursive Tests ", { # * Modeltime Refit ---- - retrain_tbl <- train_data %>% slice(1:200) - future_tbl <- train_data %>% slice(201:224) + retrain_tbl <- train_data %>% dplyr::slice_head(n = 200) + future_tbl <- train_data %>% dplyr::slice(201:224) # wflw_fit_lm_recursive %>% mdl_time_refit(retrain_tbl) @@ -205,24 +204,24 @@ test_that("Chunked Recursive Tests ", { # Jumble the data to make sure it forecasts properly m4_monthly_updated <- timetk::m4_monthly %>% - arrange(desc(id), date) %>% - mutate(id = as_factor(as.character(id))) + dplyr::arrange(desc(id), date) %>% + dplyr::mutate(id = forcats::as_factor(as.character(id))) m4_extended <- m4_monthly_updated %>% - group_by(id) %>% - future_frame( + dplyr::group_by(id) %>% + timetk::future_frame( .length_out = FORECAST_HORIZON, .bind_data = TRUE ) %>% - ungroup() + dplyr::ungroup() # Transformation Function lag_transformer_grouped <- function(data){ data %>% - group_by(id) %>% + dplyr::group_by(id) %>% # Lags - tk_augment_lags(value, .lags = c(3,6,9,12)) %>% - ungroup() + timetk::tk_augment_lags(value, .lags = c(3,6,9,12)) %>% + dplyr::ungroup() } m4_lags <- m4_extended %>% diff --git a/tests/testthat/test-recursive.R b/tests/testthat/test-recursive.R index e48608bb..2d3f24f8 100644 --- a/tests/testthat/test-recursive.R +++ b/tests/testthat/test-recursive.R @@ -203,24 +203,24 @@ test_that("Recursive Tests ", { # Jumble the data to make sure it forecasts properly m4_monthly_updated <- timetk::m4_monthly %>% - arrange(desc(id), date) %>% - mutate(id = as_factor(as.character(id))) + dplyr::arrange(desc(id), date) %>% + dplyr::mutate(id = forcats::as_factor(as.character(id))) m4_extended <- m4_monthly_updated %>% - group_by(id) %>% - future_frame( + dplyr::group_by(id) %>% + timetk::future_frame( .length_out = FORECAST_HORIZON, .bind_data = TRUE ) %>% - ungroup() + dplyr::ungroup() # Transformation Function lag_transformer_grouped <- function(data){ data %>% - group_by(id) %>% + dplyr::group_by(id) %>% # Lags - tk_augment_lags(value, .lags = 1:FORECAST_HORIZON) %>% - ungroup() + timetk::tk_augment_lags(value, .lags = 1:FORECAST_HORIZON) %>% + dplyr::ungroup() } m4_lags <- m4_extended %>% From 668f464a9c854261251060fd2872c93b34d86afc Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 21:05:20 -0400 Subject: [PATCH 03/16] Remove glubort in favor of `cli::cli_abort()`. Transition messages to cli inline markup. Note: no argument starting with a . can be put between {} --- R/dev-model_descriptions.R | 8 ++++---- R/dev-xregs.R | 14 ++++++++------ R/modeltime-accuracy-table.R | 10 +++++----- R/modeltime-calibrate.R | 15 ++++++++++----- R/modeltime-forecast-plot.R | 10 +++++++--- R/modeltime-forecast.R | 27 ++++++++++++++------------- R/modeltime-refit.R | 2 +- R/modeltime-residuals-plot.R | 2 +- R/parsnip-prophet_boost.R | 2 +- R/parsnip-prophet_reg.R | 2 +- R/parsnip-seasonal_reg.R | 6 +++--- R/utils-checks-validations.R | 17 +++++------------ 12 files changed, 60 insertions(+), 55 deletions(-) diff --git a/R/dev-model_descriptions.R b/R/dev-model_descriptions.R index b65f260b..81814783 100644 --- a/R/dev-model_descriptions.R +++ b/R/dev-model_descriptions.R @@ -37,7 +37,7 @@ get_model_description <- function(object, indicate_training = FALSE, upper_case #' @export get_model_description.default <- function(object, indicate_training = FALSE, upper_case = TRUE) { - glubort("No method for class '{class(object)[1]}'. Expecting an object of class 'workflow', 'model_spec', or 'model_fit'.") + cli::cli_abort("No method for class {.obj_type_friendly {object}}. Expecting an object of class 'workflow', 'model_spec', or 'model_fit'.") } #' @export @@ -223,7 +223,7 @@ get_arima_description <- function(object, padding = FALSE) { #' @export get_arima_description.default <- function(object, padding = FALSE) { - glubort("No method for class '{class(object)[1]}'. Expecting an object of class 'Arima'.") + cli::cli_abort("No method for class {.obj_type_friendly {object}}. Expecting an object of class 'Arima'.") } #' @export @@ -284,8 +284,8 @@ get_arima_description.Arima <- function(object, padding = FALSE) { #' @export get_tbats_description <- function(object) { - if (!(inherits(object, "tbats") || inherits(object, "bats"))) { - glubort("No method for class '{class(object)[1]}'. Expecting an object of class 'bats' or 'tbats'.") + if (!rlang::inherits_any(object, c("tbats", "bats"))) { + cli::cli_abort("No method for class {.obj_type_friendly {object}}. Expecting an object of class 'bats' or 'tbats'.") } as.character(object) diff --git a/R/dev-xregs.R b/R/dev-xregs.R index 2b218a3f..81a92bd0 100644 --- a/R/dev-xregs.R +++ b/R/dev-xregs.R @@ -246,12 +246,14 @@ prepare_xreg_recipe_from_predictors <- function(data, prepare = TRUE, } }, error = function(e) { - rlang::warn( - paste0("Failed to return valid external regressors. Proceeding without regressors.\n---", - '\nWhat most likely happened: \nIf all of the regressors have zero variance (meaning they add no predictive value to the model), they are removed leaving no valid regressors.') - ) - recipe_spec <- NULL - return(recipe_spec) + cli::cli_warn(c( + "Failed to return valid external regressors. Proceeding without regressors.", + "---", + "What most likely happened:", + i = "If all of the regressors have zero variance (meaning they add no predictive value to the model), they are removed leaving no valid regressors." + )) + # recipe_spec + return(NULL) }) } else { diff --git a/R/modeltime-accuracy-table.R b/R/modeltime-accuracy-table.R index b1b20c89..86b15602 100644 --- a/R/modeltime-accuracy-table.R +++ b/R/modeltime-accuracy-table.R @@ -102,8 +102,11 @@ table_modeltime_accuracy <- function(.data, .round_digits = 2, .interactive = TRUE, ...) { # Checks + # If using an argument inside cli inline markup that starts with . like `.data`, + # we must use {(.data)} for it. https://cli.r-lib.org/reference/inline-markup.html + if (!inherits(.data, "data.frame")) { - glubort("No method for {class(.data)[1]}. Expecting the output of 'modeltime_accuracy()'.") + cli::cli_abort("No method for {.obj_type_friendly {(.data)}}. Expecting the output of 'modeltime_accuracy()'.") } if (!all(c(".model_id", ".model_desc") %in% names(.data))) { @@ -114,11 +117,8 @@ table_modeltime_accuracy <- function(.data, .round_digits = 2, data_formatted <- .data if (!is.null(round)) { - suppressMessages({ - # If grouped, avoid message: `mutate_if()` ignored the following grouping variables data_formatted <- data_formatted %>% - dplyr::mutate_if(is.double, .funs = ~ round(., digits = .round_digits)) - }) + dplyr::mutate(dplyr::across(where(is.double), .fns = ~ round(.x, digits = .round_digits))) } # Output either reactable() or gt() diff --git a/R/modeltime-calibrate.R b/R/modeltime-calibrate.R index cdec7006..d780f204 100644 --- a/R/modeltime-calibrate.R +++ b/R/modeltime-calibrate.R @@ -96,7 +96,7 @@ modeltime_calibrate <- function(object, new_data, id = NULL, # Checks if (rlang::is_missing(new_data)) { - glubort("Missing 'new_data'. Try adding a test data set using rsample::testing(splits). See help for more info: ?modeltime_calibrate ") + cli::cli_abort("Missing 'new_data'. Try adding a test data set using rsample::testing(splits). See help for more info: {.help modeltime::modeltime_calibrate}.") } # Check `id` is in `new_data` names @@ -105,12 +105,12 @@ modeltime_calibrate <- function(object, new_data, id = NULL, tryCatch({ id }, error = function(e) { - rlang::abort("`id` must be a quoted character string that is the name of an identifier column. ") + rlang::abort("`id` must be a quoted character string that is the name of an identifier column.") }) - if (!is.character(id)) rlang::abort("`id` must be a quoted character string that is the name of an identifier column. ") + if (!is.character(id)) rlang::abort("`id` must be a quoted character string that is the name of an identifier column.") - if (!id %in% names(new_data)) glubort("`id` is not a valid column name in `new_data`. Please review column names: {stringr::str_c(names(new_data), collapse = ', ')}") + if (!id %in% names(new_data)) cli::cli_abort("`id` is not a valid column name in `new_data`. Please review column names: {(names(new_data)}.") } @@ -120,7 +120,12 @@ modeltime_calibrate <- function(object, new_data, id = NULL, #' @export modeltime_calibrate.default <- function(object, new_data, id = NULL, quiet = TRUE, ...) { - glubort("Received an object of class: {class(object)[1]}. Expected an object of class:\n 1. 'workflow' - That has been fitted (trained).\n 2. 'model_fit' - A fitted parsnip model.\n 3. 'mdl_time_tbl' - A Model Time Table made with 'modeltime_table()'.") + cli::cli_abort(c( + x = "Received an object of class: {.obj_type_friendly {object}}.", + i = "Expected an object of class:", + "1. 'workflow' - That has been fitted (trained).", + "2. 'model_fit' - A fitted parsnip model.", + "3. 'mdl_time_tbl' - A Model Time Table made with 'modeltime_table()'.")) } #' @export diff --git a/R/modeltime-forecast-plot.R b/R/modeltime-forecast-plot.R index f86e1ac4..1ecc9324 100644 --- a/R/modeltime-forecast-plot.R +++ b/R/modeltime-forecast-plot.R @@ -79,17 +79,21 @@ plot_modeltime_forecast <- function( # Checks if (!inherits(.data, "data.frame")) { - glubort("No method for {class(.data)[1]}. Expecting the output of 'modeltime_forecast()'.") + cli::cli_abort("No method for {.obj_type_friendly {(.data)}}. Expecting the output of 'modeltime_forecast()'.") } if (!all(c(".model_id", ".model_desc", ".key", ".index", ".value") %in% names(.data))) { - rlang::abort("Expecting the following names to be in the data frame: .key, .index, .value. Try using 'modeltime_forecast()' to return a data frame in the appropriate structure.") + rlang::abort(c("Expecting the following names to be in the data frame: `.key`, `.index`, `.value.`.", + "Try using 'modeltime_forecast()' to return a data frame in the appropriate structure.")) } if (.conf_interval_show) { if (!all(c(".conf_lo", ".conf_hi") %in% names(.data))) { .conf_interval_show <- FALSE - rlang::warn("Expecting the following names to be in the data frame: .conf_hi, .conf_lo. \nProceeding with '.conf_interval_show = FALSE' to visualize the forecast without confidence intervals.\nAlternatively, try using `modeltime_calibrate()` before forecasting to add confidence intervals.") + rlang::warn(c( + x = "Expecting the following names to be in the data frame: .conf_hi, .conf_lo.", + i = "Proceeding with '.conf_interval_show = FALSE' to visualize the forecast without confidence intervals.", + "Alternatively, try using `modeltime_calibrate()` before forecasting to add confidence intervals.")) } } diff --git a/R/modeltime-forecast.R b/R/modeltime-forecast.R index 6c73862f..0b009903 100644 --- a/R/modeltime-forecast.R +++ b/R/modeltime-forecast.R @@ -282,7 +282,9 @@ modeltime_forecast <- function(object, new_data = NULL, h = NULL, actual_data = modeltime_forecast.default <- function(object, new_data = NULL, h = NULL, actual_data = NULL, conf_interval = 0.95, conf_by_id = FALSE, conf_method = "conformal_default", keep_data = FALSE, arrange_index = FALSE, ...) { - glubort("Received an object of class: {class(object)[1]}. Expected an object of class:\n 1. 'mdl_time_tbl' - A Model Time Table made with 'modeltime_table()' and calibrated with 'modeltime_calibrate()'.") + cli::cli_abort(c("Received an object of class: {.obj_type_friendly {object}}.", + "Expected an object of class:", + "1. 'mdl_time_tbl' - A Model Time Table made with 'modeltime_table()' and calibrated with 'modeltime_calibrate()'.")) } #' @export @@ -296,7 +298,7 @@ modeltime_forecast.mdl_time_tbl <- function(object, new_data = NULL, h = NULL, a # HANDLE CALIBRATION DATA if (!all(c(".type", ".calibration_data") %in% names(data))) { - # glubort("Expecting columns '.type' and '.calibration_data'. Try running 'modeltime_calibrate()' before using 'modeltime_forecast()'.") + # cli::cli_abort("Expecting columns '.type' and '.calibration_data'. Try running 'modeltime_calibrate()' before using 'modeltime_forecast()'.") conf_interval = NULL data <- data %>% dplyr::mutate( @@ -381,7 +383,7 @@ modeltime_forecast.mdl_time_tbl <- function(object, new_data = NULL, h = NULL, a conf_method = conf_method, id = !! id_col ) %>% - dplyr::select(.model_id, .model_desc, .key, .index, .value, .conf_lo, .conf_hi, dplyr::all_of(names(new_data))) + dplyr::select(".model_id", ".model_desc", ".key", ".index", ".value", ".conf_lo", ".conf_hi", dplyr::all_of(names(new_data))) # Remove unnecessary columns if `keep_data = FALSE`. Required to keep the id column. if (!keep_data) { @@ -434,9 +436,9 @@ modeltime_forecast.mdl_time_tbl <- function(object, new_data = NULL, h = NULL, a # STRUCTURE ---- class(ret) <- c("mdl_forecast_tbl", class(ret)) - attr(ret, "conf_interval") <- conf_interval - attr(ret, "conf_method") <- conf_method - attr(ret, "conf_by_id") <- conf_by_id + attr(ret, "conf_interval") <- conf_interval + attr(ret, "conf_method") <- conf_method + attr(ret, "conf_by_id") <- conf_by_id return(ret) } @@ -763,9 +765,9 @@ mdl_time_forecast.model_fit <- function(object, calibration_data, new_data = NUL }, error = function(e) { if (any(c(h_provided, calib_provided))) { # Most likely issue: need to provide external regressors - glubort("Problem occurred during prediction. Most likely cause is missing external regressors. Try using 'new_data' and supply a dataset containing all required columns. {e}") + cli::cli_abort("Problem occurred during prediction. Most likely cause is missing external regressors. Try using 'new_data' and supply a dataset containing all required columns. {e}") } else { - glubort("Problem occurred during prediction. {e}") + cli::cli_abort("Problem occurred during prediction. {e}") } }) @@ -873,8 +875,7 @@ mdl_time_forecast.model_fit <- function(object, calibration_data, new_data = NUL # FINALIZE ret <- data_formatted %>% - dplyr::rename(.value = .pred) %>% - dplyr::select(.key, .index, .value) %>% + dplyr::select(.key, .index, .value = .pred) %>% dplyr::mutate(.key = factor(.key, levels = c("actual", "prediction"))) # Keep Data @@ -983,9 +984,9 @@ mdl_time_forecast.workflow <- function(object, calibration_data, new_data = NULL }, error = function(e) { if (any(c(h_provided, calib_provided))) { # Most likely issue: need to provide external regressors - glubort("Problem occurred in getting predictors from new data. Most likely cause is missing external regressors. Try using 'new_data' and supply a dataset containing all required columns. {e}") + cli::cli_abort("Problem occurred in getting predictors from new data. Most likely cause is missing external regressors. Try using 'new_data' and supply a dataset containing all required columns. {e}") } else { - glubort("Problem occurred getting predictors from new data. {e}") + cli::cli_abort("Problem occurred getting predictors from new data. {e}") } }) @@ -1016,7 +1017,7 @@ mdl_time_forecast.workflow <- function(object, calibration_data, new_data = NULL time_stamp_predictors_tbl <- new_data_forged$predictors %>% dplyr::mutate(.index = idx[new_data_missing_removed_tbl$..id]) } else { - glubort("Problem occurred combining processed data with timestamps. Most likely cause is rows being added or removed during preprocessing. Try imputing missing values to retain the full number of rows.") + cli::cli_abort("Problem occurred combining processed data with timestamps. Most likely cause is rows being added or removed during preprocessing. Try imputing missing values to retain the full number of rows.") } } } diff --git a/R/modeltime-refit.R b/R/modeltime-refit.R index 501fa88c..ad8d21b3 100644 --- a/R/modeltime-refit.R +++ b/R/modeltime-refit.R @@ -319,7 +319,7 @@ mdl_time_refit <- function(object, data, ..., control = NULL) { #' @export mdl_time_refit.default <- function(object, data, ..., control = NULL) { - glubort("No method for an object of class: {class(object)[1]}. .") + cli::cli_abort("No method for an object of class: {.obj_type_friendly {object}}. .") } #' @export diff --git a/R/modeltime-residuals-plot.R b/R/modeltime-residuals-plot.R index 30340063..2426530f 100644 --- a/R/modeltime-residuals-plot.R +++ b/R/modeltime-residuals-plot.R @@ -74,7 +74,7 @@ plot_modeltime_residuals <- function(.data, # Checks if (!inherits(.data, "data.frame")) { - glubort("No method for {class(.data)[1]}. Expecting the output of 'modeltime_residuals()'.") + cli::cli_abort("No method for {.obj_type_friendly {(.data)}}. Expecting the output of 'modeltime_residuals()'.") } if (!all(c(".model_id", ".model_desc", ".type", ".index", ".actual", ".prediction", ".residuals") %in% names(.data))) { diff --git a/R/parsnip-prophet_boost.R b/R/parsnip-prophet_boost.R index 519df31e..0064b746 100644 --- a/R/parsnip-prophet_boost.R +++ b/R/parsnip-prophet_boost.R @@ -460,7 +460,7 @@ prophet_xgboost_fit_impl <- function(x, y, if (growth == "logistic") { if (all(c(is.null(logistic_cap), is.null(logistic_floor)))) { - glubort("Capacities must be supplied for `growth = 'logistic'`. Try specifying at least one of 'logistic_cap' or 'logistic_floor'") + cli::cli_abort("Capacities must be supplied for `growth = 'logistic'`. Try specifying at least one of 'logistic_cap' or 'logistic_floor'") } } diff --git a/R/parsnip-prophet_reg.R b/R/parsnip-prophet_reg.R index 851858c6..7512cb20 100644 --- a/R/parsnip-prophet_reg.R +++ b/R/parsnip-prophet_reg.R @@ -379,7 +379,7 @@ prophet_fit_impl <- function(x, y, if (growth == "logistic") { if (all(c(is.null(logistic_cap), is.null(logistic_floor)))) { - glubort("Capacities must be supplied for `growth = 'logistic'`. Try specifying at least one of 'logistic_cap' or 'logistic_floor'") + cli::cli_abort("Capacities must be supplied for `growth = 'logistic'`. Try specifying at least one of 'logistic_cap' or 'logistic_floor'") } } diff --git a/R/parsnip-seasonal_reg.R b/R/parsnip-seasonal_reg.R index da453254..3e4c8b47 100644 --- a/R/parsnip-seasonal_reg.R +++ b/R/parsnip-seasonal_reg.R @@ -286,7 +286,7 @@ tbats_fit_impl <- function(x, y, period_1 = "auto", period_2 = NULL, period_3 = predictor <- x if (is.null(period_1) || period_1 == "none" || period_1 <=1) { - glubort("The 'seasonal_period_1' must be greater than 1 (i.e. have seasonality). Try increasing the seasonality.") + cli::cli_abort("The 'seasonal_period_1' must be greater than 1 (i.e. have seasonality). Try increasing the seasonality.") } # INDEX & PERIOD @@ -411,7 +411,7 @@ stlm_ets_fit_impl <- function(x, y, period_1 = "auto", period_2 = NULL, period_3 predictor <- x if (is.null(period_1) || period_1 == "none" || period_1 <=1) { - glubort("The 'seasonal_period_1' must be greater than 1 (i.e. have seasonality). Try increasing the seasonality.") + cli::cli_abort("The 'seasonal_period_1' must be greater than 1 (i.e. have seasonality). Try increasing the seasonality.") } # INDEX & PERIOD @@ -548,7 +548,7 @@ stlm_arima_fit_impl <- function(x, y, period_1 = "auto", period_2 = NULL, period predictor <- x if (is.null(period_1) || period_1 == "none" || period_1 <=1) { - glubort("The 'seasonal_period_1' must be greater than 1 (i.e. have seasonality). Try increasing the seasonality.") + cli::cli_abort("The 'seasonal_period_1' must be greater than 1 (i.e. have seasonality). Try increasing the seasonality.") } # INDEX & PERIOD diff --git a/R/utils-checks-validations.R b/R/utils-checks-validations.R index 0ec06c06..0eff87e3 100644 --- a/R/utils-checks-validations.R +++ b/R/utils-checks-validations.R @@ -202,11 +202,10 @@ validate_models_are_trained <- function(data) { bad_msg <- glue::glue("- Model {bad_models}: Is not trained. Try using `fit()` to train the model.") bad_msg <- glue::glue_collapse(bad_msg, sep = "\n") - glubort( + cli::cli_abort(c( "All objects must be fitted workflow or parsnip models. The following are not:", - "\n", "{bad_msg}" - ) + )) } } @@ -242,7 +241,7 @@ validate_models_are_not_null <- function(data, type = c("none", "warn", "error") "{cli::rule('End Model Failure Report', width = 60)}", "\n\n" ) - message(msg) + cli::cli_inform(msg) if (type == "warn") { @@ -320,11 +319,9 @@ validate_non_bad_class_data <- function(data, bad_classes = c("character")) { bad_msg <- glue::glue("{bad_cols}: Is class {bad_values}") bad_msg <- glue::glue_collapse(bad_msg, sep = "\n") - rlang::abort(glue::glue( + cli::cli_abort(c( "All variables must be categorical (factor) or date-like, but the following are not:", - "\n", - "{bad_msg}") - ) + "{bad_msg}")) } } @@ -395,8 +392,4 @@ glue_quote_collapse <- function(x) { glue::glue_collapse(glue::single_quote(x), sep = ", ") } -glubort <- function(..., .sep = "", .envir = parent.frame()) { - rlang::abort(glue::glue(..., .sep = .sep, .envir = .envir)) -} - From 75e077a02bc5120ba9169eef54cd3555ffa72e91 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 21:06:06 -0400 Subject: [PATCH 04/16] Remove tidyselect 1.2.0 warning --- R/parsnip-naive_reg.R | 8 ++++---- R/parsnip-window_reg.R | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/R/parsnip-naive_reg.R b/R/parsnip-naive_reg.R index a6f42aea..c5dea5a4 100644 --- a/R/parsnip-naive_reg.R +++ b/R/parsnip-naive_reg.R @@ -269,12 +269,12 @@ naive_fit_impl <- function(x, y, id = NULL, seasonal_period = "auto", ...) { if (is_grouped) { naive_model <- constructed_tbl %>% dplyr::group_by(!! rlang::sym(id)) %>% - dplyr::arrange(dplyr::all_of(idx_col)) %>% + dplyr::arrange(dplyr::pick(dplyr::all_of(idx_col))) %>% dplyr::slice_tail(n = 1) %>% dplyr::ungroup() } else { naive_model <- constructed_tbl %>% - dplyr::arrange(dplyr::all_of(idx_col)) %>% + dplyr::arrange(dplyr::pick(dplyr::all_of(idx_col))) %>% dplyr::slice_tail(n = 1) %>% dplyr::ungroup() } @@ -411,12 +411,12 @@ snaive_fit_impl <- function(x, y, id = NULL, seasonal_period = "auto", ...) { if (is_grouped) { snaive_model <- constructed_tbl %>% dplyr::group_by(!! rlang::sym(id)) %>% - dplyr::arrange(dplyr::all_of(idx_col)) %>% + dplyr::arrange(dplyr::pick(dplyr::all_of(idx_col))) %>% dplyr::slice_tail(n = period) %>% dplyr::ungroup() } else { snaive_model <- constructed_tbl %>% - dplyr::arrange(dplyr::all_of(idx_col)) %>% + dplyr::arrange(dplyr::pick(dplyr::all_of(idx_col))) %>% dplyr::slice_tail(n = period) %>% dplyr::ungroup() } diff --git a/R/parsnip-window_reg.R b/R/parsnip-window_reg.R index e833de8b..61f75e11 100644 --- a/R/parsnip-window_reg.R +++ b/R/parsnip-window_reg.R @@ -505,7 +505,7 @@ predict.window_function_fit_impl <- function(object, new_data, ...) { # # # APPLY WINDOW # window_df <- window_df %>% -# dplyr::arrange(dplyr::all_of(idx_col)) %>% +# dplyr::arrange(dplyr::pick(dplyr::all_of(idx_col))) %>% # dplyr::slice_tail(n = period) %>% # dplyr::ungroup() # From cdf9eafa8988810f72ed06522a875ccf9b292267 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 21:07:01 -0400 Subject: [PATCH 05/16] Use dplyr::reframe instead of summarise. + use rlang::as_function() to correctly pass `window_function` to `dplyr::across()` --- R/parsnip-window_reg.R | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/R/parsnip-window_reg.R b/R/parsnip-window_reg.R index 61f75e11..b8012d1d 100644 --- a/R/parsnip-window_reg.R +++ b/R/parsnip-window_reg.R @@ -344,18 +344,23 @@ window_function_fit_impl <- function(x, y, id = NULL, if (is_grouped) { window_model <- constructed_tbl %>% - dplyr::group_by(!! rlang::sym(id)) + dplyr::group_by(!!rlang::sym(id)) } else { window_model <- constructed_tbl } window_model <- window_model %>% - dplyr::arrange(dplyr::all_of(idx_col)) %>% - dplyr::slice_tail(n = period) %>% - dplyr::summarise( - dplyr::across(value, .fns = window_function, ...), - .groups = "drop") %>% - dplyr::ungroup() + dplyr::arrange(dplyr::pick(dplyr::all_of(idx_col))) %>% + dplyr::slice_tail(n = period) + + window_function <- rlang::as_function(window_function) + + + window_model <- + dplyr::reframe( + window_model, + dplyr::across(value, .fns = function(.x) window_function(.x, ...)), + ) # return(window_model) From 018e5aa5d8d36d60328e341d5333ae512fa0b3e2 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 21:50:35 -0400 Subject: [PATCH 06/16] Remove `mutate_at()` --- tests/testthat.R | 1 - tests/testthat/test-algo-arima_boost-Arima.R | 2 +- tests/testthat/test-algo-arima_boost-auto_arima.R | 2 +- tests/testthat/test-algo-arima_reg-Arima.R | 2 +- tests/testthat/test-algo-arima_reg-auto_arima.R | 2 +- tests/testthat/test-algo-exp_smoothing-ets.R | 8 ++++---- tests/testthat/test-algo-nnetar_reg.R | 2 +- tests/testthat/test-algo-prophet_boost.R | 2 +- tests/testthat/test-algo-prophet_reg.R | 2 +- tests/testthat/test-algo-seasonal_decomp_arima.R | 2 +- tests/testthat/test-algo-seasonal_decomp_ets.R | 2 +- tests/testthat/test-algo-seasonal_reg_tbats.R | 2 +- tests/testthat/test-algo-temporal_hierarchy.R | 2 +- tests/testthat/test-extended_accuracy_metric_set.R | 3 +-- tests/testthat/test-results-accuracy-tables.R | 2 +- tests/testthat/test-results-forecast-plots.R | 12 ++++++------ 16 files changed, 23 insertions(+), 25 deletions(-) diff --git a/tests/testthat.R b/tests/testthat.R index b157b97b..b3ca678c 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -15,7 +15,6 @@ library(rsample) library(recipes) library(tune) library(dials) -library(TSrepr) library(yardstick) library(slider) diff --git a/tests/testthat/test-algo-arima_boost-Arima.R b/tests/testthat/test-algo-arima_boost-Arima.R index a8f56f6c..21124960 100644 --- a/tests/testthat/test-algo-arima_boost-Arima.R +++ b/tests/testthat/test-algo-arima_boost-Arima.R @@ -228,7 +228,7 @@ test_that("arima_boost: Arima (workflow), Test Model Fit Object", { predictions_tbl <- wflw_fit %>% modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) expect_s3_class(wflw_fit$fit$fit$fit, "arima_xgboost_fit_impl") diff --git a/tests/testthat/test-algo-arima_boost-auto_arima.R b/tests/testthat/test-algo-arima_boost-auto_arima.R index 46ecb118..048f67c8 100644 --- a/tests/testthat/test-algo-arima_boost-auto_arima.R +++ b/tests/testthat/test-algo-arima_boost-auto_arima.R @@ -209,7 +209,7 @@ test_that("arima_boost: Arima (workflow), Test Model Fit Object", { predictions_tbl <- wflw_fit %>% modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) # TESTS diff --git a/tests/testthat/test-algo-arima_reg-Arima.R b/tests/testthat/test-algo-arima_reg-Arima.R index 0539bcea..ff763889 100644 --- a/tests/testthat/test-algo-arima_reg-Arima.R +++ b/tests/testthat/test-algo-arima_reg-Arima.R @@ -173,7 +173,7 @@ test_that("arima_reg: Arima (workflow), Test Model Fit Object", { predictions_tbl <- wflw_fit %>% modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) expect_s3_class(wflw_fit$fit$fit$fit, "Arima_fit_impl") diff --git a/tests/testthat/test-algo-arima_reg-auto_arima.R b/tests/testthat/test-algo-arima_reg-auto_arima.R index 33fcbfd3..35d766cb 100644 --- a/tests/testthat/test-algo-arima_reg-auto_arima.R +++ b/tests/testthat/test-algo-arima_reg-auto_arima.R @@ -156,7 +156,7 @@ test_that("arima_reg: auto.arima (Workflow), Test Model Fit Object", { predictions_tbl <- wflw_fit %>% modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) # TEST --- diff --git a/tests/testthat/test-algo-exp_smoothing-ets.R b/tests/testthat/test-algo-exp_smoothing-ets.R index 8e008aa5..e0846781 100644 --- a/tests/testthat/test-algo-exp_smoothing-ets.R +++ b/tests/testthat/test-algo-exp_smoothing-ets.R @@ -109,7 +109,7 @@ test_that("exp_smoothing: Arima (workflow), Test Model Fit Object", { modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) @@ -185,7 +185,7 @@ test_that("exp_smoothing: CROSTON", { modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) expect_s3_class(wflw_fit$fit$fit$fit, "croston_fit_impl") @@ -258,7 +258,7 @@ test_that("exp_smoothing: Theta", { modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) # @@ -406,7 +406,7 @@ test_that("exp_smoothing: Arima (workflow), Test Model Fit Object", { modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value, .conf_lo, .conf_hi), exp) + dplyr::mutate(dplyr::across(c(.value, .conf_lo, .conf_hi), exp)) }) # diff --git a/tests/testthat/test-algo-nnetar_reg.R b/tests/testthat/test-algo-nnetar_reg.R index 84084d92..dbe57b4a 100644 --- a/tests/testthat/test-algo-nnetar_reg.R +++ b/tests/testthat/test-algo-nnetar_reg.R @@ -174,7 +174,7 @@ test_that("nnetar_reg: (workflow)", { predictions_tbl <- wflw_fit %>% modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) expect_s3_class(wflw_fit$fit$fit$fit, "nnetar_fit_impl") diff --git a/tests/testthat/test-algo-prophet_boost.R b/tests/testthat/test-algo-prophet_boost.R index f5472de2..b04e8680 100644 --- a/tests/testthat/test-algo-prophet_boost.R +++ b/tests/testthat/test-algo-prophet_boost.R @@ -283,7 +283,7 @@ test_that("prophet_boost: prophet_xgboost (workflow)", { predictions_tbl <- wflw_fit %>% modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) full_data <- dplyr::bind_rows(rsample::training(splits), rsample::testing(splits)) diff --git a/tests/testthat/test-algo-prophet_reg.R b/tests/testthat/test-algo-prophet_reg.R index a779d42b..542c7a67 100644 --- a/tests/testthat/test-algo-prophet_reg.R +++ b/tests/testthat/test-algo-prophet_reg.R @@ -236,7 +236,7 @@ test_that("prophet_reg: prophet (workflow), Test Model Fit Object", { predictions_tbl <- wflw_fit %>% modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) full_data <- dplyr::bind_rows(rsample::training(splits), rsample::testing(splits)) diff --git a/tests/testthat/test-algo-seasonal_decomp_arima.R b/tests/testthat/test-algo-seasonal_decomp_arima.R index 5d46073d..45c5e46a 100644 --- a/tests/testthat/test-algo-seasonal_decomp_arima.R +++ b/tests/testthat/test-algo-seasonal_decomp_arima.R @@ -104,7 +104,7 @@ test_that("seasonal_reg - arima: parnip", { predictions_tbl <- wflw_fit %>% modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) # TEST diff --git a/tests/testthat/test-algo-seasonal_decomp_ets.R b/tests/testthat/test-algo-seasonal_decomp_ets.R index ea9d5fa4..e866a511 100644 --- a/tests/testthat/test-algo-seasonal_decomp_ets.R +++ b/tests/testthat/test-algo-seasonal_decomp_ets.R @@ -102,7 +102,7 @@ test_that("seasonal_reg: stlm_ets", { predictions_tbl <- wflw_fit %>% modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) expect_s3_class(wflw_fit$fit$fit$fit, "stlm_ets_fit_impl") diff --git a/tests/testthat/test-algo-seasonal_reg_tbats.R b/tests/testthat/test-algo-seasonal_reg_tbats.R index 122866c3..c3c3ed4e 100644 --- a/tests/testthat/test-algo-seasonal_reg_tbats.R +++ b/tests/testthat/test-algo-seasonal_reg_tbats.R @@ -111,7 +111,7 @@ test_that("seasonal_reg: workflow", { predictions_tbl <- wflw_fit %>% modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) # TEST diff --git a/tests/testthat/test-algo-temporal_hierarchy.R b/tests/testthat/test-algo-temporal_hierarchy.R index 0ffa6495..014d148b 100644 --- a/tests/testthat/test-algo-temporal_hierarchy.R +++ b/tests/testthat/test-algo-temporal_hierarchy.R @@ -92,7 +92,7 @@ test_that("thief", { modeltime_calibrate(rsample::testing(splits)) %>% modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits)) %>% - dplyr::mutate_at(dplyr::vars(.value), exp) + dplyr::mutate(dplyr::across(.value, exp)) # Tests diff --git a/tests/testthat/test-extended_accuracy_metric_set.R b/tests/testthat/test-extended_accuracy_metric_set.R index cca87601..7b6ae4fb 100644 --- a/tests/testthat/test-extended_accuracy_metric_set.R +++ b/tests/testthat/test-extended_accuracy_metric_set.R @@ -1,5 +1,4 @@ library(tidymodels) -library(tibble) library(dplyr) library(timetk) @@ -9,7 +8,7 @@ test_that("extended_forecast_accuracy_metric_set works", { set.seed(1) data <- dplyr::tibble( - time = tk_make_timeseries("2020", by = "sec", length_out = 10), + time = timetk::tk_make_timeseries("2020", by = "sec", length_out = 10), y = 1:10 + rnorm(10), y_hat = 1:10 + rnorm(10) ) diff --git a/tests/testthat/test-results-accuracy-tables.R b/tests/testthat/test-results-accuracy-tables.R index d05060f2..35369783 100644 --- a/tests/testthat/test-results-accuracy-tables.R +++ b/tests/testthat/test-results-accuracy-tables.R @@ -91,7 +91,7 @@ test_that("Test Modeltime Accuracy", { # Modifying Default Forecast Accuracy Metric Set my_metric_set <- default_forecast_accuracy_metric_set( - metric_tweak("mase12", yardstick::mase, m = 12) + yardstick::metric_tweak("mase12", yardstick::mase, m = 12) ) acc_tbl_6 <- calibration_tbl %>% diff --git a/tests/testthat/test-results-forecast-plots.R b/tests/testthat/test-results-forecast-plots.R index 48c2dddc..662959c7 100644 --- a/tests/testthat/test-results-forecast-plots.R +++ b/tests/testthat/test-results-forecast-plots.R @@ -35,14 +35,14 @@ test_that("modeltime plotting", { # * ggplot2 visualization ---- g <- forecast_tbl %>% - dplyr::mutate_at(dplyr::vars(.value:.conf_hi), exp) %>% + dplyr::mutate(dplyr::across(.value:.conf_hi, exp)) %>% plot_modeltime_forecast(.interactive = FALSE) # * plotly visualization ---- suppressWarnings({ # Needed until plotly is resolved: https://github.com/ropensci/plotly/issues/1783 p <- forecast_tbl %>% - dplyr::mutate_at(dplyr::vars(.value:.conf_hi), exp) %>% + dplyr::mutate(dplyr::across(.value:.conf_hi, exp)) %>% plot_modeltime_forecast(.interactive = TRUE) }) @@ -62,12 +62,12 @@ test_that("modeltime plotting", { # # PLOTS WITHOUT CONF INTERVALS ----- g <- forecast_tbl %>% - dplyr::mutate_at(dplyr::vars(.value:.conf_hi), exp) %>% + dplyr::mutate(dplyr::across(.value:.conf_hi, exp)) %>% plot_modeltime_forecast(.interactive = FALSE, .conf_interval_show = FALSE) p <- forecast_tbl %>% - dplyr::mutate_at(dplyr::vars(.value:.conf_hi), exp) %>% + dplyr::mutate(dplyr::across(.value:.conf_hi, exp)) %>% plot_modeltime_forecast(.interactive = TRUE, .conf_interval_show = FALSE) # Structure @@ -110,12 +110,12 @@ test_that("modeltime plot - workflow, Test Static ggplot", { # * ggplot2 visualization ---- g <- forecast_tbl %>% - dplyr::mutate_at(dplyr::vars(.value:.conf_hi), exp) %>% + dplyr::mutate(dplyr::across(.value:.conf_hi, exp)) %>% plot_modeltime_forecast(.conf_interval_show = TRUE, .interactive = FALSE) # * plotly visualization ---- p <- forecast_tbl %>% - dplyr::mutate_at(dplyr::vars(.value:.conf_hi), exp) %>% + dplyr::mutate(dplyr::across(.value:.conf_hi, exp)) %>% plot_modeltime_forecast(.conf_interval_show = TRUE, .interactive = TRUE) From a5a5f674ecc43a6cd5c9232c802db71ddc44f314 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 21:51:59 -0400 Subject: [PATCH 07/16] Misc updates. --- NAMESPACE | 1 - R/modeltime-accuracy-table.R | 7 +++++++ R/yardstick-metric-sets.R | 15 ++++++--------- man/maape_vec.Rd | 2 +- man/summarize_accuracy_metrics.Rd | 1 - 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 94a2ac58..d30814ef 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -329,7 +329,6 @@ importFrom(tibble,type_sum) importFrom(yardstick,mae) importFrom(yardstick,mape) importFrom(yardstick,mase) -importFrom(yardstick,metric_tweak) importFrom(yardstick,rmse) importFrom(yardstick,rsq) importFrom(yardstick,smape) diff --git a/R/modeltime-accuracy-table.R b/R/modeltime-accuracy-table.R index 86b15602..9ad15ed0 100644 --- a/R/modeltime-accuracy-table.R +++ b/R/modeltime-accuracy-table.R @@ -142,6 +142,13 @@ table_modeltime_accuracy <- function(.data, .round_digits = 2, defaultExpanded = .expand_groups, ... ) + # TODO gt now allows opt_interactive + # gt %>% + # gt::opt_interactive( + # use_sorting = .show_sortable, + # use_filters = .filterable, + # use_search = .searchable, + # ) } else { # gt() diff --git a/R/yardstick-metric-sets.R b/R/yardstick-metric-sets.R index 19887bb1..4ec7959c 100644 --- a/R/yardstick-metric-sets.R +++ b/R/yardstick-metric-sets.R @@ -73,8 +73,8 @@ #' #' #' @name metric_sets - -#' @importFrom yardstick mae mape mase smape rmse rsq metric_tweak +NULL +#' @importFrom yardstick mae mape mase smape rmse rsq #' @export #' @rdname metric_sets default_forecast_accuracy_metric_set <- function(...) { @@ -93,7 +93,7 @@ default_forecast_accuracy_metric_set <- function(...) { # EXTENDED FORECAST ACCURACY METRIC SET ---- -#' @importFrom yardstick mae mape mase smape rmse rsq metric_tweak +#' @importFrom yardstick mae mape mase smape rmse rsq #' @export #' @rdname metric_sets extended_forecast_accuracy_metric_set <- function(...) { @@ -122,7 +122,6 @@ extended_forecast_accuracy_metric_set <- function(...) { #' @param estimate The column identifier for the predicted results (that is also numeric). #' #' @examples -#' library(tibble) #' library(dplyr) #' #' predictions_tbl <- tibble( @@ -155,12 +154,10 @@ summarize_accuracy_metrics <- function(data, truth, estimate, metric_set) { data_tbl %>% metric_summarizer_fun(!! truth_expr, !! estimate_expr) %>% - dplyr::select(-.estimator) %>% - + dplyr::select(-".estimator") %>% dplyr::group_by(!!! rlang::syms(group_nms)) %>% dplyr::mutate(.metric = make.unique(.metric, sep = "_")) %>% dplyr::ungroup() %>% - tidyr::pivot_wider( names_from = .metric, values_from = .estimate @@ -219,12 +216,12 @@ calc_accuracy_2 <- function(train_data = NULL, test_data = NULL, metric_set, by_ #' #' @param truth The column identifier for the true results (that is numeric). #' @param estimate The column identifier for the predicted results (that is also numeric). -#' @param na_rm Not in use...NA values managed by TSrepr::maape +#' @param na_rm Not in use... `NA` values managed by `TSrepr::maape()` #' @param ... Not currently in use #' #' @export maape_vec <- function(truth, estimate, na_rm = TRUE, ...) { - + rlang::check_installed("TSrepr") maape_impl <- function(truth, estimate) { TSrepr::maape(truth, estimate) } diff --git a/man/maape_vec.Rd b/man/maape_vec.Rd index c0418e7d..1bdad2db 100644 --- a/man/maape_vec.Rd +++ b/man/maape_vec.Rd @@ -11,7 +11,7 @@ maape_vec(truth, estimate, na_rm = TRUE, ...) \item{estimate}{The column identifier for the predicted results (that is also numeric).} -\item{na_rm}{Not in use...NA values managed by TSrepr::maape} +\item{na_rm}{Not in use... \code{NA} values managed by \code{TSrepr::maape()}} \item{...}{Not currently in use} } diff --git a/man/summarize_accuracy_metrics.Rd b/man/summarize_accuracy_metrics.Rd index 50d40370..e8960a5b 100644 --- a/man/summarize_accuracy_metrics.Rd +++ b/man/summarize_accuracy_metrics.Rd @@ -20,7 +20,6 @@ forecast accuracy (regression) metrics.} This is an internal function used by \code{modeltime_accuracy()}. } \examples{ -library(tibble) library(dplyr) predictions_tbl <- tibble( From 22d0d5e45220d85647be23602472a26535c5e65e Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 22:49:54 -0400 Subject: [PATCH 08/16] Remove tidyverse dependency. --- DESCRIPTION | 1 - R/dev-constructor.R | 3 +- R/helpers-modeltime_table.R | 2 +- R/modeltime-accuracy-table.R | 2 +- R/modeltime-accuracy.R | 2 +- R/modeltime-calibrate.R | 2 +- R/modeltime-fit-workflowset.R | 6 +-- R/modeltime-forecast-plot.R | 2 +- R/modeltime-forecast.R | 3 +- R/modeltime-recursive.R | 4 +- R/modeltime-refit.R | 2 +- R/modeltime-residuals-plot.R | 3 +- R/modeltime-residuals-tests.R | 4 +- R/modeltime-residuals.R | 2 +- R/modeltime-table.R | 3 +- R/nested-modeltime_data_prep.R | 9 ++-- man/combine_modeltime_tables.Rd | 2 +- man/modeltime_accuracy.Rd | 2 +- man/modeltime_calibrate.Rd | 2 +- man/modeltime_fit_workflowset.Rd | 2 +- man/modeltime_forecast.Rd | 3 +- man/modeltime_refit.Rd | 2 +- man/modeltime_residuals.Rd | 2 +- man/modeltime_residuals_test.Rd | 3 +- man/modeltime_table.Rd | 3 +- man/new_modeltime_bridge.Rd | 3 +- man/plot_modeltime_forecast.Rd | 2 +- man/plot_modeltime_residuals.Rd | 3 +- man/prep_nested.Rd | 7 ++- man/recursive.Rd | 4 +- man/table_modeltime_accuracy.Rd | 2 +- tests/testthat.R | 2 - tests/testthat/test-algo-adam_reg-Adam.R | 4 +- tests/testthat/test-algo-adam_reg-auto_adam.R | 4 +- tests/testthat/test-algo-arima_boost-Arima.R | 4 +- .../test-algo-arima_boost-auto_arima.R | 2 +- tests/testthat/test-algo-arima_reg-Arima.R | 4 +- .../testthat/test-algo-arima_reg-auto_arima.R | 6 +-- tests/testthat/test-algo-exp_smoothing-ets.R | 8 ++-- tests/testthat/test-algo-nnetar_reg.R | 2 +- tests/testthat/test-conf_by_id.R | 4 +- tests/testthat/test-fit_workflowsets.R | 8 ++-- tests/testthat/test-modeltime_residuals.R | 2 +- .../test-modeltime_table-no-calib-refit.R | 2 +- tests/testthat/test-nested-modeltime.R | 2 +- tests/testthat/test-recursive-chunk-uneven.R | 33 ++++++-------- tests/testthat/test-recursive-chunk.R | 8 ++-- tests/testthat/test-recursive.R | 17 +++---- vignettes/extending-modeltime.Rmd | 44 +++++++++++-------- vignettes/getting-started-with-modeltime.Rmd | 14 +++++- vignettes/modeltime-spark.Rmd | 14 ++++-- vignettes/nested-forecasting.Rmd | 12 ++++- vignettes/parallel-processing.Rmd | 16 ++++++- vignettes/recursive-forecasting.Rmd | 26 +++++++---- 54 files changed, 181 insertions(+), 149 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7c402739..df09721a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -51,7 +51,6 @@ Imports: prophet, methods, cli, - tidyverse, tidymodels Suggests: rstan, diff --git a/R/dev-constructor.R b/R/dev-constructor.R index 2334dc89..075e2c82 100644 --- a/R/dev-constructor.R +++ b/R/dev-constructor.R @@ -13,8 +13,7 @@ #' #' #' @examples -#' library(stats) -#' library(tidyverse) +#' library(dplyr) #' library(lubridate) #' library(timetk) #' diff --git a/R/helpers-modeltime_table.R b/R/helpers-modeltime_table.R index afa9bd21..a2c438c5 100644 --- a/R/helpers-modeltime_table.R +++ b/R/helpers-modeltime_table.R @@ -37,8 +37,8 @@ #' #' @examples #' library(tidymodels) -#' library(tidyverse) #' library(timetk) +#' library(dplyr) #' library(lubridate) #' #' # Setup diff --git a/R/modeltime-accuracy-table.R b/R/modeltime-accuracy-table.R index 9ad15ed0..7113b9d1 100644 --- a/R/modeltime-accuracy-table.R +++ b/R/modeltime-accuracy-table.R @@ -60,7 +60,7 @@ #' #' #' @examples -#' library(tidyverse) +#' library(dplyr) #' library(lubridate) #' library(timetk) #' library(parsnip) diff --git a/R/modeltime-accuracy.R b/R/modeltime-accuracy.R index 89d43227..88fc0019 100644 --- a/R/modeltime-accuracy.R +++ b/R/modeltime-accuracy.R @@ -38,7 +38,7 @@ #' #' @examples #' library(tidymodels) -#' library(tidyverse) +#' library(dplyr) #' library(lubridate) #' library(timetk) #' diff --git a/R/modeltime-calibrate.R b/R/modeltime-calibrate.R index d780f204..233620b9 100644 --- a/R/modeltime-calibrate.R +++ b/R/modeltime-calibrate.R @@ -39,7 +39,7 @@ #' #' #' @examples -#' library(tidyverse) +#' library(dplyr) #' library(lubridate) #' library(timetk) #' library(parsnip) diff --git a/R/modeltime-fit-workflowset.R b/R/modeltime-fit-workflowset.R index 94c89876..04459eb9 100644 --- a/R/modeltime-fit-workflowset.R +++ b/R/modeltime-fit-workflowset.R @@ -17,7 +17,7 @@ #' @examples #' library(tidymodels) #' library(workflowsets) -#' library(tidyverse) +#' library(dplyr) #' library(lubridate) #' library(timetk) #' @@ -54,7 +54,7 @@ modeltime_fit_workflowset <- function(object, data, ..., control = control_fit_workflowset()) { if (!inherits(object, "workflow_set")){ - rlang::abort("object argument must be a `workflow_set` object generated by workflowsets::workflow_set() function.") + cli::cli_abort("object argument must be a `workflow_set` object generated by the {.fn workflowsets::workflow_set} function.") } # Parallel or Sequential @@ -66,7 +66,7 @@ modeltime_fit_workflowset <- function(object, data, ..., control = control_fit_w names(models) <- NULL - .model_desc <- object %>% dplyr::pull(1) %>% stringr::str_to_upper() + .model_desc <- object %>% dplyr::pull(1) %>% toupper() modeltime_tbl <- models %>% as_modeltime_table_from_workflowset(.model_desc = .model_desc) diff --git a/R/modeltime-forecast-plot.R b/R/modeltime-forecast-plot.R index 1ecc9324..08b3afa8 100644 --- a/R/modeltime-forecast-plot.R +++ b/R/modeltime-forecast-plot.R @@ -18,7 +18,7 @@ #' #' #' @examples -#' library(tidyverse) +#' library(dplyr) #' library(lubridate) #' library(timetk) #' library(parsnip) diff --git a/R/modeltime-forecast.R b/R/modeltime-forecast.R index 0b009903..70ef6ea3 100644 --- a/R/modeltime-forecast.R +++ b/R/modeltime-forecast.R @@ -156,8 +156,7 @@ #' _Journal of the American Statistical Association_ 113.523 (2018): 1094-1111. #' #' @examples -#' library(tidyverse) -#' library(lubridate) +#' library(dplyr) #' library(timetk) #' library(parsnip) #' library(rsample) diff --git a/R/modeltime-recursive.R b/R/modeltime-recursive.R index 20edb746..681609ce 100644 --- a/R/modeltime-recursive.R +++ b/R/modeltime-recursive.R @@ -71,8 +71,8 @@ #' \donttest{ #' # Libraries & Setup ---- #' library(tidymodels) -#' library(tidyverse) -#' library(lubridate) +#' library(dplyr) +#' library(tidyr) #' library(timetk) #' library(slider) #' diff --git a/R/modeltime-refit.R b/R/modeltime-refit.R index ad8d21b3..e06e80b1 100644 --- a/R/modeltime-refit.R +++ b/R/modeltime-refit.R @@ -49,7 +49,7 @@ #' #' #' @examples -#' library(tidyverse) +#' library(dplyr) #' library(lubridate) #' library(timetk) #' library(parsnip) diff --git a/R/modeltime-residuals-plot.R b/R/modeltime-residuals-plot.R index 2426530f..705954fb 100644 --- a/R/modeltime-residuals-plot.R +++ b/R/modeltime-residuals-plot.R @@ -21,8 +21,7 @@ #' #' #' @examples -#' library(tidyverse) -#' library(lubridate) +#' library(dplyr) #' library(timetk) #' library(parsnip) #' library(rsample) diff --git a/R/modeltime-residuals-tests.R b/R/modeltime-residuals-tests.R index 561251ec..e8516bf6 100644 --- a/R/modeltime-residuals-tests.R +++ b/R/modeltime-residuals-tests.R @@ -57,8 +57,7 @@ #' @return A tibble with with the p-values of the calculated statistical tests. #' #' @examples -#' library(tidyverse) -#' library(lubridate) +#' library(dplyr) #' library(timetk) #' library(parsnip) #' library(rsample) @@ -103,7 +102,6 @@ NULL #' @export #' @rdname modeltime_residuals_test -#' modeltime_residuals_test <- function(object, new_data = NULL, lag = 1, diff --git a/R/modeltime-residuals.R b/R/modeltime-residuals.R index 5e6ed5cf..b50bd049 100644 --- a/R/modeltime-residuals.R +++ b/R/modeltime-residuals.R @@ -13,7 +13,7 @@ #' @return A tibble with residuals. #' #' @examples -#' library(tidyverse) +#' library(dplyr) #' library(lubridate) #' library(timetk) #' library(parsnip) diff --git a/R/modeltime-table.R b/R/modeltime-table.R index ff059b45..6ca504d2 100644 --- a/R/modeltime-table.R +++ b/R/modeltime-table.R @@ -22,8 +22,7 @@ #' Modeltime Tables from models stored in a `list`. #' #' @examples -#' library(tidyverse) -#' library(lubridate) +#' library(dplyr) #' library(timetk) #' library(parsnip) #' library(rsample) diff --git a/R/nested-modeltime_data_prep.R b/R/nested-modeltime_data_prep.R index 5d26abcf..2325c724 100644 --- a/R/nested-modeltime_data_prep.R +++ b/R/nested-modeltime_data_prep.R @@ -69,13 +69,12 @@ #' preprocessing recipes using the `recipes` package. #' #' @examples -#' -#' library(tidyverse) +#' library(dplyr) #' library(timetk) #' #' #' nested_data_tbl <- walmart_sales_weekly %>% -#' select(id, date = Date, value = Weekly_Sales) +#' select(id, date = Date, value = Weekly_Sales) %>% #' #' # Step 1: Extends the time series by id #' extend_timeseries( @@ -90,7 +89,7 @@ #' .length_future = 52 #' ) %>% #' -#' # Step 3: Adds a column .splits that contains training/testing indicies +#' # Step 3: Adds a column .splits that contains training/testing indices #' split_nested_timeseries( #' .length_test = 52 #' ) @@ -101,7 +100,7 @@ #' extract_nested_train_split(nested_data_tbl, .row_id = 1) #' #' @name prep_nested - +NULL #' @export #' @rdname prep_nested diff --git a/man/combine_modeltime_tables.Rd b/man/combine_modeltime_tables.Rd index 0310b622..b87aba06 100644 --- a/man/combine_modeltime_tables.Rd +++ b/man/combine_modeltime_tables.Rd @@ -35,8 +35,8 @@ combined Modeltime Table. } \examples{ library(tidymodels) -library(tidyverse) library(timetk) +library(dplyr) library(lubridate) # Setup diff --git a/man/modeltime_accuracy.Rd b/man/modeltime_accuracy.Rd index 45e68796..e369f01c 100644 --- a/man/modeltime_accuracy.Rd +++ b/man/modeltime_accuracy.Rd @@ -53,7 +53,7 @@ The following accuracy metrics are included by default via \code{\link[=default_ } \examples{ library(tidymodels) -library(tidyverse) +library(dplyr) library(lubridate) library(timetk) diff --git a/man/modeltime_calibrate.Rd b/man/modeltime_calibrate.Rd index 5561f996..1b4d0f60 100644 --- a/man/modeltime_calibrate.Rd +++ b/man/modeltime_calibrate.Rd @@ -58,7 +58,7 @@ calculated from \code{new_data} (Test Data) } } \examples{ -library(tidyverse) +library(dplyr) library(lubridate) library(timetk) library(parsnip) diff --git a/man/modeltime_fit_workflowset.Rd b/man/modeltime_fit_workflowset.Rd index ee1e06ba..2dc25b7c 100644 --- a/man/modeltime_fit_workflowset.Rd +++ b/man/modeltime_fit_workflowset.Rd @@ -31,7 +31,7 @@ time series either sequentially or in parallel. \examples{ library(tidymodels) library(workflowsets) -library(tidyverse) +library(dplyr) library(lubridate) library(timetk) diff --git a/man/modeltime_forecast.Rd b/man/modeltime_forecast.Rd index 37193859..ee495f83 100644 --- a/man/modeltime_forecast.Rd +++ b/man/modeltime_forecast.Rd @@ -182,8 +182,7 @@ By default, \code{modeltime_forecast()} keeps the original order of the data. If desired, the user can sort the output by \code{.key}, \code{.model_id} and \code{.index}. } \examples{ -library(tidyverse) -library(lubridate) +library(dplyr) library(timetk) library(parsnip) library(rsample) diff --git a/man/modeltime_refit.Rd b/man/modeltime_refit.Rd index 197437ff..ff3ac34b 100644 --- a/man/modeltime_refit.Rd +++ b/man/modeltime_refit.Rd @@ -53,7 +53,7 @@ The \code{modeltime_refit()} function is used to retrain models trained with \co The XY format is not supported at this time. } \examples{ -library(tidyverse) +library(dplyr) library(lubridate) library(timetk) library(parsnip) diff --git a/man/modeltime_residuals.Rd b/man/modeltime_residuals.Rd index c501bdff..5eb33fd0 100644 --- a/man/modeltime_residuals.Rd +++ b/man/modeltime_residuals.Rd @@ -23,7 +23,7 @@ A tibble with residuals. This is a convenience function to unnest model residuals } \examples{ -library(tidyverse) +library(dplyr) library(lubridate) library(timetk) library(parsnip) diff --git a/man/modeltime_residuals_test.Rd b/man/modeltime_residuals_test.Rd index 11c066f1..9614a714 100644 --- a/man/modeltime_residuals_test.Rd +++ b/man/modeltime_residuals_test.Rd @@ -64,8 +64,7 @@ The Durbin Watson test reports a test statistic, with a value from 0 to 4, where } } \examples{ -library(tidyverse) -library(lubridate) +library(dplyr) library(timetk) library(parsnip) library(rsample) diff --git a/man/modeltime_table.Rd b/man/modeltime_table.Rd index 050ef5c6..6224e099 100644 --- a/man/modeltime_table.Rd +++ b/man/modeltime_table.Rd @@ -34,8 +34,7 @@ Converts a \code{list} of models to a modeltime table. Useful if programatically Modeltime Tables from models stored in a \code{list}. } \examples{ -library(tidyverse) -library(lubridate) +library(dplyr) library(timetk) library(parsnip) library(rsample) diff --git a/man/new_modeltime_bridge.Rd b/man/new_modeltime_bridge.Rd index fb545311..289e889a 100644 --- a/man/new_modeltime_bridge.Rd +++ b/man/new_modeltime_bridge.Rd @@ -24,8 +24,7 @@ These functions are used to construct new \code{modeltime} bridge functions that connect the \code{tidymodels} infrastructure to time-series models containing date or date-time features. } \examples{ -library(stats) -library(tidyverse) +library(dplyr) library(lubridate) library(timetk) diff --git a/man/plot_modeltime_forecast.Rd b/man/plot_modeltime_forecast.Rd index 3c53dd8d..84d3e19b 100644 --- a/man/plot_modeltime_forecast.Rd +++ b/man/plot_modeltime_forecast.Rd @@ -84,7 +84,7 @@ This is a wrapper for \code{\link[=plot_time_series]{plot_time_series()}} that g (\code{ggplot2}) plot with the forecasted data. } \examples{ -library(tidyverse) +library(dplyr) library(lubridate) library(timetk) library(parsnip) diff --git a/man/plot_modeltime_residuals.Rd b/man/plot_modeltime_residuals.Rd index 65f87c60..d40adad9 100644 --- a/man/plot_modeltime_residuals.Rd +++ b/man/plot_modeltime_residuals.Rd @@ -60,8 +60,7 @@ This is a wrapper for examining residuals using: } } \examples{ -library(tidyverse) -library(lubridate) +library(dplyr) library(timetk) library(parsnip) library(rsample) diff --git a/man/prep_nested.Rd b/man/prep_nested.Rd index 970d30fd..6978663a 100644 --- a/man/prep_nested.Rd +++ b/man/prep_nested.Rd @@ -91,13 +91,12 @@ preprocessing recipes using the \code{recipes} package. } } \examples{ - -library(tidyverse) +library(dplyr) library(timetk) nested_data_tbl <- walmart_sales_weekly \%>\% - select(id, date = Date, value = Weekly_Sales) + select(id, date = Date, value = Weekly_Sales) \%>\% # Step 1: Extends the time series by id extend_timeseries( @@ -112,7 +111,7 @@ nested_data_tbl <- walmart_sales_weekly \%>\% .length_future = 52 ) \%>\% - # Step 3: Adds a column .splits that contains training/testing indicies + # Step 3: Adds a column .splits that contains training/testing indices split_nested_timeseries( .length_test = 52 ) diff --git a/man/recursive.Rd b/man/recursive.Rd index e942868f..2b55e5a6 100644 --- a/man/recursive.Rd +++ b/man/recursive.Rd @@ -81,8 +81,8 @@ The \code{recursive()} function can be used for Panel Data with the following mo \donttest{ # Libraries & Setup ---- library(tidymodels) -library(tidyverse) -library(lubridate) +library(dplyr) +library(tidyr) library(timetk) library(slider) diff --git a/man/table_modeltime_accuracy.Rd b/man/table_modeltime_accuracy.Rd index ebfcfff5..7c5a25d6 100644 --- a/man/table_modeltime_accuracy.Rd +++ b/man/table_modeltime_accuracy.Rd @@ -83,7 +83,7 @@ Table customization is implemented using a piping workflow (\verb{\%>\%}). For more information, refer to the \href{https://gt.rstudio.com/index.html}{GT Documentation}. } \examples{ -library(tidyverse) +library(dplyr) library(lubridate) library(timetk) library(parsnip) diff --git a/tests/testthat.R b/tests/testthat.R index b3ca678c..5abb7b49 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -21,6 +21,4 @@ library(slider) library(timetk) library(modeltime) -library(tidyverse) - test_check("modeltime") diff --git a/tests/testthat/test-algo-adam_reg-Adam.R b/tests/testthat/test-algo-adam_reg-Adam.R index 749d435b..ef47b37e 100644 --- a/tests/testthat/test-algo-adam_reg-Adam.R +++ b/tests/testthat/test-algo-adam_reg-Adam.R @@ -54,7 +54,7 @@ test_that("adam_reg: Adam, (No xregs), Test Model Fit Object", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $preproc @@ -175,7 +175,7 @@ test_that("adam_reg: Adam (workflow)", { expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") - expect_true(is.null(wflw_fit$fit$fit$fit$extras$xreg_recipe)) + expect_null(wflw_fit$fit$fit$fit$extras$xreg_recipe) # $preproc mld <- wflw_fit %>% workflows::extract_mold() diff --git a/tests/testthat/test-algo-adam_reg-auto_adam.R b/tests/testthat/test-algo-adam_reg-auto_adam.R index 64d56883..18c7dcbe 100644 --- a/tests/testthat/test-algo-adam_reg-auto_adam.R +++ b/tests/testthat/test-algo-adam_reg-auto_adam.R @@ -44,7 +44,7 @@ test_that("adam_reg: Auto ADAM, (No xregs), Test Model Fit Object", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $preproc @@ -183,7 +183,7 @@ test_that("adam_reg: Auto ADAM (workflow), Test Model Fit Object", { expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") - expect_true(is.null(wflw_fit$fit$fit$fit$extras$xreg_recipe)) + expect_null(wflw_fit$fit$fit$fit$extras$xreg_recipe) # $preproc mld <- wflw_fit %>% workflows::extract_mold() diff --git a/tests/testthat/test-algo-arima_boost-Arima.R b/tests/testthat/test-algo-arima_boost-Arima.R index 21124960..99a3e0ef 100644 --- a/tests/testthat/test-algo-arima_boost-Arima.R +++ b/tests/testthat/test-algo-arima_boost-Arima.R @@ -15,7 +15,7 @@ library(tune) library(dials) library(yardstick) library(timetk) -library(tidyverse) +library(dplyr) library(lubridate) @@ -77,7 +77,7 @@ test_that("arima_boost: Arima, (No xregs), Test Model Fit Object", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $fit xgboost diff --git a/tests/testthat/test-algo-arima_boost-auto_arima.R b/tests/testthat/test-algo-arima_boost-auto_arima.R index 048f67c8..67318a57 100644 --- a/tests/testthat/test-algo-arima_boost-auto_arima.R +++ b/tests/testthat/test-algo-arima_boost-auto_arima.R @@ -61,7 +61,7 @@ test_that("arima_boost: Arima, (No xregs), Test Model Fit Object", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $fit xgboost diff --git a/tests/testthat/test-algo-arima_reg-Arima.R b/tests/testthat/test-algo-arima_reg-Arima.R index ff763889..27ee4add 100644 --- a/tests/testthat/test-algo-arima_reg-Arima.R +++ b/tests/testthat/test-algo-arima_reg-Arima.R @@ -56,7 +56,7 @@ test_that("arima_reg: Arima, (No xregs), Test Model Fit Object", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $preproc @@ -186,7 +186,7 @@ test_that("arima_reg: Arima (workflow), Test Model Fit Object", { expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") - expect_true(is.null(wflw_fit$fit$fit$fit$extras$xreg_recipe)) + expect_null(wflw_fit$fit$fit$fit$extras$xreg_recipe) # $preproc mld <- wflw_fit %>% workflows::extract_mold() diff --git a/tests/testthat/test-algo-arima_reg-auto_arima.R b/tests/testthat/test-algo-arima_reg-auto_arima.R index 35d766cb..36502eca 100644 --- a/tests/testthat/test-algo-arima_reg-auto_arima.R +++ b/tests/testthat/test-algo-arima_reg-auto_arima.R @@ -42,7 +42,7 @@ test_that("arima_reg: auto.arima (No xregs), Test Model Fit Object", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $preproc @@ -170,11 +170,11 @@ test_that("arima_reg: auto.arima (Workflow), Test Model Fit Object", { expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") - expect_true(is.null(wflw_fit$fit$fit$fit$extras$xreg_recipe)) + expect_null(wflw_fit$fit$fit$fit$extras$xreg_recipe) # $preproc mld <- wflw_fit %>% workflows::extract_mold() - expect_equal(names(mld$outcomes), "value") + expect_named(mld$outcomes, "value") # arima_reg: auto.arima (Workflow), Test Predictions diff --git a/tests/testthat/test-algo-exp_smoothing-ets.R b/tests/testthat/test-algo-exp_smoothing-ets.R index e0846781..e21045f3 100644 --- a/tests/testthat/test-algo-exp_smoothing-ets.R +++ b/tests/testthat/test-algo-exp_smoothing-ets.R @@ -45,7 +45,7 @@ test_that("exp_smoothing: ets, Test Model Fit Object", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $preproc @@ -123,7 +123,7 @@ test_that("exp_smoothing: Arima (workflow), Test Model Fit Object", { expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") - expect_true(is.null(wflw_fit$fit$fit$fit$extras$xreg_recipe)) + expect_null(wflw_fit$fit$fit$fit$extras$xreg_recipe) # $preproc mld <- wflw_fit %>% workflows::extract_mold() @@ -198,7 +198,7 @@ test_that("exp_smoothing: CROSTON", { expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") - expect_true(is.null(wflw_fit$fit$fit$fit$extras$xreg_recipe)) + expect_null(wflw_fit$fit$fit$fit$extras$xreg_recipe) # $preproc mld <- wflw_fit %>% workflows::extract_mold() @@ -272,7 +272,7 @@ test_that("exp_smoothing: Theta", { expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") - expect_true(is.null(wflw_fit$fit$fit$fit$extras$xreg_recipe)) + expect_null(wflw_fit$fit$fit$fit$extras$xreg_recipe) # $preproc mld <- wflw_fit %>% workflows::extract_mold() diff --git a/tests/testthat/test-algo-nnetar_reg.R b/tests/testthat/test-algo-nnetar_reg.R index dbe57b4a..88ce60d4 100644 --- a/tests/testthat/test-algo-nnetar_reg.R +++ b/tests/testthat/test-algo-nnetar_reg.R @@ -52,7 +52,7 @@ test_that("nnetar_reg: Parsnip", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) expect_identical(model_fit$fit$models$model_1$p, 3) diff --git a/tests/testthat/test-conf_by_id.R b/tests/testthat/test-conf_by_id.R index 4d191c83..17462ced 100644 --- a/tests/testthat/test-conf_by_id.R +++ b/tests/testthat/test-conf_by_id.R @@ -7,11 +7,11 @@ test_that("Confidence and Accuracy by ID", { library(tidymodels) library(timetk) - library(tidyverse) + library(dplyr) # Data data <- walmart_sales_weekly %>% - select(ID = id, date = Date, value = Weekly_Sales) + dplyr::select(ID = id, date = Date, value = Weekly_Sales) splits <- data %>% time_series_split(assess = "3 months", cumulative = TRUE) diff --git a/tests/testthat/test-fit_workflowsets.R b/tests/testthat/test-fit_workflowsets.R index 51802ceb..81a25e46 100644 --- a/tests/testthat/test-fit_workflowsets.R +++ b/tests/testthat/test-fit_workflowsets.R @@ -2,7 +2,7 @@ context("WORKFLOWSETS") library(tidymodels) library(workflowsets) -library(tidyverse) +library(dplyr) library(timetk) @@ -21,12 +21,12 @@ test_that("Workflowsets Tests", { rec1 <- recipes::recipe(value ~ date + id, data_set) %>% recipes::step_mutate(date_num = as.numeric(date)) %>% recipes::step_mutate(month_lbl = lubridate::month(date, label = TRUE)) %>% - step_dummy(all_nominal(), one_hot = TRUE) + recipes::step_dummy(recipes::all_nominal(), one_hot = TRUE) rec2 <- recipes::recipe(value ~ date + id, data_set) %>% recipes::step_mutate(date_num = as.numeric(date)) %>% recipes::step_mutate(month_lbl = lubridate::month(date, label = TRUE)) %>% - step_dummy(all_nominal(), one_hot = TRUE) %>% + recipes::step_dummy(recipes::all_nominal(), one_hot = TRUE) %>% step_ts_clean(value) mod_spec_prophet <- prophet_reg() %>% @@ -43,7 +43,7 @@ test_that("Workflowsets Tests", { ), cross = TRUE ) %>% - mutate(.model_id = row_number()) # Generate ID for linking to the fitted modeltime tabe + mutate(.model_id = dplyr::row_number()) # Generate ID for linking to the fitted modeltime tabe diff --git a/tests/testthat/test-modeltime_residuals.R b/tests/testthat/test-modeltime_residuals.R index 0667e05d..9688edb0 100644 --- a/tests/testthat/test-modeltime_residuals.R +++ b/tests/testthat/test-modeltime_residuals.R @@ -6,7 +6,7 @@ test_that("modeltime_residuals(): Returns correct order", { skip_on_cran() library(tidymodels) - library(tidyverse) + library(dplyr) library(timetk) diff --git a/tests/testthat/test-modeltime_table-no-calib-refit.R b/tests/testthat/test-modeltime_table-no-calib-refit.R index 1909ed00..e5c70647 100644 --- a/tests/testthat/test-modeltime_table-no-calib-refit.R +++ b/tests/testthat/test-modeltime_table-no-calib-refit.R @@ -4,7 +4,7 @@ context("TEST FORECASTING WITH NO CALIBRATION") # SIMPLE PREDICTION ---- # library(tidymodels) -# library(tidyverse) +# library(dplyr) # library(timetk) # library(lubridate) diff --git a/tests/testthat/test-nested-modeltime.R b/tests/testthat/test-nested-modeltime.R index 4fbd934e..154b45bd 100644 --- a/tests/testthat/test-nested-modeltime.R +++ b/tests/testthat/test-nested-modeltime.R @@ -12,7 +12,7 @@ test_that("MODELTIME NESTED (ITERATIVE) FORECASTING", { # SETUP library(tidymodels) - library(tidyverse) + library(dplyr) library(timetk) diff --git a/tests/testthat/test-recursive-chunk-uneven.R b/tests/testthat/test-recursive-chunk-uneven.R index b7172314..4190f935 100644 --- a/tests/testthat/test-recursive-chunk-uneven.R +++ b/tests/testthat/test-recursive-chunk-uneven.R @@ -22,16 +22,15 @@ test_that("Chunked Recursive Tests - Uneven ", { # Lag Recipe recipe_lag <- recipes::recipe(value ~ date, m750_extended) %>% - step_lag(value, lag = c(3,6,9,12)) + recipes::step_lag(value, lag = c(3,6,9,12)) # Data Transformation - m750_lagged <- recipe_lag %>% prep() %>% juice() + m750_lagged <- recipe_lag %>% recipes::prep() %>% recipes::juice() - train_data <- m750_lagged %>% - drop_na() + train_data <- tidyr::drop_na(m750_lagged) future_data <- m750_lagged %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- @@ -114,13 +113,12 @@ test_that("Chunked Recursive Tests - Uneven ", { # Data Preparation m750_lagged <- m750_extended %>% lag_transformer() %>% - select(-id) + dplyr::select(-id) - train_data <- m750_lagged %>% - drop_na() + train_data <- drop_na(m750_lagged) future_data <- m750_lagged %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- wflw_fit_lm <- workflows::workflow() %>% @@ -216,15 +214,14 @@ test_that("Chunked Recursive Tests - Uneven ", { data %>% dplyr::group_by(id) %>% # Lags - tk_augment_lags(value, .lags = c(3,6,9,12)) %>% + timetk::tk_augment_lags(value, .lags = c(3,6,9,12)) %>% dplyr::ungroup() } m4_lags <- m4_extended %>% lag_transformer_grouped() - train_data <- m4_lags %>% - tidyr::drop_na() + train_data <- tidyr::drop_na(m4_lags) future_data <- m4_lags %>% dplyr::filter(is.na(value)) @@ -352,11 +349,10 @@ test_that("Chunked Recursive Tests - Single ", { # Data Transformation m750_lagged <- recipe_lag %>% prep() %>% juice() - train_data <- m750_lagged %>% - drop_na() + train_data <- tidyr::drop_na(m750_lagged) future_data <- m750_lagged %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- @@ -439,13 +435,12 @@ test_that("Chunked Recursive Tests - Single ", { # Data Preparation m750_lagged <- m750_extended %>% lag_transformer() %>% - select(-id) + dplyr::select(-id) - train_data <- m750_lagged %>% - drop_na() + train_data <- tidyr::drop_na(m750_lagged) future_data <- m750_lagged %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- wflw_fit_lm <- workflows::workflow() %>% diff --git a/tests/testthat/test-recursive-chunk.R b/tests/testthat/test-recursive-chunk.R index d86f9ec9..d702286f 100644 --- a/tests/testthat/test-recursive-chunk.R +++ b/tests/testthat/test-recursive-chunk.R @@ -27,8 +27,7 @@ test_that("Chunked Recursive Tests ", { # Data Transformation m750_lagged <- recipe_lag %>% recipes::prep() %>% recipes::juice() - train_data <- m750_lagged %>% - tidyr::drop_na() + train_data <- tidyr::drop_na(m750_lagged) future_data <- m750_lagged %>% dplyr::filter(is.na(value)) @@ -227,11 +226,10 @@ test_that("Chunked Recursive Tests ", { m4_lags <- m4_extended %>% lag_transformer_grouped() - train_data <- m4_lags %>% - drop_na() + train_data <- tidyr::drop_na(m4_lags) future_data <- m4_lags %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- diff --git a/tests/testthat/test-recursive.R b/tests/testthat/test-recursive.R index 2d3f24f8..fc0e23bf 100644 --- a/tests/testthat/test-recursive.R +++ b/tests/testthat/test-recursive.R @@ -27,11 +27,10 @@ test_that("Recursive Tests ", { # Data Transformation m750_lagged <- recipe_lag %>% prep() %>% juice() - train_data <- m750_lagged %>% - drop_na() + train_data <- tidyr::drop_na(m750_lagged) future_data <- m750_lagged %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- @@ -118,13 +117,12 @@ test_that("Recursive Tests ", { # Data Preparation m750_lagged <- m750_extended %>% lag_transformer() %>% - select(-id) + dplyr::select(-id) - train_data <- m750_lagged %>% - drop_na() + train_data <- tidyr::drop_na(m750_lagged) future_data <- m750_lagged %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- wflw_fit_lm <- workflows::workflow() %>% @@ -226,11 +224,10 @@ test_that("Recursive Tests ", { m4_lags <- m4_extended %>% lag_transformer_grouped() - train_data <- m4_lags %>% - drop_na() + train_data <- drop_na(m4_lags) future_data <- m4_lags %>% - filter(is.na(value)) + dplyr::filter(is.na(value)) # * Recursive Modeling ---- diff --git a/vignettes/extending-modeltime.Rmd b/vignettes/extending-modeltime.Rmd index 1e370820..0ebd7d7b 100644 --- a/vignettes/extending-modeltime.Rmd +++ b/vignettes/extending-modeltime.Rmd @@ -67,7 +67,7 @@ __Note - I've added a `seasonal_reg()` function that includes the functionality To use the code in this tutorial, you'll need to have the following packages installed. -```{r setup} +```r library(parsnip) library(forecast) library(rsample) @@ -77,6 +77,18 @@ library(timetk) library(rlang) ``` +```{r setup, include=FALSE} +library(parsnip) +library(forecast) +library(rsample) +library(modeltime) +library(dplyr) +library(stringr) +library(timetk) +library(rlang) +``` + + ### Data We'll use `taylor_30_min` data from the `timetk` package, which is electricity demand data at a 30-minute interval. @@ -243,7 +255,7 @@ bridge_stlm_ets_fit_impl <- function(x, y, period_seasonal_1 = NULL, period_seas outcome_msts <- forecast::msts(outcome, seasonal.periods = seasonal.periods) # 2. Predictors - Handle Dates - index_tbl <- modeltime::parse_index_from_data(predictors) + index_tbl <- parse_index_from_data(predictors) idx_col <- names(index_tbl) idx <- timetk::tk_index(index_tbl) @@ -260,14 +272,14 @@ bridge_stlm_ets_fit_impl <- function(x, y, period_seasonal_1 = NULL, period_seas new_modeltime_bridge( class = "bridge_stlm_ets_fit_impl", models = list(model_1 = model_1), - data = tibble::tibble( + data = tibble( idx_col := idx, .actual = y, .fitted = model_1$fitted, .residuals = model_1$residuals ), extras = list(NULL), # Can add xreg preprocessors here - desc = stringr::str_c("STLM Model: ", model_1$model$method) + desc = str_c("STLM Model: ", model_1$model$method) ) } @@ -379,21 +391,17 @@ We're now ready to register the prediction function we've created. ```{r, paged.print = FALSE} set_pred( - model = "decomposition_reg", - eng = "stlm_ets", - mode = "regression", - type = "numeric", - value = list( - pre = NULL, - post = NULL, - func = c(fun = "predict"), - args = - list( - object = rlang::expr(object$fit), - new_data = rlang::expr(new_data) - ) + model = "decomposition_reg", + eng = "stlm_ets", + mode = "regression", + type = "numeric", + value = list(pre = NULL, + post = NULL, + func = c(fun = "predict"), + args = list(object = rlang::expr(object$fit), + new_data = rlang::expr(new_data)) + ) ) -) show_model_info("decomposition_reg") ``` diff --git a/vignettes/getting-started-with-modeltime.Rmd b/vignettes/getting-started-with-modeltime.Rmd index c635a15e..fcd26cec 100644 --- a/vignettes/getting-started-with-modeltime.Rmd +++ b/vignettes/getting-started-with-modeltime.Rmd @@ -78,11 +78,22 @@ Let's go through a guided tour to kick the tires on `modeltime`. Load libraries to complete this short tutorial. -```{r} +```r library(xgboost) library(tidymodels) library(modeltime) library(tidyverse) +library(timetk) + +# This toggles plots from plotly (interactive) to ggplot (static) +interactive <- FALSE +``` + +```{r, include=FALSE} +library(xgboost) +library(tidymodels) +library(modeltime) +library(dplyr) library(lubridate) library(timetk) @@ -203,6 +214,7 @@ We can model a Multivariate Adaptive Regression Spline model using `mars()`. I'v ```{r, message=TRUE} # Model 6: earth ---- + model_spec_mars <- mars(mode = "regression") %>% set_engine("earth") diff --git a/vignettes/modeltime-spark.Rmd b/vignettes/modeltime-spark.Rmd index be5713fc..b3c05a77 100644 --- a/vignettes/modeltime-spark.Rmd +++ b/vignettes/modeltime-spark.Rmd @@ -44,12 +44,20 @@ This tutorial requires: Load the following libraries. -```{r} -library(sparklyr) -library(tidymodels) +```r library(modeltime) +library(tidymodels) library(tidyverse) library(timetk) +library(sparklyr) +``` + +```{r. include = FALSE} +library(modeltime) +library(tidymodels) +library(dplyr) +library(timetk) +library(sparklyr) ``` # Spark Connection diff --git a/vignettes/nested-forecasting.Rmd b/vignettes/nested-forecasting.Rmd index 21486359..09cc7f06 100644 --- a/vignettes/nested-forecasting.Rmd +++ b/vignettes/nested-forecasting.Rmd @@ -64,13 +64,21 @@ knitr::include_graphics("nested-logs.jpg") We'll go through a short tutorial on __Nested Forecasting.__ The first thing to do is to load the following libraries: -```{r} -library(tidymodels) +```r library(modeltime) +library(tidymodels) library(tidyverse) library(timetk) ``` +```{r. include = FALSE} +library(modeltime) +library(tidymodels) +library(dplyr) +library(timetk) +``` + + ### Dataset Next, let's use the `walmart_sales_weekly` dataset that comes with `timetk`. diff --git a/vignettes/parallel-processing.Rmd b/vignettes/parallel-processing.Rmd index 09bb930d..430ccd33 100644 --- a/vignettes/parallel-processing.Rmd +++ b/vignettes/parallel-processing.Rmd @@ -50,7 +50,8 @@ Let's go through a common __Hyperparameter Tuning__ workflow that shows off the Load the following libraries. -```{r, message=FALSE, warning=FALSE} + +```r # Machine Learning library(modeltime) library(tidymodels) @@ -61,6 +62,19 @@ library(tidyverse) library(timetk) ``` +```{r. include = FALSE} +# Machine Learning +library(modeltime) +library(tidymodels) +library(workflowsets) + +# Core +library(dplyr) +library(tidyr) +library(timetk) +``` + + ## Setup Parallel Backend The `modeltime` package uses `parallel_start()` to simplify setup, which integrates multiple backend options for parallel processing including: diff --git a/vignettes/recursive-forecasting.Rmd b/vignettes/recursive-forecasting.Rmd index 867e8249..8dfc0b51 100644 --- a/vignettes/recursive-forecasting.Rmd +++ b/vignettes/recursive-forecasting.Rmd @@ -58,10 +58,18 @@ A solution that `recursive()` implements is to iteratively fill these missing va Load the following libraries. -```{r} +```r library(modeltime) library(tidymodels) library(tidyverse) +library(timetk) +``` + +```{r. include = FALSE} +library(modeltime) +library(tidymodels) +library(dplyr) +library(tidyr) library(lubridate) library(timetk) ``` @@ -84,11 +92,11 @@ We can visualize the data with `plot_time_series()`. ```{r} m750 %>% plot_time_series( - .date_var = date, - .value = value, - .facet_var = id, - .smooth = F, - .interactive = F + .date_var = date, + .value = value, + .facet_var = id, + .smooth = FALSE, + .interactive = FALSE ) ``` @@ -118,7 +126,7 @@ lag_roll_transformer <- function(data){ tk_augment_lags(value, .lags = 1:FORECAST_HORIZON) %>% tk_augment_slidify( contains("lag12"), - .f = ~mean(.x, na.rm = T), + .f = ~mean(.x, na.rm = TRUE), .period = 12, .partial = TRUE ) @@ -265,8 +273,8 @@ m4_monthly %>% .value = value, .facet_var = id, .facet_ncol = 2, - .smooth = F, - .interactive = F + .smooth = FALSE, + .interactive = FALSE ) ``` From 5e419a80dd81261fc9ea4585b321866d770f4000 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 22:50:33 -0400 Subject: [PATCH 09/16] More `expect_null()` --- tests/testthat/test-algo-exp_smoothing-ets.R | 2 +- tests/testthat/test-algo-nnetar_reg.R | 2 +- tests/testthat/test-algo-prophet_boost.R | 4 ++-- tests/testthat/test-algo-prophet_reg.R | 4 ++-- tests/testthat/test-algo-seasonal_decomp_ets.R | 4 ++-- tests/testthat/test-algo-seasonal_reg_tbats.R | 4 ++-- tests/testthat/test-algo-temporal_hierarchy.R | 4 ++-- tests/testthat/test-modeltime_residuals.R | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/testthat/test-algo-exp_smoothing-ets.R b/tests/testthat/test-algo-exp_smoothing-ets.R index e21045f3..7b0e4049 100644 --- a/tests/testthat/test-algo-exp_smoothing-ets.R +++ b/tests/testthat/test-algo-exp_smoothing-ets.R @@ -337,7 +337,7 @@ test_that("exp_smoothing: smooth", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $preproc diff --git a/tests/testthat/test-algo-nnetar_reg.R b/tests/testthat/test-algo-nnetar_reg.R index 88ce60d4..760623e1 100644 --- a/tests/testthat/test-algo-nnetar_reg.R +++ b/tests/testthat/test-algo-nnetar_reg.R @@ -186,7 +186,7 @@ test_that("nnetar_reg: (workflow)", { expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") - expect_true(is.null(wflw_fit$fit$fit$fit$extras$xreg_recipe)) + expect_null(wflw_fit$fit$fit$fit$extras$xreg_recipe) expect_identical(wflw_fit$fit$fit$fit$models$model_1$p, 3) expect_identical(wflw_fit$fit$fit$fit$models$model_1$P, 1) diff --git a/tests/testthat/test-algo-prophet_boost.R b/tests/testthat/test-algo-prophet_boost.R index b04e8680..d0bf3dad 100644 --- a/tests/testthat/test-algo-prophet_boost.R +++ b/tests/testthat/test-algo-prophet_boost.R @@ -57,7 +57,7 @@ test_that("prophet_boost: No Xregs", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $fit PROPHET @@ -347,7 +347,7 @@ test_that("prophet_reg: prophet, Logistic Growth", { expect_identical(model_fit$fit$extras$logistic_params$growth, "logistic") expect_identical(model_fit$fit$extras$logistic_params$logistic_cap, 11000) - expect_true(is.null(model_fit$fit$extras$logistic_params$logistic_floor)) + expect_null(model_fit$fit$extras$logistic_params$logistic_floor) # $preproc diff --git a/tests/testthat/test-algo-prophet_reg.R b/tests/testthat/test-algo-prophet_reg.R index 542c7a67..b73f402f 100644 --- a/tests/testthat/test-algo-prophet_reg.R +++ b/tests/testthat/test-algo-prophet_reg.R @@ -48,7 +48,7 @@ test_that("prophet_reg: prophet, (NO XREGS), Test Model Fit Object", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $fit PROPHET @@ -293,7 +293,7 @@ test_that("prophet_reg: prophet, Logistic Growth", { expect_identical(model_fit$fit$extras$logistic_params$growth, "logistic") expect_identical(model_fit$fit$extras$logistic_params$logistic_cap, 11000) - expect_true(is.null(model_fit$fit$extras$logistic_params$logistic_floor)) + expect_null(model_fit$fit$extras$logistic_params$logistic_floor) # $preproc diff --git a/tests/testthat/test-algo-seasonal_decomp_ets.R b/tests/testthat/test-algo-seasonal_decomp_ets.R index e866a511..ae6e07b6 100644 --- a/tests/testthat/test-algo-seasonal_decomp_ets.R +++ b/tests/testthat/test-algo-seasonal_decomp_ets.R @@ -57,7 +57,7 @@ test_that("seasonal_reg: stlm_ets", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $fit xgboost @@ -113,7 +113,7 @@ test_that("seasonal_reg: stlm_ets", { expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") - expect_true(is.null(wflw_fit$fit$fit$fit$extras$xreg_recipe)) + expect_null(wflw_fit$fit$fit$fit$extras$xreg_recipe) # $fit arima expect_s3_class(wflw_fit$fit$fit$fit$models$model_1, "stlm") diff --git a/tests/testthat/test-algo-seasonal_reg_tbats.R b/tests/testthat/test-algo-seasonal_reg_tbats.R index c3c3ed4e..82ef40d1 100644 --- a/tests/testthat/test-algo-seasonal_reg_tbats.R +++ b/tests/testthat/test-algo-seasonal_reg_tbats.R @@ -57,7 +57,7 @@ test_that("seasonal_reg - tbats: parsnip", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $fit xgboost @@ -123,7 +123,7 @@ test_that("seasonal_reg: workflow", { expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") - expect_true(is.null(wflw_fit$fit$fit$fit$extras$xreg_recipe)) + expect_null(wflw_fit$fit$fit$fit$extras$xreg_recipe) # $fit expect_s3_class(wflw_fit$fit$fit$fit$models$model_1, "tbats") diff --git a/tests/testthat/test-algo-temporal_hierarchy.R b/tests/testthat/test-algo-temporal_hierarchy.R index 014d148b..fd10f134 100644 --- a/tests/testthat/test-algo-temporal_hierarchy.R +++ b/tests/testthat/test-algo-temporal_hierarchy.R @@ -45,7 +45,7 @@ test_that("thief", { expect_equal(names(model_fit$fit$data)[1], "date") - expect_true(is.null(model_fit$fit$extras$xreg_recipe)) + expect_null(model_fit$fit$extras$xreg_recipe) # $preproc @@ -106,7 +106,7 @@ test_that("thief", { expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") - expect_true(is.null(wflw_fit$fit$fit$fit$extras$xreg_recipe)) + expect_null(wflw_fit$fit$fit$fit$extras$xreg_recipe) # $preproc mld <- wflw_fit %>% workflows::extract_mold() diff --git a/tests/testthat/test-modeltime_residuals.R b/tests/testthat/test-modeltime_residuals.R index 9688edb0..19d5c9c2 100644 --- a/tests/testthat/test-modeltime_residuals.R +++ b/tests/testthat/test-modeltime_residuals.R @@ -11,7 +11,7 @@ test_that("modeltime_residuals(): Returns correct order", { data <- walmart_sales_weekly %>% - select(ID = id, date = Date, value = Weekly_Sales) + dplyr::select(ID = id, date = Date, value = Weekly_Sales) splits <- data %>% time_series_split(assess = "3 months", cumulative = TRUE) From 3b49b84b83983de6283349b3eb1ab4bf56894317 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 23:12:12 -0400 Subject: [PATCH 10/16] Fix test + move lubridate to Imports. --- DESCRIPTION | 2 +- ...eltime_table-forecast-accuracy-refitting.R | 53 +++++++++---------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index df09721a..eabb2bd1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -49,6 +49,7 @@ Imports: forecast, xgboost (>= 1.2.0.1), prophet, + lubridate, methods, cli, tidymodels @@ -60,7 +61,6 @@ Suggests: recipes, rsample, tune (>= 0.2.0), - lubridate, testthat, kernlab, glmnet, diff --git a/tests/testthat/test-modeltime_table-forecast-accuracy-refitting.R b/tests/testthat/test-modeltime_table-forecast-accuracy-refitting.R index a6f5aa8e..b5883067 100644 --- a/tests/testthat/test-modeltime_table-forecast-accuracy-refitting.R +++ b/tests/testthat/test-modeltime_table-forecast-accuracy-refitting.R @@ -56,7 +56,7 @@ test_that("Auto ARIMA (Parsnip)", { # ** Accuracy ---- accuracy_tbl <- calibrated_tbl %>% - modeltime_accuracy(metric_set = metric_set(rsq, yardstick::mae)) + modeltime_accuracy(metric_set = yardstick::metric_set(rsq, yardstick::mae)) expect_equal(nrow(accuracy_tbl), 1) @@ -71,7 +71,7 @@ test_that("Auto ARIMA (Parsnip)", { modeltime_refit(data = m750) %>% modeltime_forecast(h = "3 years") - expect_equal(future_forecast_tbl$.index[1], ymd("2015-07-01")) + expect_equal(future_forecast_tbl$.index[1], lubridate::ymd("2015-07-01")) }) @@ -91,7 +91,7 @@ test_that("Auto ARIMA (Workflow)", { parsnip::set_engine("auto_arima") ) %>% workflows::add_recipe( - recipe = recipe(value ~ date, data = rsample::training(splits)) %>% + recipe = recipes::recipe(value ~ date, data = rsample::training(splits)) %>% recipes::step_date(date, features = "month") %>% recipes::step_log(value) ) %>% @@ -123,7 +123,7 @@ test_that("Auto ARIMA (Workflow)", { # ** Accuracy ---- accuracy_tbl <- calibrated_tbl %>% - modeltime_accuracy(metric_set = metric_set(rsq, yardstick::mae)) + modeltime_accuracy(metric_set = yardstick::metric_set(rsq, yardstick::mae)) expect_equal(nrow(accuracy_tbl), 1) @@ -138,7 +138,7 @@ test_that("Auto ARIMA (Workflow)", { modeltime_refit(data = m750) %>% modeltime_forecast(h = "3 years") - expect_equal(future_forecast_tbl$.index[1], ymd("2015-07-01")) + expect_equal(future_forecast_tbl$.index[1], lubridate::ymd("2015-07-01")) }) @@ -160,7 +160,7 @@ test_that("Models for Mega Test", { parsnip::set_engine("auto_arima") ) %>% workflows::add_recipe( - recipe = recipe(value ~ date, data = rsample::training(splits)) %>% + recipe = recipes::recipe(value ~ date, data = rsample::training(splits)) %>% recipes::step_date(date, features = "month") %>% recipes::step_log(value) ) %>% @@ -215,7 +215,7 @@ test_that("Models for Mega Test", { # * LM (Parsnip Model) ---- - model_fit_lm <- linear_reg() %>% + model_fit_lm <- parsnip::linear_reg() %>% parsnip::set_engine("lm") %>% fit(log(value) ~ as.numeric(date) + lubridate::month(date, label = TRUE), data = rsample::training(splits)) @@ -223,7 +223,7 @@ test_that("Models for Mega Test", { # * LM workflow ----- - model_spec <- linear_reg() %>% + model_spec <- parsnip::linear_reg() %>% parsnip::set_engine("lm") recipe_spec <- recipes::recipe(value ~ date, data = rsample::training(splits)) %>% @@ -239,7 +239,7 @@ test_that("Models for Mega Test", { # * MARS (Parsnip Model) ---- skip_if_not_installed("earth") - model_fit_mars <- mars(mode = "regression") %>% + model_fit_mars <- parsnip::mars(mode = "regression") %>% parsnip::set_engine("earth") %>% fit(log(value) ~ as.numeric(date) + lubridate::month(date, label = TRUE), data = rsample::training(splits)) @@ -249,7 +249,7 @@ test_that("Models for Mega Test", { # * MARS (Workflow) ----- - model_spec <- mars(mode = "regression") %>% + model_spec <- parsnip::mars(mode = "regression") %>% parsnip::set_engine("earth") recipe_spec <- recipes::recipe(value ~ date, data = rsample::training(splits)) %>% @@ -268,8 +268,8 @@ test_that("Models for Mega Test", { # * SVM (Parsnip Model) ---- - - model_fit_svm <- svm_rbf(mode = "regression") %>% + skip_if_not_installed("kernlab") + model_fit_svm <- parsnip::svm_rbf(mode = "regression") %>% parsnip::set_engine("kernlab") %>% fit(log(value) ~ as.numeric(date) + lubridate::month(date, label = TRUE), data = rsample::training(splits)) @@ -280,14 +280,14 @@ test_that("Models for Mega Test", { # * SVM (Workflow) ----- skip_if_not_installed("kernlab") - model_spec <- svm_rbf(mode = "regression") %>% + model_spec <- parsnip::svm_rbf(mode = "regression") %>% parsnip::set_engine("kernlab") recipe_spec <- recipes::recipe(value ~ date, data = rsample::training(splits)) %>% recipes::step_date(date, features = "month") %>% - step_rm(date) %>% + recipes::step_rm(date) %>% # SVM requires dummy variables - step_dummy(all_nominal()) %>% + recipes::step_dummy(recipes::all_nominal()) %>% recipes::step_log(value) wflw_fit_svm <- workflows::workflow() %>% @@ -302,7 +302,7 @@ test_that("Models for Mega Test", { # - Not using GLMnet because of requirement for R3.6+ # # Error if penalty value is not included - # model_fit_glmnet <- linear_reg( + # model_fit_glmnet <- parsnip::linear_reg( # penalty = 0.000388 # ) %>% # parsnip::set_engine("glmnet") %>% @@ -322,8 +322,8 @@ test_that("Models for Mega Test", { # recipe_spec <- recipes::recipe(value ~ date, data = rsample::training(splits)) %>% # recipes::step_date(date, features = "month") %>% # recipes::step_mutate(date_num = as.numeric(date)) %>% - # step_rm(date) %>% - # step_dummy(all_nominal()) %>% + # recipes::step_rm(date) %>% + # recipes::step_dummy(recipes::all_nominal()) %>% # recipes::step_log(value) # # wflw_fit_glmnet <- workflows::workflow() %>% @@ -337,7 +337,7 @@ test_that("Models for Mega Test", { # * randomForest (parsnip) ---- - model_fit_randomForest <- rand_forest(mode = "regression") %>% + model_fit_randomForest <- parsnip::rand_forest(mode = "regression") %>% parsnip::set_engine("randomForest") %>% fit(log(value) ~ as.numeric(date) + lubridate::month(date, label = TRUE), data = rsample::training(splits)) @@ -347,14 +347,14 @@ test_that("Models for Mega Test", { # * randomForest (workflow) ---- - model_spec <- rand_forest("regression") %>% + model_spec <- parsnip::rand_forest("regression") %>% parsnip::set_engine("randomForest") recipe_spec <- recipes::recipe(value ~ date, data = rsample::training(splits)) %>% recipes::step_date(date, features = "month") %>% recipes::step_mutate(date_num = as.numeric(date)) %>% - step_rm(date) %>% - step_dummy(all_nominal()) %>% + recipes::step_rm(date) %>% + recipes::step_dummy(recipes::all_nominal()) %>% recipes::step_log(value) wflw_fit_randomForest <- workflows::workflow() %>% @@ -366,7 +366,7 @@ test_that("Models for Mega Test", { # * XGBoost (parsnip) ---- - model_fit_xgboost <- boost_tree(mode = "regression") %>% + model_fit_xgboost <- parsnip::boost_tree(mode = "regression") %>% parsnip::set_engine("xgboost", objective = "reg:squarederror") %>% fit(log(value) ~ as.numeric(date) + lubridate::month(date, label = TRUE), data = rsample::training(splits)) @@ -375,14 +375,14 @@ test_that("Models for Mega Test", { # * XGBoost (workflow) ---- - model_spec <- boost_tree("regression") %>% + model_spec <- parsnip::boost_tree("regression") %>% parsnip::set_engine("xgboost", objective = "reg:squarederror") recipe_spec <- recipes::recipe(value ~ date, data = rsample::training(splits)) %>% recipes::step_date(date, features = "month") %>% recipes::step_mutate(date_num = as.numeric(date)) %>% - step_rm(date) %>% - step_dummy(all_nominal()) %>% + recipes::step_rm(date) %>% + recipes::step_dummy(recipes::all_nominal()) %>% recipes::step_log(value) wflw_fit_xgboost <- workflows::workflow() %>% @@ -499,7 +499,6 @@ test_that("Models for Mega Test", { expect_true(all(tail(actual_tbl$.index, 1) < future_predictions_tbl$.index)) - }) From dec861904536a12471000ceaab73736195532178 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 23:40:36 -0400 Subject: [PATCH 11/16] Load sparklyr alone. --- vignettes/modeltime-spark.Rmd | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vignettes/modeltime-spark.Rmd b/vignettes/modeltime-spark.Rmd index b3c05a77..72b34df2 100644 --- a/vignettes/modeltime-spark.Rmd +++ b/vignettes/modeltime-spark.Rmd @@ -49,15 +49,17 @@ library(modeltime) library(tidymodels) library(tidyverse) library(timetk) +``` +```{r} library(sparklyr) ``` + ```{r. include = FALSE} library(modeltime) library(tidymodels) library(dplyr) library(timetk) -library(sparklyr) ``` # Spark Connection From caf5b252f31cc862437a9fcf27926522e2b4c530 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 3 Oct 2023 23:55:27 -0400 Subject: [PATCH 12/16] try loading sparklyr before other ones. --- vignettes/modeltime-spark.Rmd | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/vignettes/modeltime-spark.Rmd b/vignettes/modeltime-spark.Rmd index 72b34df2..bb3cbe30 100644 --- a/vignettes/modeltime-spark.Rmd +++ b/vignettes/modeltime-spark.Rmd @@ -44,22 +44,20 @@ This tutorial requires: Load the following libraries. -```r -library(modeltime) -library(tidymodels) -library(tidyverse) -library(timetk) -``` ```{r} library(sparklyr) +library(modeltime) +library(timetk) ``` +```r +library(tidymodels) +library(tidyverse) +``` ```{r. include = FALSE} -library(modeltime) library(tidymodels) library(dplyr) -library(timetk) ``` # Spark Connection From bd28a6f18582f1b5495e7f365ed632f74db68c56 Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 4 Oct 2023 00:07:59 -0400 Subject: [PATCH 13/16] Actually not loading tidyverse --- DESCRIPTION | 2 +- vignettes/modeling-panel-data.Rmd | 10 +++++++++- vignettes/modeltime-conformal-prediction.Rmd | 10 +++++++++- vignettes/modeltime-spark.Rmd | 2 +- vignettes/nested-forecasting.Rmd | 2 +- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index eabb2bd1..df09721a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -49,7 +49,6 @@ Imports: forecast, xgboost (>= 1.2.0.1), prophet, - lubridate, methods, cli, tidymodels @@ -61,6 +60,7 @@ Suggests: recipes, rsample, tune (>= 0.2.0), + lubridate, testthat, kernlab, glmnet, diff --git a/vignettes/modeling-panel-data.Rmd b/vignettes/modeling-panel-data.Rmd index 1eb29fc8..b3961b7d 100644 --- a/vignettes/modeling-panel-data.Rmd +++ b/vignettes/modeling-panel-data.Rmd @@ -93,10 +93,18 @@ We'll cover a short tutorial on global forecasting. The first thing to do is to ```{r} library(tidymodels) library(modeltime) -library(tidyverse) library(timetk) ``` +```r +library(tidyverse) +``` + +```{r, echo=FALSE, message=FALSE} +library(dplyr) +``` + + ## Dataset Next, let's use the `walmart_sales_weekly` dataset that comes with `timetk`. diff --git a/vignettes/modeltime-conformal-prediction.Rmd b/vignettes/modeltime-conformal-prediction.Rmd index 74274325..9e7ea5a8 100644 --- a/vignettes/modeltime-conformal-prediction.Rmd +++ b/vignettes/modeltime-conformal-prediction.Rmd @@ -41,16 +41,24 @@ Modeltime integrates Conformal Prediction Intervals as part of its time series f Load libraries to complete this short tutorial. +```r +library(tidyverse) +``` + ```{r} library(tidymodels) library(modeltime) library(timetk) -library(tidyverse) # This toggles plots from plotly (interactive) to ggplot (static) interactive <- FALSE ``` +```{r, include=FALSE} +library(dplyr) +``` + + ### Step 1 - Collect data and split into training, test, and future data sets. We'll start with the Walmart Sales data set from `timetk`. diff --git a/vignettes/modeltime-spark.Rmd b/vignettes/modeltime-spark.Rmd index bb3cbe30..ddf374d0 100644 --- a/vignettes/modeltime-spark.Rmd +++ b/vignettes/modeltime-spark.Rmd @@ -55,7 +55,7 @@ library(tidymodels) library(tidyverse) ``` -```{r. include = FALSE} +```{r. echo = FALSE} library(tidymodels) library(dplyr) ``` diff --git a/vignettes/nested-forecasting.Rmd b/vignettes/nested-forecasting.Rmd index 09cc7f06..64058570 100644 --- a/vignettes/nested-forecasting.Rmd +++ b/vignettes/nested-forecasting.Rmd @@ -71,7 +71,7 @@ library(tidyverse) library(timetk) ``` -```{r. include = FALSE} +```{r. echo = FALSE} library(modeltime) library(tidymodels) library(dplyr) From b5133a2a973f424745007cd40f6207c48e3a44d1 Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 4 Oct 2023 00:08:24 -0400 Subject: [PATCH 14/16] lints --- R/modeltime-accuracy-table.R | 2 +- R/utils-control-par.R | 4 ++-- R/utils-parsnip-helpers.R | 2 +- R/utils-xgboost.R | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/modeltime-accuracy-table.R b/R/modeltime-accuracy-table.R index 7113b9d1..6085e7f5 100644 --- a/R/modeltime-accuracy-table.R +++ b/R/modeltime-accuracy-table.R @@ -118,7 +118,7 @@ table_modeltime_accuracy <- function(.data, .round_digits = 2, if (!is.null(round)) { data_formatted <- data_formatted %>% - dplyr::mutate(dplyr::across(where(is.double), .fns = ~ round(.x, digits = .round_digits))) + dplyr::mutate(dplyr::across(dplyr::where(is.double), .fns = ~ round(.x, digits = .round_digits))) } # Output either reactable() or gt() diff --git a/R/utils-control-par.R b/R/utils-control-par.R index a4bff260..26d84bd4 100644 --- a/R/utils-control-par.R +++ b/R/utils-control-par.R @@ -98,7 +98,7 @@ setup_parallel_processing <- function(control, is_par_setup, t1) { } } else if (!is_par_setup) { - # Run sequentially if parallel is not set up, cores == 1 or allow_par == FALSE + # Run sequentially if parallel is not set up, cores == 1 or allow_par = FALSE if (control$verbose) message(stringr::str_glue("Running sequential backend. If parallel was intended, set `allow_par = TRUE` and `cores > 1`.")) foreach::registerDoSEQ() } else { @@ -420,7 +420,7 @@ control_modeltime_objects <- function( } class_cores <- check_class_integer(cores) - if (class_cores == F) { + if (!class_cores) { rlang::abort( stringr::str_glue("{if (!is.null(func)) paste0(func, ': ') }Argument 'cores' should be a single integer value") ) diff --git a/R/utils-parsnip-helpers.R b/R/utils-parsnip-helpers.R index 61dbf7ce..3a84ae75 100644 --- a/R/utils-parsnip-helpers.R +++ b/R/utils-parsnip-helpers.R @@ -76,7 +76,7 @@ find_parsnip_formula_form <- function(object) { formula_found <- TRUE } - if (formula_found == FALSE){ + if (!formula_found){ check_formula_second_level <- object %>% purrr::map_dfr(~ rlang::is_formula(.)) %>% tidyr::gather() %>% diff --git a/R/utils-xgboost.R b/R/utils-xgboost.R index e11b1b32..4eab2fce 100644 --- a/R/utils-xgboost.R +++ b/R/utils-xgboost.R @@ -24,7 +24,7 @@ xgboost_impl <- function(x, y, if (!is.null(colsample_bytree)) { if (colsample_bytree == 1) { - if (counts == TRUE) { + if (counts) { rlang::warn("`colsample_bytree = 1` with `counts = TRUE` will only sample a single column. Set `counts = FALSE` to use a proportion (100% of columns).") } @@ -32,7 +32,7 @@ xgboost_impl <- function(x, y, } if (!is.null(colsample_bynode)) { if (colsample_bynode == 1) { - if (counts == TRUE) { + if (counts) { rlang::warn("`colsample_bynode = 1` with `counts = TRUE` will only sample a single column. Set `counts = FALSE` to use a proportion (100% of columns).") } From 1d63ca226def3ac4581d65401a1b4938108ac3b0 Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 4 Oct 2023 00:19:31 -0400 Subject: [PATCH 15/16] inline group_by to pass R CMD CHECK --- vignettes/modeltime-spark.Rmd | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/vignettes/modeltime-spark.Rmd b/vignettes/modeltime-spark.Rmd index ddf374d0..b5640ce0 100644 --- a/vignettes/modeltime-spark.Rmd +++ b/vignettes/modeltime-spark.Rmd @@ -48,14 +48,6 @@ Load the following libraries. library(sparklyr) library(modeltime) library(timetk) -``` - -```r -library(tidymodels) -library(tidyverse) -``` - -```{r. echo = FALSE} library(tidymodels) library(dplyr) ``` @@ -96,8 +88,8 @@ The dataset we'll be forecasting is the `walmart_sales_weekly`, which we modify ```{r} walmart_sales_weekly %>% - select(id, date = Date, value = Weekly_Sales) %>% - group_by(id) %>% + dplyr::select(id, date = Date, value = Weekly_Sales) %>% + dplyr::group_by(id) %>% plot_time_series(date, value, .facet_ncol = 2, .interactive = FALSE) ``` @@ -105,7 +97,7 @@ We prepare as nested data using the Nested Forecasting preparation functions. ```{r} nested_data_tbl <- walmart_sales_weekly %>% - select(id, date = Date, value = Weekly_Sales) %>% + dplyr::select(id, date = Date, value = Weekly_Sales) %>% extend_timeseries( .id_var = id, .date_var = date, @@ -200,7 +192,7 @@ Next, we can examine the test forecast for each of the models. ```{r} nested_modeltime_tbl %>% extract_nested_test_forecast() %>% - group_by(id) %>% + dplyr::group_by(id) %>% plot_modeltime_forecast(.facet_ncol = 2, .interactive = F) ``` From f699659d262a175fe5575dbf373685214de9c805 Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 4 Oct 2023 00:35:07 -0400 Subject: [PATCH 16/16] oops, I put a dot instead of a comma. --- vignettes/nested-forecasting.Rmd | 2 +- vignettes/parallel-processing.Rmd | 2 +- vignettes/recursive-forecasting.Rmd | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/vignettes/nested-forecasting.Rmd b/vignettes/nested-forecasting.Rmd index 64058570..98f2ecb9 100644 --- a/vignettes/nested-forecasting.Rmd +++ b/vignettes/nested-forecasting.Rmd @@ -71,7 +71,7 @@ library(tidyverse) library(timetk) ``` -```{r. echo = FALSE} +```{r, echo = FALSE} library(modeltime) library(tidymodels) library(dplyr) diff --git a/vignettes/parallel-processing.Rmd b/vignettes/parallel-processing.Rmd index 430ccd33..5347fb5a 100644 --- a/vignettes/parallel-processing.Rmd +++ b/vignettes/parallel-processing.Rmd @@ -62,7 +62,7 @@ library(tidyverse) library(timetk) ``` -```{r. include = FALSE} +```{r, include = FALSE} # Machine Learning library(modeltime) library(tidymodels) diff --git a/vignettes/recursive-forecasting.Rmd b/vignettes/recursive-forecasting.Rmd index 8dfc0b51..b31b960a 100644 --- a/vignettes/recursive-forecasting.Rmd +++ b/vignettes/recursive-forecasting.Rmd @@ -65,7 +65,7 @@ library(tidyverse) library(timetk) ``` -```{r. include = FALSE} +```{r, include = FALSE} library(modeltime) library(tidymodels) library(dplyr)