How to find the linearity of 5 points I am currently developing a software in C. I have an array (matrix) that contains some coordinate points like this (125 points for now):
\begin{bmatrix}x1&x2&x3&x4&x5&...&x125\\y1&y2&y3&y4&y5&...&y125\end{bmatrix}
What I would like to do is calculating how linear each group of 5 points are and due to the source of the data, the points will never be perfecly linear in my case. I am thinking that there will be error value that shows how these 5 points far from the perfect linearity and if this error value small from the specified value, the result will show the points as linear and if not it will show as non-linear.
I drew a simple model. The algorithm will return 'linear' for the left one and 'not linear' for the right one.

I looked at the linear regression method, but I am not sure if this is the right way. Can you help me if there is a better and more importantly an efficient way to do that.
 A: Regression, specifically simple linear regression, is exactly how I would go about this.
Your regression line is given by a linear equation
$$ y = \alpha+\beta x. $$
The coefficients $\alpha$ and $\beta$ are typically set to values that will minimize the sum of squared vertical (!) distances between the data points and the corresponding point on the line. A little mathematics (see Wikipedia) gives us explicit formulas:
$$ \beta = \frac{\sum_{i=1}^n(x_i-\bar{x})(y_i-\bar{y})}{\sum_{i=1}^n(x_i-\bar{x})^2} $$
and
$$ \alpha = \bar{y}-\beta\bar{x},$$
where $\bar{x}$ and $\bar{y}$ are the averages of the $x_i$ and the $y_i$, respectively.
Given this regression line, I would calculate the mean squared error, i.e., the mean of the squared vertical distances between the points and the line per above:
$$ \text{MSE} = \frac{1}{n}\sum_{i=1}^n\big(y_i-(\alpha+\beta x_i)\big)^2. $$
Then you can use a cutoff for the MSE to classify your points as linear or not. This cutoff will need to depend on your $n$, on the spread of your data, and on your substantive question.

Now, you should check for yourself whether this does what you want. Take a number of "realistic" samples, plot them and the regression line, and see whether you can find a useful cutoff for the MSE that classifies your samples the way you want.
Things to look out for are, e.g., "steep" clouds of points, with a large slope $\beta$ - since the MSE looks at vertical distances, it will be large for such point clouds, which will then be more easily classified as "not linear", even though they "look" linear to us.
If this is a problem, you could look at alternative like least absolute deviations, where the regression line is calculated to minimize not the sum of squared vertical distances, but the sum of absolute vertical distances, or alternatively orthogonal regression, which minimizes the sum of orthogonal squared distances from the regression line, rather than vertical ones. However, these methods are quite a bit more complex than simple linear regression, where we have the closed form formulas above. (Another alternative: calculate the regression line as above, but then classify your point clouds by the absolute vertical or squared/absolute orthogonal distance from that "vanilla" regression line.)
