Excel中的LINEST是什么?

是否有任何inbuitfunction,或者我们需要写我们自己的。 在以后的情况下,请给我一些链接,它已经实施。 它是如何工作的?

谢谢

C#中没有内置函数来使用最小二乘法计算最佳拟合线。 由于Excel用于数据操作/统计,C#是一种通用编程语言,所以我不希望这样做。

尽pipe有许多人已经将实现发布到各个站点。 我build议检查一下,学习algorithm背后的algorithm。

以下是一个实现的链接:

C#中的mathalgorithm:线性最小二乘拟合

联机帮助中有相当丰富的文档。 不,这在C#中默认是不可用的。 C#/ .NET和Excel都有不同的用途,因此具有不同的function集。

我find了答案,并张贴在这个网站

试图用这个问题和其他相似/相同的问题解决这个问题,我无法得到一个很好的例子来说明如何做到这一点。 但是,汇集了许多post(以及Office帮助对LINEST实际所做的描述),我想我会发布我的解决scheme代码。

/// <summary> /// Finds the Gradient using the Least Squares Method /// </summary> /// <returns>The y intercept of a trendline of best fit through the data X and Y</returns> public decimal LeastSquaresGradient() { //The DataSetsMatch method ensures that X and Y //(both List<decimal> in this situation) have the same number of elements if (!DataSetsMatch()) { throw new ArgumentException("X and Y must contain the same number of elements"); } //These variables are used store the variances of each point from its associated mean List<decimal> varX = new List<decimal>(); List<decimal> varY = new List<decimal>(); foreach (decimal x in X) { varX.Add(x - AverageX()); } foreach (decimal y in Y) { varY.Add(y - AverageY()); } decimal topLine = 0; decimal bottomLine = 0; for (int i = 0; i < X.Count; i++) { topLine += (varX[i] * varY[i]); bottomLine += (varX[i] * varX[i]); } if (bottomLine != 0) { return topLine / bottomLine; } else { return 0; } } /// <summary> /// Finds the Y Intercept using the Least Squares Method /// </summary> /// <returns>The y intercept of a trendline of best fit through the data X and Y</returns> public decimal LeastSquaresYIntercept() { return AverageY() - (LeastSquaresGradient() * AverageX()); } /// <summary> /// Averages the Y. /// </summary> /// <returns>The average of the List Y</returns> public decimal AverageX() { decimal temp = 0; foreach (decimal t in X) { temp += t; } if (X.Count == 0) { return 0; } return temp / X.Count; } /// <summary> /// Averages the Y. /// </summary> /// <returns>The average of the List Y</returns> public decimal AverageY() { decimal temp = 0; foreach (decimal t in Y) { temp += t; } if (Y.Count == 0) { return 0; } return temp / Y.Count; }