How can I get maximum and minimum values after applying LOESS

Asked

Viewed 122 times

5

My data is brought from a database and I simply apply:

ggplot (data = df_postgres, aes (x = date_time, y = duracao)) + geom_point() + stat_smooth(method = "loess") 

And I get the following smoothing:

Suavização obtida

There is the possibility of obtaining the maximum and minimum points of the curve in blue?

  • That’s right, folks. In both ways I got the result I expected. Thanks! :)

2 answers

2

When you use the function stat_smooth of ggplot2, he performs the following steps:

I’ll use the database mtcars, but you can easily replace it with your own.

modelo <- loess(mpg ~ hp, data = mtcars) # ajusta o modelo  loess
mtcars$pred <- predict(modelo) # calcula as predicoes

ggplot (data = mtcars, aes (x = hp, y = mpg)) + geom_point() + 
 geom_line(aes(y = pred), colour = "blue") # plota as predicoes usando linha

See that this graph is equal to the graph made using:

ggplot (data = mtcars, aes (x = hp, y = mpg)) + geom_point() + 
 stat_smooth(method = "loess")

Therefore, to get the maximum value of the curve just filter in your date.frame:

mtcars[mtcars$pred == max(mtcars$pred), ]

2


ggplot uses the function loess base R. You can run the setting outside the graphical command and get the values using predict:

set.seed(4)
dados <- data.frame(date_time = 1:20, duracao = rnorm(20))
dados.loess <- loess(duracao ~ date_time, data = dados)

summary(predict(dados.loess))
#    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
#-0.06347  0.15680  0.34110  0.35310  0.56030  0.74970

Confirming the result, we can add the lines on the chart:

library(ggplot2)
ggplot(data = dados, aes (x = date_time, y = duracao)) +
  geom_point() + stat_smooth(method = "loess") +
  geom_hline(aes(yintercept = min(predict(dados.loess)))) +
  geom_hline(aes(yintercept = max(predict(dados.loess))))

inserir a descrição da imagem aqui

  • Can you tell me if you can find, too, for every change of the curve, the maximum and minimum value?

  • Do you say the tilt signal changes? If yes, you will need the derivative of the data, which can be obtained with diff. This would probably be more appropriate in a new question, but take a look at this code. Use the answer and change only where you have 20 to 1000. y <- predict(dados.loess);&#xA;plot(y, type = "l");&#xA;m <- abs(diff(y, 1));&#xA;match(sort(m), m)[1:10];&#xA;abline(v=c(6953, 2621)).

  • That, every change in the tilt signal, would also like to get the maximum points and the minimum points. But in this case it takes the maxima and minima of the points and not of the curve. Wanted of the curve that is generated by the LOESS also.

  • @Samara I suggest you ask a new question for this problem and include an example of your data to facilitate the work of those who will answer.

  • All right, thank you.

  • It worked!!! That’s right.. deriving the data. It was like this: dados.frame <- data.frame(dados); plot(dados.frame); dados.loess <- loess(duracao ~date_time, data=dados.frame); xl <- with(dados.frame, seq(min(date_time),max(date_time), (max(date_time) - min(date_time))/1000)); y.predict <- predict(dados.loess, xl); lines(xl,y.predict); infl <- c(FALSE, diff(diff(y.predict)>0)!=0); points(xl[infl ], y.predict[infl ], col="Red", pch=19, cex=1.25) Thanks for your help

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.