# What is a good way to measure the “linearity” of a dataset?

I have an empirically gathered dataset which relates two variables. Over a small range the relationship appears linear, however over a larger range there is clearly some second order polynomial relationship as can be seen in the image at http://imgur.com/W7f9p.

I'm trying to get a measure of linearity for different ranges considered. E.g. at 20 < x < 60 or 100 < x < 120 it is very linear, but at 20 < x < 180 it is not very linear. I have tried to fit a straight line to the data and calculate the R^2 data (goodness of fit) but this shows that the straight line over the larger range has a better fit than over the smaller range. While this may be true with MS Excel, from the image it is clear that the larger range is less linear...if you hold the side of a piece of paper against the points.

Is there a better way to measure the "linearity" of a dataset?

-
This question probably should (and likely will be) migrated to the statistics site. There are a ton of great stats software for free out there. You might lookup the program R and give it a try. –  AdamRedwine Jun 6 '12 at 12:27
You might be interested in this paper ncbi.nlm.nih.gov/pubmed/16724492 The idea is to fit a piecewise linear model and to test the equality of slopes (I do not comment anymore because I haven't read it) –  Stéphane Laurent Jun 6 '12 at 17:54
Are you truly interested in the linearity of the data or in the linearity of the underlying curve? The distinction is that because the data (may) sample the curve unevenly, a measure based on the data would vary according to the nature of the sampling, whereas an estimate of the linearity of the curve would be more stable under changes of sampling program. Also, do you conceive of "linearity" as an absolute property (and thereby dependent on the units of measurement) or is it a property of the shape of the curve (and thereby invariant under affine transformations of x and y)? –  whuber Jun 6 '12 at 22:11

## migrated from physics.stackexchange.comJun 6 '12 at 14:44

This question came from our site for active researchers, academics and students of physics.

Fit a quadratic instead of a linear function. The absolute value of the estimate of the highest coefficient of the quadratic serves as a sensible measure of linearity, which is zero if the data lie exactly on a line. Moreover, if the data come from a linear model with Gaussian noise, the Gauss-Markov theorem guarantees that the coefficient estimates are unbiased, hence under repetition of the fit with multiple data from the same model distribution, the expected value of the coefficient will be zero.

Of course in a single fit, one usually doesn't get zero, so one would have to use some test for the significance of the coefficients.

-
could you elaborate a little on why it serves as a sensible measure of linearity? –  Lucas Reis Jun 6 '12 at 16:55
@LucasReis: I added some rationale. –  Arnold Neumaier Jun 6 '12 at 17:20
(+1) It strikes me that any reasonable measure of "linearity" ought to be invariant under changes of location and scale (in both the dependent and independent variables). That rules out using the quadratic term, but suggests there may be merit in considering using the quadratic term when standardized variables are used in the regression. Note, though, that the quadratic term will not capture complex departures from linearity such as a wave-like pattern. –  whuber Jun 7 '12 at 21:54
@whuber: good point. The answer by Michael Chernick has this property and hence is to be preferred to mine. –  Arnold Neumaier Jun 8 '12 at 15:43
Actually, I prefer your solution as modified by @Douglas Maynard (who, by using betas, achieves an invariant result). I, too, initially liked Chernick's answer, but when I examined it more deeply, it appeared wanting in some key respects. One is that it strongly depends on the sampling. E.g., heavy sampling at extreme $x$ values will drive $|\rho|$ to $1$ without changing the overall curvature. Another is that it mis-characterizes horizontal lines, which are perfectly linear but for which $|rho|\approx 0$! The quadratic term, on the other hand, is robust to such capricious changes. –  whuber Jun 8 '12 at 15:48