CKNNRLD: Cluster-based KNN Regression for Longitudinal Data (CKNNRLD)

View source: R/CKNNRLD.R

CKNNRLDR Documentation

Cluster-based KNN Regression for Longitudinal Data (CKNNRLD)

Description

This function implements a clustering-based KNN regression method designed for longitudinal datasets. It first clusters the training data using longitudinal clustering (via latrend) and then performs KNN regression within each selected cluster.

Usage

CKNNRLD(xnew, y, x, k = 5, c = 4, cluster_method = "kml")

Arguments

xnew

A matrix of predictor values for test data (new observations).

y

A matrix or data frame of longitudinal responses (subjects x timepoints).

x

A matrix or data frame of predictors for training data.

k

Number of nearest neighbors to use. Can be a scalar (same k for all clusters) or a vector (different k per cluster).

c

Number of clusters for longitudinal clustering.

cluster_method

Clustering method to use. Currently supports "kml" (default).

Value

A data frame with predicted values and cluster assignment for each observation in xnew. Columns: cluster, Y1, Y2, ..., Yd (where d = number of timepoints).

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)
result <- CKNNRLD(
  x = x[train_idx, ],
  y = y[train_idx, ],
  xnew = x[test_idx, ],
  k = 3,
  c = 2
)
head(result)



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

Related to CKNNRLD in CKNNRLD...