'R: smooth.spline LOOCV-error depends on order of datapoints?

I wanted to fit a smoothing spline to some data and I noticed that the internally computed LOOCV-error seems to depend on whether the data is unordered or not. Concretely, I only get the expected result when the data is ordered.

I don't see why this is to be expected? Any help?

set.seed(0)
x <- seq(1:10)
y <- x^2 + rnorm(10,0,2)

fit.ss <- smooth.spline(x=x, y=y,  cv=TRUE)
cat("CV ordered: ",format(fit.ss$cv.crit))
# CV ordered:  13.46173

xu <- sample(x)
yu <- y[xu]
fit.ss.u <- smooth.spline(x=xu, y=yu,  cv=TRUE)
cat("CV unorderd: ",format(fit.ss.u$cv.crit))
# CV unorderd:  65552.74

spar.opt <- fit.ss$spar
preds <- rep(NA, 10)
for (i in 1:10){
  ss <- smooth.spline(x=x[-i], y=y[-i],  cv=TRUE, spar=spar.opt)
  preds[i] <- predict(ss,x=x[i])$y
}
cat("CV manual: ",format(mean((preds - y)**2)))
# CV manual:  13.49424

CV ordered and CV manual are (almost) the same and as expected, whereas the unordered version is completely off.

Note that this is a duplicate of https://stats.stackexchange.com/q/561802/213798, where I don't seem to get any input.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source