LINEST
Returns statistics that describe a linear trend matching known data points, by fitting a straight line using the least squares method.
Syntax
expression.LINEST(arg1, arg2, arg3, arg4);
expression
- A variable that represents a ApiWorksheetFunction class.
Parameters
Name | Required/Optional | Data type | Default | Description |
---|---|---|---|---|
arg1 | Required | ApiRange | ApiName | The set of y-values from the <em>y = mx + b</em> equation. | |
arg2 | Optional | ApiRange | ApiName | An optional set of x-values from the <em>y = mx + b</em> equation. | |
arg3 | Optional | ApiRange | ApiName | boolean | A logical value: the constant <em>b</em> is calculated normally if this parameter is set to -true or omitted,\ and <em>b</em> is set equal to 0 if the parameter is -false. | |
arg4 | Optional | ApiRange | ApiName | boolean | A logical value: return additional regression statistics if this parameter is set to -true,\ and return m-coefficients and the constant <em>b</em> if the parameter is -false or omitted. |
Returns
number
Example
- Code
- Result
const worksheet = Api.GetActiveSheet();
//configure function parameters
let yValues = [1.5, 2, 3];
let xValues = [2, 3.1, 3.5];
let constant = true;
let stats = true;
//set values in cells
for (let i = 0; i < yValues.length; i++) {
worksheet.GetRange("A" + (i + 1)).SetValue(yValues[i]);
}
for (let n = 0; n < xValues.length; n++) {
worksheet.GetRange("B" + (n + 1)).SetValue(xValues[n]);
}
//get x and y ranges
let yRange = worksheet.GetRange("A1:A3");
let xRange = worksheet.GetRange("B1:B3");
let func = Api.GetWorksheetFunction();
//invoke LINEST method
let ans = func.LINEST(yRange, xRange, constant, stats);
//print answer
worksheet.GetRange("D1").SetValue(ans);