English 中文(简体)
使用LinEst()
原标题:Using LinEst () in .net

I want to use Excel s buil-in function called LINEST() to do regression analysis in .net. I am able to use the function with squred matrix array, but when it is not square matrix say of order[12,3] then it gives error as:

班轮职班方法失败

Please help me out with this as it is very important for me to complete this code. This is my complete code:

        System.Data.DataTable dt = new System.Data.DataTable();
        SqlCommand  cmd =new SqlCommand("Select QtytoTransfer from DEmo ",con);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);

        List<double> yDatapoints =new List<double>();

        foreach (DataRow dr in dt.Rows)
        {
            yDatapoints.Add(Convert.ToDouble( dr["QtytoTransfer"].ToString()));
        }



        System.Data.DataTable dt1 = new System.Data.DataTable();

        SqlCommand sqlcmd = new SqlCommand("Select CurrentQoh,QtySold,GameTime from DEmo ", con);

        SqlDataAdapter adp1 = new SqlDataAdapter(sqlcmd);
        adp1.Fill(dt1);

        double[,] xAll = new double[dt1.Rows.Count, dt1.Columns.Count];
        for (int i = 0; i < dt1.Rows.Count; ++i)
        {
            for (int j = 0; j < dt1.Columns.Count; ++j)
            {
                xAll[i, j] = Convert.ToDouble(dt1.Rows[i][j].ToString());
            }
        }

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

        Microsoft.Office.Interop.Excel.WorksheetFunction wsf = xl.WorksheetFunction;
        object[,] reslut = (object[,])wsf.LinEst(yDatapoints.ToArray(), xAll, missing, true);
问题回答

如果您的XAll有一个方面,即[12,3] 您的圆点长度应为3, 以便LinEst(......)正常运作。

using System;

namespace InteropExcel {
    class Program {
        static void Main(string[] args) {
            Random rand = new Random();
            double[] yDatapoints = new double[3];
            for (int i = 0; i < 3; i++) {
                yDatapoints[i]=rand.Next(20, 60);
            }
            double[,] xAll = new double[12, 3];
            for (int i = 0; i < 12; i++) {
                for (int j = 0; j < 3; j++) {

                    xAll[i, j] = rand.Next(2, 100);
                }
            }
            Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.WorksheetFunction wsf = xl.WorksheetFunction;
            object[,] result = (object[,])wsf.LinEst(yDatapoints, xAll, Type.Missing, true);



        }
    }
}

xAll的柱体大小应等于“DataPoint”阵列的长度。 请尝试,让我知道。

在C#实施Excel s LINEST(LINEST)职能。 这可能比对微软的依赖更容易。 办公室。 Excel DL文档。

这套数据在斜坡上回归,采用同样的“最小平方”方法实现正常化,准则EST(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;
}

此外,我在此写道,采用一种方法,使Excel s LINEST的功能产生“b”(y-intercept)的价值。

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

由于这些方法仅用于one<>>>/em>成套数据,如果你希望提供多套线回归数据,我将建议将其放在空白处。

这一联系帮助我找到了我的回答:https://agrawalreetesh.blogspot.com/201111/how-to-calculate-linest-of-of-given.html





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...