Esempio n. 1
0
 public void getLrModel(string modelPath, bool BuildModel = false)
 {
     using (System.IO.StreamReader sr = new System.IO.StreamReader(modelPath))
     {
         string     mType = sr.ReadLine();
         modelTypes m     = (modelTypes)Enum.Parse(typeof(modelTypes), mType);
         if (m != modelTypes.LogisticRegression)
         {
             System.Windows.Forms.MessageBox.Show("Model file specified is not a Logistic Regression Model!!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
             return;
         }
         InTablePath           = sr.ReadLine();
         IndependentFieldNames = sr.ReadLine().Split(new char[] { ',' });
         DependentFieldNames   = sr.ReadLine().Split(new char[] { ',' });
         ClassFieldNames       = sr.ReadLine().Split(new char[] { ',' });
         categories            = sr.ReadLine().Split(new char[] { ',' });
         nvars         = System.Convert.ToInt32(sr.ReadLine());
         ncat          = System.Convert.ToInt32(sr.ReadLine());
         loglikelihood = System.Convert.ToDouble(sr.ReadLine());
         deviance      = System.Convert.ToDouble(sr.ReadLine());
         x2            = System.Convert.ToDouble(sr.ReadLine());
         pv            = System.Convert.ToDouble(sr.ReadLine());
         minValues     = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
         maxValues     = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
         sumValues     = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
         coefficients  = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
         standarderror = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
         waldstat      = new Accord.Statistics.Testing.WaldTest[coefficients.Length];
         double[] waldStatVl  = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
         double[] waldStatPVl = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
         for (int i = 0; i < waldStatVl.Length; i++)
         {
             double wVl = waldStatVl[i];
             Accord.Statistics.Testing.WaldTest wt = new Accord.Statistics.Testing.WaldTest(wVl);
             waldstat[i] = wt;
         }
         sr.Close();
         if (BuildModel)
         {
             buildModel();
         }
     }
 }
Esempio n. 2
0
        private MultinomialLogisticRegression buildModel()
        {
            if (independent == null)
            {
                formatData();
            }
            mlr = new MultinomialLogisticRegression(nvars, ncat);
            LowerBoundNewtonRaphson lbn = new LowerBoundNewtonRaphson(mlr);

            do
            {
                delta = lbn.Run(independent, dependent);
                iteration++;
            } while (iteration < totit && delta > converg);
            coefficients  = mlr.Coefficients;
            standarderror = new double[ncat - 1][];
            waldstat      = new double[ncat - 1][];
            waldpvalue    = new double[ncat - 1][];
            for (int i = 0; i < coefficients.Length; i++)
            {
                double[] steArr        = new double[nvars + 1];
                double[] waldStatArr   = new double[nvars + 1];
                double[] waldPvalueArr = new double[nvars + 1];
                for (int j = 0; j < nvars + 1; j++)
                {
                    Accord.Statistics.Testing.WaldTest wt = mlr.GetWaldTest(i, j);
                    steArr[j]        = wt.StandardError;
                    waldStatArr[j]   = wt.Statistic;
                    waldPvalueArr[j] = wt.PValue;
                }
                waldstat[i]      = waldStatArr;
                waldpvalue[i]    = waldPvalueArr;
                standarderror[i] = steArr;
            }
            loglikelihood = mlr.GetLogLikelihood(independent, dependent);
            deviance      = mlr.GetDeviance(independent, dependent);
            x2            = mlr.ChiSquare(independent, dependent).Statistic;
            pv            = mlr.ChiSquare(independent, dependent).PValue;
            return(mlr);
        }
Esempio n. 3
0
        private void buildModel()
        {
            if (independentVls == null)
            {
                getMatrix();
            }
            Accord.Statistics.Links.ILinkFunction lFunc = new Accord.Statistics.Links.IdentityLinkFunction();
            switch (Link)
            {
            case LinkFunction.Absolute:
                lFunc = new Accord.Statistics.Links.AbsoluteLinkFunction();
                break;

            case LinkFunction.Cauchit:
                lFunc = new Accord.Statistics.Links.CauchitLinkFunction();
                break;

            case LinkFunction.Inverse:
                lFunc = new Accord.Statistics.Links.InverseLinkFunction();
                break;

            case LinkFunction.InverseSquared:
                lFunc = new Accord.Statistics.Links.InverseSquaredLinkFunction();
                break;

            case LinkFunction.Logit:
                lFunc = new Accord.Statistics.Links.LogitLinkFunction();
                break;

            case LinkFunction.Log:
                lFunc = new Accord.Statistics.Links.LogLinkFunction();
                break;

            case LinkFunction.LogLog:
                lFunc = new Accord.Statistics.Links.LogLogLinkFunction();
                break;

            case LinkFunction.Probit:
                lFunc = new Accord.Statistics.Links.ProbitLinkFunction();
                break;

            case LinkFunction.Sin:
                lFunc = new Accord.Statistics.Links.SinLinkFunction();
                break;

            case LinkFunction.Threshold:
                lFunc = new Accord.Statistics.Links.ThresholdLinkFunction();
                break;

            default:
                break;
            }
            glm = new Accord.Statistics.Models.Regression.GeneralizedLinearRegression(lFunc, NumberOfVariables);
            Accord.Statistics.Models.Regression.Fitting.IterativeReweightedLeastSquares isl = new Accord.Statistics.Models.Regression.Fitting.IterativeReweightedLeastSquares(glm);
            delta = 0;
            do
            {
                delta       = isl.Run(independentVls, dependentVls);
                iterations += 1;
            }while (delta > converge && iterations < totiterations);
            coefficients  = glm.Coefficients;
            stdError      = glm.StandardErrors;
            loglikelihood = glm.GetLogLikelihood(independentVls, dependentVls);
            if (Double.IsNaN(loglikelihood))
            {
                loglikelihood = 0;
            }
            loglikelihoodratio = glm.GetLogLikelihoodRatio(independentVls, dependentVls, glm);
            if (Double.IsNaN(loglikelihoodratio))
            {
                loglikelihoodratio = 0;
            }
            deviance = glm.GetDeviance(independentVls, dependentVls);
            if (Double.IsNaN(deviance))
            {
                deviance = 0;
            }
            Accord.Statistics.Testing.ChiSquareTest chiTest = glm.ChiSquare(independentVls, dependentVls);
            pv = chiTest.PValue;
            if (Double.IsNaN(pv))
            {
                pv = 0;
            }
            chisqr = chiTest.Statistic;
            if (double.IsNaN(chisqr))
            {
                chisqr = 0;
            }
            waldTestValues  = new double[IndependentFieldNames.Length];
            waldTestPValues = new double[waldTestValues.Length];
            for (int i = 0; i < waldTestValues.Length; i++)
            {
                Accord.Statistics.Testing.WaldTest wTest = glm.GetWaldTest(i);
                double wS = wTest.Statistic;
                double wP = wTest.PValue;
                //if (Double.IsNaN(wS)) wS = 0;
                //if (Double.IsNaN(wP)) wP = 0;
                waldTestValues[i]  = wS;
                waldTestPValues[i] = wP;
            }
            //if (stdError.Length != coefficients.Length) stdError = new double[coefficients.Length];
            //for (int i = 0; i < stdError.Length; i++)
            //{
            //    double vl = stdError[i];
            //    if (Double.IsNaN(vl)) stdError[i] = 0;
            //}
        }
        public void getLrModel(string modelPath,bool BuildModel=false)
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(modelPath))
            {
                string mType = sr.ReadLine();
                modelTypes m = (modelTypes)Enum.Parse(typeof(modelTypes), mType);
                if (m != modelTypes.LogisticRegression)
                {
                    System.Windows.Forms.MessageBox.Show("Model file specified is not a Logistic Regression Model!!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return;
                }
                InTablePath = sr.ReadLine();
                IndependentFieldNames = sr.ReadLine().Split(new char[] { ',' });
                DependentFieldNames = sr.ReadLine().Split(new char[] { ',' });
                ClassFieldNames = sr.ReadLine().Split(new char[] { ',' });
                categories = sr.ReadLine().Split(new char[] { ',' });
                nvars = System.Convert.ToInt32(sr.ReadLine());
                ncat = System.Convert.ToInt32(sr.ReadLine());
                loglikelihood = System.Convert.ToDouble(sr.ReadLine());
                deviance = System.Convert.ToDouble(sr.ReadLine());
                x2 = System.Convert.ToDouble(sr.ReadLine());
                pv = System.Convert.ToDouble(sr.ReadLine());
                minValues = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
                maxValues = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
                sumValues = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
                coefficients = (from string s in (sr.ReadLine().Split(new char[]{','})) select System.Convert.ToDouble(s)).ToArray();
                standarderror = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
                waldstat = new Accord.Statistics.Testing.WaldTest[coefficients.Length];
                double[] waldStatVl = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
                double[] waldStatPVl = (from string s in (sr.ReadLine().Split(new char[] { ',' })) select System.Convert.ToDouble(s)).ToArray();
                for (int i = 0; i < waldStatVl.Length; i++)
                {
                    double wVl = waldStatVl[i];
                    Accord.Statistics.Testing.WaldTest wt = new Accord.Statistics.Testing.WaldTest(wVl);
                    waldstat[i] = wt;
                }
                sr.Close();
                if (BuildModel) buildModel();

            }
        }