### 7.2.4 Linear regression: linear_regression linear_regression_plot

Given a set of points (x_{0},y_{0}),…,(x_{n−1},y_{n−1}), linear
regression finds the line y=mx+b that comes
closest to passing through all of the points; i.e., that makes
√(y_{0} − (m x_{0} + b))^{2} + … + (y_{n−1} − (m x_{n−1} + b))^{2}
as small as possible. Given a set of points (a two-column matrix) or
two lists of numbers (the x- and y-coordinates), the
linear_regression command will find the values of m and
b which determine the line. For example, if you enter

linear_regression([[0,0],[1,1],[2,4],[3,9],[4,16]])

or

linear_regression([0,1,2,3,4],[0,1,4,9,16])

you will get

4, -2

which means that the line y = 4x − 2 is the best fit line.

The best fit line can be drawn with the
linear_regression_plot command; if you enter

linear_regression_plot([0,1,2,3,4],[0,1,4,9,16])

you will get

This will draw the line (in this case y=4x−2) and give you the
equation at the top, as well as the R^{2} value, which is

(The R^{2} value will be between 0 and 1 and is one measure of how
good the line fits the data; a value close to 1 indicates a good fit,
a value close to 0 indicates a bad fit.)