在.net中使用LinEst() [英] Using LinEst () in .net

查看:144
本文介绍了在.net中使用LinEst()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Excel的调用函数LINEST()在.net中进行回归分析。
我可以使用具有squred矩阵数组的函数,但是当它不是正方形矩阵表示顺序[12,3]时,它会给出错误:

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:

WorksheetFunction类的LinEst方法失败

LinEst method of WorksheetFunction class failed

请帮助我,因为它非常重要我完成这段代码
这是我的完整代码:

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 ]您的yDataPoints长度应为3,以正常运行LinEst()。

if your xAll has a dimension of [12,3] your yDataPoints length should be 3 for proper functioning of 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的列大小应等于yDataPoints数组的长度。请尝试让我知道。

The column size of xAll should be equal to the length of yDataPoints array. Please try and let me know.

这篇关于在.net中使用LinEst()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆