Exemple #1
0
        /// <summary>
        /// Fits data provided as xcolumn and ycolumn with a polynomial base.
        /// </summary>
        /// <param name="order">The order of the fit (1:linear, 2:quadratic, etc.)</param>
        /// <param name="xcolumn">The column of x-values.</param>
        /// <param name="ycolumn">The column of y-values.</param>
        /// <returns>The fit.</returns>
        public static LinearFitBySvd Fit(int order, Altaxo.Data.DataColumn xcolumn, Altaxo.Data.DataColumn ycolumn)
        {
            if (!(xcolumn is Altaxo.Data.INumericColumn))
            {
                throw new ArgumentException("The x-column must be numeric", "xcolumn");
            }
            if (!(ycolumn is Altaxo.Data.INumericColumn))
            {
                throw new ArgumentException("The y-column must be numeric", "ycolumn");
            }

            int firstIndex = 0;
            int count      = Math.Min(xcolumn.Count, ycolumn.Count);

            double[] xarr = new double[count];
            double[] yarr = new double[count];
            double[] earr = new double[count];

            var xcol = (Altaxo.Data.INumericColumn)xcolumn;
            var ycol = (Altaxo.Data.INumericColumn)ycolumn;

            int numberOfDataPoints = 0;
            int endIndex           = firstIndex + count;

            for (int i = firstIndex; i < endIndex; i++)
            {
                double x = xcol[i];
                double y = ycol[i];
                if (double.IsNaN(x) || double.IsNaN(y))
                {
                    continue;
                }

                xarr[numberOfDataPoints] = x;
                yarr[numberOfDataPoints] = y;
                earr[numberOfDataPoints] = 1;
                numberOfDataPoints++;
            }

            return(LinearFitBySvd.FitPolymomialDestructive(order, xarr, yarr, earr, numberOfDataPoints));
        }
Exemple #2
0
        public static string Fit(Altaxo.Gui.Graph.Gdi.Viewing.IGraphController ctrl, int order, double fitCurveXmin, double fitCurveXmax, bool showFormulaOnGraph)
        {
            string error;

            error = GetActivePlotPoints(ctrl, out var xarr, out var yarr);
            int numberOfDataPoints = xarr.Length;

            if (null != error)
            {
                return(error);
            }

            string[] plotNames = GetActivePlotName(ctrl);

            int numberOfParameter = order + 1;

            double[] parameter = new double[numberOfParameter];

            var fit = LinearFitBySvd.FitPolymomialDestructive(order, xarr, yarr, null, numberOfDataPoints);

            // Output of results

            Current.Console.WriteLine("");
            Current.Console.WriteLine("---- " + DateTime.Now.ToString() + " -----------------------");
            Current.Console.WriteLine("Polynomial regression of order {0} of {1} over {2}", order, plotNames[1], plotNames[0]);

            Current.Console.WriteLine(
                "Name           Value               Error               F-Value             Prob>F");

            for (int i = 0; i < fit.Parameter.Length; i++)
            {
                Current.Console.WriteLine("A{0,-3} {1,20} {2,20} {3,20} {4,20}",
                                          i,
                                          fit.Parameter[i],
                                          fit.StandardErrorOfParameter(i),
                                          fit.TofParameter(i),
                                          1 - FDistribution.CDF(fit.TofParameter(i), numberOfParameter, numberOfDataPoints - 1)
                                          );
            }

            Current.Console.WriteLine("R²: {0}, Adjusted R²: {1}",
                                      fit.RSquared,
                                      fit.AdjustedRSquared);

            Current.Console.WriteLine("Condition number: {0}, Loss of precision (digits): {1}", fit.ConditionNumber, Math.Log10(fit.ConditionNumber));

            Current.Console.WriteLine("------------------------------------------------------------");
            Current.Console.WriteLine("Source of  Degrees of");
            Current.Console.WriteLine("variation  freedom          Sum of Squares          Mean Square          F0                   P value");

            double regressionmeansquare = fit.RegressionCorrectedSumOfSquares / numberOfParameter;
            double residualmeansquare   = fit.ResidualSumOfSquares / (numberOfDataPoints - numberOfParameter - 1);

            Current.Console.WriteLine("Regression {0,10} {1,20} {2,20} {3,20} {4,20}",
                                      numberOfParameter,
                                      fit.RegressionCorrectedSumOfSquares,
                                      fit.RegressionCorrectedSumOfSquares / numberOfParameter,
                                      regressionmeansquare / residualmeansquare,
                                      1 - FDistribution.CDF(regressionmeansquare / residualmeansquare, numberOfParameter, numberOfDataPoints - 1)
                                      );

            Current.Console.WriteLine("Residual   {0,10} {1,20} {2,20}",
                                      numberOfDataPoints - 1 - numberOfParameter,
                                      fit.ResidualSumOfSquares,
                                      residualmeansquare
                                      );

            Current.Console.WriteLine("Total      {0,10} {1,20}",
                                      numberOfDataPoints - 1,
                                      fit.TotalCorrectedSumOfSquares

                                      );

            Current.Console.WriteLine("------------------------------------------------------------");

            // add the fit curve to the graph
            IScalarFunctionDD plotfunction = new PolynomialFunction(fit.Parameter);
            var fittedCurve = new XYFunctionPlotItem(new XYFunctionPlotData(plotfunction), new G2DPlotStyleCollection(LineScatterPlotStyleKind.Line, ctrl.Doc.GetPropertyContext()));

            var xylayer = ctrl.ActiveLayer as XYPlotLayer;

            if (null != xylayer)
            {
                xylayer.PlotItems.Add(fittedCurve);
            }

            return(null);
        }