Fits a functional regression model using kernel smoothing (Nadaraya-Watson). Supports fixed bandwidth (h), or k-nearest neighbors with global (kNN.gCV) or local (kNN.lCV) cross-validation.
Usage
fregre.np(
fdataobj,
y,
h = NULL,
knn = NULL,
type.S = c("S.NW", "kNN.gCV", "kNN.lCV"),
Ker = "norm",
metric = metric.lp,
...
)Arguments
- fdataobj
An object of class 'fdata' (functional covariate).
- y
Response vector.
- h
Bandwidth parameter. If NULL and knn is NULL, computed automatically.
- knn
Number of nearest neighbors to consider for bandwidth selection. Only used when
type.Sis "kNN.gCV" or "kNN.lCV".- type.S
Type of smoother: "S.NW" for Nadaraya-Watson with fixed h (default), "kNN.gCV" for k-NN with global CV (single k for all observations), "kNN.lCV" for k-NN with local CV (different k per observation).
- Ker
Kernel type for smoothing. Default is "norm" (Gaussian).
- metric
Distance metric function. Default is metric.lp.
- ...
Additional arguments passed to metric function.
Value
A fitted regression object of class 'fregre.np' with components:
- fitted.values
Fitted values
- residuals
Residuals
- h.opt
Optimal/used bandwidth (for type.S = "S.NW")
- knn
Number of neighbors used (for kNN methods)
- k.opt
Optimal k value(s) - scalar for global, vector for local
- type.S
Type of smoother used
- Ker
Kernel type used
- fdataobj
Original functional data
- y
Response vector
- mdist
Distance matrix
- sr2
Residual variance
- metric
Metric function used
- call
The function call
Details
Three smoothing approaches are available:
Fixed bandwidth (type.S = "S.NW"): Uses a single bandwidth h for all predictions. If h is not provided, it is set to the median of non-zero pairwise distances.
k-NN Global CV (type.S = "kNN.gCV"): Selects a single optimal k for all observations using leave-one-out cross-validation. The bandwidth at each point is set to include k neighbors.
k-NN Local CV (type.S = "kNN.lCV"): Selects an optimal k_i for each observation i, allowing adaptive smoothing. Useful when the data has varying density across the functional space.
Examples
# Create functional data
t <- seq(0, 1, length.out = 50)
n <- 50
X <- matrix(0, n, 50)
for (i in 1:n) X[i, ] <- sin(2*pi*t) * i/n + rnorm(50, sd = 0.1)
y <- rowMeans(X) + rnorm(n, sd = 0.1)
fd <- fdata(X, argvals = t)
# Fixed bandwidth
fit1 <- fregre.np(fd, y, h = 0.5)
# k-NN with global CV
fit2 <- fregre.np(fd, y, type.S = "kNN.gCV", knn = 20)
# k-NN with local CV
fit3 <- fregre.np(fd, y, type.S = "kNN.lCV", knn = 20)