# 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)
Run the code above in your browser using DataLab