Skip to main content
2 of 4
added 301 characters in body
MD004
  • 187
  • 1
  • 1
  • 7

There are a several ways you could do this. First recall that the linear best fit line is the line which minimizes the sum of squared residuals (see least squares): $$\sum_{i=1}^{n}{r_i^2}$$ where $r_i$ is the residual for data point $i$, and $n$ is the number of data points. A residual is the distance between a point in your data and a point on your line.

With this in mind, here's a couple ideas of how to check how well your line fits the data:

  • Calculate the max absolute distance between your data and the line. This would tell you if you have any data points that are really far away. $$\max_i{|r_1|}$$

  • Calculate the average distance between your data and the line (average of L1 norm of your residuals). This would tell you how far away most of your data points are. $$\frac{\sum_{i=1}^{n}{|r_i|}}{n}$$

  • Calculate the coefficient of determination, $R^2$: $$ R^2 = 1 - \frac{\sum_{i=1}^{n}{r_i^2}}{\sum_{i=1}^{n}{(y_i - \bar{y})}} $$ where $y_i$ represents the value of each of your data points, and $\bar{y}$ is the mean of your data.

MD004
  • 187
  • 1
  • 1
  • 7