slope()

Calculates the slope of the linear regression line for two numeric expressions.

Syntax

slope(EXPR_X, EXPR_Y)

Parameters

EXPR_X
An expression that returns the X values (independent variable).
EXPR_Y
An expression that returns the Y values (dependent variable).

Description

The slope() function calculates the slope of the linear regression line from pairs of EXPR_X and EXPR_Y values. The slope is computed as Cov(X, Y) / Var(X), which is the covariance of X and Y divided by the variance of X. If either EXPR_X or EXPR_Y is null or non-numeric for a record, that record is ignored. If there are no valid values, it returns null. The return value is a 64-bit floating-point number (double).

Use this function in aggregation commands such as stats and timechart.

Error codes

N/A

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. Calculate the linear regression slope between HTTP status code and response size

    table WEB_APACHE_SAMPLE | stats slope(status, bytes)
    
  2. Calculate the slope between status code and response size per HTTP method

    table WEB_APACHE_SAMPLE | stats slope(status, bytes) by method
    
  3. Null value handling

    json "[{'x': 1, 'y': 2}, {'x': null, 'y': 4}, {'x': 3, 'y': 6}]"
    | stats slope(x, y)
    | # Records where x is null are excluded from aggregation.
    

Compatibility

The slope() function has been available since before Logpresso Sonar 4.0.