4

I need to get information out of LINEST Excel function. I'd like to know if there is libraries that have similar functions or if It's possible to use native Excel functions in C# somehow? It might be important that I'm working in MVS 2010 and using Windows Forms. I wrote a least square estimate function that calculates parameters only for 2-factor model, but I need to calculate parameters for a model with more then 2 factors.

P.S. Actually I did google for answers and i saw that somebody used package Microsoft.Office.Excel to calculate necessary information but there are no such an imports in my IDE. I don't want to invent a wheel, so I appreciate totally any information. Thank You.

Update: Finally i added a Microsoft.Office.Excel reference to my Project and solved my problem. Source code is attached below. The result is 2D Excel-like array. Code was written in a hurry so It's not really good and need to be modified. I just hope that It will help for other people. /w me if you need help

        Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.WorksheetFunction wsf = xl.WorksheetFunction;

        List<double> x = new List<double> { 107, 109, 110, 113, 120, 122, 123, 128, 136, 140, 145, 150 };
        List<double> y = new List<double> { 102, 105, 108, 110, 115, 117, 119, 125, 132, 130, 141, 144 };

        object result = wsf.LinEst(y.ToArray(), x.ToArray(), true, true);

        Array resArray = (Array)result;

        double[,] linest = new double[5, 2];
        int i = 0, j = 0;
        Console.WriteLine("{0,15:f10}", resArray.Length);
        foreach (var element in resArray)
        {
            if (i == 0 || i == 2 || i == 4 || i == 6 || i == 8)
            {
                linest[j, 0] = (double)element;                    
            }
            else
            {
                linest[j, 1] = (double)element;
                j++;
            }
            i++;                
        }

        Console.WriteLine();
        for (i = 0; i < linest.GetLength(0); i++)
        {
            for (j = 0; j < linest.GetLength(1); j++)
                Console.Write("{0,25:f13}", linest[i, j]);
            Console.WriteLine();
        }
Nobody One
  • 213
  • 4
  • 13
  • Possible duplicate of [Non-linear regression in C#](https://stackoverflow.com/questions/7792088/non-linear-regression-in-c-sharp) – John Alexiou Jul 17 '19 at 00:17

1 Answers1

1

Try the linear algebra library in Math net numerics and avoid using excel altogether.

Richard Todd
  • 2,406
  • 5
  • 32
  • 40
  • It's not really thing i need, because calculations of R2, F- and T-tests and other things are really important to me. However, I googled for such a libraries and found out that there are a lot of them, so can you advise me that one, which suits for me? If nobody will help me, I'll have to follow Your advice. Thank You. – Nobody One Apr 06 '13 at 14:33
  • Will accept Your answer, because You are the only who decided to help me. Have a nice day! ;) – Nobody One Apr 06 '13 at 17:20
  • 1
    Have a look at this blog. I would have written more but was posting from a mobile phone. http://christoph.ruegg.name/blog/2012/9/9/linear-regression-mathnet-numerics.html – Richard Todd Apr 06 '13 at 23:15