# Looking for a stochastic curve fitting method

I am looking for a curve fitting method that provides upper and lower bounds confidence intervals for the fitted curve which is illustrated as figure below. Assume that the red solid curve is the fitted curve that could be obtained by ordinary curve fitting tools. However, such curve somehow predicts the expected values. I am looking for the dashed curves that are upper and lower bounds for the fitted curve. Note that my data have heteroscedasticity properties. Any help is much appreciated.

• I think the only methods to do this that will work for any estimator of the original curve are generic methods for computing confidence bounds, such as bootstrapping. Usually, one uses a method specific to the curve estimator. – Kodiologist Aug 9 '16 at 5:47

You can use the geom_smooth() function of the ggplot2 library in R.

Here is an example:

x = seq(0, 100, by=0.5)
y = sqrt(x)
y = y + rnorm(n = length(y),mean = 0,sd = 3)
df = data.frame(cbind(x,y))
require(ggplot2)
ggplot(data = df, aes(x = x,y = y)) + geom_smooth()


This is the output:

geom_smooth calls a curve fitting method called loess, which does local regression. With the default options, for every x, it considers a neighborhood containing 75% of the data points and fits a quadratic using weighted least squares. It assumes the errors are normally distributed and computes confidence intervals as described in pages 44-46 of http://www.netlib.org/a/cloess.pdf

Here is the documentation for loess: https://stat.ethz.ch/R-manual/R-devel/library/stats/html/loess.html

• Telling us about the math behind the geom_smooth() function would enhance your answer. – Mark L. Stone Aug 9 '16 at 16:54