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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: funcml
Type: Package
Title: Functional Machine Learning Framework
Version: 0.9.10
Version: 0.9.11
Depends: R (>= 3.5.0)
Authors@R: c(
person(given = "Imad", family = "El Badisy", role = c("aut", "cre"), email = "elbadisyimad@gmail.com")
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export(roc_curve)
export(roccurve)
export(rsq)
export(specificity)
export(splits)
export(theme_funcml)
export(themefuncml)
export(time_cv)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# funcml 0.9.11

* New `splits()` draws the train/test row indices of any resampling scheme
(`cv()`, `holdout()`, `groupcv()`, `timecv()`) outside `evaluate()`. For a
holdout split the indices are returned directly as `$train` and `$test`.
The result is frozen: passing it back to `evaluate()`, `compare()` or
`tune()` on the same data reuses the same folds.

# funcml 0.9.10

* `brier()` and `logloss()` given a plain probability vector for a binary
Expand Down
57 changes: 57 additions & 0 deletions R/cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,60 @@ generate_folds <- function(n, y = NULL, resampling = cv(), data = NULL) {
resampling$n <- n
resampling
}

#' Draw the train/test row indices of a resampling scheme.
#'
#' `evaluate()` and `compare()` draw their folds internally. `splits()` draws
#' them up front so the row indices can be used outside funcml (for example to
#' fit a model on the training rows of a `holdout()` split by hand). The
#' result is a frozen resampling object: passing it back to `evaluate()`,
#' `compare()` or `tune()` on the same data reuses exactly these folds.
#'
#' @param resampling A resampling object from [cv()], [holdout()], [groupcv()]
#' or [timecv()].
#' @param data Data frame the folds refer to.
#' @param formula Optional model formula. When given (or when `outcome` is),
#' classification outcomes are stratified as in `evaluate()`, so the folds
#' are identical to those `evaluate()` draws with the same seed.
#' @param outcome Optional outcome column name, an alternative to `formula`
#' for stratification.
#' @return A `funcml_cv` object whose `folds` element is a list of splits,
#' each with `train` and `test` row indices, `repeat_id` and `fold`. For a
#' holdout split the indices are also returned directly as `train` and
#' `test`.
#' @examples
#' s <- splits(holdout(prop = 0.75, seed = 1), mtcars)
#' length(s$train)
#' fit_train <- fit(mpg ~ wt, mtcars[s$train, ], model = "glm")
#' s5 <- splits(cv(5, seed = 1), mtcars)
#' lengths(lapply(s5$folds, `[[`, "test"))
#' @export
splits <- function(resampling, data, formula = NULL, outcome = NULL) {
if (!inherits(resampling, "funcml_cv")) {
stop("`resampling` must be created by cv(), holdout(), groupcv() or timecv().", call. = FALSE)
}
if (!is.data.frame(data)) {
stop("`data` must be a data frame.", call. = FALSE)
}
y <- NULL
if (!is.null(formula)) {
y <- stats::model.response(stats::model.frame(formula, data, na.action = stats::na.pass))
} else if (!is.null(outcome)) {
if (!is.character(outcome) || length(outcome) != 1L || !outcome %in% names(data)) {
stop("`outcome` must be the name of a column of `data`.", call. = FALSE)
}
y <- data[[outcome]]
}
if (!is.null(y) && infer_task(y) == "classification") {
y <- factor(y)
}
resampling$folds <- NULL
resampling$fixed <- NULL
out <- generate_folds(nrow(data), y, resampling, data = data)
out$fixed <- TRUE
if (identical(out$method, "holdout")) {
out$train <- out$folds[[1L]]$train
out$test <- out$folds[[1L]]$test
}
out
}
41 changes: 41 additions & 0 deletions man/splits.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions tests/testthat/test-splits.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
test_that("splits() returns holdout indices that partition the rows", {
s <- splits(holdout(prop = 0.75, seed = 1), mtcars)
expect_s3_class(s, "funcml_cv")
expect_length(s$train, floor(0.75 * nrow(mtcars)))
expect_setequal(c(s$train, s$test), seq_len(nrow(mtcars)))
expect_length(intersect(s$train, s$test), 0L)
expect_identical(s$train, s$folds[[1]]$train)
})

test_that("splits() matches the folds evaluate() draws and is reused", {
d <- iris
d$Species <- factor(d$Species == "setosa")
rs <- cv(4, seed = 11)
s <- splits(rs, d, formula = Species ~ Sepal.Length)
ev <- evaluate(d, Species ~ Sepal.Length, model = "glm", resampling = rs,
metrics = "accuracy")
expect_identical(
lapply(s$folds, `[[`, "test"),
lapply(ev$resampling$folds, `[[`, "test")
)
ev2 <- evaluate(d, Species ~ Sepal.Length, model = "glm", resampling = s,
metrics = "accuracy")
expect_identical(ev2$resampling$folds, s$folds)
# stratified: each test fold keeps the class balance
props <- vapply(s$folds, function(f) mean(d$Species[f$test] == "TRUE"), numeric(1))
expect_true(all(abs(props - 1 / 3) < 0.05))
})

test_that("splits() checks its inputs", {
expect_error(splits(list(), mtcars), "resampling")
expect_error(splits(cv(3), mtcars, outcome = "nope"), "outcome")
})
Loading