View source: R/Tuning_KNNRLD.R
| KNNRLD.tune | R Documentation |
Finds the optimal number of neighbors for KNN regression in longitudinal data using k-fold cross-validation. Evaluates k values from 2 to A.
KNNRLD.tune(
y,
x,
nfolds = 10,
folds = NULL,
seed = NULL,
A = 10,
graph = FALSE
)
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. |
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 |
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.