Calculation of pearson-correlation not working

Proposal:
Calculation of pearson-correlation with InfluxDB

Current behavior:
Calculation isn´t working, no error message. Only “No data” despite there is data in the time range.

Desired behavior:
Calculation of the pearson-correlation with the cov() function and print it on the Grafana Dashboard as Single Stat.

Alternatives considered:
Also tried pearsonr() function or grouping the tables, but nothing worked for me.

Use case:
Show correlation between two variables on the Grafana Dashboard.

image

Hello @GrafanaForEver,
What error are you getting?
Can you verify that you’re getting data for corr1 and corr2?
Next can you verify that the _values are floats?
Or convert to float with:

        |> toFloat()

Does the following work for you:

import "generate"

stream1 =
    generate.from(
        count: 5,
        fn: (n) => n * n,
        start: 2021-01-01T00:00:00Z,
        stop: 2021-01-01T00:01:00Z,
    )
        |> toFloat()

stream2 =
    generate.from(
        count: 5,
        fn: (n) => n * n * n / 2,
        start: 2021-01-01T00:00:00Z,
        stop: 2021-01-01T00:01:00Z,
    )
        |> toFloat()

pearsonr(x: stream1, y: stream2, on: ["_time"])

Hi Anaisdg,
thanks for your help.

The code you´ve posted with the pearsonr()-example is running.

Sure, I´m getting data from corr1 and corr2. I´ve tested it in single queries.

Normally, the _values are floats. I already added the |> toFloat() command, but this changed nothing.

And the most confusing point is, that I geht no error messages.
The query takes about 0.2s - 0.5s (so there must be something calculation), but the dash tells me “No Results”:

Got the solution by my own…I had an issue with the timestamp, because of the not matching Milliseconds. Solved it by adding “|> truncateTimeColumn(unit: 1s)” to every query.

Thanks for your help.