New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Help] Linear Regression examples #149
Comments
What format is the data, lat/long? Do you have a code example? |
Yes, the data format is lat/long. Is it required to convert data to cartesian form before applying the Here's the code and this is how my plot on the map looks right now. The following files are relevant, |
One thing might be to check if your coordinates cross the prime meridian. |
So, passing lat/long data as input to the functions are not an issue, right ? |
And how does checking if coordinates cross the prime meridian help ? I don't know of the logic behind that. |
Ah, I was thinking of the possibility of coordinates crossing from 180 to -180. Sorry. |
Would you please provide an example array of geographic coordinates? Lets isolate this issue a bit. |
OK, I did some experimentation. It seems like the regression line code needs several steps. Here is my work-in-progress: 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 regressionLine = ss.linearRegressionLine(regression);
// Create an y coordinate based on the first coordinate in array
var regressionStart = regressionLine(coordinates[0][0]);
// Create a y coordinate for last x coordinate in array
var regressionEnd = regressionLine(coordinates[2][0]); |
Oh cool, did you get a chance to test how the line looks on a map ? These are points that aren't too far apart on the Arabian sea. I clicked at a series on points manually on the map and extracted their lat,long values. |
I am still workin g on the process. I think I am on the right path, or line :-) |
@brylie oh that's great. I just happened to edit my previous comment FYI. |
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 |
Does that seem to work as expected? |
Wow. That does look legit. I'm just gonna check on my map for sanity. I now realize that you only had to pass the start and end points of the regression line. I was iterating over all the points in the coordinate list and that obviously wasn't helping the cause :-) |
@brylie it works perfectly ! Thank you so much for your help :-) 👍 💯 |
You're welcome :-) One more thing though, what happens if a shipping route goes from, say, Tokyo to Los Angeles? I am concerned how the linear regression might perform when crossing the international date line, or 180 to -180. |
I honestly haven't thought about that because I only had the southern coast of India in mind. Let's see how your algo works in that case. Posting the screenshot soon. |
OK, cool. |
@brylie hmm, your concerns seem to have come true. Also worth noting that I'm drawing these lines for every 5th marker that's created on the map. |
Well there might be a geospatial processing/analysis library that is better equipped to handle the international date line issue. E.g. Turf.js seems promising. |
Thanks! There are still a few more things on the TODO (eg: these lat/long inputs should come from a remote device - android phone for eg) and I'm working on that as well. I'll take a look at Turf.js as soon as other things are sorted out :) |
Cool. Neat project :-) |
Fixes the FIXME in the previous commit Thanks to @brylie : simple-statistics/simple-statistics#149
Fixes the FIXME in the previous commit Thanks to @brylie : simple-statistics/simple-statistics#149
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()
orlinearRegressionLine()
functions can be used for this purpose.Any help would be much appreciated.
The text was updated successfully, but these errors were encountered: