Skip to content
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

simple fit trace / linear regression example provided #108

Open
cgfoed opened this issue Jan 13, 2021 · 3 comments
Open

simple fit trace / linear regression example provided #108

cgfoed opened this issue Jan 13, 2021 · 3 comments

Comments

@cgfoed
Copy link

cgfoed commented Jan 13, 2021

I just want to provide my simple code for plotting a linear fit into a scatter.
A feature that was requested for some time in plotly JS (plotly/plotly.js#4921), maybe you can just add this code into your example collection, so people will be able to find this solution and modified it to their needs:

function linearRegression(x,y){
        var lr = {};
        var n = y.length;
        var sum_x = 0;
        var sum_y = 0;
        var sum_xy = 0;
        var sum_xx = 0;
        var sum_yy = 0;

        for (var i = 0; i < y.length; i++) {

            sum_x += x[i];
            sum_y += y[i];
            sum_xy += (x[i]*y[i]);
            sum_xx += (x[i]*x[i]);
            sum_yy += (y[i]*y[i]);
        } 

        lr['sl'] = (n * sum_xy - sum_x * sum_y) / (n*sum_xx - sum_x * sum_x);
        lr['off'] = (sum_y - lr.sl * sum_x)/n;
        lr['r2'] = Math.pow((n*sum_xy - sum_x*sum_y)/Math.sqrt((n*sum_xx-sum_x*sum_x)*(n*sum_yy-sum_y*sum_y)),2);

        return lr;
}

var trace = {
      x: [9.87, 9.69, 9.14, 9.71, 9.19, 9.5, 9.85, 9.52, 9.34, 9.42, 9.71, 9.53, 9.13, 9.05, 9.3, 9.81, 9.32, 9.8, 9.5, 10, 9.47, 9.19, 9, 9.94, 9.4, 9.18, 9.06, 9.39, 9.59, 9.26, 9.15],
      y: [9.93, 9.85, 9.34, 9.69, 9.13, 9.4, 9.75, 9.5, 9.23, 9.45, 9.95, 9.68, 9.17, 9.2, 9.1, 10.01, 9.17, 9.99, 9.29, 10.04, 9.56, 9.2, 9.06, 9.77, 9.61, 9.09, 9.2, 9.18, 9.72, 9.1, 9.27],
      name: "Scatter",
      "marker": {"size": 5},
      "mode": "markers",
    "type": "scatter" };  
var lr = linearRegression(trace.x, trace.y);
var fit_from = Math.min(...trace.x)
var fit_to = Math.max(...trace.x)
var fit = {
  x: [fit_from, fit_to],
  y: [fit_from*lr.sl+lr.off, fit_to*lr.sl+lr.off],
  mode: 'lines',
  type: 'scatter',
  name: "R2=".concat((Math.round(lr.r2 * 10000) / 10000).toString())
};

var data = [ trace, fit ];
Plotly.newPlot('myDiv', data);

image

@cgfoed cgfoed changed the title simple fit trace example provided simple fit trace / linear regression example provided Jan 13, 2021
@jxu
Copy link

jxu commented Jun 7, 2021

shouldn't linear regression calculations be handled by an appropriate statistical library

@avithacalpine
Copy link

avithacalpine commented Nov 17, 2021

The above example can be done using layout.shapes too.

@nhoktenz
Copy link

nhoktenz commented Feb 6, 2023

Is there a way that we can have the error bar from this linear regression line? I am trying to make the error bar similar to this plot https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggscatterstats.html but in JS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants