##' Calculates the LOO CV score for given data, regression prediction function and 'span' ##' ##' @param data : regression data, a data.frame with columns 'x', 'y' ##' @param reg.fn : regr.prediction function ; with arguments: ##' reg.x: regression x-values ##' reg.y: regression y-values ##' xout : x-value(s) of evaluation point(s) ##' span : smoothing parameter ##' value: prediction at the x-values xout ##' @param span : smoothing parameter, passed to reg.fn() ##' @return LOOCV score loocv <- function(data, reg.fn, span) { ## check arguments stopifnot(is.function(reg.fn), is.numeric(data$x), is.numeric(data$y), length(n <- length(data$x)) == 1, n >= 1) ## Calculate Leave-One-Out (LOO) regression values: loo.pred <- rep(NA, n) for(i in 1:n) { loo.pred[i] <- reg.fn(data$x[-i], data$y[-i], xout = data$x[i], span = span) } ## Calculate and return cv.MSE mean((data$y - loo.pred)^2) }