示例#1
0
        private void button_Regression_Click(object sender, EventArgs e)
        {
            string ColNums = textBox_Cols.Text;

            char[] separator = { ',' };
            //string是以逗号分隔的
            string[] AllNum = ColNums.Split(separator);
            //按照逗号分割
            List <int> Cols = new List <int>();

            foreach (string SingleNum in AllNum)
            {
                if (SingleNum != "")
                {
                    Cols.Add(Convert.ToInt32(SingleNum) - 1);
                }
            }

            int[] AllColNums = Cols.ToArray();
            //MessageBox.Show(AllColNums[0].ToString());
            //MessageBox.Show(AllColNums[1].ToString());
            int yCol = Tabulation.FindCol(MainForm.MainDT, comboBox_y.Text);
            List <List <string> > data = new List <List <string> >();
            int RowsCount      = MainForm.MainDT.Rows.Count;
            int InputColsCount = AllColNums.Length;
            //计算总共要录入的列数
            int count = 0;
            //计算实际录入数据数
            List <string> Ydata = new List <string>();

            for (int i = 0; i < InputColsCount; i++)
            {
                data.Add(new List <string>());
            }
            for (int i = 0; i < RowsCount; i++)
            {
                if (Tabulation.IdentifyNARow(MainForm.MainDT, i, AllColNums))
                {
                    //确认该行无空格
                    if (MainForm.MainDT.Rows[i][yCol].ToString().Trim() != "")
                    {
                        Ydata.Add(MainForm.MainDT.Rows[i][yCol].ToString().Trim());
                        for (int j = 0; j < InputColsCount; j++)
                        {
                            data[j].Add(MainForm.MainDT.Rows[i][AllColNums[j]].ToString());     //此处队长有bug,已改正
                        }
                        count++;
                    }
                }
            }

            if (count > 2)
            {
                BigDecimal[,] IndependentVariables = new BigDecimal[count, InputColsCount + 1];
                StringBuilder Result = new StringBuilder();
                if (count <= InputColsCount + 1)
                {
                    Result.Append("样本量过少,无法估计");
                }
                else
                {
                    //第一列全是1
                    for (int i = 0; i < count; i++)
                    {
                        IndependentVariables[i, 0] = 1;
                    }
                    for (int i = 0; i < count; i++)
                    {
                        for (int j = 0; j < InputColsCount; j++)
                        {
                            //录入时,BigDecimal数组列数要+1,因为第一列全是1
                            IndependentVariables[i, j + 1] = data[j][i];
                        }
                    }
                    //MathV.ArrayPrint(IndependentVariables);
                    BigDecimal[,] DependentVariable = new BigDecimal[count, 1];
                    for (int i = 0; i < count; i++)
                    {
                        DependentVariable[i, 0] = Ydata[i];
                    }
                    int len12 = IndependentVariables.GetLength(1);//列数
                    BigDecimal[,] b1 = MathV.MatTrans(IndependentVariables);
                    BigDecimal[,] b2 = MathV.MatTimes(b1, IndependentVariables);
                    try
                    {
                        BigDecimal[,] b3 = MathV.MatInv(b2, len12);
                        if (b3 != null)
                        {
                            BigDecimal[,] bhat       = Stat.MultiRegBeta(b1, b3, IndependentVariables, DependentVariable);
                            BigDecimal[,] value_beta = Stat.MultiRegP(b3, bhat, IndependentVariables, DependentVariable);
                            string[] RandF = Stat.MultiRegR(bhat, IndependentVariables, DependentVariable).Split(separator);
                            //MathV.ArrayPrint(bhat);
                            Result.Append(StrManipulation.PadRightX(StrManipulation.VariableNamePolish("拟合R^2:"), ' ', 12));
                            Result.Append(MathV.NumberPolish(RandF[0]));
                            Result.Append("\r\n");
                            Result.Append(StrManipulation.PadRightX(StrManipulation.VariableNamePolish("调整后R^2:"), ' ', 12));
                            Result.Append(MathV.NumberPolish(RandF[1]));
                            Result.Append("\r\n");
                            Result.Append(StrManipulation.PadRightX(StrManipulation.VariableNamePolish("回归F值:"), ' ', 12));
                            Result.Append(MathV.NumberPolish(RandF[2]));
                            Result.Append("\r\n");
                            Result.Append(StrManipulation.PadRightX(StrManipulation.VariableNamePolish("回归P值:"), ' ', 12));
                            Result.Append(MathV.NumberPolish(Stat.FINV(Convert.ToDouble(RandF[2]), count - InputColsCount - 1, InputColsCount).ToString()));
                            Result.Append("\r\n \r\n");
                            Result.Append(comboBox_y.Text + " = ");

                            int ColumnNumberCount = 0;


                            foreach (BigDecimal EachNum in bhat)
                            {
                                if (ColumnNumberCount == 0)
                                {
                                    Result.Append(MathV.NumberPolish(EachNum.ToString()));
                                    Result.Append(" + ");
                                }
                                else if (ColumnNumberCount == InputColsCount)
                                {
                                    Result.Append(MathV.NumberPolish(EachNum.ToString()));
                                    Result.Append(" ");
                                    Result.Append(MainForm.MainDT.Columns[AllColNums[ColumnNumberCount - 1]].ColumnName);
                                }
                                else
                                {
                                    Result.Append(MathV.NumberPolish(EachNum.ToString()));
                                    Result.Append(MainForm.MainDT.Columns[AllColNums[ColumnNumberCount - 1]].ColumnName);
                                    Result.Append(" + ");
                                }
                                ColumnNumberCount++;
                            }
                            Result.Append("\r\n检验P值:   ");

                            for (int i = 0; i < InputColsCount + 1; i++)
                            {
                                Result.Append(MathV.NumberPolish(value_beta[i, 0].ToString()));
                                Result.Append("  \t");
                            }
                            Result.Append("\r\n检验t值:   ");
                            for (int i = 0; i < InputColsCount + 1; i++)
                            {
                                Result.Append(MathV.NumberPolish(value_beta[i, 1].ToString()));
                                Result.Append("  \t");
                            }
                            Result.Append("\r\n \r\n");
                        }
                        else
                        {
                            Result.Append("矩阵不可逆,回归方程无解");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("存在重复自变量");
                    }
                }
                MainForm.S.richTextBox1.AppendText(Result.ToString());
            }
        }