Ejemplo n.º 1
0
        private static void TestFit()
        {
            var lrm = new LinearRegressionModel {
                Weights = np.array(new double[] { 2, 4, 6, 2, 2 })
            };
            var x = np.array(new double[, ] {
                { 1, 1, 1, 1 }, { 2, 5, 2, 2 }, { 2, 3, 2, 2 }, { 5, 2, 2, 2 }, { 2, 2, 3, 2 }, { 7, 2, 2, 2 }, { 2, 6, 2, 2 }, { 2, 4, 2, 2 }, { 2, 3, 3, 2 }, { 2, 2, 2, 8 }
            });

            var list = LinearRegressionModel.MatrixToDataSet(x);

            var yList = lrm.Predict(list);
            var y     = np.array(yList.Cast <double>().ToArray());

            Console.WriteLine($"Weights {lrm.Weights.ToString()}");
            Console.WriteLine($"X {x.ToString()}");
            Console.WriteLine($"Y { y.ToString()}");

            lrm.Fit(list, yList);

            Console.WriteLine($"Weights after fit {lrm.Weights.ToString()}");
        }
Ejemplo n.º 2
0
        private void buttonAnalyze_Click(object sender, EventArgs e)
        {
            Model  newModel = new Model();
            string formula  = "SalePrice ~ " + string.Join(" + ", typeof(Flat).GetProperties()
                                                           .Where(o => o.Name != "Id" && o.Name != "SalePrice").Select(o => o.Name).ToArray());

            string[] stats = null;
            Extreme.DataAnalysis.Models.RegressionModel regression = null;
            switch (comboBoxModels.Text)
            {
            case "Linear regression":
                newModel.Name = "Linear regression";
                LinearRegressionModel model1 = new LinearRegressionModel(DataFrame.FromDataTable(flatsRepos.GetDataFrame()), formula);
                model1.Fit();

                newModel.ResidualStandardError = model1.StandardError;
                newModel.RSquared            = model1.RSquared;
                newModel.AdjustedRSquared    = model1.AdjustedRSquared;
                newModel.FStatistic          = model1.FStatistic;
                newModel.CorrespondingPValue = model1.PValue;

                stats = new string[]
                {
                    $"Residual standard error:   {model1.StandardError:f2}",
                    $"R-Squared:   {model1.RSquared:f2}",
                    $"Adjusted R-Squared:   {model1.AdjustedRSquared:f2}",
                    $"F-statistic:   {model1.FStatistic:f2}",
                    $"Corresponding p-value:   {model1.PValue:f2}"
                };
                regression = model1;
                break;

            /* case "Logistic regression":
             *  LogisticRegressionModel model2 = new LogisticRegressionModel(DataFrame.FromDataTable(flatsRepos.GetDataFrame()), formula);
             *  model2.Compute();
             *
             *  Extreme.Statistics.Tests.SimpleHypothesisTest lrt = model2.GetLikelihoodRatioTest();
             *  newModel.LogLikehood = model2.LogLikelihood;
             *  newModel.ChiSquared = lrt.Statistic;
             *  newModel.CorrespondingPValue = lrt.PValue;
             *
             *  stats = new string[]
             *  {
             *      $"Log-likelihood:   {model2.LogLikelihood:f2}",
             *      $"Chi-Squared:   {lrt.Statistic:f2}",
             *      $"P-Value:   {lrt.PValue:f2}"
             *  };
             *  //regression = model2;
             *  break;*/
            case "Generalized linear regression":
                newModel.Name = "Generalized linear regression: " + comboBoxGeneralizedType.Text;
                GeneralizedLinearModel model3 = new GeneralizedLinearModel(DataFrame.FromDataTable(flatsRepos.GetDataFrame()), formula);
                switch (comboBoxGeneralizedType.Text)
                {
                case "Binomial":
                    model3.ModelFamily = ModelFamily.Binomial;
                    break;

                case "InverseGaussian":
                    model3.ModelFamily = ModelFamily.InverseGaussian;
                    break;

                case "Normal":
                    model3.ModelFamily = ModelFamily.Normal;
                    break;

                case "Poisson":
                    model3.ModelFamily = ModelFamily.Poisson;
                    break;

                default:
                    break;
                }
                model3.Fit();
                if (comboBoxGeneralizedType.Text == "Normal" || comboBoxGeneralizedType.Text == "Poisson")
                {
                    newModel.LogLikehood       = model3.LogLikelihood;
                    newModel.KernelLogLikehood = model3.GetKernelLogLikelihood();
                    newModel.Akaike            = model3.GetAkaikeInformationCriterion();
                    newModel.CorrectedAIC      = model3.GetCorrectedAkaikeInformationCriterion();
                    newModel.Bayesian          = model3.GetBayesianInformationCriterion();
                    newModel.ChiSquared        = model3.GetChiSquare();
                    stats = new string[]
                    {
                        $"Log likelihood   {model3.LogLikelihood:f2}",
                        $"Kernel log likelihood:   {model3.GetKernelLogLikelihood():f2}",
                        $"Akaike (AIC):   {model3.GetAkaikeInformationCriterion():f2}",
                        $"Corrected AIC:   {model3.GetCorrectedAkaikeInformationCriterion():f2}",
                        $"Bayesian (BIC):   { model3.GetBayesianInformationCriterion():f2}",
                        $"Chi Square:   { model3.GetChiSquare():f2}"
                    };
                }
                regression = model3;
                break;
            }
            foreach (Parameter parameter in regression.Parameters)
            {
                typeof(Model).GetProperty(parameter.Name).SetValue(newModel, parameter.Value);
            }
            dataGridViewParams.DataSource = regression.Parameters.ToArray();
            dataGridViewParams.Columns[dataGridViewParams.Columns.Count - 1].Visible = false;
            listBoxStats.Items.Clear();
            if (stats != null)
            {
                listBoxStats.Items.AddRange(stats);
            }
            modelsRepos.Add(newModel);
        }