For _nadarayawatson <- function(y, x, x0, kern, ...), it uses y and x to fit the model, and it predicts the output with x0 parameter. That caused problem for your y_hat_test:
y_hat_test <- nadaraya_watson(y_test, x_test, x_test, kern = kernel_k_nearest_neighbors, k = k)
You should still use _ytrain and _xtrain to fit the model, and predict _y_hattest with _xtest.
Also, your homework looks incomplete. For question 4 and 5, https://biostat.app.vumc.org/wiki/pub/Main/CourseDSI5640/kNN-CV.R would be one good way to conduct cross-validation test and plot the figure. Just think about how to replace knnreg() with _nadarayawatson().
Hi,
For _nadarayawatson <- function(y, x, x0, kern, ...), it uses y and x to fit the model, and it predicts the output with x0 parameter. That caused problem for your y_hat_test:
y_hat_test <- nadaraya_watson(y_test, x_test, x_test, kern = kernel_k_nearest_neighbors, k = k)
You should still use _ytrain and _xtrain to fit the model, and predict _y_hattest with _xtest.Also, your homework looks incomplete. For question 4 and 5, https://biostat.app.vumc.org/wiki/pub/Main/CourseDSI5640/kNN-CV.R would be one good way to conduct cross-validation test and plot the figure. Just think about how to replace knnreg() with _nadarayawatson().