Trace:

fitting

This shows you the differences between two versions of the page.

Both sides previous revision Previous revision Next revision | Previous revision | ||

fitting [Thu Feb 24 13:59:37 2011] Alexander |
fitting [Sun Jul 21 14:09:36 2019] (current) Alexander |
||
---|---|---|---|

Line 2: | Line 2: | ||

===== Creating a Fit Plot ===== | ===== Creating a Fit Plot ===== | ||

- | Nonlinear least squares data fitting can be performed using Fit Plot. | + | Nonlinear least squares data fitting (nonlinear regression) can be performed using Fit Plot. |

- | To create a Fit Plot, select your X and Y columns in Table, then select ''Table -> Create Fit Plot'' in main menu, or use the same item in Table context menu, or use ''Create Fit Plot'' button in the toolbar. | + | To create a Fit Plot, select your X and Y columns in Table, then select ''Table -> Create Fit Plot'' in the main menu, or use the same item in the Table context menu, or use ''Create Fit Plot'' button in the toolbar. |

- | {{:create_fit_plot_context_menu.png|Creating Fit Plot using Table context menu}} | + | {{:create_fit_plot_context_menu.png?nolink|Creating Fit Plot using Table context menu}} |

+ | | ||

+ | ==== MagicPlot has been verified with NIST Datasets ==== | ||

+ | National Institute of Standards and Technology (NIST) has created the Statistical Reference Datasets Project which includes [[http://www.itl.nist.gov/div898/strd/nls/nls_main.shtml|26 datasets]] for testing the nonlinear fit algorithms. MagicPlot has been successfully tested on these datasets. Our report on MagicPlot testing with NIST datasets is available here: [[http://magicplot.com/downloads/MagicPlot-NIST-Test.pdf|Report]]. | ||

===== Fitting Methodology ===== | ===== Fitting Methodology ===== | ||

- | '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. | + | 'Nonlinear' means here that analytical fitting function depends nonlinearly on varying parameters (fit parameters). |

+ | Fit procedure iteratively varies the parameters of the fit function to minimize the residual sum of squares. The nonlinear fitting algorithm needs the user to set the initial values of fit parameters. | ||

To fit the data, implement these steps: | To fit the data, implement these steps: | ||

- | - Create a Fit Plot, specify Y errors in Fit Plot properties, if any | + | - Create a Fit Plot, specify Y errors in Data tab of Curve Properties dialog for the data curve, if any |

- Specify fit function by adding Fit Curves | - Specify fit function by adding Fit Curves | ||

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

- Specify used X data interval | - Specify used X data interval | ||

- Run fitting | - Run fitting | ||

+ | |||

+ | You can undo fit and also undo changing initial parameters as any other action using ''Undo'' function. It is a handy feature when experimenting with different models and initial parameters. | ||

==== Further reading ==== | ==== Further reading ==== | ||

Line 22: | Line 28: | ||

* H. Motulsky and A. Christopoulos, //Fitting Models to Biological Data Using Linear and Nonlinear Regression: A Practical Guide to Curve Fitting.// 2003, GraphPad Software Inc., San Diego CA, graphpad.com. PDF is available for free [[http://www.graphpad.com/manuals/prism4/RegressionBook.pdf|here]]. | * H. Motulsky and A. Christopoulos, //Fitting Models to Biological Data Using Linear and Nonlinear Regression: A Practical Guide to Curve Fitting.// 2003, GraphPad Software Inc., San Diego CA, graphpad.com. PDF is available for free [[http://www.graphpad.com/manuals/prism4/RegressionBook.pdf|here]]. | ||

- | {{:fit_example.png|Fit example}} | + | {{:fit_example.png?nolink|Fit example}} |

===== Fit Function is a Sum of Fit Curves ===== | ===== Fit Function is a Sum of Fit Curves ===== | ||

Line 29: | Line 35: | ||

Fit Plot window contains the list of Fit Curves. Each Fit Curve in the list has three checkboxes: | Fit Plot window contains the list of Fit Curves. Each Fit Curve in the list has three checkboxes: | ||

- | {{:curves_table.png|Fit Curves table}} | + | {{:curves_table.png?nolink|Fit Curves table}} |

- | * ''Show'': Specifies whether to show this Fit Curve on plot. Active only if Baseline checkbox is not set | + | * ''Show'': Specifies whether to show this Fit Curve on the plot. Active only if Baseline checkbox is not set |

* ''Baseline'': Toggles the subtracting of this Fit Curve from experimental data. You also can use ''Residual'' button to subtract all Fit Sum from data | * ''Baseline'': Toggles the subtracting of this Fit Curve from experimental data. You also can use ''Residual'' button to subtract all Fit Sum from data | ||

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

- | Below the Fit Curves list is a parameters table which shows names, values, and descriptions of parameters relating to selected Fit Curve. | + | Below the Fit Curves list, is a parameters table which shows names, values, and descriptions of parameters relating to the selected Fit Curve. |

- | ===== Copying and Pasting Fit Curves ===== | + | ==== Fitting by Sum and Fitting One Curve ==== |

+ | MagicPlot allows two alternatives buttons to run the fit: | ||

+ | * ''Fit by Sum'' button will fit the data with the sum of Fit Curves for which the ''Sum'' checkbox is set. Data interval from ''Fit Interval'' tab will be used. This button must be used for example to fit the spectrum with the sum of peaks. | ||

+ | * ''Fit One Curve'' button will fit the data with the one currently selected Fit Curve. The individual interval for each Fit Curve will be used. Set ''Edit Interval'' checkbox to edit individual interval for each Fit Curve. | ||

+ | | ||

+ | ==== Copying and Pasting Fit Curves ==== | ||

You can copy and paste Fit Curves from one Fit Plot to another Fit Plot or Figure. You can also paste the copied Fit Curves to the same Fit Plot to create a copy. | You can copy and paste Fit Curves from one Fit Plot to another Fit Plot or Figure. You can also paste the copied Fit Curves to the same Fit Plot to create a copy. | ||

* The copy of Fit Curves with the same parameters and styles will be created if you paste Fit Curves to a Fit Plot. | * The copy of Fit Curves with the same parameters and styles will be created if you paste Fit Curves to a Fit Plot. | ||

* A link to the source Fit Curves will be inserted if you paste Fit Curves in a Figure. | * A link to the source Fit Curves will be inserted if you paste Fit Curves in a Figure. | ||

+ | |||

+ | ==== Fit Curves Reordering ==== | ||

+ | You can reorder Fit Curves by dragging them in the table. The data curve is always drawn the first and fit sum is drawn the last. | ||

===== Setting Initial Values of Parameters ===== | ===== 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 Fit Curves of predefined types (not custom equation): you can drag curves on plot. Initial parameters values for each Fit Curve can also be set in parameter table. | + | Nonlinear fitting assumes that certain initial values of parameters are set before fitting. This procedure is very easy if you use Fit Curves of predefined types (not custom equation): you can drag curves on the plot. Initial parameters values for each Fit Curve can also be set in the parameter table. |

- | You can adjust Parameters using mouse wheel (scrolling): Hold Ctrl (Cmd on Mac) key and scroll. If Shift key is also pressed the step will be increased. | + | {{:moving_curves.png?nolink|Moving curves with mouse}} |

- | {{:moving_curves.png|Moving curves with mouse}} | + | ==== Adjusting Parameters with Mouse Wheel ==== |

+ | You can adjust Parameters in the table using mouse wheel scrolling when the mouse cursor is on the desired parameter: Hold Ctrl key (Cmd key on Mac) and scroll. If the Shift key is also pressed the parameter step for one wheel 'click' will be increased. | ||

+ | | ||

+ | ===== Guessing Peaks ===== | ||

+ | If you are fitting a spectrum with multiple peaks, MagicPlot may automatically add and approximately locate peaks before fitting (Pro edition only). See [[guess_peaks]] for details. Guessed peaks should be used only as of the initial estimate for fitting: don't forget to click the Fit button after peaks are added. | ||

===== Parameter Locking ===== | ===== 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 to lock parameter. | + | You can lock (fix) parameter(s) to prevent varying this parameter(s) during the fit and to prevent its changing due to set initial values by mouse dragging (for built-in functions). Set the checkbox in ''Lock'' column in the parameter list to lock parameter. |

+ | | ||

+ | {{:parameters_table.png?nolink|Table of Parameters}} | ||

+ | | ||

+ | ===== Parameters Joining ===== | ||

+ | MagicPlot allows joining (sometimes referred to as coupling, binding, linking) of fit parameters of different Fit Curves. See [[joining]] for details. | ||

+ | | ||

+ | ===== Weighting of Data Points Using Y Errors ===== | ||

+ | MagicPlot allows the weighting of data points with Y error data. You can specify Y error data in Fit Plot properties dialog. If no Y error data are specified weighting is not used. | ||

+ | | ||

+ | Weights are calculated as ''1 / Y<sub>error</sub><sup>2</sup>'' for every point. See [[fit_formulas]] for details. | ||

- | {{:parameters_table.png|Table of Parameters}} | + | Weights must be positive and finite for all points so the Y error values must be positive and non-zero (to prevent infinite weights). MagicPlot checks this condition before fitting and shows an error message if Y errors cannot be used to compute weights. |

- | ===== Fit Intervals ===== | + | ===== Specifying 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. | + | You can set the X intervals of the data which will be used for fitting. Data points outside these intervals are not used to compute the minimizing residual sum of squares. 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: | + | Select ''Fit Interval'' tab to set intervals visually or edit accurate borders values in the table. |

- | * Double click on interval to split it | + | * Double click on the interval to split it |

* Drag the interval border to move it. If intervals intersect, they will be merged | * 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 | * Use context menu on the plot to create, delete and split intervals | ||

- | | {{:interval_context_menu2.png|Interval context menu}} | {{:interval_context_menu1.png|Interval context menu}} | | + | **Note:** Data intervals from the ''Fit Interval'' tab are used for fitting Sum only. To set individual data intervals for the one Curve fitting use ''Edit Interval'' checkbox. |

- | ===== Baseline Fitting and Extraction ===== | + | {{:interval_context_menu1.png?nolink|Fit interval context menu}} |

- | 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|spline]]. | + | ===== 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. | ||

- | 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. | + | Note that if you use data processing (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 behavior to exclude baseline from data before integrating, see [[integration]] for more information. |

===== 'Data-Baseline' Table Column ===== | ===== '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). | + | The 'Data-Baseline' column is appended to the Table with initial (X and Y) data when you create a 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 as data. |

- | | + | |

- | 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. | 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. | ||

- | ===== Viewing the Difference between Data and Fit Sum Function (Residual) ===== | + | ===== Viewing the Residual Plot ===== |

- | MagicPlot offers two different ways to view the difference between data and Fit Sum function: | + | Residual means here the difference between initial data, baseline function and Fit Sum function. MagicPlot offers two different ways to view the residual: |

- | * You can set ''Baseline'' checkboxes for all fit function components to subtract them from data and explore the residual plot | + | * Press and hold the ''Residual'' button. The residual will be shown while the button is pressed. You can use either mouse or space key (if the button is selected) to hold ''Residual'' button. |

- | or | + | * You can either set ''Baseline'' checkboxes for all summed Fit Curves to subtract them from data and explore the residual plot |

- | * You can press and hold ''Residual'' button. The difference between data and Fit Sum function is shown while button is pressed. You can use either mouse or space key (if button is selected) to hold ''Residual'' button. | + | |

- | ===== Fit One Curve ===== | + | ===== Fitting ===== |

- | 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. | + | To execute the fit click the ''Fit by Sum'' button of ''Fit One Curve'' button (see below). |

- | 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. | + | MagicPlot indicates the fit process with a special window. Fitting curves are periodically updated on the plot while fitting so you can see how fit converges. |

- | ===== Joining the Parameters of Fit Curves ===== | + | {{:fit_progress.png?nolink|Fit progress window}} |

- | MagicPlot allows coupling of fit parameters. See [[joining]] for details. | + | |

- | ===== Fit Progress Window ===== | + | MagicPlot shows the current iteration number and deviation decrement with two progress bars while the fit is performed. The fit process stops when one of these progress bars reaches the end. |

- | 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.png|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: | You can see two buttons on fit progress window: | ||

Line 104: | Line 123: | ||

* ''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. | * ''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. | ||

- | ===== Undoing Fit ===== | + | ===== Fitting One Curve ===== |

- | 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. | + | You can 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 (from ''Fit Interval'' tab) is ignored. Select ''Edit 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 do not contain signal (peaks) and contain only noise. | ||

+ | | ||

+ | {{:fit_one_curve.png?nolink|'Fit One Curve' button}} | ||

+ | | ||

+ | ===== Why My Fit is Not Converged? ===== | ||

+ | In some cases, the fit procedure may fail to find the optimal parameters values. The actual mathematical reason for this error is the impossibility to invert the matrix α calculated from partial derivatives of the fit function with respect to fit parameters. This inverted matrix is used to compute the new values of parameters for the next step of fit (like gradient descent). In most cases, this error occurs when the matrix α is ill-conditioned or nearly singular and the inverse cannot be calculated accurately enough with used floating-point arithmetic. | ||

+ | | ||

+ | === The origin of this error may be: === | ||

+ | * Fit is not converged through one or more parameters: some parameters were taking unrealistically great values during iterations. There is no local minimum of residual sum of squares near the initial values of these parameters. MagicPlot highlights the suspicious Fit Curve in this case. | ||

+ | * Mutual dependency exists between some parameters. The algorithm cannot resolve which parameter to vary. | ||

+ | * Fit function is ill-conditioned: the minimized residual sum of squares depends on some parameters much more than on other ones. | ||

+ | * Numeric overflow (or underflow) when calculating fit function with initial parameter values or on the next steps. | ||

+ | | ||

+ | === Try one of the following: === | ||

+ | * Specify more accurate initial values of parameters. | ||

+ | * Simplify the fit function (e.g. remove some peaks). | ||

+ | * Lock some parameters. | ||

===== See Also ===== | ===== See Also ===== | ||

Line 114: | Line 151: | ||

* [[guess_peaks]] | * [[guess_peaks]] | ||

* [[fit_equations]] | * [[fit_equations]] | ||

- | * [[transform_xy]] | ||

* [[interval_statistics]] | * [[interval_statistics]] | ||

+ | * [[table_from_curves]] |

fitting.1298545177.txt.gz · Last modified: Sun Nov 8 12:20:32 2015 (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International