Fit non-linear customised model

I have a data.frame that have two cols, $x=mz$ and $y=res$. There are about ~2 million rows in the DF. When I plot the graph I get the below.

What I'd like to do is find a way to define two quadratics to fit to get the two curves badly sketched in orange. it would be nice to be able to do it in ggplot. I have tried to fit a stat_smooth but I haven't been able to come close to the result I want.

plot - ggplot(data=df, aes(mz, res)) + geom_point()
plot + stat_smooth(method = lm, formula = y ~ I(x^-2) - x)

Apologies if this is something really trivial and obvious, but I am not a data scientist and I am just trying to help a friend.

Topic ggplot2 linear-regression r

Category Data Science


You could try quantile regression using a "generalised additive" model. With R you could use the qgam package, I guess. What you need to do is to estimate a model for upper/lower values of the distribution. The figure below shows a plot from the qgam vignette linked above.

enter image description here

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.