# How to fit a curve at a 95% “envelope” over data points

I'm lacking the proper precise language to easily look up what I've been asked to do, so please forgive me if this is a trivial task (but please do tell me what this is called).

I have some data set which I'm fitting a curve to, and perform standard least-squares regression. I now want to fit curves with the same functional form with parameters such that:

1. 95% of the data falls above the curve
2. 95% of the data falls below the curve

to "envelope" the data at some percentile as a way of visually representing the relationship between my original fit and the data.

For clarity, I'm not trying to calculate confidence intervals in my fit parameters, but rather to estimate "best case" and "worst case" scenarios of my functional relationship.

I'm largely using matlab's curve fitting toolbox right now, but it looks like I'll have to do this manually so I'm not married to my tools.

EDIT: Whuber has explained to me that I'm trying to do a Quantile Regression at the 5th and 95th percentiles for the models I'm trying to fit.

• Could you please elaborate on the distinction you make between "best case ... worst case" and confidence intervals for the parameters? They would seem to be based on the same underlying objectives, so how exactly are they supposed to differ? – whuber Jun 15 '17 at 13:44
• @whuber Great question- a little context on what I'm doing might help here. I'm fitting dose-response curves to various outbreaks, and I've been asked to a) fit my probit/beta-poisson/exponential models to the data directly, b) create a version of each model that fits the data at the high and low percentile marks (5th/95th), which have a physical meaning close to "this is a conservative/aggressive model for this pathogen that over/underestimates illnesses." This is quite different from confidence intervals in the parameters, which tell me more about how good my fit is. – dhruvfire Jun 16 '17 at 19:56
• Is a quantile regression not what I'm trying to do? I've been reading up bits and pieces on that since you added the tag to my question and it seems to do exactly what I've described? EDIT: Sorry, misunderstood, will edit original question. – dhruvfire Jun 16 '17 at 21:20

If your data set is quite large, you could consider fitting a 5%- and a 95%-quantile regression, e.g. representing the functional relationship by splines.

One possibile procedure to consider:

A) First fit the data normally, perhaps to a spline.

B) Add an offset to the fitted equation, perhaps iteratively, until 95% of the points are below the now-offset equation. This would be the upper bound.

C) Subtract an offset from the fitted equation, perhaps iteratively, until 95% of the points are above the now-offset equation. This would be the lower bound.

• This solution can work for ordinary least squares, but in this setting involving "functional forms" (which would seem to imply least squares to fit nonlinear curves) it can produce nonsense, since it assumes that the offset curves are in the same family--which often they are not. – whuber Jun 15 '17 at 13:43
• My meaning is that a single curve is fitted to all data in step A. Steps B and C either add or subtract an offset to that same equation. – James Phillips Jun 15 '17 at 23:01
• Suppose, to be concrete about this, that the intended functional form of the curve is $x\to y=ae^{bx}$. Then no curve of the form $ae^{bx}+c$ for $c\ne 0$ will be in that form--and so your solution cannot even get started. – whuber Jun 16 '17 at 18:11
• @JamesPhillips whuber is correct here- I'm looking to refit my model parameters, not shift a curve of the same shape. I've included a bit more information on the goal in a response to whuber's comment on the question above. – dhruvfire Jun 16 '17 at 20:04