What is the C # equivalent for LINEST from Excel?

Is there any inbuit function, or do we need to write our own. In a later case, you could give me a link where it was implemented. And how does it work?

thanks

+4
source share
5 answers

I found the answer and sent to this site

0
source

There is no built-in functionality in C # to calculate the best line using the least squares method. I would not expect that there will be one of them either since Excel is used to process data / statistics, and C # is a general-purpose programming language.

There are many people who published implementations on different sites. I would suggest checking them out and exploring the algorithm underlying their calculations.

Here is a link to one implementation:

Maths Algorithms in C #: Linear Least Squares Fit

+4
source

The online help has fairly extensive documentation. And no, this is not available in C # by default. Both C # /. NET and Excel have completely different uses, hence different functionality.

+1
source

Trying to solve this problem using this question and other questions that are similar / identical, I could not get a good example of how to do this. However, combining many messages (and a description of Office help about what LINEST actually does) I thought I would post my solution code.

/// <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; } 
0
source

Here is an implementation of the Excel LINEST () function in C #. It returns the slope for a given dataset, normalized using the same least-squares method used in LINEST ():

 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; } 

Also described here is the method that I wrote to get the value of "b" (y-intercept) that the Excel LINEST function generates.

 private double CalculateYIntercept(double[] x, double[] y, double linest) { return (y.Average() - linest * x.Average()); } 

Since these methods only work for one dataset, I would recommend calling them inside a loop if you want to create multiple linear regression datasets.

This link helped me find my answer: https://agrawalreetesh.blogspot.com/2011/11/how-to-calculate-linest-of-given.html

0
source

Source: https://habr.com/ru/post/1307673/


All Articles