def
func:fitLinearRegression
fitLinearRegression(grid, opts: null)
Given a grid of x, y coordinates compute the best fit linear regression equation using the ordinary least squares method. The first column of the grid is used for x
and the second column is y
. Any rows without a Number for both x and y are skipped. Any special Numbers (infinity/NaN) are skipped.
Options:
x
: column name to use for x if not first columny
: column name to use for y if not second column
The resulting linear equation is:
yᵢ = mxᵢ + b
The equation is returned as a dictionary with these keys:
m
: slope of the best fit regression lineb
: intercept of the best fit regression liner2
: R² coefficient of determination as a number between 1.0 (perfect correlation) and 0.0 (no correlation)xmin
: minimum value of x variable in sample dataxmax
: maximum value of x variable in sample dataymin
: minimum value of y variable in sample dataymax
: maximum value of y variable in sample data
Also see matrixFitLinearRegression()
to compute a multiple linear regression.
Example:
data: [{x:1, y:2}, {x:2, y:4}, {x:4, y:4}, {x:6, y:5}].toGrid fitLinearRegression(data) >>> {m:0.4915, b: 2.1525, r2: 0.7502}