Hi, I was interested in the asymptotic complexity so I tried adapting
your example code using R-devel, and I got a result that looks like
linear memory, but for time it looks more like cubic than quadratic:
https://github.com/tdhock/atime/issues/135#issuecomment-6668217045

Then I tried applying your patch, and I get an error at N=39810:

Error in predLoess(object$y, object$x, newx = if (is.null(newdata))
object$x else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)),  (from
atime-135.R#1) :
  workspace required (2377642345) is too large probably because of
setting 'se = TRUE'.

Is that expected? (your patch uses a lot more memory?)

Toby Hocking

On Thu, Nov 5, 2026 at 11:51 AM Dong Yin via R-devel
<[email protected]> wrote:
>
> 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

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to