Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline function calls in tests to prepare for removing some `library(… #234

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ library(testthat)

library(xgboost)
library(randomForest)
library(kernlab)
library(earth)
library(thief)
library(smooth)
library(greybox)
Expand All @@ -25,7 +23,5 @@ library(timetk)
library(modeltime)

library(tidyverse)
library(lubridate)


test_check("modeltime")
58 changes: 29 additions & 29 deletions tests/testthat/test-algo-adam_reg-Adam.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ context("TEST adam_reg: ADAM")
# SETUP ----

# Data
m750 <- m4_monthly %>% filter(id == "M750")
m750 <- timetk::m4_monthly %>% dplyr::filter(id == "M750")

# Split Data 80/20
splits <- initial_time_split(m750, prop = 0.8)
splits <- rsample::initial_time_split(m750, prop = 0.8)



Expand All @@ -28,7 +28,7 @@ test_that("adam_reg: Adam, (No xregs), Test Model Fit Object", {
seasonal_differences = 0,
seasonal_ma = 1
) %>%
set_engine("adam")
parsnip::set_engine("adam")


# PARSNIP ----
Expand All @@ -37,12 +37,12 @@ test_that("adam_reg: Adam, (No xregs), Test Model Fit Object", {

# Fit Spec
model_fit <- model_spec %>%
fit(value ~ date, data = training(splits))
fit(value ~ date, data = rsample::training(splits))

# Predictions
predictions_tbl <- model_fit %>%
modeltime_calibrate(testing(splits)) %>%
modeltime_forecast(new_data = testing(splits))
modeltime_calibrate(rsample::testing(splits)) %>%
modeltime_forecast(new_data = rsample::testing(splits))

expect_s3_class(model_fit$fit, "Adam_fit_impl")

Expand All @@ -61,12 +61,12 @@ test_that("adam_reg: Adam, (No xregs), Test Model Fit Object", {
expect_equal(model_fit$preproc$y_var, "value")

# Structure
expect_identical(nrow(testing(splits)), nrow(predictions_tbl))
expect_identical(testing(splits)$date, predictions_tbl$.index)
expect_identical(nrow(rsample::testing(splits)), nrow(predictions_tbl))
expect_identical(rsample::testing(splits)$date, predictions_tbl$.index)

# Out-of-Sample Accuracy Tests

resid <- testing(splits)$value - predictions_tbl$.value
resid <- rsample::testing(splits)$value - predictions_tbl$.value

# - Max Error less than 1500
expect_lte(max(abs(resid)), 3000)
Expand All @@ -78,20 +78,20 @@ test_that("adam_reg: Adam, (No xregs), Test Model Fit Object", {
# * XREGS ----

# Data
m750 <- m4_monthly %>% filter(id == "M750") %>%
mutate(month = month(date, label = TRUE))
m750 <- timetk::m4_monthly %>% dplyr::filter(id == "M750") %>%
dplyr::mutate(month = lubridate::month(date, label = TRUE))

# Split Data 80/20
splits <- initial_time_split(m750, prop = 0.8)
splits <- rsample::initial_time_split(m750, prop = 0.8)

# Fit Spec
model_fit <- model_spec %>%
fit(value ~ date + month, data = training(splits))
fit(value ~ date + month, data = rsample::training(splits))

# Predictions
predictions_tbl <- model_fit %>%
modeltime_calibrate(testing(splits)) %>%
modeltime_forecast(new_data = testing(splits))
modeltime_calibrate(rsample::testing(splits)) %>%
modeltime_forecast(new_data = rsample::testing(splits))


# Model Fit ----
Expand All @@ -115,12 +115,12 @@ test_that("adam_reg: Adam, (No xregs), Test Model Fit Object", {


# Structure
expect_identical(nrow(testing(splits)), nrow(predictions_tbl))
expect_identical(testing(splits)$date, predictions_tbl$.index)
expect_identical(nrow(rsample::testing(splits)), nrow(predictions_tbl))
expect_identical(rsample::testing(splits)$date, predictions_tbl$.index)

# Out-of-Sample Accuracy Tests

resid <- testing(splits)$value - predictions_tbl$.value
resid <- rsample::testing(splits)$value - predictions_tbl$.value

# - Max Error less than 1500
expect_lte(max(abs(resid)), 2000)
Expand All @@ -147,23 +147,23 @@ test_that("adam_reg: Adam (workflow)", {
seasonal_differences = 0,
seasonal_ma = 1
) %>%
set_engine("adam")
parsnip::set_engine("adam")

# Recipe spec
recipe_spec <- recipe(value ~ date, data = training(splits))
recipe_spec <- recipes::recipe(value ~ date, data = rsample::training(splits))

# Workflow
wflw <- workflow() %>%
add_recipe(recipe_spec) %>%
add_model(model_spec)
wflw <- workflows::workflow() %>%
workflows::add_recipe(recipe_spec) %>%
workflows::add_model(model_spec)

wflw_fit <- wflw %>%
fit(training(splits))
fit(rsample::training(splits))

# Forecast
predictions_tbl <- wflw_fit %>%
modeltime_calibrate(testing(splits)) %>%
modeltime_forecast(new_data = testing(splits), actual_data = training(splits))
modeltime_calibrate(rsample::testing(splits)) %>%
modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits))

expect_s3_class(wflw_fit$fit$fit$fit, "Adam_fit_impl")

Expand All @@ -183,15 +183,15 @@ test_that("adam_reg: Adam (workflow)", {

# * Test Predictions ----

full_data <- bind_rows(training(splits), testing(splits))
full_data <- dplyr::bind_rows(rsample::training(splits), rsample::testing(splits))

# Structure
expect_identical(nrow(full_data), nrow(predictions_tbl))
expect_identical(full_data$date, predictions_tbl$.index)

# Out-of-Sample Accuracy Tests
predictions_tbl <- predictions_tbl %>% filter(.key == "prediction")
resid <- testing(splits)$value - predictions_tbl$.value
predictions_tbl <- predictions_tbl %>% dplyr::filter(.key == "prediction")
resid <- rsample::testing(splits)$value - predictions_tbl$.value

# - Max Error less than 1500
expect_lte(max(abs(resid)), 3000)
Expand Down
62 changes: 31 additions & 31 deletions tests/testthat/test-algo-adam_reg-auto_adam.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ test_that("adam_reg: Auto ADAM, (No xregs), Test Model Fit Object", {
# SETUP

# Data
m750 <- m4_monthly %>% filter(id == "M750")
m750 <- timetk::m4_monthly %>% dplyr::filter(id == "M750")

# Split Data 80/20
splits <- initial_time_split(m750, prop = 0.8)
splits <- rsample::initial_time_split(m750, prop = 0.8)

# Model Spec
model_spec <- adam_reg(
seasonal_period = 12
) %>%
set_engine("auto_adam")
parsnip::set_engine("auto_adam")

# Fit Spec
model_fit <- model_spec %>%
fit(value ~ date, data = training(splits))
fit(value ~ date, data = rsample::training(splits))

# Predictions
predictions_tbl <- model_fit %>%
modeltime_calibrate(testing(splits), quiet = FALSE) %>%
modeltime_forecast(new_data = testing(splits))
modeltime_calibrate(rsample::testing(splits), quiet = FALSE) %>%
modeltime_forecast(new_data = rsample::testing(splits))

expect_s3_class(model_fit$fit, "Auto_adam_fit_impl")

Expand All @@ -51,12 +51,12 @@ test_that("adam_reg: Auto ADAM, (No xregs), Test Model Fit Object", {
expect_equal(model_fit$preproc$y_var, "value")

# Structure
expect_identical(nrow(testing(splits)), nrow(predictions_tbl))
expect_identical(testing(splits)$date, predictions_tbl$.index)
expect_identical(nrow(rsample::testing(splits)), nrow(predictions_tbl))
expect_identical(rsample::testing(splits)$date, predictions_tbl$.index)

# Out-of-Sample Accuracy Tests

resid <- testing(splits)$value - predictions_tbl$.value
resid <- rsample::testing(splits)$value - predictions_tbl$.value

# - Max Error less than 1500
expect_lte(max(abs(resid)), 3000)
Expand All @@ -76,25 +76,25 @@ test_that("adam_reg: Auto ADAM, (XREGS)", {


# Data
m750 <- m4_monthly %>% filter(id == "M750") %>% mutate(month = month(date, label = TRUE))
m750 <- timetk::m4_monthly %>% dplyr::filter(id == "M750") %>% dplyr::mutate(month = lubridate::month(date, label = TRUE))

# Split Data 80/20
splits <- initial_time_split(m750, prop = 0.8)
splits <- rsample::initial_time_split(m750, prop = 0.8)

# Model Spec
model_spec <- adam_reg(
seasonal_period = 12
) %>%
set_engine("auto_adam")
parsnip::set_engine("auto_adam")

# Fit Spec
model_fit <- model_spec %>%
fit(value ~ date + month, data = training(splits))
fit(value ~ date + month, data = rsample::training(splits))

# Predictions
predictions_tbl <- model_fit %>%
modeltime_calibrate(testing(splits)) %>%
modeltime_forecast(new_data = testing(splits))
modeltime_calibrate(rsample::testing(splits)) %>%
modeltime_forecast(new_data = rsample::testing(splits))

expect_s3_class(model_fit$fit, "Auto_adam_fit_impl")

Expand All @@ -115,12 +115,12 @@ test_that("adam_reg: Auto ADAM, (XREGS)", {


# Structure
expect_identical(nrow(testing(splits)), nrow(predictions_tbl))
expect_identical(testing(splits)$date, predictions_tbl$.index)
expect_identical(nrow(rsample::testing(splits)), nrow(predictions_tbl))
expect_identical(rsample::testing(splits)$date, predictions_tbl$.index)

# Out-of-Sample Accuracy Tests

resid <- testing(splits)$value - predictions_tbl$.value
resid <- rsample::testing(splits)$value - predictions_tbl$.value

# - Max Error less than 1500
expect_lte(max(abs(resid)), 3000)
Expand All @@ -139,10 +139,10 @@ test_that("adam_reg: Auto ADAM (workflow), Test Model Fit Object", {
skip_on_cran()

# Data
m750 <- m4_monthly %>% filter(id == "M750") %>% mutate(month = month(date, label = TRUE))
m750 <- timetk::m4_monthly %>% dplyr::filter(id == "M750") %>% dplyr::mutate(month = lubridate::month(date, label = TRUE))

# Split Data 80/20
splits <- initial_time_split(m750, prop = 0.8)
splits <- rsample::initial_time_split(m750, prop = 0.8)

# Model Spec
model_spec <- adam_reg(
Expand All @@ -154,23 +154,23 @@ test_that("adam_reg: Auto ADAM (workflow), Test Model Fit Object", {
seasonal_differences = 0,
seasonal_ma = 1
) %>%
set_engine("auto_adam")
parsnip::set_engine("auto_adam")

# Recipe spec
recipe_spec <- recipe(value ~ date, data = training(splits))
recipe_spec <- recipes::recipe(value ~ date, data = rsample::training(splits))

# Workflow
wflw <- workflow() %>%
add_recipe(recipe_spec) %>%
add_model(model_spec)
wflw <- workflows::workflow() %>%
workflows::add_recipe(recipe_spec) %>%
workflows::add_model(model_spec)

wflw_fit <- wflw %>%
fit(training(splits))
fit(rsample::training(splits))

# Forecast
predictions_tbl <- wflw_fit %>%
modeltime_calibrate(testing(splits)) %>%
modeltime_forecast(new_data = testing(splits), actual_data = training(splits))
modeltime_calibrate(rsample::testing(splits)) %>%
modeltime_forecast(new_data = rsample::testing(splits), actual_data = rsample::training(splits))


expect_s3_class(wflw_fit$fit$fit$fit, "Auto_adam_fit_impl")
Expand All @@ -190,15 +190,15 @@ test_that("adam_reg: Auto ADAM (workflow), Test Model Fit Object", {
expect_equal(names(mld$outcomes), "value")


full_data <- bind_rows(training(splits), testing(splits))
full_data <- dplyr::bind_rows(rsample::training(splits), rsample::testing(splits))

# Structure
expect_identical(nrow(full_data), nrow(predictions_tbl))
expect_identical(full_data$date, predictions_tbl$.index)

# Out-of-Sample Accuracy Tests
predictions_tbl <- predictions_tbl %>% filter(.key == "prediction")
resid <- testing(splits)$value - predictions_tbl$.value
predictions_tbl <- predictions_tbl %>% dplyr::filter(.key == "prediction")
resid <- rsample::testing(splits)$value - predictions_tbl$.value

# - Max Error less than 1500
expect_lte(max(abs(resid)), 3000)
Expand Down
Loading