Nonlinear Curve Fitting: Fit Plot

Nonlinear least squares data fitting can be performed using Fit Plot. To create a Fit Plot, select x and y columns in table, then select Table -> Create Fit Plot in main menu, or use context menu in table, or use Create Fit Plot button in the toolbar.

'Nonlinear' means here that analytical fitting function depends nonlinearly on varying parameters (fit parameters). Linear fitting is a quite simple method, which is based on solving the system of linear equations. Unlike linear fitting, nonlinear fitting is performed by iterative algorithm which needs the user to set the initial values of fit parameters.

To fit the data, implement these steps:

Create a Fit Plot, specify y weighting in Plot properties, if any

Specify fit function by adding Fit Curves

Specify initial values of fit parameters (drag curves or enter accurate values)

Specify used x data interval

Run fitting

Fit example

Fit Function is a Sum of Fit Curves

MagicPlot considers fit function as a sum of Fit Curves. Ordinarily in peaks fitting each Fit Curve corresponds to one peak on the experimental data. There are a number of predefined Fit Curves (Line, Parabola, Gauss, Lorentz, etc.) You can specify a custom Fit Curve too. Baseline fitting components also may be added to the fitting sum.

Fit Plot window contains the list of Fit Curves. Each Fit Curve in the list has three check boxes: Show, Baseline, Sum:

Show: Specifies whether to show this Fit Curve on plot. Active only if Baseline check box is not set

Baseline: Toggles the subtracting of this Fit Curve from experimental data

Sum: Specifies whether to use this Fit Curve in sum fit function

Below the Fit Curves list is a parameter table which shows parameters names and values, and descriptions of selected Fit Curve.

Copying Fit Curves from One Fit Plot to Another

You can copy and paste Fit Curves in curves table as usual. Use context menu in curves table or press Ctrl+C, Ctrl+V on PC and Cmd C, Cmd V on Mac (curves table must have focus, not the plot itself). MagicPlot copies Fit Curves parameters values, baseline/sum/show checkbox values and style.

Setting Initial Values of Parameters

Nonlinear fitting assumes that certain initial values of parameters are set before fitting. This procedure is very easy if you use predefined Fit Curves: you can drag curves on plot.

Initial parameters values for each Fit Curve can also be set in parameter table.

Parameter Locking

You can lock parameter(s) to prevent varying this parameter during fit and to prevent its changing due to setting initial values by mouse dragging (for built-in functions). Set the checkbox in Lock column in parameters list.

Fit Intervals

You can set the x intervals of the data. Data points outside these intervals are not used to compute the minimizing residual sum of squares (see below). You can use this feature if some data points (especially in the beginning or the end) are inaccurate, e.g. noisy.

Select Fit Interval tab to set intervals visually or edit accurate borders values:

Double click on interval to split it

Drag the interval border to move it. If intervals intersect, they will be merged

Use context menu on the plot to create, delete and split intervals

Interval context menu	Interval context menu

Baseline Fitting and Extraction

Fit Interval is also usable when baseline fitting. Before baseline fitting you can specify the interval which does not contain any signal points and contains baseline only. Set Baseline checkboxes at baseline Fit Curves after baseline fitting to subtract baseline from data. Then specify the whole interval and fit the data.

The most appropriate curve type for baseline fitting is spline.

Note that if you execute one of data processing algorithms (integration, FFT, etc.) on Fit Plot, then the difference between the data and baseline curves (which you do see on the plot) will be processed. You can use this behaviour to exclude baseline from data before integrating, see Integration for more information.

'Data-Baseline' Table Column

The 'Data-Baseline' column is appended to the Table with initial (x, y) fit data when you create Fit Plot. The 'Data-Baseline' column contains the difference between initial y data and baseline approximation (the sum of Fit Curves for which Baseline checkbox is set).

It is 'Data-Baseline' column that is actually plotted on Fit Plot.

Use 'Data-Baseline' column in Table if you want to process the data without baseline. This column is also used as initial data if you use Processing menu when Fit Plot is active.

Fit One Curve

You can also use MagicPlot to fit the data with single selected Fit Curve by pressing Fit One Curve button. In this case a specific data interval for each Fit Curve is used and the main fitting data interval (set in Fit Interval tab) is ignored. Select Set Interval checkbox in the bottom of the Fit Plot panel to set specific fit intervals for each Fit Curve.

Because of using individual data interval this method is useful for baseline fitting. In order to fit baseline specify the intervals which does not contain signal (peaks) and contain only noise.

Joining the Parameters of Fit Curves

In some cases you may want to fit the data with two Gauss or Lorentz peaks with the same width but different positions and amplitudes, for example. You can do this in two ways: by specifying custom Fit Curve with your equation or by joining the 'width' parameters of two peaks.

To join parameters of two or more Fit Curves select one of desired Fit Curves, select desired parameter in parameters table and press Join button. Add parameters which will be joint in the opened dialog window. Joined parameters are treated as one fit parameter.

Joined parameters are shown with blue color (instead of black) in parameters table.

Fitting Algorithm

MagicPlot uses iterative Levenberg–Marquardt nonlinear least squares curve fitting algorithm which is widely used in most software.

Fit procedure iteratively varies the parameters βk of fit function f(x, β1, ..., βp) to minimize the residual sum of squares (RSS, χ2):

here:

xi and yi are the data points,

N is total number of points,

f(x, β1,...,βp) is the fit function which depends on value of x and fit parameters βk,

p is the number of fit parameters βk,

wi are normalized y data weighting coefficients for each point yi:

Calculation of the new guess of parameters on each fit iteration is based on the fit function partial derivatives for current values of fit parameters and for each x value:

To start minimization, you have to provide an initial guess for the parameters.

Fit Procedure Stop Criteria

After each iteration except the first MagicPlot evaluates deviation decrement D:

Deviation decrement shows how the residual sum of squares (RSS) on current iteration relatively differs from that on the previous iteration.

The iterative fit procedure stops on one of two conditions:

If the deviation decrement D is less than minimum allowable deviation decrement, which is 10-9 by default

or (and)

If the number of iterations exceeds maximum number of iterations, which is 100 by default

You can change the minimum allowable deviation decrement and maximum number of iterations in Fitting tab of MagicPlot Preferences.

Fit progress window

MagicPlot indicates fit process with a special window. Fitting curves are periodically updated on plot while fitting so you can see how fit converges.

Fit progress window

MagicPlot shows current iteration number and deviation decrement with two progress bars while fit is performed. The fit process stops when one of these progress bars reaches the end.

You can see two buttons on fit progress window:

Break Iterations: Breaks iterations after current iteration. Use this button if you suspect that further iterations will not change the result.

Undo Fit: Breaks iterations and reverts fit parameters to their initial (before fit) values. Use this button if you see that fit process converges to wrong result; change initial values of parameters and run fit again.

Weighting of y data

MagicPlot can use weighting of y values based on y errors si:

If standard y errors are not specified: all wi=1

If standard y errors si are specified:

here C is normalizing coefficient (to make the sum of wi be equal to one):

In Fit Plot Properties dialog (Plot Data tab) you can set one of the following methods to evaluate standard y errors si:

Get y errors from table column(s),

Percent of data for every point,

Fixed value or Standard deviation --- do not use in weighting because in this case the error values are the same for all data points.

Standard Deviation of Fit Parameters Calculation

Standard deviations (std. dev.) of fit parameters βk are evaluated after fit using the following formula:

here α is the matrix of partial derivatives of fit function by parameters βm and βn which is used for fitting:

Undoing Fit

You can undo fit and undo changing initial parameters as usual using Undo function. It is a handy feature when experimenting with different models and initial parameters (e.g. peaks positions).

Formulas

In the table below you can find the formulas which MagicPlot uses to calculate fit parameters and values in Fit Report tab.

Because of some confusion in the names of the parameters in different sources (books and software), we also give many different names of same parameter in note column.

Parameter Name	Symbol	Formula	Note
Original Data and Fit Model Properties
Number of used data points	---	This is the number of data points inside specified Fit Interval.
Fit parameters	β1,...,βp	---	For peak-like functions (Gauss, Lorentz) these parameters are amplitude, position and half width at half maximum.
Number of fit function parameters β	---	This is the total number of parameters of all fit curves which are summarized to fit.
Degrees of freedom
Estimated mean of data
Estimated variance of data	Not used by fit algorithm, only for comparison.
Data total sum of squares, TSS	TSS	TSS is also called sum of squares about the mean and acronym SST is also used.
Fit Result
Residual sum of squares, RSS	This value is minimized during the fit to find the optimal fit function parameters. RSS is also called the sum of squared residuals (SSR), the error sum of squares (ESS), the sum of squares due to error (SSE).
Reduced χ2	The advantage of the reduced chi-squared is that it already normalizes for the number of data points and model (fit function) complexity. Reduced χ2 is also called mean square error (MSE) or the residual mean square.
Standard deviation of the model	s	Standard deviation is also called root mean square of the error (Root MSE)
Coefficient of determination	R2 will be equal to one if fit is perfect, and to zero otherwise. This is a biased estimate of the population R2, and will never decrease if additional fit parameters (fit curves) are added, even if they are irrelevant.
Adjusted R2	Adjusted R2 (or degrees of freedom adjusted R-square) is a slightly modified version of R2, designed to penalize for the excess number of fit parameters (fit curves) which do not add to the explanatory power of the regression. This statistic is always smaller than R2, can decrease as you add new fit curves, and even be negative for poorly fitting models

See Also

Specifying Custom Fit Equation

Using Spline for Baseline Subtraction

Guessing Peaks

Predefined Fit Curves Equations

transform_xy

Calculating Integrals and Statistics on Intervals using Fit Plot