I am trying to calculate the least squares regression line y=mx+b for a set of data points where the sample data for y is an angle in the range -180..180 and x is unbounded scalar. I realize such a linear equation will produce y values outside of the range -180..180, but that is OK as I can easily re-bound them after the fact. The trouble I am running into is calculating the least squares coefficients (m and b) over data where y has a circular range.

To clarify the data, consider the following:

1) X represents time

2) Y is an angle returned from a piece of hardware, in the range -180 to 180. The Y values are cyclical, they will continue to wrap if the sample period is sufficiently long. A reasonable analogy would be the angle of a particular spoke on a wheel over time.

3) There is significant noise in the angle data, so I plan to be sampling for long enough to get probably a few thousand samples per collection. Also, this is the reason I intend on using something like a least squares regression.

4) I am changing the conditions in each sample collection. The change of conditions impact the rate of change in Y over time, but it is always linear, and is not predicable from the conditions without measuring. Some samples of data may include more than a single cycle of Y (It can wrap around) if the rate is high enough or the sample period long enough

4) My ultimate goal is to create a y=mx+b equation for each of the condition sets. This will allow me to estimate angle Y based on time X. I am in effect creating a calibration table.

Here is some sample data, where I would expect a slope m of approximately 1: (110, 150), (120, 170), (130, -175), (140, 180), (150, -175), (160, -160), (170, -140), (180, -140)


The way you have the data, linear regression is not going to work correctly... Here's a quick plot of the data with a regression line. enter image description here

In fact, the regression line has a negative slope, quite the opposite of what you wanted. This effect is called Simpson's Paradox.

If I understand correctly, an angle of -170 corresponds to an angle of 190. So just transform your data onto the range 0 to 360 and things will work out for you.

enter image description here

This has a slope of 0.904 as desired.

| cite | improve this answer | |
  • $\begingroup$ I'm finding a slightly higher correlation between $x$ and the sine this "transformed" $y$ than between $x$ and $y$. (Be sure to take sine in degrees, not radians.) $\endgroup$ – Michael Hardy Jun 16 '17 at 18:56
  • $\begingroup$ But I find EXACTLY $0$ correlation between $x^2$ and the residuals from regeression of (this transformed) $y$ on $x$. Of course there must be $0$ correlation between $x$ and these residuals, but why between $x^2$ and these residuals? $\endgroup$ – Michael Hardy Jun 16 '17 at 19:05
  • $\begingroup$ The "Regression line, Corrected Y" is exactly what I am trying to calculate. I guess the problem I encounter is for an arbitrary point, I do not know which direction to "correct" to move it towards the regression line without visual inspection. In fact, my data might "wrap" more than once time in a given set. I feel like I need to map the angles on to a unit circle or something, but I don't know how to do the regression calculation with the result. $\endgroup$ – Steve Jun 16 '17 at 19:51
  • $\begingroup$ I'm not sure I really understand your data. Why can't you just convert negative angles to their positive analogue? To repeat myself, an angle of $-170$ is the same as the angle $190$, yes? $\endgroup$ – knrumsey Jun 16 '17 at 20:28
  • 1
    $\begingroup$ I certainly could convert them to 0..360. But then if my data looks like 340, 10, 355, 20, 25, 45, 50. I have the same averaging problem, I've just moved the wrap point. This is further complicated if my data wraps from 360 back to zero a second time (which can happen). $\endgroup$ – Steve Jun 16 '17 at 20:43

A good solution could be augmenting the datasets with value +/-360 degrees, obtaining a triple dataset. There are two options from there:

  1. Fit a robust line using RANSAC. Considering there is 1/3 chance to find two points from the same cluster, this is expected to work quite well, and there are many implementations available.
  2. Try fitting two parallel regression lines e.g. http://people.inf.ethz.ch/arbenz/MatlabKurs/node86.html
| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.