Search code examples
rsplinesmoothing

obtain global maximum of a fitted smoothing spline from R function 'smooth.spline'


I smoothed my data using smooth.spline(x,y) and now I want to calculate the mode or maximum of smoothed curve.

x <- vect.1
y <- vect.2
plot(x, y, type = 'l')
smth <- smooth.spline(x, y)
lines(smth, col = 'red', lwd = 2)

data graph along with its smoothed curve

My current approach is simply to look up the x which gives me the maximum y which is not so accurate. Is there a better way for doing this?


Solution

  • psmth <- predict(smth, x= seq(1,100, by=0.1) )
    mode_psmth <- psmth$y[ which.max( psmth$y ) ]
    

    Or even:

    mode_psmth <- max(psmth$y)
    

    If this is not "sufficiently accurate", then please explain why.