Skip to content
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
70 changes: 35 additions & 35 deletions R/04_S7_Resampler.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ method(desc_alt, ResamplerConfig) <- function(x) {
)
} # /rtemis::desc.ResamplerConfig

# KFoldParams ----
#' @title KFoldParams
# KFoldConfig ----
#' @title KFoldConfig
#'
#' @description
#' ResamplerConfig subclass for k-fold resampling.
#'
#' @author EDG
#' @noRd
KFoldParams <- new_class(
name = "KFoldParams",
KFoldConfig <- new_class(
name = "KFoldConfig",
parent = ResamplerConfig,
properties = list(
stratify_var = class_character | NULL,
Expand All @@ -124,18 +124,18 @@ KFoldParams <- new_class(
seed = seed
)
}
) # /KFoldParams
) # /KFoldConfig

# StratSubParams ----
#' @title StratSubParams
# StratSubConfig ----
#' @title StratSubConfig
#'
#' @description
#' ResamplerConfig subclass for stratified subsampling.
#'
#' @author EDG
#' @noRd
StratSubParams <- new_class(
name = "StratSubParams",
StratSubConfig <- new_class(
name = "StratSubConfig",
parent = ResamplerConfig,
properties = list(
n = scalar_int_pos,
Expand Down Expand Up @@ -165,18 +165,18 @@ StratSubParams <- new_class(
seed = seed
)
}
) # /StratSubParams
) # /StratSubConfig

# StratBootParams ----
#' @title StratBootParams
# StratBootConfig ----
#' @title StratBootConfig
#'
#' @description
#' ResamplerConfig subclass for stratified bootstrapping.
#'
#' @author EDG
#' @noRd
StratBootParams <- new_class(
name = "StratBootParams",
StratBootConfig <- new_class(
name = "StratBootConfig",
parent = ResamplerConfig,
properties = list(
stratify_var = class_character | NULL,
Expand Down Expand Up @@ -208,18 +208,18 @@ StratBootParams <- new_class(
seed = seed
)
}
) # /StratBootParams
) # /StratBootConfig

# BootstrapParams ----
#' @title BootstrapParams
# BootstrapConfig ----
#' @title BootstrapConfig
#'
#' @description
#' ResamplerConfig subclass for bootstrap resampling.
#'
#' @author EDG
#' @noRd
BootstrapParams <- new_class(
name = "BootstrapParams",
BootstrapConfig <- new_class(
name = "BootstrapConfig",
parent = ResamplerConfig,
properties = list(
id_strat = class_vector | NULL,
Expand All @@ -235,18 +235,18 @@ BootstrapParams <- new_class(
seed = seed
)
}
) # /BootstrapParams
) # /BootstrapConfig

# LOOCVParams ----
#' @title LOOCVParams
# LOOCVConfig ----
#' @title LOOCVConfig
#'
#' @description
#' ResamplerConfig subclass for leave-one-out cross-validation.
#'
#' @author EDG
#' @noRd
LOOCVParams <- new_class(
name = "LOOCVParams",
LOOCVConfig <- new_class(
name = "LOOCVConfig",
parent = ResamplerConfig,
constructor = function(n) {
new_object(
Expand All @@ -256,18 +256,18 @@ LOOCVParams <- new_class(
)
)
}
) # /LOOCVParams
) # /LOOCVConfig

# CustomParams ----
#' @title CustomParams
# CustomConfig ----
#' @title CustomConfig
#'
#' @description
#' ResamplerConfig subclass for custom resampling.
#'
#' @author EDG
#' @noRd
CustomParams <- new_class(
name = "CustomParams",
CustomConfig <- new_class(
name = "CustomConfig",
parent = ResamplerConfig,
constructor = function(n) {
new_object(
Expand All @@ -277,7 +277,7 @@ CustomParams <- new_class(
)
)
}
) # /CustomParams
) # /CustomConfig

# setup_Resampler() ----
#' Setup Resampler
Expand Down Expand Up @@ -324,15 +324,15 @@ setup_Resampler <- function(
seed <- clean_int(seed)

if (type == "KFold") {
KFoldParams(
KFoldConfig(
n = n_resamples,
stratify_var = stratify_var,
strat_n_bins = strat_n_bins,
id_strat = id_strat,
seed = seed
)
} else if (type == "StratSub") {
StratSubParams(
StratSubConfig(
n = n_resamples,
train_p = train_p,
stratify_var = stratify_var,
Expand All @@ -341,7 +341,7 @@ setup_Resampler <- function(
seed = seed
)
} else if (type == "StratBoot") {
StratBootParams(
StratBootConfig(
n = n_resamples,
train_p = train_p,
stratify_var = stratify_var,
Expand All @@ -351,13 +351,13 @@ setup_Resampler <- function(
seed = seed
)
} else if (type == "Bootstrap") {
BootstrapParams(
BootstrapConfig(
n = n_resamples,
id_strat = id_strat,
seed = seed
)
} else if (type == "LOOCV") {
LOOCVParams(
LOOCVConfig(
n = NA_integer_
)
} else {
Expand Down
58 changes: 29 additions & 29 deletions tests/testthat/test_Resampler.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@

# library(testthat)

# StratSubParams ----
test_that("StratSubParams succeeds", {
rsp <- StratSubParams(
# StratSubConfig ----
test_that("StratSubConfig succeeds", {
rsp <- StratSubConfig(
n = 10L,
stratify_var = NULL,
train_p = .75,
strat_n_bins = 4L,
id_strat = NULL,
seed = NULL
)
expect_s7_class(rsp, StratSubParams)
expect_s7_class(rsp, StratSubConfig)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This test correctly checks the class of the created object. To make it more robust, I recommend also asserting that the object's properties are initialized with the correct values. This provides a stronger guarantee that the constructor is working as expected. This principle can be applied to other similar tests in this file.

  expect_s7_class(rsp, StratSubConfig)
  expect_equal(rsp@n, 10L)
  expect_equal(rsp@train_p, 0.75)

})

# KFoldParams ----
test_that("KFoldParams succeeds", {
rsp <- KFoldParams(
# KFoldConfig ----
test_that("KFoldConfig succeeds", {
rsp <- KFoldConfig(
n = 10L,
stratify_var = NULL,
strat_n_bins = 4L,
id_strat = NULL,
seed = NULL
)
expect_s7_class(rsp, KFoldParams)
expect_s7_class(rsp, KFoldConfig)
})

# BootstrapParams ----
test_that("BootstrapParams succeeds", {
rsp <- BootstrapParams(
# BootstrapConfig ----
test_that("BootstrapConfig succeeds", {
rsp <- BootstrapConfig(
n = 10L,
id_strat = NULL,
seed = NULL
)
expect_s7_class(rsp, BootstrapParams)
expect_s7_class(rsp, BootstrapConfig)
})

# StratBootParams ----
test_that("StratBootParams succeeds", {
rsp <- StratBootParams(
# StratBootConfig ----
test_that("StratBootConfig succeeds", {
rsp <- StratBootConfig(
n = 10L,
stratify_var = NULL,
train_p = .75,
Expand All @@ -50,23 +50,23 @@ test_that("StratBootParams succeeds", {
id_strat = NULL,
seed = NULL
)
expect_s7_class(rsp, StratBootParams)
expect_s7_class(rsp, StratBootConfig)
})

# LOOCVParams ----
test_that("LOOCVParams succeeds", {
rsp <- LOOCVParams(
# LOOCVConfig ----
test_that("LOOCVConfig succeeds", {
rsp <- LOOCVConfig(
n = 10L
)
expect_s7_class(rsp, LOOCVParams)
expect_s7_class(rsp, LOOCVConfig)
})

# CustomParams ----
test_that("CustomParams succeeds", {
rsp <- CustomParams(
# CustomConfig ----
test_that("CustomConfig succeeds", {
rsp <- CustomConfig(
n = 10L
)
expect_s7_class(rsp, CustomParams)
expect_s7_class(rsp, CustomConfig)
})

# setup_Resampler() defaults ----
Expand All @@ -78,19 +78,19 @@ test_that("setup_Resampler() succeeds", {
# setup_Resampler() kfold ----
test_that("setup_Resampler() kfold succeeds", {
rsp <- setup_Resampler(type = "KFold")
expect_s7_class(rsp, KFoldParams)
expect_s7_class(rsp, KFoldConfig)
})

# setup_Resampler() strat_sub ----
test_that("setup_Resampler() strat_sub succeeds", {
rsp <- setup_Resampler(type = "StratSub")
expect_s7_class(rsp, StratSubParams)
expect_s7_class(rsp, StratSubConfig)
})

# setup_Resampler() strat_boot ----
test_that("setup_Resampler() strat_boot succeeds", {
rsp <- setup_Resampler(type = "StratBoot")
expect_s7_class(rsp, StratBootParams)
expect_s7_class(rsp, StratBootConfig)
})

test_that("setup_Resampler() strat_boot fails with invalid train_p", {
Expand All @@ -102,13 +102,13 @@ test_that("setup_Resampler() strat_boot fails with invalid train_p", {
# setup_Resampler() bootstrap ----
test_that("setup_Resampler() bootstrap succeeds", {
rsp <- setup_Resampler(type = "Bootstrap")
expect_s7_class(rsp, BootstrapParams)
expect_s7_class(rsp, BootstrapConfig)
})

# setup_Resampler() loocv ----
test_that("setup_Resampler() loocv succeeds", {
rsp <- setup_Resampler(type = "LOOCV")
expect_s7_class(rsp, LOOCVParams)
expect_s7_class(rsp, LOOCVConfig)
})

# Resampler ----
Expand Down