# How is correlation calculated?

**URL:** <https://discourse.nodered.org/t/how-is-correlation-calculated/52352>\
**Category:** General\
**Created:** [18 October 2021 07:47 UTC](https://discourse.nodered.org/t/how-is-correlation-calculated/52352 "2021-10-18T07:47:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)\
**Post date:** [18 October 2021 07:47 UTC](https://discourse.nodered.org/t/how-is-correlation-calculated/52352/1 "2021-10-18T07:47:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [18 October 2021 07:54 UTC](https://discourse.nodered.org/t/how-is-correlation-calculated/52352/2 "2021-10-18T07:54:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)\
**Post date:** [18 October 2021 08:31 UTC](https://discourse.nodered.org/t/how-is-correlation-calculated/52352/3 "2021-10-18T08:31:27Z")

</div>

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

> **[node-red-contrib-gpu](https://flows.nodered.org/node/node-red-contrib-gpu)**
>
> Node-RED GPU functions

> **[node-red-contrib-prib-functions](https://flows.nodered.org/node/node-red-contrib-prib-functions)**
>
> Node-RED added node functions.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 October 2021 08:35 UTC](https://discourse.nodered.org/t/how-is-correlation-calculated/52352/4 "2021-10-18T08:35:43Z")

</div>

What formula are you using to calculate the correlation?

---

<div class="post-metadata">

**Author:** ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)\
**Post date:** [18 October 2021 08:38 UTC](https://discourse.nodered.org/t/how-is-correlation-calculated/52352/5 "2021-10-18T08:38:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 October 2021 08:51 UTC](https://discourse.nodered.org/t/how-is-correlation-calculated/52352/6 "2021-10-18T08:51:08Z")

</div>

I cannot find an existing node-red node that will do that for you. However, I think the npm module [calculate-correlation](https://www.npmjs.com/package/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

---

<div class="post-metadata">

**Author:** ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)\
**Post date:** [18 October 2021 10:02 UTC](https://discourse.nodered.org/t/how-is-correlation-calculated/52352/7 "2021-10-18T10:02:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 October 2021 10:03 UTC](https://discourse.nodered.org/t/how-is-correlation-calculated/52352/8 "2021-10-18T10:03:01Z")

</div>

You can do Pearson correlation in a simple function node.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/90580a96cd2d27b0be0b9f90d7d2b2d4086b90f5.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f7a85f35e4623282c1c30572089d3110cd985532.png)

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)\
**Post date:** [18 October 2021 10:08 UTC](https://discourse.nodered.org/t/how-is-correlation-calculated/52352/9 "2021-10-18T10:08:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 November 2021 10:08 UTC](https://discourse.nodered.org/t/how-is-correlation-calculated/52352/10 "2021-11-01T10:08:31Z")

</div>

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