C#与Excel中的LINEST等价的是什么?

C#与Excel中的LINEST等价的是什么?,c#,excel,C#,Excel,是否有任何内置函数,或者我们需要编写自己的函数。 在以后的情况下,你可以给我一些链接,它已经实施。 它是如何工作的 谢谢中有大量文档。不,默认情况下,这在C#中不可用。C#/.NET和Excel都有不同的用途,因此功能集不同。C#中没有使用最小二乘法计算最佳拟合线的内置功能。我也不认为会有这样的版本,因为Excel用于数据处理/统计,而C#是一种通用编程语言 尽管如此,还是有很多人在不同的网站上发布了实现。我建议检查他们,学习他们计算背后的算法 以下是一个实现的链接: 我试图用这个问题和其他类似

是否有任何内置函数,或者我们需要编写自己的函数。 在以后的情况下,你可以给我一些链接,它已经实施。 它是如何工作的


谢谢

中有大量文档。不,默认情况下,这在C#中不可用。C#/.NET和Excel都有不同的用途,因此功能集不同。

C#中没有使用最小二乘法计算最佳拟合线的内置功能。我也不认为会有这样的版本,因为Excel用于数据处理/统计,而C#是一种通用编程语言

尽管如此,还是有很多人在不同的网站上发布了实现。我建议检查他们,学习他们计算背后的算法

以下是一个实现的链接:


我试图用这个问题和其他类似/相同的问题来解决这个问题,但我找不到一个很好的例子来说明如何做到这一点。然而,在汇集了许多帖子(以及对LINEST实际功能的描述)后,我想我会发布我的解决方案代码

    /// <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;
    }
//
///使用最小二乘法查找渐变
/// 
///通过数据X和y的最佳拟合趋势线的y截距
公共十进制LeastSquaresGradient()
{
//DataSetsMatch方法确保X和Y
//(在这种情况下,两个列表)具有相同数量的元素
如果(!DataSetsMatch())
{
抛出新ArgumentException(“X和Y必须包含相同数量的元素”);
}
//这些变量用于存储每个点与其相关平均值的方差
List varX=新列表();
列表变量=新列表();
foreach(x中的十进制x)
{
Add(x-AverageX());
}
foreach(y中的十进制y)
{
varY.Add(y-AverageY());
}
小数顶线=0;
小数底线=0;
对于(int i=0;i
下面是Excel的LINEST()函数在C#中的一个实现。它返回给定数据集的斜率,使用LINEST()使用的相同“最小二乘”方法进行归一化:

由于这些方法只适用于一组数据,如果希望生成多组线性回归数据,我建议在循环内部调用它们


这个链接帮助我找到了答案:

如何使用它。因为LINEST使用一组x和一组y,M和B的值为常量。但在本例中,它只接受一个数组。这是因为本例使用一个点数组(x,y集)。您应该能够找出如何修改算法以采用两个数组(如果您真的需要的话)。链接不再起作用:(@harrydev-我已经更新了链接。博主将其内容移动到了一个新的域名。该链接不再存在。该页面不再存在。@Butzke:没有人阻止您查找当前链接并在中编辑它;-)是的,我知道,但是你可以用另一个链接来替换,而不是删除这个链接。如果没有这个链接,它不会回答问题。
public static double CalculateLinest(double[] y, double[] x)
{
   double linest = 0;
   if (y.Length == x.Length)
   {
      double avgY = y.Average();
      double avgX = x.Average();
      double[] dividend = new double[y.Length];
      double[] divisor = new double[y.Length];
      for (int i = 0; i < y.Length; i++)
      {
         dividend[i] = (x[i] - avgX) * (y[i] - avgY);
         divisor[i] = Math.Pow((x[i] - avgX), 2);
      }
      linest = dividend.Sum() / divisor.Sum();
   }
   return linest;
}
private double CalculateYIntercept(double[] x, double[] y, double linest)
{
    return (y.Average() - linest * x.Average());
}