admin管理员组文章数量:1389768
I'm trying to make a Forecast function in JavaScript based on the code from Excel, explained at
But I don't understand what is the x with a trait on top (also y) from the formula and so I don't know how to translate it in JavaScript. How can I achieve this?
I'm trying to make a Forecast function in JavaScript based on the code from Excel, explained at https://support.office./en-US/article/FORECAST-function-50CA49C9-7B40-4892-94E4-7AD38BBEDA99
But I don't understand what is the x with a trait on top (also y) from the formula and so I don't know how to translate it in JavaScript. How can I achieve this?
Share Improve this question edited Jul 28, 2024 at 22:42 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Sep 7, 2016 at 15:00 Cyril N.Cyril N. 39.9k40 gold badges162 silver badges268 bronze badges1 Answer
Reset to default 9x with a trait on top is the mean of x (i.e. the average of all xs). the same is with y. if x values are 20,28,31,38,40 the x with the trait on top is 31.4
The below script gives you the forecast without error handling.
You can call this using the below method:
forecast(30,[6,7,9,15,21],[20,28,31,38,40])
function forecast(x, ky, kx){
var i=0, nr=0, dr=0,ax=0,ay=0,a=0,b=0;
function average(ar) {
var r=0;
for (i=0;i<ar.length;i++){
r = r+ar[i];
}
return r/ar.length;
}
ax=average(kx);
ay=average(ky);
for (i=0;i<kx.length;i++){
nr = nr + ((kx[i]-ax) * (ky[i]-ay));
dr = dr + ((kx[i]-ax)*(kx[i]-ax))
}
b=nr/dr;
a=ay-b*ax;
return (a+b*x);
}
console.log(forecast(30,[6,7,9,15,21],[20,28,31,38,40]));
本文标签: Forecast formula from Excel in JavaScriptStack Overflow
版权声明:本文标题:Forecast formula from Excel in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744655081a2617916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论