KNNRLD: Standard K-Nearest Neighbor Regression for Longitudinal Data

View source: R/KNNRLD.R

KNNRLDR Documentation

Standard K-Nearest Neighbor Regression for Longitudinal Data

Description

This function performs KNN regression for longitudinal data without clustering. It predicts longitudinal outcomes for new observations based on the weighted average of their k nearest neighbors in the predictor space.

Usage

KNNRLD(xnew, y, x, k = 5)

Arguments

xnew

A matrix of predictor values for prediction (test set).

y

A matrix or data frame of longitudinal responses (training set).

x

A matrix or data frame of training predictor values.

k

Number of nearest neighbors to use. Can be a scalar or a vector.

Value

A list of matrices with predicted values for each value of k. Each matrix has dimensions nrow(xnew) x ncol(y).

Examples


set.seed(123)
n <- 30
T <- 3
d <- 2
x <- matrix(runif(n * d), nrow = n)
y <- matrix(rnorm(n * T), nrow = n)
train_idx <- sample(1:n, 20)
test_idx <- setdiff(1:n, train_idx)
pred <- KNNRLD(
  xnew = x[test_idx, ],
  y = y[train_idx, ],
  x = x[train_idx, ],
  k = 3
)
head(pred[[1]])



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

Related to KNNRLD in CKNNRLD...