KNNRLD.tune: Tune k in KNNRLD using Cross-Validation

View source: R/Tuning_KNNRLD.R

KNNRLD.tuneR Documentation

Tune k in KNNRLD using Cross-Validation

Description

Finds the optimal number of neighbors for KNN regression in longitudinal data using k-fold cross-validation. Evaluates k values from 2 to A.

Usage

KNNRLD.tune(
  y,
  x,
  nfolds = 10,
  folds = NULL,
  seed = NULL,
  A = 10,
  graph = FALSE
)

Arguments

y

Matrix of longitudinal outcomes (subjects x timepoints).

x

Matrix of predictor variables (subjects x features).

nfolds

Number of cross-validation folds (default = 10).

folds

Optional list of pre-specified fold indices. If provided, nfolds is ignored.

seed

Optional random seed for reproducibility.

A

Maximum number of neighbors to evaluate (searches from 2 to A, default = 10).

graph

Logical; if TRUE, plots MSPE vs. k.

Value

A list containing:

crit

Mean squared prediction error (MSPE) for each k value

best_k

Optimal number of neighbors (minimizes MSPE)

performance

Minimum MSPE value

runtime

Elapsed computation time

Examples


set.seed(123)
n <- 30
T <- 3
d <- 2
x <- matrix(runif(n * d), nrow = n)
y <- matrix(rnorm(n * T), nrow = n)
tune_result <- KNNRLD.tune(
  y = y,
  x = x,
  nfolds = 3,
  A = 4
)
str(tune_result)



CKNNRLD documentation built on May 29, 2026, 1:06 a.m.

Related to KNNRLD.tune in CKNNRLD...