You might like the quantile regression, which is available in the quantreg package. Will it be useful, it will depend on whether you want the absolute maximum in your "windows" to be admissible some kind of extreme quantile, say, the 95th or 99th? If you are not familiar with quantile regression, then consider linear regression that matches the expected or average response model due to model covariates. Quantum regression for the average quantile (0.5) would be consistent with the model for the median response due to model covariates.
Here is an example of using the quantreg package to show you what I mean. First, create some dummy data, similar to the data you display:
set.seed(1) N <- 5000 DF <- data.frame(Y = rev(sort(rlnorm(N, -0.9))) + rnorm(N), X = seq_len(N)) plot(Y ~ X, data = DF)
Then fit the model to the 99th percentile (or 0.99 quantile):
mod <- rq(Y ~ log(X), data = DF, tau = .99)
To generate a “fitted line”, we predict from the model 100 uniformly spaced values in X
pDF <- data.frame(X = seq(1, 5000, length = 100)) pDF <- within(pDF, Y <- predict(mod, newdata = pDF))
and add the appropriate model to the diagram:
lines(Y ~ X, data = pDF, col = "red", lwd = 2)
This should give you the following:
