OpenBI Docs Buy

LINESTX

A least-squares line through expressions over a table's rows, in one row: slopes, intercept, standard errors, R², F and sums of squares.

Group: Statistics. Compatible with DAX.

Syntax

LINESTX ( <table>, <expressionY>, <expressionX> [, <expressionX>] … [, <const>] )
ParameterTypeDescription
tableTableThe rows to fit the line to.
expressionYExpression
evaluated for each row
The value to explain, for each row.
expressionXExpression
repeatable, evaluated for each row
A value that explains it, for each row; one or more.
constBoolean
optional
TRUE or left out to fit an intercept; FALSE for a line through the origin. Written as TRUE or FALSE, last.

Returns

Table. One row: Slope1 … SlopeN, Intercept, StandardErrorSlope1 … StandardErrorSlopeN, StandardErrorIntercept, CoefficientOfDetermination, StandardError, FStatistic, DegreesOfFreedom, RegressionSumOfSquares and ResidualSumOfSquares.

Remarks

  • The expressions are worked out row by row, so they may use RELATED, or measures: over VALUES ( Sales[Month] ), [Liters sold] is each month's own.
  • A blank y or x counts as 0: a row drops out only when all its values, y and every x, are blank (example 3). Leave a row out with FILTER when a blank means missing.
  • The numbers are least squares as R's lm gives them: R squared, the F statistic and the standard errors included.
  • An x that follows from the others (collinear), such as one twice another or a constant, gets a slope of 0 and a standard error of 0, and the others share the fit.
  • With const FALSE there is no Intercept column, nor its standard error.
  • No more rows than coefficients is an error: one x with an intercept needs three rows at least.

Examples

A trend through eight weeks

EVALUATE
VAR Weeks =
    DATATABLE (
        "Week", INTEGER, "Liters", INTEGER,
        { { 1, 410 }, { 2, 435 }, { 3, 470 }, { 4, 462 }, { 5, 505 }, { 6, 530 }, { 7, 528 }, { 8, 571 } }
    )
RETURN
    LINESTX ( Weeks, [Liters], [Week] )
Slope1InterceptStandardErrorSlope1StandardErrorInterceptCoefficientOfDeterminationStandardErrorFStatisticDegreesOfFreedomRegressionSumOfSquaresResidualSumOfSquares
21.6071428571429391.6428571428571.804307958992689.111308571086310.95984150585174811.6932520223051143.407992686431619608.4821428571820.392857142855

Two explaining values

Liters a week against the price and the number of sunny days.

EVALUATE
VAR Shops =
    DATATABLE (
        "Liters", INTEGER, "Price", DOUBLE, "Sunny days", INTEGER,
        {
            { 520, 38, 12 }, { 480, 40, 10 }, { 610, 36, 18 }, { 450, 42, 9 },
            { 590, 37, 15 }, { 500, 41, 14 }, { 560, 38, 13 }
        }
    )
RETURN
    SELECTCOLUMNS (
        LINESTX ( Shops, [Liters], [Price], [Sunny days] ),
        "Per krone of price", [Slope1],
        "Per sunny day", [Slope2],
        "Intercept", [Intercept],
        "R squared", [CoefficientOfDetermination]
    )
Per krone of pricePer sunny dayInterceptR squared
-16.50381679389328.185387131951991064.881134133050.960942873919975

A blank x counts as 0

The third row's x is blank: the line is the same as with 0 written there. Leaving the row out with FILTER gives another line.

EVALUATE
VAR Points =
    DATATABLE (
        "x", DOUBLE, "y", DOUBLE,
        { { 1, 2.5 }, { 2, 3.8 }, { BLANK (), 3.2 }, { 4, 8.1 }, { 5, 10.4 } }
    )
VAR Zero = SELECTCOLUMNS ( Points, "x", IF ( ISBLANK ( [x] ), 0, [x] ), "y", [y] )
RETURN
    UNION (
        SELECTCOLUMNS ( LINESTX ( Points, [y], [x] ), "Rows", "Blank x", "Slope", [Slope1], "Intercept", [Intercept] ),
        SELECTCOLUMNS ( LINESTX ( Zero, [y], [x] ), "Rows", "0 written", "Slope", [Slope1], "Intercept", [Intercept] ),
        SELECTCOLUMNS (
            LINESTX ( FILTER ( Points, NOT ISBLANK ( [x] ) ), [y], [x] ),
            "Rows", "Row left out", "Slope", [Slope1], "Intercept", [Intercept]
        )
    )
ORDER BY [Rows]
RowsSlopeIntercept
0 written1.587209302325581.79069767441861
Blank x1.587209302325581.79069767441861
Row left out2.010.170000000000002

LINEST, TRENDLINE, CORREL, T.DIST.2T.

Last updated · OpenBI Studio 0.3.74