simple-statistics: [Help] Linear Regression examples

I know there’s an example on Linear Regression but that is done by including D3 libraries to draw the plots etc. My requirement is to create simple plots/lines using linear regression on a mapbox project using the leafletjs API. I don’t think there’s a need to include the D3 libraries et. all for this purpose. There must be an easier way out. All I need is to display a line on the map, which can be achieved by using Polylines. I’m just not sure how the values returned by the linearRegression() or linearRegressionLine() functions can be used for this purpose.

Any help would be much appreciated.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 22

Commits related to this issue

Most upvoted comments

Cool. Neat project 😃

Does that seem to work as expected?

OK, here is my working code:

var ss = require("simple-statistics");

var coordinates = [
    [61.508122,23.746948], // Tampere, Finland
    [60.984933,24.472046], // Hämeenlinna, Finland
    [60.191889,24.927979] // Helsinki, Finland
];

// Create a regression object in slope intercept form
var regression = ss.linearRegression(coordinates);

// Create a linear regression function, 
// which provides a y coordinate for any given x coordinate
var regressionFunction = ss.linearRegressionLine(regression);

// Set up starting and ending X coordinates
var regressionStartX = coordinates[0][0];
var lastCoordinateIndex = coordinates.length - 1;
var regressionEndX = coordinates[lastCoordinateIndex][0];

// Calculate starting and ending Y coordinates
var regressionStartY = regressionFunction(regressionStartX);
var regressionEndY = regressionFunction(regressionEndX);

var regressionLine = [
    [regressionStartX, regressionStartY],
    [regressionEndX, regressionEndY]
];


regressionLine;

You can try it out by pasting it in the NPM simple-statistics package sandbox. Then, click Run and select Map coordinates list in the dropdown box next to the Array output.