admin管理员组文章数量:1296895
I'm trying to create a curved line showing a trend in data in a graph, but I can't figure out how to generate the necessary data points, similar to the second graph in this image:
All of the documentation and examples I find use math that goes over my head, any pseudocode would be great.
I'm trying to create a curved line showing a trend in data in a graph, but I can't figure out how to generate the necessary data points, similar to the second graph in this image:
All of the documentation and examples I find use math that goes over my head, any pseudocode would be great.
Share asked Nov 27, 2012 at 18:42 ZikesZikes 5,8861 gold badge31 silver badges44 bronze badges1 Answer
Reset to default 10I was able to plot an exponential regression line with the below code:
function square(x){return Math.pow(x,2);};
function array_sum(arr){
var total = 0;
arr.forEach(function(d){total+=d;});
return total;
}
function exp_regression(Y){
var n = Y.length;
var X = d3.range(1,n+1);
var sum_x = array_sum(X);
var sum_y = array_sum(Y);
var y_mean = array_sum(Y) / n;
var log_y = Y.map(function(d){return Math.log(d)});
var x_squared = X.map(function(d){return square(d)});
var sum_x_squared = array_sum(x_squared);
var sum_log_y = array_sum(log_y);
var x_log_y = X.map(function(d,i){return d*log_y[i]});
var sum_x_log_y = array_sum(x_log_y);
a = (sum_log_y*sum_x_squared - sum_x*sum_x_log_y) /
(n * sum_x_squared - square(sum_x));
b = (n * sum_x_log_y - sum_x*sum_log_y) /
(n * sum_x_squared - square(sum_x));
var y_fit = [];
X.forEach(function(x){
y_fit.push(Math.exp(a)*Math.exp(b*x));
});
return y_fit;
}
本文标签: javascriptHow can I use d3js to create a trendexponential regression lineStack Overflow
版权声明:本文标题:javascript - How can I use d3.js to create a trendexponential regression line? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741601201a2387710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论