使用Java Excel从Excel预测公式 [英] Forecast formula from Excel in Javascript
问题描述
我正在尝试基于Excel中的代码在Javascript中创建Forecast函数,有关解释见
I'm trying to make a Forecast function in Javascript based on the code from Excel, explained at https://support.office.com/en-US/article/FORECAST-function-50CA49C9-7B40-4892-94E4-7AD38BBEDA99
但是我不知道该公式的顶部是x还是y的x是什么,所以我不知道如何用Javascript进行翻译.
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.
有人可以帮我吗?
谢谢.
推荐答案
在顶部带有特征的x是x的平均值(即所有xs的平均值). y也一样.如果x的值为20、28、31、38、40,则特征在顶部的x为31.4
x 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
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);
}
上面的脚本为您提供了预测,而没有错误处理.
The above 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]);
这篇关于使用Java Excel从Excel预测公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!