D3.js线性回归 [英] D3.js linear regression
问题描述
我搜索了一些关于建立线性回归的帮助,并找到了一些例子:
非线性回归函数
和一些js库应该覆盖这个,但不幸的是我不能以使它们正常工作:
simple-statistics。 js ,而这一个: regression.js
用 regression.js
我可以得到 m
和 b
值,所以我可以使用 y = m * x + b
绘制我的图的线性回归之后的线,但不能应用这些值到行生成器,我尝试的代码如下:
d3.csv(typeStatsTom.csv,function错误,数据集){
//这里我绘制其他东西,设置x& y缩放正确等
//然后绘制线:
var data = [x.domain(),y.domain()];
var result = regression('linear',data);
console.log(result)
console.log(result.equation [0]);
var linereg = d3.svg.line()
.x(function(d){return x(d.Ascendenti);})
.y ((result.equation [0] * d.Ascendenti)+ result.equation [1]);});
var reglinepath = svg.append(path)
.attr(class,line)
.attr(d,linereg(dataset))
.attr(fill,none)
.attr(stroke,#386cb0)
.attr(stroke-width,1 +px);
结果的值在控制台中如下:
Object
pre>
Equation:Array [2]
0:1.8909425770308126
1:0.042557422969139225
length:2
__proto__:Array [0]
points:Array [2]
string:y = 1.89x + 0.04
__proto__:Object
从我可以在控制台中我应该设置
x
和y
值,但是当然生成的svg中的路径不显示(但是绘制),所以我不知道该怎么办。
任何帮助真的很感激,即使涉及simple.statistics.js
库的解决方案也会有所帮助!
感谢!解决方案我使用下面的代码找到了 此处 :
function linearRegression(y,x){
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
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 ['slope'] =(n * sum_xy - sum_x * sum_y)/(n * sum_xx - sum_x * sum_x);
lr ['intercept'] =(sum_y - lr.slope * 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 yval = dataset.map(function(d){return parseFloat(d.xHeight);});
var xval = dataset.map(function(d){return parseFloat(d.Ascendenti);});
var lr = linearRegression(yval,xval);
//现在你有:
// lr.slope
// lr.intercept
// lr.r2
console.log(lr);
然后绘制一条线:
var max = d3.max(dataset,function(d){return d.OvershootingSuperiore;});
var myLine = svg.append(svg:line)
.attr(x1,x(0))
.attr(y1,y(lr.intercept) )
.attr(x2,x(max))
.attr(y2,y((max * lr.slope)+ lr.intercept))
.style stroke,black);
使用我找到的代码 此处
I searched for some help on building linear regression and found some examples here:
nonlinear regression function
and also some js libraries that should cover this, but unfortunately I wasn't able to make them work properly:
simple-statistics.js and this one: regression.js
Withregression.js
I was able to get them
andb
values for the line, so I could usey = m*x + b
to plot the line that followed the linear regression of my graph, but couldn't apply those values to the line generator, the code I tried is the following:d3.csv("typeStatsTom.csv", function (error, dataset) { //Here I plot other stuff, setup the x & y scale correctly etc. //Then to plot the line: var data = [x.domain(), y.domain()]; var result = regression('linear', data); console.log(result) console.log(result.equation[0]); var linereg = d3.svg.line() .x(function (d) { return x(d.Ascendenti); }) .y(function (d) { return y((result.equation[0] * d.Ascendenti) + result.equation[1]); }); var reglinepath = svg.append("path") .attr("class", "line") .attr("d", linereg(dataset)) .attr("fill", "none") .attr("stroke", "#386cb0") .attr("stroke-width", 1 + "px");
The values of result are the following in the console:
Object equation: Array[2] 0: 1.8909425770308126 1: 0.042557422969139225 length: 2 __proto__: Array[0] points: Array[2] string: "y = 1.89x + 0.04" __proto__: Object
From what I can tell in the console I should have set up the
x
andy
values correctly, but of course the path in the resulting svg is not shown (but drawn), so I don't know what to do anymore.
Any help is really really appreciated, even a solution involving thesimple.statistics.js
library would be helpful!
Thanks!解决方案I made it work using the following code found here:
function linearRegression(y,x){ 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['slope'] = (n * sum_xy - sum_x * sum_y) / (n*sum_xx - sum_x * sum_x); lr['intercept'] = (sum_y - lr.slope * 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 yval = dataset.map(function (d) { return parseFloat(d.xHeight); }); var xval = dataset.map(function (d) { return parseFloat(d.Ascendenti); }); var lr = linearRegression(yval,xval); // now you have: // lr.slope // lr.intercept // lr.r2 console.log(lr);
And then plotting a line with:
var max = d3.max(dataset, function (d) { return d.OvershootingSuperiore; }); var myLine = svg.append("svg:line") .attr("x1", x(0)) .attr("y1", y(lr.intercept)) .attr("x2", x(max)) .attr("y2", y( (max * lr.slope) + lr.intercept )) .style("stroke", "black");
Using the code I found here
这篇关于D3.js线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!