Sorry, it looks like the list stripped the text of my previous message, so here it is again without the images.
I am a student working on neuroimaging. I found a repeated linear search in ehg191 that makes predict.loess(se = TRUE) much slower than it needs to be. It is still present in R-devel r90840 (src/library/stats/src/loessf.f, lines 799 to 809). For every training observation and every k-d tree vertex, the routine scans that vertex's neighbour list backwards, so with a fixed span the search work grows with n squared. This is the path ggplot2's geom_smooth(method = "loess") takes by default, since it draws a confidence band, so I suspect quite a few people wait on it without knowing why. The proof of concept (ehg191-lookup.diff.txt, attached to my previous message: https://stat.ethz.ch/pipermail/r-devel/attachments/20261105/f1a77502/attachment.txt) builds the inverse neighbour positions once and replaces the scan with a lookup. On an Apple M4 chip, predicting at 80 points with n = 40,000 and span = 0.5 took 24.2 s before and 0.22 s after (medians of three paired runs). L and the full prediction result were bit for bit identical in every timing configuration and in 1,000 random configurations with one to three predictors, and 24 further fits, including statistics = "exact", were identical too. I think the slow part can be reproduced with: set.seed(20261105) n <- 20000L x <- runif(n, -3, 3) y <- sin(x) + rnorm(n, sd = 0.35) f <- loess(y ~ x) nd <- data.frame(x = seq(min(x), max(x), length.out = 80L)) system.time(predict(f, nd, se = TRUE)) I rebuilt the trunk loess.R, loessc.c and loessf.f, unchanged and patched, under R 4.5.2 on macOS arm64. Would this be worth turning into a proper patch with a regression test? I would be glad of advice on how you would prefer the workspace to be allocated. Thanks, Don ________________________________ From: Dong Yin Sent: Thursday, 05 November 2026 14:44:33 To: [email protected] <[email protected]> Cc: Y. Fang <[email protected]> Subject: loess: repeated neighbour list search in ehg191 makes predict(se = TRUE) slow Hi all, I am a student working on NeuroImaging. I found a repeated linear search in ehg191 that makes predict.loess(se = TRUE) much slower than it needs to be. It is still present in R-devel r90840 (src/library/stats/src/loessf.f, lines 799 to 809). so, for every training observation and every k-d tree vertex, the routine scans that vertex's neighbour list backwards, so with a fixed span the search work grows with n squared. This is the path ggplot2's geom_smooth(method = "loess") takes by default, since it draws a confidence band, so I suspect quite a few people wait on it without knowing why The attached POC builds the inverse neighbour positions once and replaces the scan with a lookup. The change in loessf.f is: On an apple m4 chip, predicting at 80 points with n = 40,000 and span = 0.5 took 24.2 s before and 0.22 s after (medians of three paired runs). L and the full prediction result were bit for bit identical in every timing configuration and in 1,000 random configurations with one to three predictors, and 24 further fits, including statistics = "exact", were identical too I think the slow part can be reproduced with: set.seed(20261105) n <- 20000L x <- runif(n, -3, 3) y <- sin(x) + rnorm(n, sd = 0.35) f <- loess(y ~ x) nd <- data.frame(x = seq(min(x), max(x), length.out = 80L)) system.time(predict(f, nd, se = TRUE)) I rebuilt the trunk loess.R, loessc.c and loessf.f, unchanged and patched, under R 4.5.2 on macOS arm64 Would this be worth turning into a proper patch with a regression test or some Pls? I would be glad of advice on how you would prefer the workspace to be allocated. Thanks, Don [[alternative HTML version deleted]] ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
