# Fitting line to data

#### RHassanpour

I want to fit two lines to the data in the attached image; one line from the top and one line from the bottom. Like what you see in the attachment. What is the best way to do this? Is there any software that can do this?

#### DarnItJimImAnEngineer

It depends on what you want the lines to represent.
This kind of resembles the confidence interval of the curve fit (also known as the ScheffÃ© band), although unless you set the confidence level extremely high, it would be a much tighter fit than that.

You could also estimate a precision interval of the curve fit using the same technique.

In both of these cases, you would start by curve-fitting the data (which you can do in Excel) and then calculating the standard error of the fit (which you can also do in Excel.

I don't know if this is what you want, though.

#### RHassanpour

What I need here is the slope and intercept of the two upper and lower lines. As far as I know, in Excel, only one line fits into the data.

#### [email protected]

What I need here is the slope and intercept of the two upper and lower lines. As far as I know, in Excel, only one line fits into the data.
Yes, but the red curves are not straight lines if you look at it. So you can't get a slope and intercept.

Try quantile regression for what you want.

Last edited by a moderator:

#### DarnItJimImAnEngineer

Yes, but the red curves are not straight lines if you look at it. So you can't get a slope an intercept.
That's not even the problem. Polynomial, exponential, etc. regressions are totally possible; even a linear regression would get you some kind of estimate.

The problem is, regressions try to fit the data set (as a whole). OP is trying to fit the upper limit and lower limit of the data set. There is no simple or unique way to define this.

Does quantile regression address this somehow? (I'm honestly not familiar with the concept, and couldn't follow the Wikipedia article at first glance.)

#### [email protected]

That's not even the problem. Polynomial, exponential, etc. regressions are totally possible; even a linear regression would get you some kind of estimate.

#### DarnItJimImAnEngineer

No, I know. He was asking how to get the slope and intercept of a curve, and you were pointing out neither curve was a straight line and therefore neither had a singular slope or intercept. I got that.

I was skipping ahead and responding to the next question. OP might have asked, "If we make it a straight line instead, how can we find the slope and intercept?" Or, "If we model it as a quadratic equation, how can we get the coefficients of the quadratic equation?" Except the only good way I saw to do that was with some kind of regression, and linear/polynomial/exponential/etc. regression tries to fit the entire data set. We would need to select some form of "edge detection" algorithm to select points to represent the upper boundary and the lower boundary, and curve fit each of these. This, of course, begs the definition of what constitutes an "edge point."

And that's kind of why I was asking what the lines were supposed to represent in the first place. Is OP looking for something like, "draw a curve that will contains 95 % of the data points from an infinite set?" Because we can do this with some assumptions about the probability distribution. Or do they just want a curve that looks somewhat like the general shape we discern as humans? Because in that case, I'd just grab a French curve, pick three points, and trace it.

(And seriously, if quantile regression does what they want, then I have egg on my face. I still don't understand what it is or does.)

1 person