Trend line slope calculation / linear regression

I’m trying to simply fit a line over the last period of observations, then calculate the slope of that line (percentage change over time). Is this not possible with Flux?

So far, I’ve only found this old thread with a messy solution I can’t even being to understand :slight_smile:

Not trying to predict anything with Holt-Winters, and this is financial data without seasonality.


Hello @mnguyen,
You might find the following contrib package useful.

import "contrib/anaisdg/statsmodels"
original = from(bucket: "TEST bucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "cpu")
  |> filter(fn: (r) => r["_field"] == "usage_system")
  |> filter(fn: (r) => r["cpu"] == "cpu-total")
original |> yield(name: "original")

|> statsmodels.linearRegression()
|> map(fn: (r) => ({ r with _value: r.y_hat }))
|> yield(name: "linreg")
1 Like

Thanks, that seems like the right package.

Pardon the dumb question (hopefully useful feedback will come out of it), but how do I use it? I get

500 Internal Server Error : {“error”:“type error 2:3-2:39: unknown import path: “contrib/anaisdg/statsmodels””}

I’m experienced with InfluxQL, but new to Flux. Googling “how to use Flux package” leads nowhere. The contributed functions docs don’t mention how to use those not listed there (such as this statsmodels package). I’m not even clear if these packages are client-side or need to be installed/compiled server-side. (Update: they’re client-side, but still no idea how to import them from JS code other than copy/pasting the function code from stdlib/contrib/anaisdg/statsmodels/linearreg.flux into my query.)

I’m running influxd v1.8.6 and was able to run the forward compatibility API Flux querying example via the flux-enabled = true setting in influxdb.conf, but it seems like I’m stuck here.

Might be this module needs latest influxdb2.

Try creating a separate environment with v2 and see if the backwards compatibility apis for influxql where needed until you’ve migrated things over. I haven’t seen an end date mentioned yet, but it’s reasonable to assume that 1.x won’t be maintained for much longer , except maybe for enterprise customers

1 Like

Is there any further documentation on this function? When trying to use this function on multiple tables at once, it uses the values of all of the tables combined rather than evaluating each table individually.