curvefit

Performs linear regression to calculate the polynomial curve that best fits the input data. Uses the least squares method to estimate a polynomial function between the independent and dependent variables.

Command properties

ItemDescription
Command typeProcessing query
Required permissionNone
License usageN/A
Parallel executionNot supported
Distributed executionNot supported

Syntax

curvefit [degree=INT] X_FIELD, Y_FIELD

Options

degree=INT
Degree of the polynomial function to fit to the input data. Default: 3

Target

X_FIELD
Field to use as the independent variable. The field value must be numeric.
Y_FIELD
Field to use as the dependent variable. The field value must be numeric.

Output fields

FieldTypeDescription
_xfloatValue of the independent variable field
_pfloatPredicted value computed by the polynomial function

Error codes

Parse errors
Error codeMessageDescription
40804Machine learning license is required.Machine learning license is not present.
missing-curvefit-fields-Both the independent and dependent variable fields are not specified.
Runtime errors

N/A

Description

The curvefit command performs least squares linear regression on up to 10,000 input records. The independent variable field value is output as the _x field, and the predicted value computed by the polynomial function is output as the _p field. Records beyond 10,000 are ignored.

Records where the independent or dependent variable value is not numeric are excluded from the analysis. Use the degree option to specify the polynomial degree. The default is a 3rd-degree polynomial.

A machine learning license is required to use this command.

Examples

  1. Fitting data with a 3rd-degree polynomial

    json "[{'x': 1, 'y': 2}, {'x': 2, 'y': 5}, {'x': 3, 'y': 10}, {'x': 4, 'y': 17}, {'x': 5, 'y': 26}]"
    | curvefit x, y
    

    Fits a default 3rd-degree polynomial using the x field as the independent variable and the y field as the dependent variable.

  2. Fitting CPU usage trends with a 10th-degree polynomial

    table duration=1h sys_cpu_logs
    | eval x = datediff(dateadd(now(), "hour", -1), _time, "sec")
    | eval total = kernel + user
    | curvefit degree=10 x, total
    

    Fits the past hour's CPU usage trend with a 10th-degree polynomial. The predicted value is output in the _p field.

  3. Regression analysis with a specified degree

    table duration=1d sensor_data
    | curvefit degree=5 seq, temperature
    

    Fits a 5th-degree polynomial using the seq field as the independent variable and the temperature field as the dependent variable.