-1

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?

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
Masih
  • 920
  • 2
  • 19
  • 36

1 Answers1

0
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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • thanks i think it should be psmth$'x'[which.max(psmth$'y')] to work, you may know supervisors are hard to be convinced, they want computer to tell the number not the eye! – Masih Sep 05 '14 at 05:11
  • Right ... It probably should be something different but I think `psmth$y` would be the mode. And mow that I think about it wouldn't it be easier to just use `max(psmth$y)`?I test my code when a reproducible test case is offered. – IRTFM Sep 05 '14 at 16:02