How is correlation calculated?

Hello, how can I calculate the correlation. I tried several nodes but failed.

Hi @sefaguntepe

Correlation of what?

Please try to describe what you'd like to do in enough detail so we can help. If you have tried some nodes, it would be helpful to mention which ones you have tried and in what way they failed.

Hello, I have 2 series of numbers. I need the correlation result of these two series. When I calculate the sample numbers below in excel, the correlation is 0.9.

sample:
14.2-554
27.5-635
40.2-699
65.4-1262
74.4-2062
78.6-2155

What formula are you using to calculate the correlation?

https://support.microsoft.com/en-us/office/correl-function-995dcef7-0c0a-4bed-a3fb-239d7b68ca92

I cannot find an existing node-red node that will do that for you. However, I think the npm module calculate-correlation should do it (though I have not checked the spec in detail). If it does do what you want then you can call it quite easily in a function node.

See the node red docs page Writing Functions for how to do that, in particular the section Loading Additional Modules and subsection Using the functionExternalModules option

Can you provide information on how to use it? or an example. I'm a bit of a novice at this.

You can do Pearson correlation in a simple function node.

image

image

[{"id":"95281ed3cac1ab80","type":"function","z":"eb3bd3ff.33367","name":"pearsonCorrelation","func":"\n\n\nmsg.payload = pearsonCorrelation(msg.a1, msg.a2);\n\nreturn msg;\n\n\nfunction pearsonCorrelation(x, y) {\n    let n = x.length\n    const promedio = l => l.reduce((s, a) => s + a, 0) / l.length\n    const calc = (v, prom) => Math.sqrt(v.reduce((s, a) => (s + a * a), 0) - n * prom * prom)\n    let nn = 0\n    for (let i = 0; i < n; i++, nn++) {\n        if ((!x[i] && x[i] !== 0) || (!y[i] && y[i] !== 0)) {\n            nn--\n            continue\n        }\n        x[nn] = x[i]\n        y[nn] = y[i]\n    }\n    if (n !== nn) {\n        x = x.splice(0, nn)\n        y = y.splice(0, nn)\n        n = nn\n    }\n    const prom_x = promedio(x), prom_y = promedio(y)\n    return (x\n        .map((e, i) => ({ x: e, y: y[i] }))\n        .reduce((v, a) => v + a.x * a.y, 0) - n * prom_x * prom_y\n    ) / (calc(x, prom_x) * calc(y, prom_y))\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1330,"y":180,"wires":[["e727ff500b4071ec"]]},{"id":"669a260113301fa7","type":"inject","z":"eb3bd3ff.33367","name":"inject msg.a1 and msg.a2","props":[{"p":"a1","v":"[80.026413,80.330908,76.68564,72.095302,75.473899,82.647118]","vt":"json"},{"p":"a2","v":"[81.662683,85.802179,78.148427,81.126326,77.853491,85.974228]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1290,"y":120,"wires":[["95281ed3cac1ab80"]]},{"id":"e727ff500b4071ec","type":"debug","z":"eb3bd3ff.33367","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"msg","x":1370,"y":240,"wires":[]}]
1 Like

It was very important to my project, thank you Steve-Mcl.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.