From 93bc914c079be486084895451ee19b0f783eb1c1 Mon Sep 17 00:00:00 2001 From: ielbadisy Date: Sun, 15 Nov 2026 05:10:42 +0000 Subject: [PATCH] Use exact k and expose kernel in kknn, expose epsilon in e1071_svm --- NEWS.md | 6 +++++ R/fit.R | 8 +++++++ R/registry.R | 11 +++++---- man/fit.Rd | 8 +++++++ tests/testthat/test-kknn-svm-spec.R | 35 +++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/test-kknn-svm-spec.R diff --git a/NEWS.md b/NEWS.md index fbdc8e9..32c648a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,11 @@ # funcml 0.9.11 +* `kknn` treated `k` as the largest k of a leave-one-out search, so the + fitted model could use fewer neighbours than asked. `k` is now the exact + number of neighbours. The kernel, fixed at `"optimal"` before, is now the + spec entry `kernel` (default `"optimal"`). +* `e1071_svm` gains the spec entry `epsilon` (default 0.1, the e1071 + default), so support vector regression can be tuned on it. * 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`. diff --git a/R/fit.R b/R/fit.R index 5eb8e14..52cf39a 100644 --- a/R/fit.R +++ b/R/fit.R @@ -21,6 +21,14 @@ #' meta-features are stored at `$state$meta_x`. `superlearner` works the same #' way with a `resampling` object instead of `folds`. #' +#' `kknn` takes `spec = list(k, distance, kernel)`: `k` is the exact number +#' of neighbours, `distance` the Minkowski power and `kernel` any kernel +#' accepted by [kknn::kknn()] (default `"optimal"`; for example +#' `"rectangular"` gives an unweighted k-nearest-neighbour vote or mean). +#' `e1071_svm` takes `spec = list(cost, gamma, kernel, epsilon)`; `epsilon` +#' (default 0.1) is the width of the insensitive tube in support vector +#' regression and is ignored for classification. +#' #' @param formula Model formula. #' @param data Data frame. #' @param model Learner id (see `learners()`). diff --git a/R/registry.R b/R/registry.R index 7bf7152..0c1aa81 100644 --- a/R/registry.R +++ b/R/registry.R @@ -565,7 +565,7 @@ build_registry <- function() { e1071_svm = list( package = "e1071", tasks = c("regression", "classification"), - defaults = list(cost = 1, gamma = NULL, kernel = "radial"), + defaults = list(cost = 1, gamma = NULL, kernel = "radial", epsilon = 0.1), supports = list(prob = TRUE, multiclass = TRUE, importance = FALSE), fit_xy = function(X, y, spec, task, ...) { assert_package("e1071", "e1071_svm") @@ -574,6 +574,7 @@ build_registry <- function() { fit <- e1071::svm( y ~ ., data = df, cost = spec$cost, gamma = gamma, kernel = spec$kernel, + epsilon = spec$epsilon, type = if (task == "regression") "eps-regression" else "C-classification", probability = task == "classification" ) @@ -719,16 +720,18 @@ build_registry <- function() { kknn = list( package = "kknn", tasks = c("regression", "classification"), - defaults = list(k = 7, distance = 2), + defaults = list(k = 7, distance = 2, kernel = "optimal"), supports = list(prob = TRUE, multiclass = TRUE, importance = FALSE), fit_xy = function(X, y, spec, task, ...) { assert_package("kknn", "kknn") df <- data.frame(y = y, X) + # `ks` fixes the number of neighbours at `k`; `kmax` would let the + # leave-one-out search pick any k up to `k`. fit <- kknn::train.kknn( y ~ ., data = df, - kmax = spec$k, + ks = spec$k, distance = spec$distance, - kernel = "optimal" + kernel = spec$kernel ) list(state = fit) }, diff --git a/man/fit.Rd b/man/fit.Rd index e78b03c..2138ffe 100644 --- a/man/fit.Rd +++ b/man/fit.Rd @@ -61,6 +61,14 @@ current random stream, so \code{seed} makes them reproducible); the base learners are then refitted on all rows for prediction. The out-of-fold meta-features are stored at \verb{$state$meta_x}. \code{superlearner} works the same way with a \code{resampling} object instead of \code{folds}. + +\code{kknn} takes \code{spec = list(k, distance, kernel)}: \code{k} is the exact number +of neighbours, \code{distance} the Minkowski power and \code{kernel} any kernel +accepted by \code{\link[kknn:kknn]{kknn::kknn()}} (default \code{"optimal"}; for example +\code{"rectangular"} gives an unweighted k-nearest-neighbour vote or mean). +\code{e1071_svm} takes \code{spec = list(cost, gamma, kernel, epsilon)}; \code{epsilon} +(default 0.1) is the width of the insensitive tube in support vector +regression and is ignored for classification. } \examples{ fit_obj <- fit(mpg ~ wt + hp, data = mtcars, model = "glm") diff --git a/tests/testthat/test-kknn-svm-spec.R b/tests/testthat/test-kknn-svm-spec.R new file mode 100644 index 0000000..ce3e79e --- /dev/null +++ b/tests/testthat/test-kknn-svm-spec.R @@ -0,0 +1,35 @@ +test_that("kknn uses exactly k neighbours and the requested kernel", { + skip_if_not_installed("kknn") + d <- mtcars + for (kern in c("rectangular", "triangular")) { + f <- fit(mpg ~ wt + hp, d[1:24, ], model = "kknn", + spec = list(k = 5, distance = 2, kernel = kern)) + ref <- kknn::kknn(mpg ~ wt + hp, train = d[1:24, ], test = d[25:32, ], + k = 5, distance = 2, kernel = kern) + expect_equal(as.numeric(predict(f, d[25:32, ])), as.numeric(ref$fitted.values)) + expect_equal(enginemodel(f)$best.parameters$k, 5) + } + # rectangular kernel = plain mean of the k nearest outcomes (scaled X) + f <- fit(mpg ~ wt + hp, d[1:24, ], model = "kknn", + spec = list(k = 3, kernel = "rectangular")) + X <- scale(d[1:24, c("wt", "hp")]) + xn <- scale(d[25, c("wt", "hp")], center = attr(X, "scaled:center"), + scale = attr(X, "scaled:scale")) + dist <- sqrt(colSums((t(X) - as.numeric(xn))^2)) + expect_equal(as.numeric(predict(f, d[25, ])), mean(d$mpg[1:24][order(dist)[1:3]])) +}) + +test_that("e1071_svm passes epsilon to support vector regression", { + skip_if_not_installed("e1071") + d <- mtcars + for (eps in c(0.01, 1)) { + f <- fit(mpg ~ wt + hp, d, model = "e1071_svm", spec = list(epsilon = eps)) + ref <- e1071::svm(mpg ~ wt + hp, data = d, type = "eps-regression", + kernel = "radial", cost = 1, gamma = 1 / 2, epsilon = eps) + expect_equal(enginemodel(f)$epsilon, eps) + expect_equal(as.numeric(predict(f, d)), as.numeric(predict(ref, d)), tolerance = 1e-6) + } + f_lo <- fit(mpg ~ wt + hp, d, model = "e1071_svm", spec = list(epsilon = 0.01)) + f_hi <- fit(mpg ~ wt + hp, d, model = "e1071_svm", spec = list(epsilon = 0.5)) + expect_gt(enginemodel(f_lo)$tot.nSV, enginemodel(f_hi)$tot.nSV) +})