title: Linear regression in Objective-C
body: <p>I´m trying to implement a method that fits a line to a set of points in 2D.
  I wrote the following code that reads the data from two Array (X, Y coordinate) 
  and should calculate the parameters of the best fitting line with the least squares method.
  I used the formulas given here:
  <a href="http://mathworld.wolfram.com/LeastSquaresFitting.html" rel="nofollow">mathworld.wolfram</a></p>
  
  <pre><code>- (void) linearRegressionOfUserAcceleration 
  {
       double avgX = [[_accelBufferX valueForKeyPath:@"@avg.doubleValue"] doubleValue];
       double avgY = [[_accelBufferY valueForKeyPath:@"@avg.doubleValue"] doubleValue];
       int n = _accelBufferX.count;
  
       double ssX, ssY, ssXY;
       ssX = ssY = ssXY = 0;
       int i;
  
       // Sum of squares X, Y &amp; X*Y
       for (i = 0; i &lt; n; i++)
       {
           ssX += pow([[_accelBufferX objectAtIndex:i] doubleValue],2);
           ssY += pow([[_accelBufferY objectAtIndex:i] doubleValue],2);
           ssXY += [[_accelBufferX objectAtIndex:i] doubleValue] * [[_accelBufferY objectAtIndex:i] doubleValue];
       }
  
       ssX = ssX - n * pow(avgX,2);
       ssY = ssY - n * pow(avgY,2);
       ssXY = ssXY - n * avgX * avgY;
  
       // Best fit of line y_i = a + b * x_i 
       b = ssXY / ssX;
       a = avgY - b * avgX;
  
       // Correlationcoefficent gives the quality of the estimate: 1 = perfect to 0 = no fit
       corCoeff = pow(ssXY,2) / ssX * ssY;
  
       NSLog(@"n: %d, a: %f --- b: %f --- cor: %f --- avgX: %f --- avgY: %f --- ssX: %f - ssY: %f - ssXY: %f", n, a, b, corCoeff, avgX, avgY, ssX, ssY, ssXY);
  }
  </code></pre>
  
  <p>I get outputs like this:</p>
  
  <pre><code>  n: 15, a: -0.095204 --- b: 0.929245 --- cor: 3.567163   --- avgX: -0.017827 -- avgY: -0.111770 --- ssX: 2.176048 - ssY: 1.898429 - ssXY: 2.022081
  </code></pre>
  
  <p>The resulting line does not fit the data at all and although the corelationCoefficient is sometimes bigger than one, which IMHO should never happen if everything works correctly. </p>
  
  <p>Does anyone see any errors in my implementation?</p>
  
  <p><strong>- EDIT -</strong></p>
  
  <p>This is the corrected code, following the tip from CRD. 
  I used this to extract the direction vector of the sampled userAcceleration in the horizontal plane between two steps, to get the step direction.</p>
  
  <p>This worked for me:</p>
  
  <pre><code>- (void) linearRegressionOfUserAcceleration
  {
      NSUInteger n = _accelBufferX.count;
      double ax, ay, sX, sY, ssX, ssY, ssXY, avgX, avgY;
  
      // Sum of squares X, Y &amp; X*Y
      for (NSUInteger i = 0; i &lt; n; i++)
      {
          @synchronized(self) {
              ax = [[_accelBufferX objectAtIndex:i] doubleValue];
              ay = [[_accelBufferY objectAtIndex:i] doubleValue];
          }
          sX += ax;
          sY += ay;
          ssX += ax * ax;
          ssY += ay * ay;
          ssXY += ax * ay;
      }
  
      avgX = sX / n;
      avgY = sY / n;
      radius = hypot(avgX, avgY);
      ssX = ssX - n * (avgX * avgX);
      ssY = ssY - n * (avgY * avgY);
      ssXY = ssXY - n * avgX * avgY;
  
      // Best fit of line y_i = a + b * x_i
      b = ssXY / ssX;
      a = (avgY - b * avgX);
      theta = atan2(1, b);
  
  
      // Correlationcoefficent gives the quality of the estimate: 1 = perfect to 0 = no fit
      corCoeff = (ssXY * ssXY) / (ssX * ssY);
  
      NSLog(@"n: %d, a: %f --- b: %f --- cor: %f   --- avgX: %f -- avgY: %f --- ssX: %f - ssY: %f - ssXY: %f", n, a, b, corCoeff, avgX, avgY, ssX, ssY, ssXY);
  }
  </code></pre>