0

I have a daraset with three columns. I'm trying to implement LOOCV for KNN regression. I 'm able to do it with the LOOCV library but I'm unable to write a manual code like a function. How can I write this?

Here is how my data looks

A1      A2      A3
a        b      m
c        d      n
.        .      .
.        .      . 
.        .      . 

I've tried the sample below:

loocv_tmp <- matrix(NA, nrow = n_train, ncol = length(df))
for (k in 1:n_train) {
  train_xy <- xy[-k, ]
  test_xy <- xy[k, ]
  x <- train_xy$x
  y <- train_xy$y
  fitted_models <- apply(t(df), 2, function(degf) lm(y ~ ns(x, df = degf)))
  pred <- mapply(function(obj, degf) predict(obj, data.frame(x = test_xy$x)),
                 fitted_models, df)
  loocv_tmp[k, ] <- (test_xy$y - pred)^2
}
loocv <- colMeans(loocv_tmp)

plot(df, mse, type = "l", lwd = 2, col = gray(.4), ylab = "Prediction error",
     xlab = "Flexibilty (spline's degrees of freedom [log scaled])",
     main = "Leave-One-Out Cross-Validation", ylim = c(.1, .8), log = "x")
lines(df, cv, lwd = 2, col = "steelblue2", lty = 2)
lines(df, loocv, lwd = 2, col = "darkorange")
legend(x = "topright", legend = c("Training error", "10-fold CV error", "LOOCV error"),
       lty = c(1, 2, 1), lwd = rep(2, 3), col = c(gray(.4), "steelblue2", "darkorange"),
       text.width = .3, cex = .85)

But I want something like

lcv(train.data, train.label, K, numfold)

Any suggestion on how can I write the lcv function to perform LOOCV.

Thanks in advance.

  • can you be a bit more specific? which part of the code you would like to change and how does the desired output looks like? Here you included the plotting function which is also not relevant? – StupidWolf May 19 '20 at 09:50
  • @StupidWolf, Thanks for the reply. Actually I want to implement LOOCV manually. The code I posted above is a sample I'm referring from. I want to implement ```lcv(train.data, train.label, K, numfold)``` like this wher K goes from 1 to 10. I'm unable to figure it out. Any help would be appreciated. Thanks. – Krishna Manchodu May 19 '20 at 10:11

0 Answers0