Exemple #1
0
        public void Test_LinearRegression_RandomXs()
        {
            int    pointCount   = 50;
            double actualSlope  = 3;
            double actualOffset = 200;

            (double[] xs, double[] ys) = GetNoisyLinearData_RandomlySpaced(pointCount, actualSlope, actualOffset);

            // fit the random data with the linear regression model
            var model = new ScottPlot.Statistics.LinearRegressionLine(xs, ys);

            // plot to visually assess goodness of fit
            var plt = new ScottPlot.Plot(450, 300);

            plt.Title($"Y = {model.slope:0.0000}x + {model.offset:0.0}\nR² = {model.rSquared:0.0000}");
            plt.AddScatterPoints(xs, ys);
            // ADD THIS BACK IN!
            //plt.PlotLine(model.slope, model.offset, (xs.Min(), xs.Max()), lineWidth: 2, label: "model", lineStyle: ScottPlot.LineStyle.Dash);
            //plt.PlotLine(actualSlope, actualOffset, (xs.Min(), xs.Max()), lineWidth: 2, label: "actual");
            plt.Legend();
            TestTools.SaveFig(plt);

            // ensure the fit is good
            Assert.AreEqual(actualSlope, model.slope, .5);
            Assert.AreEqual(actualOffset, model.offset, 10);
        }