Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing a regression line in Highcharts using mx + b

I'm trying to draw a linear regression line in Highcharts based off a mx+b (slope + y-intercept) formula, using regression-js to generate the regression formula.

The problem that I'm encountering is that Highcharts seems to want regression lines in [[x1, y1], [x2, y2]] format, but I can't necessarily get a beginning and end from a slope and y-intercept.

So: Is it possible to draw a line in Highcharts based off a slope? Or is there a JS regression library that will output a [[x1, y1], [x2, y2]]-formatted line based on a data array like I've got below?

What I'm working with right now:

data = [[11.6,14.7],[12.2,15.9],[10.7,14.8],[14,11.7],[12.5,13.2],[10,11.3],[10.1,11],[13.5,19.1]];

slope = regression('linear', data); // result: slope.equation = [slope, y-intercept]

$('#scatter').highcharts({
    chart: {
        type: 'scatter',
        zoomType: 'xy'
    },
    plotOptions: {
        scatter: {
            marker: {
                radius: 5,
                states: {
                    hover: {
                        enabled: true,
                        lineColor: 'rgb(100,100,100)'
                    }
                }
            },
            states: {
                hover: {
                    marker: {
                        enabled: false
                    }
                }
            },
            tooltip: {
                headerFormat: '<b>{series.name}</b><br>',
                pointFormat: '{point.x} cm, {point.y} kg'
            }
        }
    },
    series: [
        {
            type: 'line',
            name: 'Regression Line',
            data: [[0, 0], [5, 4.51]], // You see the problem here: I've got mx + b and this wants x1 y1 x2 y2
            marker: {
                enabled: false
            },
            states: {
                hover: {
                    lineWidth: 0
                }
            },
            enableMouseTracking: false
        },
        {
        name: 'Water Temperature vs. Air Temperature',
        color: 'rgba(119, 152, 191, .5)',
        data: data
    }]
});
like image 242
caitlin Avatar asked Oct 25 '25 15:10

caitlin


1 Answers

Neat question. Here's how I did it:

var data = [[11.6,14.7],[12.2,15.9],[10.7,14.8],[14,11.7],[12.5,13.2],[10,11.3],[10.1,11],[13.5,19.1]];

var ymxb = regression('linear', data); 

// get the slope and x intercept from the equation
var m = ymxb.equation[0], b = ymxb.equation[1];

// create array of x values
var xs = [];
data.forEach(function(d){
    xs.push(d[0]);
});

// get the max and min values of x, and calculate 
// the corresponding y value using that x, m, and b
var x0 = Math.min.apply(null, xs), 
    y0 = m*x0 + b;
var xf = Math.max.apply(null, xs), 
    yf = m*xf + b;

...

// that gives you your two coordinates
series: [
    {
        type: 'line',
        name: 'Regression Line',
        data: [[x0, y0], [xf, yf]],

...

Result:

enter image description here

JSFiddle

like image 119
Charles Clayton Avatar answered Oct 27 '25 07:10

Charles Clayton