# Regression Node - Help

**URL:** <https://discourse.nodered.org/t/regression-node-help/20597>\
**Category:** General\
**Created:** [19 January 2020 21:44 UTC](https://discourse.nodered.org/t/regression-node-help/20597 "2020-01-19T21:44:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mich](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Mich](https://discourse.nodered.org/u/Mich)\
**Post date:** [19 January 2020 21:44 UTC](https://discourse.nodered.org/t/regression-node-help/20597/1 "2020-01-19T21:44:39Z")

</div>

Here is my problem:

I'm trying to set up a flow where I receive temperature data from my connected Photon, and then based on the temperature data calculate the linear regression. I just cannot for the life of me figure out how to use the regression node.

What I've done so far: I made a function to change the incoming temperature data and timestamp from the Photon from string to float. For the regression node, I want to use the temperature as my y-value input property, and the time as the x-value. I do this and I'm just left with a blank equation. Anyone have any insight on how to fix the problem?

Here's my flow:

`[{"id":"b6bcbc03.65ec4","type":"tab","label":"Only Temperature Data","disabled":false,"info":""},{"id":"b7d9d04.488f63","type":"particle-SSE","z":"b6bcbc03.65ec4","pcloud":"","subscribetype":"devid","devprodslug":"Michelle_photon","devid":"","evtname":"temperature_reading","strict":0,"x":210,"y":220,"wires":[["87b630ae.88781","d3f0959f.66e028","e45ba3a8.747c8"]]},{"id":"87b630ae.88781","type":"debug","z":"b6bcbc03.65ec4","name":"Incoming Data","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":560,"y":120,"wires":[]},{"id":"154a32ad.fd256d","type":"switch","z":"b6bcbc03.65ec4","name":"temp thresh","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"20","vt":"str","v2":"30","v2t":"str"},{"t":"gt","v":"30","vt":"str"},{"t":"lt","v":"20","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":1230,"y":240,"wires":[[],["a9acf0f7.a3f13","8ee29a46.399f08"],[]]},{"id":"b94cbe16.703dc","type":"template","z":"b6bcbc03.65ec4","name":"safe","field":"payload","fieldType":"msg","syntax":"mustache","template":"Temperature({{payload}}) within safe limits","x":1430,"y":200,"wires":[[]]},{"id":"8ee29a46.399f08","type":"template","z":"b6bcbc03.65ec4","name":"danger","field":"payload","fieldType":"msg","syntax":"mustache","template":"Temperature ({{payload}}) critical","x":1420,"y":260,"wires":[["6313c0aa.18ea5"]]},{"id":"6313c0aa.18ea5","type":"debug","z":"b6bcbc03.65ec4","name":"Status","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1570,"y":220,"wires":[]},{"id":"1ef26fec.3e80f","type":"cloudant out","z":"b6bcbc03.65ec4","name":"Temp Data from Photon","cloudant":"","database":"sensordata","service":"SensorData-cloudantNoSQLDB","payonly":false,"operation":"insert","x":750,"y":60,"wires":[]},{"id":"f2ebfb46.1b53a8","type":"debug","z":"b6bcbc03.65ec4","name":"Display Temperature Reading","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":810,"y":260,"wires":[]},{"id":"d3f0959f.66e028","type":"function","z":"b6bcbc03.65ec4","name":"Temp","func":"msg.topic = \"temp\";\nreturn{payload:msg.payload.data};","outputs":2,"noerr":0,"x":530,"y":220,"wires":[["f2ebfb46.1b53a8","6b2e9123.300a8","a62bdab8.9b5bc8"],[]]},{"id":"6b2e9123.300a8","type":"ui_chart","z":"b6bcbc03.65ec4","name":"Temperature Data","group":"588c1a22.34d654","order":1,"width":"0","height":"0","label":"Temperature Data from Incubator","chartType":"line","legend":"true","xformat":"HH:mm","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"300","removeOlder":"1","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":770,"y":220,"wires":[[]]},{"id":"a62bdab8.9b5bc8","type":"ui_gauge","z":"b6bcbc03.65ec4","name":"Current Temperature","group":"588c1a22.34d654","order":1,"width":"0","height":"0","gtype":"gage","title":"Current Temperature","label":"Celsius","format":"{{value}}","min":"10","max":"40","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":780,"y":180,"wires":[]},{"id":"634d4130.d915c","type":"regression","z":"b6bcbc03.65ec4","name":"","dataSetSize":0,"regressionType":"linear","polynomialOrder":"1","precision":2,"xInputField":"payload.time","xInputFieldType":"payload.x","yInputField":"payload.data","yInputFieldType":"msg","yOutputField":"out","yOutputFieldType":"msg","functionOutputField":"string","functionOutputFieldType":"msg","resultOnly":false,"x":590,"y":360,"wires":[["a807d7cb.ed5978"]]},{"id":"a807d7cb.ed5978","type":"debug","z":"b6bcbc03.65ec4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"string","targetType":"msg","x":770,"y":360,"wires":[]},{"id":"a520519c.3a6df","type":"debug","z":"b6bcbc03.65ec4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":770,"y":420,"wires":[]},{"id":"e45ba3a8.747c8","type":"function","z":"b6bcbc03.65ec4","name":"SwitchForRegression","func":"var time = new Date(msg.payload.published_at);\nreturn {payload: {time: time.getTime(), data: parseFloat(msg.payload.data)}};","outputs":1,"noerr":0,"x":360,"y":420,"wires":[["634d4130.d915c","a520519c.3a6df"]]},{"id":"ad3e0878.411908","type":"comment","z":"b6bcbc03.65ec4","name":"","info":"Data to Database (should be connceted to Photon node)","x":720,"y":20,"wires":[]},{"id":"a9acf0f7.a3f13","type":"debug","z":"b6bcbc03.65ec4","name":"Status","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1310,"y":320,"wires":[]},{"id":"b41b8f27.52f52","type":"delay","z":"b6bcbc03.65ec4","name":"Database Logging","pauseType":"delay","timeout":"12","timeoutUnits":"hours","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":510,"y":60,"wires":[["1ef26fec.3e80f"]]},{"id":"588c1a22.34d654","type":"ui_group","z":"","name":"Temperature Sensor Data","tab":"72da592c.299d38","disp":true,"width":"6","collapse":false},{"id":"72da592c.299d38","type":"ui_tab","z":"","name":"Photon","icon":"dashboard","disabled":false,"hidden":false}]`

---

<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:** [19 January 2020 21:57 UTC](https://discourse.nodered.org/t/regression-node-help/20597/2 "2020-01-19T21:57:35Z")

</div>

Unfortunately your flow is not importable, please see below for how to fix that. You can edit your previous flow.

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

**Author:** ![Mich](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Mich](https://discourse.nodered.org/u/Mich)\
**Post date:** [19 January 2020 22:16 UTC](https://discourse.nodered.org/t/regression-node-help/20597/3 "2020-01-19T22:16:41Z")

</div>

thanks, Colin. I'm still new to this 😅

---

<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:** [20 January 2020 09:44 UTC](https://discourse.nodered.org/t/regression-node-help/20597/4 "2020-01-20T09:44:37Z")

</div>

Change the second debug node to Show Complete Message and show us what you see from the two debug nodes. Expand the outputs so as to see all the data.

---

<div class="post-metadata">

**Author:** ![Mich](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Mich](https://discourse.nodered.org/u/Mich)\
**Post date:** [20 January 2020 10:24 UTC](https://discourse.nodered.org/t/regression-node-help/20597/5 "2020-01-20T10:24:12Z")

</div>

The debug attached to the "Switch for regression" node is outputting the data in the correct format (I know the temperature is really high but that's no worry).

 ![Screen Shot 2020-01-20 at 11.19.26 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/33e90731cf704a1eaf03671422cbef36e83ff8a2.png)

---

<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:** [20 January 2020 10:43 UTC](https://discourse.nodered.org/t/regression-node-help/20597/6 "2020-01-20T10:43:09Z")

</div>

You said it was giving you a blank equation, it doesn't seem to be blank. What happens if you give it another couple of values, obviously it can't calculate it on just one.

---

<div class="post-metadata">

**Author:** ![Mich](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Mich](https://discourse.nodered.org/u/Mich)\
**Post date:** [20 January 2020 12:09 UTC](https://discourse.nodered.org/t/regression-node-help/20597/7 "2020-01-20T12:09:28Z")

</div>

I left the photon running for a couple hours, the regression equation still reads "y = 0x + NaN"

---

<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:** [20 January 2020 12:15 UTC](https://discourse.nodered.org/t/regression-node-help/20597/8 "2020-01-20T12:15:51Z")

</div>

Make a simple flow that uses an inject and function node to send three sample data messages to the regression node and post that here so we can try it.

---

<div class="post-metadata">

**Author:** ![Mich](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Mich](https://discourse.nodered.org/u/Mich)\
**Post date:** [20 January 2020 13:23 UTC](https://discourse.nodered.org/t/regression-node-help/20597/9 "2020-01-20T13:23:44Z")

</div>

I tried injecting a simulated temperature and a timestamp into the regression node. I also tried using a "join" node. I figured that using the msg.topic would work, but no luck.

`[{"id":"52fa474f.4d1438","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"eeb6d5ba.4ff948","type":"inject","z":"52fa474f.4d1438","name":"Simulated data","topic":"YValue","payload":"25","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":640,"wires":[["73fbb737.3d52e8","eb3b2a8a.82ad18"]]},{"id":"40bb5c04.649d64","type":"inject","z":"52fa474f.4d1438","name":"","topic":"XValue","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":190,"y":700,"wires":[["fe128a1d.1bee38","eb3b2a8a.82ad18"]]},{"id":"eb3b2a8a.82ad18","type":"regression","z":"52fa474f.4d1438","name":"","dataSetSize":0,"regressionType":"linear","polynomialOrder":"1","precision":2,"xInputField":"topic.XValue","xInputFieldType":"msg","yInputField":"topic.YValue","yInputFieldType":"msg","yOutputField":"topic.YValue","yOutputFieldType":"msg","functionOutputField":"string","functionOutputFieldType":"msg","resultOnly":false,"x":570,"y":680,"wires":[["b08bb56f.00e0c8"]]},{"id":"b08bb56f.00e0c8","type":"debug","z":"52fa474f.4d1438","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"string","targetType":"msg","x":770,"y":680,"wires":[]},{"id":"fe128a1d.1bee38","type":"debug","z":"52fa474f.4d1438","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":310,"y":800,"wires":[]},{"id":"73fbb737.3d52e8","type":"debug","z":"52fa474f.4d1438","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":310,"y":560,"wires":[]},{"id":"13b84713.fc0b19","type":"inject","z":"52fa474f.4d1438","name":"Simulated data","topic":"YValue","payload":"25","payloadType":"num","repeat":"15","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":980,"wires":[["5423a343.25aefc","8d553494.88eac8"]]},{"id":"cce0d7ce.537c78","type":"inject","z":"52fa474f.4d1438","name":"","topic":"XValue","payload":"","payloadType":"date","repeat":"15","crontab":"","once":true,"onceDelay":0.1,"x":170,"y":1040,"wires":[["937dd46e.2e32c8","8d553494.88eac8"]]},{"id":"c3ffba39.8a8b38","type":"regression","z":"52fa474f.4d1438","name":"","dataSetSize":0,"regressionType":"linear","polynomialOrder":"1","precision":2,"xInputField":"topic.XValue","xInputFieldType":"msg","yInputField":"topic.YValue","yInputFieldType":"msg","yOutputField":"topic.YValue","yOutputFieldType":"msg","functionOutputField":"string","functionOutputFieldType":"msg","resultOnly":false,"x":590,"y":1020,"wires":[["6662e4d3.94ba4c"]]},{"id":"6662e4d3.94ba4c","type":"debug","z":"52fa474f.4d1438","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"string","targetType":"msg","x":790,"y":1020,"wires":[]},{"id":"937dd46e.2e32c8","type":"debug","z":"52fa474f.4d1438","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":290,"y":1140,"wires":[]},{"id":"5423a343.25aefc","type":"debug","z":"52fa474f.4d1438","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":290,"y":900,"wires":[]},{"id":"8d553494.88eac8","type":"join","z":"52fa474f.4d1438","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":430,"y":1020,"wires":[["5431c71b.3a46d8","c3ffba39.8a8b38"]]},{"id":"5431c71b.3a46d8","type":"debug","z":"52fa474f.4d1438","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":560,"y":940,"wires":[]}]`

---

<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:** [20 January 2020 17:29 UTC](https://discourse.nodered.org/t/regression-node-help/20597/10 "2020-01-20T17:29:13Z")

</div>

I meant something like this, which works fine

```auto
[{"id":"b1ae979e.46e4d","type":"inject","z":"6f3191a3.0ba77","name":"Inject Sample 1","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":119,"y":76,"wires":[["9fce9d45.d6cc18"]]},{"id":"9fce9d45.d6cc18","type":"function","z":"6f3191a3.0ba77","name":"sample 1","func":"msg.payload = {time: 1, data: 7}\nreturn msg","outputs":1,"noerr":0,"x":316,"y":76,"wires":[["55768fed.1f3638"]]},{"id":"55768fed.1f3638","type":"regression","z":"6f3191a3.0ba77","name":"","dataSetSize":0,"regressionType":"linear","polynomialOrder":"1","precision":2,"xInputField":"payload.time","xInputFieldType":"msg","yInputField":"payload.data","yInputFieldType":"msg","yOutputField":"topic","yOutputFieldType":"msg","functionOutputField":"string","functionOutputFieldType":"msg","resultOnly":false,"x":493,"y":77,"wires":[["86b78717.1e33f8"]]},{"id":"86b78717.1e33f8","type":"debug","z":"6f3191a3.0ba77","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":574,"y":164,"wires":[]},{"id":"3b186be8.64346c","type":"function","z":"6f3191a3.0ba77","name":"sample 2","func":"msg.payload = {time: 2, data: 10}\nreturn msg","outputs":1,"noerr":0,"x":306,"y":156,"wires":[["55768fed.1f3638"]]},{"id":"8d88124.30593f","type":"inject","z":"6f3191a3.0ba77","name":"Inject Sample 2","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":122,"y":150,"wires":[["3b186be8.64346c"]]},{"id":"b79893b7.7345f","type":"inject","z":"6f3191a3.0ba77","name":"Clear data","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":295,"y":247,"wires":[["55768fed.1f3638"]]}]

```

Are you remembering to do a full deploy every time? In the mode you are using it the regression node uses all the data it has previously received so if you once get bad data data into it causing a Not a Number error then that won't go away until you deploy the Recursion node or do a full deploy, unless you clear the buffer by sending a message with the x property missing, as my Clear inject node does above.

---

<div class="post-metadata">

**Author:** ![Mich](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Mich](https://discourse.nodered.org/u/Mich)\
**Post date:** [21 January 2020 17:57 UTC](https://discourse.nodered.org/t/regression-node-help/20597/11 "2020-01-21T17:57:45Z")

</div>

Thanks for the example. I think there's a problem with my code in my "switch for regression" function node. When I disconnect it from my regression node and connect inject nodes instead (as you did in your example), the regression works fine. I'll play around with the code and see if I can get it to work

I'm thinking it might be because my time value is too large

---

<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:** [21 January 2020 19:05 UTC](https://discourse.nodered.org/t/regression-node-help/20597/12 "2020-01-21T19:05:15Z")

</div>

> [@Mich](#):
>
> I'm thinking it might be because my time value is too large

Well that shouldn't be a problem, it is not large in the JavaScript number world.

---

<div class="post-metadata">

**Author:** ![Mich](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Mich](https://discourse.nodered.org/u/Mich)\
**Post date:** [22 January 2020 13:53 UTC](https://discourse.nodered.org/t/regression-node-help/20597/13 "2020-01-22T13:53:14Z")

</div>

I was using the getTime() method to return the time in milliseconds, which I guess was too large. I reduced it to a smaller number and now the regression function seems to be working
