Question Details

No question body available.

Tags

r

Answers (3)

Accepted Answer Available
Accepted Answer
June 21, 2025 Score: 4 Rep: 167,708 Quality: Expert Completeness: 60%

I think you want a 2-step use of cummin/cummax:

#' @param x numeric or integer #' @param i integer; if negative, it will be converted to length(x)+1+i, #' indexing from the right-side of the vector, -1 maps to length(x); #' it is an error if i == 0 or i > length(x) fun
June 21, 2025 Score: 2 Rep: 273,533 Quality: Low Completeness: 50%

Take the cumulative minimum of the first p points and then the cumulative maximum of the vector formed from the last cumulative minimum followed by the remaining points and then at the end omit the the first of these cumulative maximum points. Concatenate those two vectors to get the result.

f
June 22, 2025 Score: 0 Rep: 11,812 Quality: Medium Completeness: 80%

It appears more logical to me to perform an isotonic regression; Details section from help file, see help(isoreg):

The algorithm determines the convex minorant 𝑚 ( 𝑥 ) m(x) of the cumulative data (i.e., cumsum(y)) which is piecewise linear and the result is 𝑚 ′ ( 𝑥 ) m ′ (x), a step function with level changes at locations where the convex 𝑚 ( 𝑥 ) m(x) touches the cumulative data polygon and changes slope. as.stepfun() returns a stepfun object which can be more parsimonious.

dat = c(3, 4, 2, 10, 1, 23, 11, 44)
ir = isoreg(dat)
> ir
Isotonic regression from isoreg(x = dat),
 with 5 knots / breaks at obs.nr. 1 3 5 7 8 ;
 initially ordered 'x'
 and further components List of 4
$ x : num [1:8] 1 2 3 4 5 6 7 8
$ y : num [1:8] 3 4 2 10 1 23 11 44
$ yf: num [1:8] 3 3 3 5.5 5.5 17 17 44
$ yc: num [1:9] 0 3 7 9 19 20 43 54 98
plot(ir$x, ir$yf, type='b')
points(ir$x, ir$y, col='red')

enter image description here