# Linear Regression

Linear regression predicts the value of a dependent variable from the value of an independent variable. It models the relationship between the variables as a linear equation, and fits a line that minimizes the differences between the predicted and actual values.

## Parameters

**Sampling interval**: Sampling intervals are 1m, 2m, 3m, 5m, 10m, 15m, 30m, 1h, and 2h.

**Numeric parameter**: Has the possible values of 1, 2, or 3.

## How it works

The function fits a least-squares line through each series' samples in the range window, then extends that line the requested number of seconds past the evaluation time. The extrapolated point is the predicted value — which makes it the classic early-warning alert: fire when a value _will_ cross a threshold, not when it already has.

## In Dashboards

To use `linear regression` in a dashboard, apply the following function:

```none
predict_linear( \
  ${promql} \ (1)
  ${prediction_in_seconds} \(2)
)
```

|     |     |
| --- | --- |
| **1** | `${promql}`: PromQL query to evaluate |
| **2** | `${prediction_in_seconds}`: Predicts the value of a time series the specified number of seconds in the future. |

For the operator reference — syntax, parameters, and a validated example — see [predict_linear](https://docs.kloudfuse.com/platform/latest/query-languages/promql/trend/#predict-linear) in the PromQL documentation.

## Limitations

- Use only with **Gauge** metric types.
- Makes assumptions regarding the linearity of the data. Exhibits problems with outliers as it attempts to "overfit" the data, making the detection inconsistent.
- Do not use with anomaly detection functions that manipulate the underlying data, as it makes anomaly detection unreliable.
- Each series needs at least two samples in the range window to fit a line; sparse series drop out of the result.

## Next steps

For an in-depth discussion of linear-regression, see these external resources:

- [predict_linear()](https://prometheus.io/docs/prometheus/latest/querying/functions/#predict_linear) from the Prometheus documentation
- [Linear Regression](https://en.wikipedia.org/wiki/Linear_regression) in Wikipedia
