Curvefit MATLAB Toolbox
Curvefit MATLAB Toolbox
Users Guide
Version 1
Product enhancement suggestions Bug reports Documentation error reports Order status, license renewals, passcodes Sales, pricing, and general information
508-647-7000 (Phone) 508-647-7001 (Fax) The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098
For contact information about worldwide offices, see the MathWorks Web site. Curve Fitting Toolbox Users Guide COPYRIGHT 20012006 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the governments needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc.
Trademarks
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, and xPC TargetBox are registered trademarks, and SimBiology, SimEvents, and SimHydraulics are trademarks of The MathWorks, Inc. Other product or brand names are trademarks or registered trademarks of their respective holders.
Patents
The MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information.
Revision History
July 2001 July 2002 June 2004 October 2004 March 2005 June 2005 September 2005 March 2006 September 2006
First printing Second printing Online only Online only Online only Third printing Online only Online only Online only
New for Version 1 (Release 12.1) Revised for Version 1.1 (Release 13) Revised for Version 1.1.1 (Release 14) Revised for Version 1.1.2 (Release 14SP1) Revised for Version 1.1.3 (Release 14SP2) Minor revision for Version 1.1.3 Revised for Version 1.1.4 (Release 14SP3) Revised for Version 1.1.5 (Release 2006a) Revised for Version 1.1.6 (Release 2006b)
Contents
Getting Started
1
What Is the Curve Fitting Toolbox? . . . . . . . . . . . . . . . . . . Major Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interactive and Programmatic Environments . . . . . . . . . . . Interactive Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . Opening the Curve Fitting Tool . . . . . . . . . . . . . . . . . . . . . . Importing the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interactive Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Analyzing the Fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Saving Your Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Programmatic Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . Curve Fitting Objects and Methods . . . . . . . . . . . . . . . . . . . Interactive Code Generation . . . . . . . . . . . . . . . . . . . . . . . . 1-2 1-2 1-2 1-4 1-4 1-5 1-7 1-18 1-20 1-23 1-23 1-26
2
Importing, Viewing, and Preprocessing Data . . . . . . . . . Importing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Viewing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Smoothing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Excluding and Sectioning Data . . . . . . . . . . . . . . . . . . . . . . Additional Preprocessing Steps . . . . . . . . . . . . . . . . . . . . . . Selected Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 2-2 2-6 2-9 2-26 2-40 2-43
Fitting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-44 The Fitting Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-44 Parametric Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-46 Nonparametric Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-116 Selected Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-124
3
Curve Fitting Objects and Methods . . . . . . . . . . . . . . . . . . Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Curve Fitting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Curve Fitting Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Workflow for Object-Oriented Fitting . . . . . . . . . . . . . . . . . Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interactive Code Generation . . . . . . . . . . . . . . . . . . . . . . . Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Generated M-file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running the Generated M-file . . . . . . . . . . . . . . . . . . . . . . . Understanding the Components of the Generated M-File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modifying the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 3-3 3-4 3-5 3-7 3-9 3-24 3-24 3-25 3-27 3-29 3-32
Functions By Category
4
Preprocessing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Fitting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Curve Fit Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Fit Type Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Postprocessing Fits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Information and Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 4-2 4-2 4-3 4-4 4-5
vi
Contents
5
Index
vii
viii
Contents
1
Getting Started
This chapter briefly introduces the major features of the Curve Fitting Toolbox with a single example. The example fits census data with several models, finds the best fit, and extrapolates the fit to predict future population. The data modeling process is explored interactively, using the Curve Fitting Tool, and then programmatically, using curve fitting objects and methods. What Is the Curve Fitting Toolbox? (p. 1-2) Interactive Curve Fitting (p. 1-4) Programmatic Curve Fitting (p. 1-23) Introduction to the Curve Fitting Toolbox Using the Curve Fitting Tool Using curve fitting objects and methods
Getting Started
Major Features
The Curve Fitting Toolbox is a collection of graphical user interfaces (GUIs) and M-file functions that operate in the MATLAB technical computing environment. The toolbox supplements MATLAB with these main features: Data preprocessing capabilities, such as sectioning, excluding data, and smoothing Data fitting using parametric and nonparametric models:
The toolbox includes a library of parametric models, with polynomials, exponentials, rationals, sums of Gaussians, Fourier polynomials, and many others You can also define custom models to precisely reflect the goals of your data analysis Nonparametric models are available through a variety of smoothers and interpolants
Fitting methods for linear least squares, nonlinear least squares, weighted least squares, constrained least squares, and robust fitting are available Data and fit statistics to assist you in analyzing your models Postprocessing capabilities that allow you to interpolate, extrapolate, differentiate, and integrate the fit The ability to save your work in various formats, including workspace variables, binary files, and automatically generated MATLAB code
1-2
An interactive environment, the Curve Fitting Tool, which is composed of multiple graphical user interfaces A programmatic environment that allows you to write object-oriented MATLAB code using curve fitting methods To open the Curve Fitting Tool, type
cftool
To list the functions in the Curve Fitting Toolbox for use in MATLAB programming, type
help curvefit
The code for any function can be opened in the MATLAB Editor by typing
edit function_name
You can change the way any toolbox function works by copying and renaming its M-file, examining your copy in the editor, and then modifying it. You can also extend the toolbox by adding your own M-files, or by using your code in combination with functions from other toolboxes, such as the Statistics Toolbox or the Optimization Toolbox.
1-3
Getting Started
Importing, viewing, and smoothing data Fitting data, and comparing fits and data sets Marking data points to be excluded from a fit Selecting which fits and data sets are displayed in the tool Interpolating, extrapolating, differentiating, or integrating fits
You open the Curve Fitting Tool with the cftool command.
cftool
1-4
The workspace now contains two new variables, cdate and pop: cdate is a column vector containing the years 1790 to 1990 in 10-year increments. pop is a column vector with the US population figures that correspond to the years in cdate. You can import data into the Curve Fitting Tool with the Data GUI. You open this GUI by clicking the Data button on the Curve Fitting Tool. As shown below, the Data GUI consists of two panes: Data sets and Smooth. The Data Sets pane allows you to
1-5
Getting Started
Import predictor (X) data, response (Y) data, and weights. If you do not import weights, then they are assumed to be 1 for all data points. Specify the name of the data set. Preview the data. To load cdate and pop into the Curve Fitting Tool, select the appropriate variable names from the X Data and Y Data lists. The data is then displayed in the Preview window. Click the Create data set button to complete the data import process.
The Smooth pane is described in Importing, Viewing, and Preprocessing Data on page 2-2.
1-6
Interactive Fitting
You fit data with the Fitting GUI. You open this GUI by clicking the Fitting button on the Curve Fitting Tool. The Fitting GUI consists of two parts: the Fit Editor and the Table of Fits. The Fit Editor allows you to Specify the fit name, the current data set, and the exclusion rule. Explore various fits to the current data set using a library or custom equation, a smoothing spline, or an interpolant. Override the default fit options such as the coefficient starting values. Compare fit results including the fitted coefficients and goodness of fit statistics. The Table of Fits allows you to Keep track of all the fits and their data sets for the current session. Display a summary of the fit results. Save or delete the fit results.
Note that this action always defaults to a linear polynomial fit type. You use New Fit at the beginning of your curve fitting session, and when you are exploring different fit types for a given data set.
2 Because the initial fit uses a second degree polynomial, select quadratic
library model, fitted coefficients, and goodness of fit statistics are displayed in the Results area.
1-7
Getting Started
For fits of a given type (for example, polynomials), you should use Copy Fit instead of New Fit because copying a fit retains the current fit type state thereby requiring fewer steps than creating a new fit each time.
1-8
The Fitting GUI is shown below with the results of fitting the census data with a quadratic polynomial.
1-9
Getting Started
The data, fit, and residuals are shown below. You display the residuals as a line plot by selecting the menu item View > Residuals > Line plot from the Curve Fitting Tool.
The residuals indicate that a better fit may be possible. Therefore, you should continue fitting the census data following the procedure outlined in the beginning of this section. The residuals from a good fit should look random with no apparent pattern. A pattern, such as a tendency for consecutive residuals to have the same sign, can be an indication that a better model exists. When you fit higher degree polynomials, the Results area displays this warning:
Equation is badly conditioned. Remove repeated data points
1-10
The warning arises because the fitting procedure uses the cdate values as the basis for a matrix with very large values. The spread of the cdate values results in scaling problems. To address this problem, you can normalize the cdate data. Normalization is a process of scaling the predictor data to improve the accuracy of the subsequent numeric computations. A way to normalize cdate is to center it at zero mean and scale it to unit standard deviation.
(cdate - mean(cdate))./std(cdate)
To normalize data with the Curve Fitting Tool, select the Center and scale X data check box.
Note Because the predictor data changes after normalizing, the values of the fitted coefficients also change when compared to the original data. However, the functional form of the data and the resulting goodness of fit statistics do not change. Additionally, the data is displayed in the Curve Fitting Tool using the original scale.
1-11
Getting Started
Use the Plotting GUI to remove exp1 from the scatter plot display.
1-12
Because the goal of fitting the census data is to extrapolate the best fit to predict future population values, you should explore the behavior of the fits up to the year 2050. You can change the axes limits of the Curve Fitting Tool by selecting the menu item Tools > Axes Limit Control. The census data and fits are shown below for an upper abscissa limit of 2050. The behavior of the sixth degree polynomial fit beyond the data range makes it a poor choice for extrapolation.
As you can see, you should exercise caution when extrapolating with polynomial fits because they can diverge wildly outside the data range.
1-13
Getting Started
Examining the Numerical Fit Results. Because you can no longer eliminate fits by examining them graphically, you should examine the numerical fit results. There are two types of numerical fit results displayed in the Fitting GUI: goodness of fit statistics and confidence intervals on the fitted coefficients. The goodness of fit statistics help you determine how well the curve fits the data. The confidence intervals on the coefficients determine their accuracy. Some goodness of fit statistics are displayed in the Results area of the Fit Editor for a single fit. All goodness of fit statistics are displayed in the Table of Fits for all fits, which allows for easy comparison. In this example, the sum of squares due to error (SSE) and the adjusted R-square statistics are used to help determine the best fit. As described in Goodness-of-Fit Statistics on page 2-74, the SSE statistic is the least squares error of the fit, with a value closer to zero indicating a better fit. The adjusted R-square statistic is generally the best indicator of the fit quality when you add additional coefficients to your model. You can modify the information displayed in the Table of Fits with the Table Options GUI. You open this GUI by clicking the Table options button on the Fitting GUI. As shown below, select the adjusted R-square statistic and clear the R-square statistic.
The numerical fit results are shown below. You can click the Table of Fits column headings to sort by statistics results.
1-14
The SSE for exp1 indicates it is a poor fit, which was already determined by examining the fit and residuals. The lowest SSE value is associated with poly6. However, the behavior of this fit beyond the data range makes it a poor choice for extrapolation. The next best SSE value is associated with the fifth degree polynomial fit, poly5, suggesting it may be the best fit. However, the SSE and adjusted R-square values for the remaining polynomial fits are all very close to each other. Which one should you choose?
1-15
Getting Started
To resolve this issue, examine the confidence bounds for the remaining fits. By default, 95% confidence bounds are calculated. You can change this level by selecting the menu item View > Confidence Level from the Curve Fitting Tool. The p1, p2, and p3 coefficients for the fifth degree polynomial suggest that it overfits the census data. However, the confidence bounds for the quadratic fit, poly2, indicate that the fitted coefficients are known fairly accurately. Therefore, after examining both the graphical and numerical fit results, it appears that you should use poly2 to extrapolate the census data.
Note The fitted coefficients associated with the constant, linear, and quadratic terms are nearly identical for each polynomial equation. However, as the polynomial degree increases, the coefficient bounds associated with the higher degree terms increase, which suggests overfitting.
For more information about confidence bounds, refer to Confidence and Prediction Bounds on page 2-77.
1-16
Name fittedmodel1
Size 1x1
Bytes 6178
The cfit object display includes the model, the fitted coefficients, and the confidence bounds for the fitted coefficients.
fittedmodel1 fittedmodel1 = Linear model Poly2: fittedmodel1(x) = p1*x^2 + p2*x + p3 Coefficients (with 95% confidence bounds): p1 = 0.006541 (0.006124, 0.006958) p2 = -23.51 (-25.09, -21.93) p3 = 2.113e+004 (1.964e+004, 2.262e+004)
The output1 structure contains additional information associated with the fit.
output1 output1 = numobs: numparam: residuals: Jacobian: exitflag: algorithm:
1-17
Getting Started
The extrapolated values and the census data set are displayed together in a new figure window.
1-18
1-19
Getting Started
1-20
The session is stored in binary form in a cfit file, and contains this information: All data sets and associated fits The state of the Fitting GUI, including Table of Fits entries and exclusion rules The state of the Plotting GUI To avoid saving unwanted data sets, you should delete them from the Curve Fitting Tool. You delete data sets using the Data Sets pane of the Data GUI. If there are fits associated with the unwanted data sets, they are deleted as well. You can load a saved session by selecting the menu item File > Load Session from the Curve Fitting Tool. When the session is loaded, the saved state of the Curve Fitting Tool display is reproduced, and may display the data, fits, residuals, and so on. If you open the Fitting GUI, then the loaded fits are displayed in the Table of Fits. Select a fit from this table to continue your curve fitting session.
Generate an M-File
You may want to generate an M-file that captures your work, so that you can continue your analysis outside of the Curve Fitting Tool. The M-file can be used without modification, or it can be edited as needed.
1-21
Getting Started
To generate an M-file from a session in the Curve Fitting Tool, select the menu item File > Generate M-file. The M-file captures the following information from the Curve Fitting Tool: Names of variables, fits, and residuals Fit options, such as whether the data should be normalized, initial values for the coefficients, and the fitting method Curve fitting objects and methods used to create the fit You can recreate your Curve Fitting Tool session by calling the M-file from the command line with your original data as input arguments. You can also call the M-file with new data, and automate the process of fitting multiple data sets. For more information on working with a generated M-file, see Interactive Code Generation on page 1-26.
1-22
1-23
Getting Started
Create a fit options structure and a fittype object for the custom nonlinear model y = a(xb)n, where a and b are coefficients and n is a problem-dependent parameter:
s = fitoptions('Method','NonlinearLeastSquares',... 'Lower',[0,0],... 'Upper',[Inf,max(cdate)],... 'Startpoint',[1 1]); f = fittype('a*(x-b)^n','problem','n','options',s);
1-24
1-25
Getting Started
1-26
% % % % %
or with different data. You may want to edit the function to customize the code and this help message. Number of datasets: Number of fits: 1 1
% Data from dataset "census": % X = cdate: % Y = pop: % Unweighted % % This function was automatically generated on 22-Jul-2006 10:09:39 % Set up figure to receive datasets and fits f_ = clf; figure(f_); set(f_,'Units','Pixels','Position',[183.6 68.1 814.4 571.8]); legh_ = []; legt_ = {}; % handles and text for legend xlim_ = [Inf -Inf]; % limits of x axis ax_ = axes; set(ax_,'Units','normalized','OuterPosition',[0 0 1 1]); set(ax_,'Box','on'); axes(ax_); hold on;
% --- Plot data originally in dataset "census" cdate = cdate(:); pop = pop(:); h_ = line(cdate,pop,'Parent',ax_,'Color',[0.333333 0 0.666667],... 'LineStyle','none', 'LineWidth',1,... 'Marker','.', 'MarkerSize',12); xlim_(1) = min(xlim_(1),min(cdate)); xlim_(2) = max(xlim_(2),max(cdate)); legh_(end+1) = h_; legt_{end+1} = 'census'; % Nudge axis limits beyond data limits if all(isfinite(xlim_)) xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
1-27
Getting Started
set(ax_,'XLim',xlim_) end
% --- Create fit "censusfit" fo_ = fitoptions('method','NonlinearLeastSquares','Lower',[0 0 ]); ok_ = ~(isnan(cdate) | isnan(pop)); st_ = [1 1 ]; set(fo_,'Startpoint',st_); ft_ = fittype('a*(x-b)^3',... 'dependent',{'y'},'independent',{'x'},... 'coefficients',{'a', 'b'}); % Fit this model using new data cf_ = fit(cdate(ok_),pop(ok_),ft_,fo_); % Or use coefficients from the original fit: if 0 cv_ = {1.359437793879e-005, 1724.696932124}; cf_ = cfit(ft_,cv_{:}); end % Plot this fit h_ = plot(cf_,'fit',0.95); legend off; % turn off legend from plot method call set(h_(1),'Color',[1 0 0],... 'LineStyle','-', 'LineWidth',2,... 'Marker','none', 'MarkerSize',6); legh_(end+1) = h_(1); legt_{end+1} = 'censusfit'; % Done plotting data and fits. Now finish up loose ends. hold off; h_ = legend(ax_,legh_,legt_,'Location','NorthEast'); set(h_,'Interpreter','none'); xlabel(ax_,''); % remove x label ylabel(ax_,''); % remove y label
1-28
A quick look through the code shows that it has automatically assembled for you many of the curve fitting methods from the Curve Fitting Toolbox, such as fitoptions, fittype, fit, and plot. A natural modification of the M-file would be to edit the function declaration at the top of the file to return the cfit object created by the fit, as follows:
function cf_ = myfit(cdate,pop)
You might also modify the code to produce a variety of different plots, or to return goodness-of-fit statistics. Coding with curve fitting objects and methods is given complete treatment in Chapter 3, Programmatic Curve Fitting.
1-29
Getting Started
1-30
2
Interactive Curve Fitting
Importing, Viewing, and Preprocessing Data (p. 2-2) Fitting Data (p. 2-44) Preparing data for a fit Parametric and nonparametric fitting methods
Viewing Data (p. 2-6) Smoothing Data (p. 2-9) Excluding and Sectioning Data (p. 2-26) Additional Preprocessing Steps (p. 2-40) Selected Bibliography (p. 2-43)
Importing Data
You import data sets into the Curve Fitting Tool with the Data Sets pane of the Data GUI. Using this pane, you can Select workspace variables that compose a data set Display a list of all imported data sets View, delete, or rename one or more data sets The Data Sets pane is shown below followed by a description of its features.
2-2
X data Select the predictor data. Y data Select the response data. Weights Select the weights associated with the response data. If weights are not imported, they are assumed to be 1 for all data points.
Preview The selected workspace vectors are displayed graphically in the preview window. Weights are not displayed. Data set name The name of the imported data set. The toolbox automatically creates a unique name for each imported data set. You can change the name by editing this field. Click the Create data set button to complete the data import process.
2-3
Click View to open the View Data Set GUI. Using this GUI, you can view a single data set both graphically and numerically. Additionally, you can display data points to be excluded in a fit by selecting an exclusion rule. Click Rename to change the name of a single data set. Click Delete to delete one or more data sets. To select multiple data sets, you can use the Ctrl key and the mouse to select data sets one by one, or you can use the Shift key and the mouse to select a range of data sets.
The workspace contains two new variables, pressure and month: pressure is the monthly averaged atmospheric pressure differences between Easter Island and Darwin, Australia. This difference drives the trade winds in the southern hemisphere. month is the relative time in months. Alternatively, you can import data by specifying the variable names as arguments to the cftool function.
cftool(month,pressure)
In this case, the Data GUI is not opened. Data Import Process. The data import process is described below:
1 Select workspace variables.
2-4
The predictor and response data are displayed graphically in the Preview window. Weights and data points containing Infs or NaNs are not displayed.
2 Specify the data set name.
You should specify a meaningful name when you import multiple data sets. If you do not specify a name, the default name, which is constructed from the selected variable names, is used.
3 Click the Create data set button.
The Data sets list box displays all the data sets added to the toolbox. Note that you can construct data sets from workspace variables, or by smoothing an existing data set. If your data contains Infs or complex values, a warning message such as the message shown below is displayed.
After you click the Create data set window. The Data Sets pane shown below displays the imported ENSO data in the Preview button, the data set enso is added to the Data sets list box. You can then view, rename, or delete enso by selecting it in the list box and clicking the appropriate button.
2-5
Viewing Data
The Curve Fitting Toolbox provides two ways to view imported data: Graphically in a scatter plot Numerically in a table
2-6
determine whether you should fit the data with a straight line, a periodic function, a sum of Gaussians, and so on. Enhancing the Graphical Display. The Curve Fitting Toolbox provides several tools for enhancing the graphical display of a data set. These tools are available through the Tools menu, the GUI toolbar, and right-click menus. You can zoom in or out, turn on or off the grid, and so on using the Tools menu and the GUI toolbar shown below.
Tools Menu
GUI Toolbar
You can change the color, line width, line style, and marker type of the displayed data points using the right-click menu shown below. You activate this menu by placing your mouse over a data point and right-clicking. Note that a similar menu is available for fitted curves.
2-7
The ENSO data is shown below after the display has been enhanced using several of these tools.
Display the legend for the ENSO data set. Display data tips using MATLABs click functionality. Change the color, marker type and line style for the data. Display the grid.
2-8
The View Data Set GUI for the ENSO data set is shown below, followed by a description of its features.
Data set Lists the names of the viewed data set and the associated variables. The data is displayed graphically below this list. The index, predictor data (X), response data (Y), and weights (if imported) are displayed numerically in the table. If the data contains Infs or NaNs, those values are labeled ignored. If the data contains complex numbers, only the real part is displayed. Exclusion rules Lists all the exclusion rules that are compatible with the viewed data set. When you select an exclusion rule, the data points marked for exclusion are grayed in the table, and are identified with an x in the graphical display. To exclude the data points while fitting, you must create the exclusion rule in the Exclude GUI and select the exclusion rule in the Fitting GUI. An exclusion rule is compatible with the viewed data set if their lengths are the same, or if it is created by sectioning only.
Smoothing Data
If your data is noisy, you might need to apply a smoothing algorithm to expose its features, and to provide a reasonable starting approach for parametric fitting. The two basic assumptions that underlie smoothing are
2-9
The relationship between the response data and the predictor data is smooth. The smoothing process results in a smoothed value that is a better estimate of the original value because the noise has been reduced. The smoothing process attempts to estimate the average of the distribution of each response value. The estimation is based on a specified number of neighboring response values. You can think of smoothing as a local fit because a new response value is created for each original response value. Therefore, smoothing is similar to some of the nonparametric fit types supported by the toolbox, such as smoothing spline and cubic interpolation. However, this type of fitting is not the same as parametric fitting, which results in a global parameterization of the data.
Note You should not fit data with a parametric model after smoothing, because the act of smoothing invalidates the assumption that the errors are normally distributed. Instead, you should consider smoothing to be a data exploration technique.
There are two common types of smoothing methods: filtering (averaging) and local regression. Each smoothing method requires a span. The span defines a window of neighboring points to include in the smoothing calculation for each data point. This window moves across the data set as the smoothed response value is calculated for each predictor value. A large span increases the smoothness but decreases the resolution of the smoothed data set, while a small span decreases the smoothness but increases the resolution of the smoothed data set. The optimal span value depends on your data set and the smoothing method, and usually requires some experimentation to find. The Curve Fitting Toolbox supports these smoothing methods: Moving average filtering Lowpass filter that takes the average of neighboring data points. Lowess and loess Locally weighted scatter plot smooth. These methods use linear least squares fitting, and a first-degree polynomial (lowess) or a
2-10
second-degree polynomial (loess). Robust lowess and loess methods that are resistant to outliers are also available. Savitzky-Golay filtering A generalized moving average where you derive the filter coefficients by performing an unweighted linear least squares fit using a polynomial of the specified degree. Note that you can also smooth data using a smoothing spline. Refer to Nonparametric Fitting on page 2-116 for more information. You smooth data with the Smooth pane of the Data GUI. The pane is shown below followed by a description of its features.
Data Sets
Original data set Select the data set you want to smooth.
2-11
Smoothed data set Specify the name of the smoothed data set. Note that the process of smoothing the original data set always produces a new data set containing smoothed response values.
Moving average Filter the data by calculating an average. Lowess Locally weighted scatter plot smooth using linear least squares fitting and a first-degree polynomial. Loess Locally weighted scatter plot smooth using linear least squares fitting and a second-degree polynomial. Savitzky-Golay Filter the data with an unweighted linear least squares fit using a polynomial of the specified degree. Robust Lowess Lowess method that is resistant to outliers. Robust Loess Loess method that is resistant to outliers.
Span The number of data points used to compute each smoothed value. For the moving average and Savitzky-Golay methods, the span must be odd. For all locally weighted smoothing methods, if the span is less than 1, it is interpreted as the percentage of the total number of data points. Degree The degree of the polynomial used in the Savitzky-Golay method. The degree must be smaller than the span.
Click View to open the View Data Set GUI. Using this GUI, you can view a single data set both graphically and numerically. Additionally, you can display data points to be excluded in a fit by selecting an exclusion rule.
2-12
Click Rename to change the name of a single data set. Click Delete to delete one or more data sets. To select multiple data sets, you can use the Ctrl key and the mouse to select data sets one by one, or you can use the Shift key and the mouse to select a range of data sets. Click Save to workspace to save a single data set to a structure.
where ys(i) is the smoothed value for the ith data point, N is the number of neighboring data points on either side of ys(i), and 2N+1 is the span. The moving average smoothing method used by the Curve Fitting Toolbox follows these rules: The span must be odd. The data point to be smoothed must be at the center of the span. The span is adjusted for data points that cannot accommodate the specified number of neighbors on either side. The end points are not smoothed because a span cannot be defined. Note that you can use filter function to implement difference equations such as the one shown above. However, because of the way that the end points are treated, the toolbox moving average result will differ from the result returned by filter. Refer to Difference Equations and Filtering in the MATLAB documentation for more information. For example, suppose you smooth data using a moving average filter with a span of 5. Using the rules described above, the first four elements of ys are given by
2-13
= = = =
Note that ys(1), ys(2), ... ,ys(end) refer to the order of the data after sorting, and not necessarily the original order. The smoothed values and spans for the first four data points of a generated data set are shown below.
Plot (a) indicates that the first data point is not smoothed because a span cannot be constructed. Plot (b) indicates that the second data point is smoothed using a span of three. Plots (c) and (d) indicate that a span of five is used to calculate the smoothed value.
2-14
x is the predictor value associated with the response value to be smoothed, xi are the nearest neighbors of x as defined by the span, and d(x) is the distance along the abscissa from x to the most distant predictor value within the span. The weights have these characteristics:
2-15
The data point to be smoothed has the largest weight and the most influence on the fit. Data points outside the span have zero weight and no influence on the fit.
2 A weighted linear least squares regression is performed. For lowess, the
regression uses a first degree polynomial. For loess, the regression uses a second degree polynomial.
3 The smoothed value is given by the weighted regression at the predictor
value of interest. If the smooth calculation involves the same number of neighboring data points on either side of the smoothed data point, the weight function is symmetric. However, if the number of neighboring points is not symmetric about the smoothed data point, then the weight function is not symmetric. Note that unlike the moving average smoothing process, the span never changes. For example, when you smooth the data point with the smallest predictor value, the shape of the weight function is truncated by one half, the leftmost data point in the span has the largest weight, and all the neighboring points are to the right of the smoothed value.
2-16
The weight function for an end point and for an interior point is shown below for a span of 31 data points.
Using the lowess method with a span of five, the smoothed values and associated regressions for the first four data points of a generated data set are shown below.
2-17
Notice that the span does not change as the smoothing process progresses from data point to data point. However, depending on the number of nearest neighbors, the regression weight function might not be symmetric about the data point to be smoothed. In particular, plots (a) and (b) use an asymmetric weight function, while plots (c) and (d) use a symmetric weight function. For the loess method, the graphs would look the same except the smoothed value would be generated by a second-degree polynomial.
2-18
Robust Smoothing Procedure. If your data contains outliers, the smoothed values can become distorted, and not reflect the behavior of the bulk of the neighboring data points. To overcome this problem, you can smooth the data using a robust procedure that is not influenced by a small fraction of outliers. For a description of outliers, refer to Marking Outliers on page 2-28. The Curve Fitting Toolbox provides a robust version for both the lowess and loess smoothing methods. These robust methods include an additional calculation of robust weights, which is resistant to outliers. The robust smoothing procedure follows these steps:
1 Calculate the residuals from the smoothing procedure described in the
previous section.
2 Compute the robust weights for each data point in the span. The weights
ri is the residual of the ith data point produced by the regression smoothing procedure, and MAD is the median absolute deviation of the residuals:
The median absolute deviation is a measure of how spread out the residuals are. If ri is small compared to 6MAD, then the robust weight is close to 1. If ri is greater than 6MAD, the robust weight is 0 and the associated data point is excluded from the smooth calculation.
3 Smooth the data again using the robust weights. The final smoothed value
is calculated using both the local regression weight and the robust weight.
4 Repeat the previous two steps for a total of five iterations.
The smoothing results of the lowess procedure are compared below to the results of the robust lowess procedure for a generated data set that contains a single outlier. The span for both procedures is 11 data points.
2-19
Plot (a) shows that the outlier influences the smoothed value for several nearest neighbors. Plot (b) suggests that the residual of the outlier is greater than six median absolute deviations. Therefore, the robust weight is zero for this data point. Plot (c) shows that the smoothed values neighboring the outlier reflect the bulk of the data.
Savitzky-Golay Filtering
Savitzky-Golay filtering can be thought of as a generalized moving average. You derive the filter coefficients by performing an unweighted linear least squares fit using a polynomial of a given degree. For this reason, a Savitzky-Golay filter is also called a digital smoothing polynomial filter or a least squares smoothing filter. Note that a higher degree polynomial makes it possible to achieve a high level of smoothing without attenuation of data features.
2-20
The Savitzky-Golay filtering method is often used with frequency data or with spectroscopic (peak) data. For frequency data, the method is effective at preserving the high-frequency components of the signal. For spectroscopic data, the method is effective at preserving higher moments of the peak such as the line width. By comparison, the moving average filter tends to filter out a significant portion of the signals high-frequency content, and it can only preserve the lower moments of a peak such as the centroid. However, Savitzky-Golay filtering can be less successful than a moving average filter at rejecting noise. The Savitzky-Golay smoothing method used by the Curve Fitting Toolbox follows these rules: The span must be odd. The polynomial degree must be less than the span. The data points are not required to have uniform spacing. Normally, Savitzky-Golay filtering requires uniform spacing of the predictor data. However, the algorithm provided by the Curve Fitting Toolbox supports nonuniform spacing. Therefore, you are not required to perform an additional filtering step to create data with uniform spacing. The plot shown below displays generated Gaussian data and several attempts at smoothing using the Savitzky-Golay method. The data is very noisy and the peak widths vary from broad to narrow. The span is equal to 5% of the number of data points.
2-21
Plot (a) shows the noisy data. To more easily compare the smoothed results, plots (b) and (c) show the data without the added noise. Plot (b) shows the result of smoothing with a quadratic polynomial. Notice that the method performs poorly for the narrow peaks. Plot (c) shows the result of smoothing with a quartic polynomial. In general, higher degree polynomials can more accurately capture the heights and widths of narrow peaks, but can do poorly at smoothing wider peaks.
2-22
Because the data appears noisy, smoothing might help uncover its structure.
2-23
The Smooth pane shown below displays all the new data sets generated by smoothing the original ENSO data set. Whenever you smooth a data set, a new data set of smoothed values is created. The smoothed data sets are automatically displayed in the Curve Fitting Tool. You can also display a single data set graphically and numerically by clicking the View button.
A new data set composed of smoothed values is created from the original data set.
The View Data Set GUI displays the selected data set graphically and numerically.
2-24
Use the Plotting GUI to display only the data sets of interest. As shown below, the periodic structure of the ENSO data set becomes apparent when it is smoothed using a moving average filter with the default span. Not surprisingly, the uncovered structure is periodic, which suggests that a reasonable parametric model should include trigonometric functions.
Display only the data set created with the moving average method.
Refer to Example: Custom Model (Fourier Series) on page 2-99 for an example that fits the ENSO data using a sum of sine and cosine functions.
2-25
Saving the Results. By clicking the Save to workspace button, you can save a smoothed data set as a structure to the MATLAB workspace. This example saves the moving average results contained in the enso (ma) data set.
The saved structure contains the original predictor data x and the smoothed data y.
smootheddata1 smootheddata1 = x: [168x1 double] y: [168x1 double]
2-26
To exclude data while fitting, you use the Fitting GUI to associate the appropriate exclusion rule with the data set to be fit. Refer to Example: Robust Fitting on page 2-109 for more information about fitting a data set using an exclusion rule. You mark data to be excluded from a fit with the Exclude GUI, which you open from the Curve Fitting Tool. The GUI is shown below followed by a description of its features.
Exclusion Rule
Exclusion rule name Specify the name of the exclusion rule that identifies the data points to be excluded from subsequent fits. Existing exclusion rules Lists the names of all exclusion rules created during the current session. When you select an existing exclusion rule, you can perform these actions:
Click Copy to copy the exclusion rule. The exclusions associated with the original exclusion rule are recreated in the GUI. You can modify these exclusions and then click Create exclusion rule to save them to the copied rule. Click Rename to change the name of the exclusion rule.
2-27
Click Delete to delete the exclusion rule. To select multiple exclusion rules, you can use the Ctrl key and the mouse to select exclusion rules one by one, or you can use the Shift key and the mouse to select a range of exclusion rules. Click View to display the exclusion rule graphically. If a data set is associated with the exclusion rule, the data is also displayed.
Exclude X Specify beginning and ending intervals in the predictor data to be excluded. Exclude Y Specify beginning and ending intervals in the response data to be excluded.
Marking Outliers
Outliers are defined as individual data points that you exclude from a fit because they are inconsistent with the statistical nature of the bulk of the data, and will adversely affect the fit results. Outliers are often readily identified by a scatter plot of response data versus predictor data. Marking outliers with the Curve Fitting Toolbox follows these rules:
2-28
You must specify a data set before creating an exclusion rule. In general, you should use the exclusion rule only with the specific data set it was based on. However, the toolbox does not prevent you from using the exclusion rule with another data set provided the size is the same. Using the Exclude GUI, you can exclude outliers either graphically or numerically. As described in Parametric Fitting on page 2-46, one of the basic assumptions underlying curve fitting is that the data is statistical in nature and is described by a particular distribution, which is often assumed to be Gaussian. The statistical nature of the data implies that it contains random variations along with a deterministic component. data = deterministic component + random component However, your data set might contain one or more data points that are nonstatistical in nature, or are described by a different statistical distribution. These data points might be easy to identify, or they might be buried in the data and difficult to identify. A nonstatistical process can involve the measurement of a physical variable such as temperature or voltage in which the random variation is negligible compared to the systematic errors. For example, if your sensor calibration is inaccurate, the data measured with that sensor will be systematically inaccurate. In some cases, you might be able to quantify this nonstatistical data component and correct the data accordingly. However, if the scatter plot reveals that a handful of response values are far removed from neighboring response values, these data points are considered outliers and should be excluded from the fit. Outliers are usually difficult to explain away. For example, it might be that your sensor experienced a power surge or someone wrote down the wrong number in a log book. If you decide there is justification, you should mark outliers to be excluded from subsequent fits particularly parametric fits. Removing these data points can have a dramatic effect on the fit results because the fitting process minimizes the square of the residuals. If you do not exclude outliers, the resulting fit will be poor for a large portion of your data. Conversely, if you do exclude the outliers and choose the appropriate model, the fit results should be reasonable.
2-29
Because outliers can have a significant effect on a fit, they are considered influential data. However, not all influential data points are outliers. For example, your data set can contain valid data points that are far removed from the rest of the data. The data is valid because it is well described by the model used in the fit. The data is influential because its exclusion will dramatically affect the fit results. Two types of influential data points are shown below for generated data. Also shown are cubic polynomial fits and a robust fit that is resistant to outliers.
Plot (a) shows that the two influential data points are outliers and adversely affect the fit. Plot (b) shows that the two influential data points are consistent with the model and do not adversely affect the fit. Plot (c) shows that a robust fitting procedure is an acceptable alternative to marking outliers for exclusion. Robust fitting is described in Robust Least Squares on page 2-55.
2-30
Sectioning
Sectioning involves specifying response or predictor data to exclude. You might want to section a data set because different parts of the data set are described by different models or are corrupted by noise, large systematic errors, and so on. Sectioning data with the Curve Fitting Toolbox follows these rules: If you are only sectioning data and not excluding individual data points, then you can create an exclusion rule without specifying a data set name. You can associate an exclusion rule with any data set provided that the exclusion rule overlaps with the data. This is useful if you have multiple data sets from which you want to exclude data points using the same rule. Use the Exclude GUI to create the exclusion rule. You can exclude vertical strips at the edges of the data, horizontal strips at the edges of the data, or a border around the data. Refer to Example: Excluding and Sectioning Data on page 2-33 for an example. To exclude multiple sections of data, you can use the excludedata function from the MATLAB command line.
2-31
Two examples of sectioning by domain are shown below for generated data.
The upper shows the data set sectioned by fit type. The section to the left of 4 is fit with a linear polynomial, as shown by the bold, dashed line. The section to the right of 4 is fit with a cubic polynomial, as shown by the bold, solid line. The lower plot shows the data set sectioned by fit type and by valid data. Here, the rightmost section is not part of any fit because the data is corrupted by noise.
Note For illustrative purposes, the preceding figures have been enhanced to show portions of the curves with bold markers. The Curve Fitting Toolbox does not use bold markers in plots.
2-32
Import the variables month and yy as the new data set enso1, and open the Exclude GUI. Assume that the first and last eight months of the data set are unreliable, and should be excluded from subsequent fits. The simplest way to exclude these data points is to section the predictor data. To do this, specify the data you want to exclude in the Exclude Sections field of the Exclude GUI.
There are two ways to exclude individual data points: using the Check to exclude point table or graphically. For this example, the simplest way to exclude the outliers is graphically. To do this, select the data set name and click the Exclude graphically button, which opens the Select Points for Exclusion Rule GUI.
2-33
To mark data points for exclusion in the GUI, place the mouse cursor over the data point and left-click. The excluded data point is marked with a red x. To include an excluded data point, right-click the data point or select the Includes Them radio button and left-click. Included data points are marked with a blue circle. To select multiple data points, click the left mouse button and drag the selection rubber band so that the rubber band box encompasses the desired data points. Note that the GUI identifies sectioned data with gray strips. You cannot graphically include sectioned data. As shown below, the first and last eight months of data are excluded from the data set by sectioning, and the two outliers are excluded graphically. Note that the graphically excluded data points are identified in the Check to exclude point table. If you decide to include an excluded data point using the table, the graph is automatically updated.
If there are fits associated with the data, you can exclude data points based on the residuals of the fit by selecting the residual data in the Y list.
2-34
To save the exclusion rule, click the Create exclusion rule button. To exclude the data from a fit, you must select the exclusion rule from the Fitting GUI. Because the exclusion rule created in this example uses individually excluded data points, you can use it only with data sets that are the same size as the ENSO data set. Viewing the Exclusion Rule. To view the exclusion rule, select an existing exclusion rule name and click the View button.
2-35
The View Exclusion Rule GUI shown below displays the modified ENSO data set and the excluded data points, which are grayed in the table.
2-36
Import the variables t and noisysine, and fit the data with a single-term sine equation. The Fitting GUI, Fit Options GUI, and Curve Fitting Tool are shown below. To display the fit starting values, click the Fit options button. Note that the amplitude starting point is reasonably close to the expected value, but the frequency and phase constant are not, which produces a poor fit.
2-37
1 Create an exclusion rule that includes one or two periods, and excludes
the remaining data. As shown below, an exclusion rule is created graphically by using the selection rubber band to exclude all data points outside the first period. The exclusion rule is named 1Period.
2 Create a new fit using the single-term sine equation with the exclusion
2-38
The fit looks reasonable throughout the entire data set. However, because the global fit was based on a small fraction of data, goodness of fit statistics will not provide much insight into the fit quality.
3 Fit the entire data set using the fitted coefficient values from the previous
2-39
The Fitting GUI, Fit Options GUI, and Curve Fitting Tool are shown below. Both the numerical and graphical fit results indicate a reasonable fit.
2-40
Note You must transform variables at the MATLAB command line, and then import those variables into the Curve Fitting Toolbox. You cannot transform variables using any of the graphical user interfaces.
For example, suppose you want to use the following model to fit your data.
If you decide to use the power transform y-1, then the transformed model is given by
You can now use linear least squares fitting procedures. There are several disadvantages associated with performing transformations: For the log transformation, negative response values cannot be processed.
2-41
For all transformations, the basic assumption that the residual variance is constant is violated. To avoid this problem, you could plot the residuals on the transformed scale. For the power transformation shown above, the transformed scale is given by the residuals
Note that the residual plot associated with the Curve Fitting Tool does not support transformed scales. Deciding on a particular transformation is not always obvious. However, a scatter plot will often reveal the best form to use. In practice you can experiment with various transforms and then plot the residuals from the command line using the transformed scale. If the errors are reasonable (they appear random with minimal scatter, and dont exhibit any systematic behavior), the transform is a good candidate.
To remove NaNs, you can use the isnan function. For examples that remove NaNs and outliers from a data set, refer to Removing and Interpolating Missing Values in the MATLAB documentation.
2-42
Selected Bibliography
[1] Cleveland, W.S., Robust Locally Weighted Regression and Smoothing Scatterplots, Journal of the American Statistical Association, Vol. 74, pp. 829-836, 1979. [2] Cleveland, W.S. and S.J. Devlin, Locally Weighted Regression: An Approach to Regression Analysis by Local Fitting, Journal of the American Statistical Association, Vol. 83, pp. 596-610, 1988. [3] Chambers, J., W.S. Cleveland, B. Kleiner, and P. Tukey, Graphical Methods for Data Analysis, Wadsworth International Group, Belmont, CA, 1983. [4] Press, W.H., S.A. Teukolsky, W.T. Vetterling, and B.P. Flannery, Numerical Recipes in C, The Art of Scientific Computing, Cambridge University Press, Cambridge, England, 1993. [5] Goodall, C., A Survey of Smoothing Techniques, Modern Methods of Data Analysis, (J. Fox and J.S. Long, eds.), Sage Publications, Newbury Park, CA, pp. 126-176, 1990. [6] Hutcheson, M.C., Trimmed Resistant Weighted Scatterplot Smooth, Masters Thesis, Cornell University, Ithaca, NY, 1995. [7] Orfanidis, S.J., Introduction to Signal Processing, Prentice-Hall, Englewood Cliffs, NJ, 1996.
2-43
Fitting Data
This section describes how to fit data and evaluate the fit with the Curve Fitting Toolbox. The Fitting Process (p. 2-44) Parametric Fitting (p. 2-46) Nonparametric Fitting (p. 2-116) Selected Bibliography (p. 2-124) Steps to follow when fitting any data set Fit data with parametric models Fit data with nonparametric models References
2-44
Fitting Data
Select the name of the current fit. When you click New fit or Copy fit, a default fit name is automatically created in the Fit name field. You can specify a new fit name by editing this field. Select the name of the current data set from the Data set list. All imported and smoothed data sets are listed.
2 Select an exclusion rule.
2-45
If you want to exclude data from a fit, select an exclusion rule from the Exclusion rule list. The list contains only exclusion rules that are compatible with the current data set. An exclusion rule is compatible with the current data set if their lengths are identical, or if it is created by sectioning only.
3 Select a fit type and fit options, fit the data, and evaluate the goodness of fit.
The fit type can be a library or custom parametric model, a smoothing spline, or an interpolant. Select fit options such as the fitting algorithm, and coefficient starting points and constraints. Depending on your data and model, accepting the default fit options often produces an excellent fit. Fit the data by clicking the Apply button or by selecting the Immediate apply check box. Examine the fitted curve, residuals, goodness of fit statistics, confidence bounds, and prediction bounds for the current fit.
4 Compare fits.
Compare the current fit and data set to previous fits and data sets by examining the goodness of fit statistics. Use the Table Options GUI to modify which goodness of fit statistics are displayed in the Table of Fits. You can sort the table by clicking on any column heading.
5 Save the fit results.
If the fit is good, save the results as a structure to the MATLAB workspace. Otherwise, modify the fit options or select another model.
Parametric Fitting
Parametric fitting involves finding coefficients (parameters) for one or more models that you fit to data. The data is assumed to be statistical in nature and is divided into two components: a deterministic component and a random component. data = deterministic component + random component
2-46
Fitting Data
The deterministic component is given by a parametric model and the random component is often described as error associated with the data. data = model + error The model is a function of the independent (predictor) variable and one or more coefficients. The error represents random variations in the data that follow a specific probability distribution (usually Gaussian). The variations can come from many different sources, but are always present at some level when you are dealing with measured data. Systematic variations can also exist, but they can lead to a fitted model that does not represent the data well; see Evaluating the Goodness of Fit on page 2-71. The model coefficients often have physical significance. For example, suppose you have collected data that corresponds to a single decay mode of a radioactive nuclide, and you want to estimate the half-life (T1/2) of the decay. The law of radioactive decay states that the activity of a radioactive substance decays exponentially in time. Therefore, the model to use in the fit is given by
where y0 is the number of nuclei at time t = 0, and is the decay constant. The data can be described by
Both y0 and are coefficients that are estimated by the fit. Because T1/2 = ln(2)/, the fitted value of the decay constant yields the fitted half-life. However, because the data contains some error, the deterministic component of the equation cannot be determined exactly from the data. Therefore, the coefficients and half-life calculation will have some uncertainty associated with them. If the uncertainty is acceptable, then you are done fitting the data. If the uncertainty is not acceptable, then you might have to take steps to reduce it either by collecting more data or by reducing measurement error and collecting new data and repeating the model fit.
2-47
In other situations where there is no theory to dictate a model, you might also modify the model by adding or removing terms, or substitute an entirely different model.
The components of this expression are described below. Normal Distribution. The errors are assumed to be normally distributed because the normal distribution often provides an adequate approximation to the distribution of many measured quantities. Although the least squares fitting method does not assume normally distributed errors when calculating parameter estimates, the method works best for data that does not contain a large number of random errors with extreme values. The normal distribution is one of the probability distributions in which extreme random errors are uncommon. However, statistical results such as confidence and prediction bounds do require normally distributed errors for their validity. Zero Mean. If the mean of the errors is zero, then the errors are purely random. If the mean is not zero, then it might be that the model is not the right choice for your data, or the errors are not purely random and contain systematic errors. Constant Variance. A constant variance in the data implies that the spread of errors is constant. Data that has the same variance is sometimes said to be of equal quality.
2-48
Fitting Data
The assumption that the random errors have constant variance is not implicit to weighted least squares regression. Instead, it is assumed that the weights provided in the fitting procedure correctly indicate the differing levels of quality present in the data. The weights are then used to adjust the amount of influence each data point has on the estimates of the fitted coefficients to an appropriate level.
where n is the number of data points included in the fit and S is the sum of squares error estimate. The supported types of least squares fitting include Linear least squares Weighted linear least squares Robust least squares Nonlinear least squares
2-49
Linear Least Squares. The Curve Fitting Toolbox uses the linear least squares method to fit a linear model to data. A linear model is defined as an equation that is linear in the coefficients. For example, polynomials are linear but Gaussians are not. To illustrate the linear least squares fitting process, suppose you have n data points that can be modeled by a first-degree polynomial.
To solve this equation for the unknown coefficients p1 and p2, you write S as a system of n simultaneous linear equations in two unknowns. If n is greater than the number of unknowns, then the system of equations is overdetermined.
Because the least squares fitting process minimizes the summed square of the residuals, the coefficients are determined by differentiating S with respect to each parameter, and setting the result equal to zero.
The estimates of the true parameters are usually represented by b. Substituting b1 and b2 for p1 and p2, the previous equations become
where the summations run from i =1 to n. The normal equations are defined as
2-50
Fitting Data
Solving for b1
As you can see, estimating the coefficients p1 and p2 requires only a few simple calculations. Extending this example to a higher degree polynomial is straightforward although a bit tedious. All that is required is an additional normal equation for each linear term added to the model. In matrix form, linear models are given by the formula
where y is an n-by-1 vector of responses. is a m-by-1 vector of coefficients. X is the n-by-m design matrix for the model. is an n-by-1 vector of errors. For the first-degree polynomial, the n equations in two unknowns are expressed in terms of y, X, and as
2-51
The least squares solution to the problem is a vector b, which estimates the unknown vector of coefficients . The normal equations are given by
In MATLAB, you can use the backslash operator to solve a system of simultaneous linear equations for unknown coefficients. Because inverting XTX can lead to unacceptable rounding errors, MATLAB uses QR decomposition with pivoting, which is a very stable algorithm numerically. Refer to Arithmetic Operators in the MATLAB documentation for more information about the backslash operator and QR decomposition. You can plug b back into the model formula to get the predicted response values, .
2-52
Fitting Data
A hat (circumflex) over a letter denotes an estimate of a parameter or a prediction from a model. The projection matrix H is called the hat matrix, because it puts the hat on y. The residuals are given by
Refer to [1] or [2] for a complete description of the matrix representation of least squares regression. Weighted Linear Least Squares. As described in Basic Assumptions About the Error on page 2-48, it is usually assumed that the response data is of equal quality and, therefore, has constant variance. If this assumption is violated, your fit might be unduly influenced by data of poor quality. To improve the fit, you can use weighted least squares regression where an additional scale factor (the weight) is included in the fitting process. Weighted least squares regression minimizes the error estimate
where wi are the weights. The weights determine how much each response value influences the final parameter estimates. A high-quality data point influences the fit more than a low-quality data point. Weighting your data is recommended if the weights are known, or if there is justification that they follow a particular form. The weights modify the expression for the parameter estimates b in the following way,
where W is given by the diagonal elements of the weight matrix w. You can often determine whether the variances are not constant by fitting the data and plotting the residuals. In the plot shown below, the data contains
2-53
replicate data of various quality and the fit is assumed to be correct. The poor quality data is revealed in the plot of residuals, which has a funnel shape where small predictor values yield a bigger scatter in the response values than large predictor values.
The weights you supply should transform the response variances to a constant value. If you know the variances of the measurement errors in your data, then the weights are given by
Or, if you only have estimates of the error variable for each data point, it usually suffices to use those estimates in place of the true variance. If you do not know the variances, it suffices to specify weights on a relative scale. Note that an overall variance term is estimated even when weights have been specified. In this instance, the weights define the relative weight to each point in the fit, but are not taken to specify the exact variance of each point.
2-54
Fitting Data
For example, if each data point is the mean of several independent measurements, it might make sense to use those numbers of measurements as weights. Robust Least Squares. As described in Basic Assumptions About the Error on page 2-48, it is usually assumed that the response errors follow a normal distribution, and that extreme values are rare. Still, extreme values calledoutliers do occur. The main disadvantage of least squares fitting is its sensitivity to outliers. Outliers have a large influence on the fit because squaring the residuals magnifies the effects of these extreme data points. To minimize the influence of outliers, you can fit your data using robust least squares regression. The toolbox provides these two robust regression methods: Least absolute residuals (LAR) The LAR method finds a curve that minimizes the absolute difference of the residuals, rather than the squared differences. Therefore, extreme values have a lesser influence on the fit. Bisquare weights This method minimizes a weighted sum of squares, where the weight given to each data point depends on how far the point is from the fitted line. Points near the line get full weight. Points farther from the line get reduced weight. Points that are farther from the line than would be expected by random chance get zero weight. For most cases, the bisquare weight method is preferred over LAR because it simultaneously seeks to find a curve that fits the bulk of the data using the usual least squares approach, and it minimizes the effect of outliers. Robust fitting with bisquare weights uses an iteratively reweighted least squares algorithm, and follows this procedure:
1 Fit the model by weighted least squares. 2 Compute the adjusted residuals and standardize them. The adjusted
2-55
ri are the usual least squares residuals and hi are leverages that adjust the residuals by downweighting high-leverage data points, which have a large effect on the least squares fit. The standardized adjusted residuals are given by
K is a tuning constant equal to 4.685, and s is the robust variance given by MAD/0.6745 where MAD is the median absolute deviation of the residuals. Refer to [7] for a detailed description of h, K, and s.
3 Compute the robust weights as a function of u. The bisquare weights are
given by
Note that if you supply your own regression weight vector, the final weight is the product of the robust weight and the regression weight.
4 If the fit converges, then you are done. Otherwise, perform the next
iteration of the fitting procedure by returning to the first step. The plot shown below compares a regular linear fit with a robust fit using bisquare weights. Notice that the robust fit follows the bulk of the data and is not strongly influenced by the outliers.
2-56
Fitting Data
Instead of minimizing the effects of outliers by using robust regression, you can mark data points to be excluded from the fit. Refer to Excluding and Sectioning Data on page 2-26 for more information. Nonlinear Least Squares. The Curve Fitting Toolbox uses the nonlinear least squares formulation to fit a nonlinear model to data. A nonlinear model is defined as an equation that is nonlinear in the coefficients, or a combination of linear and nonlinear in the coefficients. For example, Gaussians, ratios of polynomials, and power functions are all nonlinear. In matrix form, nonlinear models are given by the formula
2-57
f is a function of and X. is a m-by-1 vector of coefficients. X is the n-by-m design matrix for the model. is an n-by-1 vector of errors. Nonlinear models are more difficult to fit than linear models because the coefficients cannot be estimated using simple matrix techniques. Instead, an iterative approach is required that follows these steps:
1 Start with an initial estimate for each coefficient. For some nonlinear
models, a heuristic approach is provided that produces reasonable starting values. For other models, random values on the interval [0,1] are provided.
2 Produce the fitted curve for the current set of coefficients. The fitted
response value
is given by
and involves the calculation of the Jacobian of f(X,b), which is defined as a matrix of partial derivatives taken with respect to the coefficients.
3 Adjust the coefficients and determine whether the fit improves. The
direction and magnitude of the adjustment depend on the fitting algorithm. The toolbox provides these algorithms: Trust-region This is the default algorithm and must be used if you specify coefficient constraints. It can solve difficult nonlinear problems more efficiently than the other algorithms and it represents an improvement over the popular Levenberg-Marquardt algorithm. Levenberg-Marquardt This algorithm has been used for many years and has proved to work most of the time for a wide range of nonlinear models and starting values. If the trust-region algorithm does not produce a reasonable fit, and you do not have coefficient constraints, you should try the Levenberg-Marquardt algorithm. Gauss-Newton This algorithm is potentially faster than the other algorithms, but it assumes that the residuals are close to zero. Its included with the toolbox for pedagogical reasons and should be the last choice for most models and data sets.
2-58
Fitting Data
For more information about the trust region algorithm, refer to [4] and to Trust-Region Methods for Nonlinear Minimization in the Optimization Toolbox documentation. For more information about the Levenberg-Marquardt and Gauss-Newton algorithms, refer to Nonlinear Least-Squares Implementation in the same guide. Additionally, the Levenberg-Marquardt algorithm is described in [5] and [6].
4 Iterate the process by returning to step 2 until the fit reaches the specified
convergence criteria. You can use weights and robust fitting for nonlinear models, and the fitting process is modified accordingly. Because of the nature of the approximation process, no algorithm is foolproof for all nonlinear models, data sets, and starting points. Therefore, if you do not achieve a reasonable fit using the default starting points, algorithm, and convergence criteria, you should experiment with different options. Refer to Specifying Fit Options on page 2-67 for a description of how to modify the default options. Because nonlinear models can be particularly sensitive to the starting points, this should be the first fit option you modify.
Library Models
The parametric library models provided by the Curve Fitting Toolbox are described below. Exponentials. The toolbox provides a one-term and a two-term exponential model.
Exponentials are often used when the rate of change of a quantity is proportional to the initial amount of the quantity. If the coefficient associated with e is negative, y represents exponential decay. If the coefficient is positive, y represents exponential growth. For example, a single radioactive decay mode of a nuclide is described by a one-term exponential. a is interpreted as the initial number of nuclei, b is the
2-59
decay constant, x is time, and y is the number of remaining nuclei after a specific amount of time passes. If two decay modes exist, then you must use the two-term exponential model. For each additional decay mode, you add another exponential term to the model. Examples of exponential growth include contagious diseases for which a cure is unavailable, and biological populations whose growth is uninhibited by predation, environmental factors, and so on. Fourier Series. The Fourier series is a sum of sine and cosine functions that is used to describe a periodic signal. It is represented in either the trigonometric form or the exponential form. The toolbox provides the trigonometric Fourier series form shown below,
where a0 models a constant (intercept) term in the data and is associated with the i = 0 cosine term, w is the fundamental frequency of the signal, n is the number of terms (harmonics) in the series, and . For more information about the Fourier series, refer to Fourier Analysis in the MATLAB documentation. For an example that fits the ENSO data to a custom Fourier series model, refer to Example: Custom Model (Fourier Series) on page 2-99. Gaussian. The Gaussian model is used for fitting peaks, and is given by the equation
where a is the amplitude, b is the centroid (location), c is related to the peak width, n is the number of peaks to fit, and .
2-60
Fitting Data
Gaussian peaks are encountered in many areas of science and engineering. For example, line emission spectra and chemical concentration assays can be described by Gaussian peaks. For an example that fits two Gaussian peaks and an exponential background, refer to Example: Custom Model (Gaussian with Exponential Background) on page 2-105. Polynomials. Polynomial models are given by
where n + 1 is the order of the polynomial, n is the degree of the polynomial, and . The order gives the number of coefficients to be fit, and the degree gives the highest power of the predictor variable. In this guide, polynomials are described in terms of their degree. For example, a third-degree (cubic) polynomial is given by
Polynomials are often used when a simple empirical model is required. The model can be used for interpolation or extrapolation, or it can be used to characterize data using a global fit. For example, the temperature-to-voltage conversion for a Type J thermocouple in the 0o to 760o temperature range is described by a seventh-degree polynomial.
Note If you do not require a global parametric fit and want to maximize the flexibility of the fit, piecewise polynomials might provide the best approach. Refer to Nonparametric Fitting on page 2-116 for more information.
The main advantages of polynomial fits include reasonable flexibility for data that is not too complicated, and they are linear, which means the fitting process is simple. The main disadvantage is that high-degree fits can become unstable. Additionally, polynomials of any degree can provide a good fit within the data range, but can diverge wildly outside that range. Therefore,
2-61
you should exercise caution when extrapolating with polynomials. Refer to Determining the Best Fit on page 1-11 for examples of good and poor polynomial fits to census data. Note that when you fit with high-degree polynomials, the fitting procedure uses the predictor values as the basis for a matrix with very large values, which can result in scaling problems. To deal with this, you should normalize the data by centering it at zero mean and scaling it to unit standard deviation. You normalize data by selecting the Center and scale X data check box on the Fitting GUI. Power Series. The toolbox provides a one-term and a two-term power series model.
Power series models are used to describe a variety of data. For example, the rate at which reactants are consumed in a chemical reaction is generally proportional to the concentration of the reactant raised to some power. Rationals. Rational models are defined as ratios of polynomials and are given by
where n is the degree of the numerator polynomial and , while m is the degree of the denominator polynomial and . Note that the is always 1. This makes the numerator and coefficient associated with denominator unique when the polynomial degrees are the same.
2-62
Fitting Data
In this guide, rationals are described in terms of the degree of the numerator/the degree of the denominator. For example, a quadratic/cubic rational equation is given by
Like polynomials, rationals are often used when a simple empirical model is required. The main advantage of rationals is their flexibility with data that has complicated structure. The main disadvantage is that they become unstable when the denominator is around zero. For an example that uses rational polynomials of various degrees, refer to Example: Rational Fit on page 2-87. Sum of Sines. The sum of sines model is used for fitting periodic functions, and is given by the equation
where a is the amplitude, b is the frequency, and c is the phase constant for each sine wave term. n is the number of terms in the series and . This equation is closely related to the Fourier series described previously. The main difference is that the sum of sines equation includes the phase constant, and does not include a constant (intercept) term. Weibull Distribution. The Weibull distribution is widely used in reliability and life (failure rate) data analysis. The toolbox provides the two-parameter Weibull distribution
where a is the scale parameter and b is the shape parameter. Note that there is also a three-parameter Weibull distribution with x replaced by x c where c is the location parameter. Additionally, there is a one-parameter Weibull
2-63
distribution where the shape parameter is fixed and only the scale parameter is fitted. To use these distributions, you must create a custom equation. Note that the Curve Fitting Toolbox does not fit Weibull probability distributions to a sample of data. Instead, it fits curves to response and predictor data such that the curve has the same shape as a Weibull distribution.
Custom Models
If the toolbox library does not contain the desired parametric equation, you can create your own custom model. Library models, however, offer the best chance for rapid convergence. This is because For most models, optimal default coefficient starting points are calculated. For custom equations, the default starting points are chosen at random on the interval [0,1]. Refer to Default Coefficient Parameters on page 2-70 for more information. An analytic Jacobian is used instead of finite differencing. When using the Analysis GUI, analytic derivatives are calculated as well as analytic integrals if the integral can be expressed in closed form.
Note To save custom equations for later use, you should save the curve-fitting session with the File > Save Session menu item.
You create custom equations with the Create Custom Equation GUI. The GUI contains two panes: a pane for creating linear equations and a pane for creating general (nonlinear) equations. These panes are described below.
2-64
Fitting Data
Linear Equations. Linear models are defined by equations that are linear in the parameters. For example, the polynomial library equations are linear. The Linear Equations pane is shown below followed by a description of its parameters.
Independent variable Symbol representing the independent (predictor) variable. The default symbol is x. Equation Symbol representing the dependent (response) variable followed by the linear equation. The default symbol is y.
Unknown Coefficients The unknown coefficients to be determined by the fit. The default symbols are a, b, c, and so on. Terms Functions that depend only on the independent variable and constants. Note that if you attempt to define a term that contains a coefficient to be fitted, an error is returned. Unknown constant coefficient If selected, a constant term is included in the equations to be fit. Otherwise, a constant term is not included. Add a term Add a term to the equation. An unknown coefficient is automatically added for each new term. Remove last term Remove the last term added to the equation.
2-65
Equation The custom equation. Equation name The name of the equation. By default, the name is automatically updated to be identical to the custom equation given by Equation. If you override the default, the name is no longer automatically updated. General Equations. General (nonlinear) equations are defined as equations that are nonlinear in the parameters, or are a combination of linear and nonlinear in the parameters. For example, the exponential library equations are nonlinear. The General Equations pane is shown below followed by a brief description of its parameters.
Independent variable Symbol representing the independent (predictor) variable. The default symbol is x. Equation Symbol representing the dependent (response) variable followed by the general equation. As you type in the terms of the equation, the unknown coefficients, associated starting values, and constraints automatically populate the table. By default, the starting values are randomly selected on the interval [0,1] and are unconstrained. You can immediately change the default starting values and constraints in this table, or you can change them later using the Fit Options GUI.
2-66
Fitting Data
Equation name The name of the equation. By default, the name is automatically updated to be identical to the custom equation given by Equation. If you override the default, the name is no longer automatically updated. Note that even if you define a linear equation, a nonlinear fitting procedure is used. Although this is allowed by the toolbox, it is an inefficient process and can result in less than optimal fitted coefficients. Instead, you should use the Linear Equations pane to define the equation.
The available GUI options depend on whether you are fitting your data using a linear model, a nonlinear model, or a nonparametric fit type. All the options described below are available for nonlinear models. Method, Robust, and coefficient constraints (Lower and Upper) are available for linear models.
2-67
Interpolants and smoothing splines include Method, but no configurable options. Fitting Method and Algorithm. Method The fitting method. The method is automatically selected based on the library or custom model you use. For linear models, the method is LinearLeastSquares. For nonlinear models, the method is NonlinearLeastSquares. Robust Specify whether to use the robust least squares fitting method. The values are
Off Do not use robust fitting (default). On Fit with default robust method (bisquare weights). LAR Fit by minimizing the least absolute residuals (LAR). Bisquare Fit by minimizing the summed square of the residuals, and downweight outliers using bisquare weights. In most cases, this is the best choice for robust fitting.
Trust-Region This is the default algorithm and must be used if you specify coefficient constraints. Levenberg-Marquardt If the trust-region algorithm does not produce a reasonable fit, and you do not have coefficient constraints, you should try the Levenberg-Marquardt algorithm. Gauss-Newton This algorithm is included for pedagogical reasons and should be the last choice for most models and data sets.
Finite Differencing Parameters. DiffMinChange Minimum change in coefficients for finite difference Jacobians. The default value is 10-8. DiffMaxChange Maximum change in coefficients for finite difference Jacobians. The default value is 0.1. Note that DiffMinChange and DiffMaxChange apply to
2-68
Fitting Data
Any nonlinear custom equation that is, a nonlinear equation that you write. Some, but not all, of the nonlinear equations provided with the Curve Fitting Toolbox. However, DiffMinChange and DiffMaxChange do not apply to any linear equations. Fit Convergence Criteria. MaxFunEvals Maximum number of function (model) evaluations allowed. The default value is 600. MaxIter Maximum number of fit iterations allowed. The default value is 400. TolFun Termination tolerance used on stopping conditions involving the function (model) value. The default value is 10-6. TolX Termination tolerance used on stopping conditions involving the coefficients. The default value is 10-6. Coefficient Parameters. Unknowns Symbols for the unknown coefficients to be fitted. StartPoint The coefficient starting values. The default values depend on the model. For rational, Weibull, and custom models, default values are randomly selected within the range [0,1]. For all other nonlinear library models, the starting values depend on the data set and are calculated heuristically. Lower Lower bounds on the fitted coefficients. The bounds are used only with the trust region fitting algorithm. The default lower bounds for most library models are -Inf, which indicates that the coefficients are unconstrained. However, a few models have finite default lower bounds. For example, Gaussians have the width parameter constrained so that it cannot be less than 0. Upper Upper bounds on the fitted coefficients. The bounds are used only with the trust region fitting algorithm. The default upper bounds
2-69
for all library models are Inf, which indicates that the coefficients are unconstrained. For more information about these fit options, refer to Optimization Options in the Optimization Toolbox documentation. Default Coefficient Parameters. The default coefficient starting points and constraints for library and custom models are given below. If the starting points are optimized, then they are calculated heuristically based on the current data set. Random starting points are defined on the interval [0,1] and linear models do not require starting points. If a model does not have constraints, the coefficients have neither a lower bound nor an upper bound. You can override the default starting points and constraints by providing your own values using the Fit Options GUI. Default Starting Points and Constraints Model Custom linear Custom nonlinear Exponentials Fourier series Gaussians Polynomials Power series Rationals Sum of sines Weibull Starting Points N/A Random Optimized Optimized Optimized N/A Optimized Random Optimized Random Constraints None None None None ci > 0 None None None bi > 0 a, b > 0
Note that the sum of sines and Fourier series models are particularly sensitive to starting points, and the optimized values might be accurate for only a few terms in the associated equations. For an example that overrides the default starting values for the sum of sines model, refer to Example: Sectioning Periodic Data on page 2-36.
2-70
Fitting Data
2-71
data. The numerical measures are more narrowly focused on a particular aspect of the data and often try to compress that information into a single number. In practice, depending on your data and analysis requirements, you might need to use both types to determine the best fit. Note that it is possible that none of your fits can be considered suitable for your data, based on these methods. In this case, it might be that you need to select a different model. It is also possible that all the goodness-of-fit measures indicate that a particular fit is suitable. However, if your goal is to extract fitted coefficients that have physical meaning, but your model does not reflect the physics of the data, the resulting coefficients are useless. In this case, understanding what your data represents and how it was measured is just as important as evaluating the goodness of fit. Residual Analysis. The residuals from a fitted model are defined as the differences between the response data and the fit to the response data at each predictor value. residual = data - fit You display the residuals in the Curve Fitting Tool by selecting the menu item View > Residuals. Mathematically, the residual for a specific predictor value is the difference between the response value y and the predicted response value .
Assuming the model you fit to the data is correct, the residuals approximate the random errors. Therefore, if the residuals appear to behave randomly, it suggests that the model fits the data well. However, if the residuals display a systematic pattern, it is a clear sign that the model fits the data poorly. Always bear in mind that many results of model fitting, such as confidence bounds, will be invalid should the model be grossly inappropriate for the data.
2-72
Fitting Data
A graphical display of the residuals for a first degree polynomial fit is shown below. The top plot shows that the residuals are calculated as the vertical distance from the data point to the fitted curve. The bottom plot displays the residuals relative to the fit, which is the zero line.
The residuals appear randomly scattered around zero indicating that the model describes the data well.
2-73
A graphical display of the residuals for a second-degree polynomial fit is shown below. The model includes only the quadratic term, and does not include a linear or constant term.
The residuals are systematically positive for much of the data range indicating that this model is a poor fit for the data. Goodness-of-Fit Statistics. After using graphical methods to evaluate the goodness of fit, you should examine the goodness-of-fit statistics. The Curve Fitting Toolbox supports these goodness-of-fit statistics for parametric models: The sum of squares due to error (SSE) R-square Adjusted R-square Root mean squared error (RMSE)
2-74
Fitting Data
For the current fit, these statistics are displayed in the Results list box in the Fit Editor. For all fits in the current curve-fitting session, you can compare the goodness-of-fit statistics in the Table of fits.
A value closer to 0 indicates that the model has a smaller random error component, and that the fit will be more useful for prediction. Note that the SSE was previously defined in The Least Squares Fitting Method on page 2-49.
R-Square
This statistic measures how successful the fit is in explaining the variation of the data. Put another way, R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination. R-square is defined as the ratio of the sum of squares of the regression (SSR) and the total sum of squares (SST). SSR is defined as
SST is also called the sum of squares about the mean, and is defined as
2-75
R-square can take on any value between 0 and 1, with a value closer to 1 indicating that a greater proportion of variance is accounted for by the model. For example, an R-square value of 0.8234 means that the fit explains 82.34% of the total variation in the data about the average. If you increase the number of fitted coefficients in your model, R-square will increase although the fit may not improve in a practical sense. To avoid this situation, you should use the degrees of freedom adjusted R-square statistic described below. Note that it is possible to get a negative R-square for equations that do not contain a constant term. Because R-square is defined as the proportion of variance explained by the fit, if the fit is actually worse than just fitting a horizontal line then R-square is negative. In this case, R-square cannot be interpreted as the square of a correlation. Such situations indicate that a constant term should be added to the model.
v indicates the number of independent pieces of information involving the n data points that are required to calculate the sum of squares. Note that if parameters are bounded and one or more of the estimates are at their
2-76
Fitting Data
bounds, then those estimates are regarded as fixed. The degrees of freedom is increased by the number of such parameters. The adjusted R-square statistic is generally the best indicator of the fit quality when you compare two models that are nested that is, a series of models each of which adds additional coefficients to the previous model.
The adjusted R-square statistic can take on any value less than or equal to 1, with a value closer to 1 indicating a better fit. Negative values can occur when the model contains terms that do not help to predict the response.
where MSE is the mean square error or the residual mean square
Just as with SSE, an MSE value closer to 0 indicates a fit that is more useful for prediction. Confidence and Prediction Bounds. With the Curve Fitting Toolbox, you can calculate confidence bounds for the fitted coefficients, and prediction bounds for new observations or for the fitted function. Additionally, for prediction bounds, you can calculate simultaneous bounds, which take into account all predictor values, or you can calculate nonsimultaneous bounds, which take into account only individual predictor values. The coefficient confidence bounds are presented numerically, while the prediction bounds are displayed graphically and are also available numerically.
2-77
The available confidence and prediction bounds are summarized below. Types of Confidence and Prediction Bounds Interval Type Fitted coefficients New observation New function Description Confidence bounds for the fitted coefficients Prediction bounds for a new observation (response value) Prediction bounds for a new function value
Note Prediction bounds are also often described as confidence bounds because you are calculating a confidence interval for a predicted response.
Confidence and prediction bounds define the lower and upper values of the associated interval, and define the width of the interval. The width of the interval indicates how uncertain you are about the fitted coefficients, the predicted observation, or the predicted fit. For example, a very wide interval for the fitted coefficients can indicate that you should use more data when fitting before you can say anything very definite about the coefficients. The bounds are defined with a level of certainty that you specify. The level of certainty is often 95%, but it can be any value such as 90%, 99%, 99.9%, and so on. For example, you might want to take a 5% chance of being incorrect about predicting a new observation. Therefore, you would calculate a 95% prediction interval. This interval indicates that you have a 95% chance that the new observation is actually contained within the lower and upper prediction bounds.
2-78
Fitting Data
where b are the coefficients produced by the fit, t depends on the confidence level, and is computed using the inverse of Students t cumulative distribution function, and S is a vector of the diagonal elements from the estimated covariance matrix of the coefficient estimates, (XTX)-1s2. In a linear fit, X is the design matrix, while for a nonlinear fit X is the Jacobian of the fitted values with respect to the coefficients. XT is the transpose of X, and s2 is the mean squared error. Refer to the tinv function, included with the Statistics Toolbox, for a description of t. Refer to Linear Least Squares on page 2-50 for more information about X and XT. The confidence bounds are displayed in the Results list box in the Fit Editor using the following format.
p1 = 1.275 (1.113, 1.437)
The fitted value for the coefficient p1 is 1.275, the lower bound is 1.113, the upper bound is 1.437, and the interval width is 0.324. By default, the confidence level for the bounds is 95%. You can change this level to any value with the View > Confidence Level menu item in the Curve Fitting Tool. You can calculate confidence intervals at the command line with the confint function.
2-79
The nonsimultaneous prediction bounds for a new observation at the predictor value x are given by
where s2 is the mean squared error, t depends on the confidence level, and is computed using the inverse of Students t cumulative distribution function, and S is the covariance matrix of the coefficient estimates, (XTX)-1s2. Note that x is defined as a row vector of the design matrix or Jacobian evaluated at a specified predictor value. The simultaneous prediction bounds for a new observation and for all predictor values are given by
where f depends on the confidence level, and is computed using the inverse of the F cumulative distribution function. Refer to the finv function, included with the Statistics Toolbox, for a description of f. The nonsimultaneous prediction bounds for the function at a single predictor value x are given by
The simultaneous prediction bounds for the function and for all predictor values are given by
You can graphically display prediction bounds two ways: using the Curve Fitting Tool or using the Analysis GUI. With the Curve Fitting Tool, you can display nonsimultaneous prediction bounds for new observations with View > Prediction Bounds. By default, the confidence level for the bounds is 95%. You can change this level to any value with View > Confidence Level. With the Analysis GUI, you can display nonsimultaneous prediction
2-80
Fitting Data
bounds for the function or for new observations. Additionally, you can view prediction bounds in the Results box of the Analysis GUI. You can display numerical prediction bounds of any type at the command line with the predint function. To understand the quantities associated with each type of prediction interval, recall that the data, fit, and residuals are related through the formula data = fit + residuals where the fit and residuals terms are estimates of terms in the formula data = model + random error Suppose you plan to take a new observation at the predictor value xn+1. Call the new observation yn+1(xn+1) and the associated error en+1. Then yn+1(xn+1) satisfies the equation
where f(xn+1) is the true but unknown function you want to estimate at xn+1. The likely values for the new observation or for the estimated function are provided by the nonsimultaneous prediction bounds. If instead you want the likely value of the new observation to be associated with any predictor value, the previous equation becomes
The likely values for this new observation or for the estimated function are provided by the simultaneous prediction bounds. The types of prediction bounds are summarized below.
2-81
Types of Prediction Bounds Type of Bound Observation Associated Equation Nonsimultaneous Simultaneous Function Nonsimultaneous Simultaneous yn+1(xn+1) yn+1(x), globally for any x f(xn+1) f(x), simultaneously for all x
The nonsimultaneous and simultaneous prediction bounds for a new observation and the fitted function are shown below. Each graph contains three curves: the fit, the lower confidence bounds, and the upper confidence bounds. The fit is a single-term exponential to generated data and the bounds reflect a 95% confidence level. Note that the intervals associated with a new observation are wider than the fitted function intervals because of the additional uncertainty in predicting a new response value (the fit plus random errors).
2-82
Fitting Data
Example: Goodness of Fit. This example fits several polynomial models to generated data and evaluates how well those models fit the data and how precisely they can predict. The data is generated from a cubic curve, and there is a large gap in the range of the x variable where no data exist.
rand('state',0) x = [1:0.1:3 9:0.1:10]'; c = [2.5 -0.5 1.3 -0.1]; y = c(1) + c(2)*x + c(3)*x.^2 + c(4)*x.^3 + (rand(size(x))-0.5);
2-83
After you import the data, fit it using a cubic polynomial and a fifth degree polynomial. The data, fits, and residuals are shown below. You display the residuals in the Curve Fitting Tool with the View > Residuals menu item.
Both models appear to fit the data well, and the residuals appear to be randomly distributed around zero. Therefore, a graphical evaluation of the fits does not reveal any obvious differences between the two equations.
2-84
Fitting Data
As expected, the fit results for poly3 are reasonable because the generated data follows a cubic curve. The 95% confidence bounds on the fitted coefficients indicate that they are acceptably precise. However, the 95% confidence bounds for poly5 indicate that the fitted coefficients are not known precisely. The goodness-of-fit statistics are shown below. By default, the adjusted R-square and RMSE statistics are not displayed in the Table of Fits. To display these statistics, open the Table Options GUI by clicking the Table options button. The statistics do not reveal a substantial difference between the two equations.
The statistics do not reveal a substantial difference between the two equations. Open the Table Options GUI and select Adj R-sq and RMSE.
The 95% nonsimultaneous prediction bounds for new observations are shown below. To display prediction bounds in the Curve Fitting Tool, select the View > Prediction Bounds menu item. Alternatively, you can view
2-85
prediction bounds for the function or for new observations using the Analysis GUI.
The prediction bounds for poly3 indicate that new observations can be predicted with a small uncertainty throughout the entire data range. This is not the case for poly5. It has wider prediction bounds in the area where no data exist, apparently because the data does not contain enough information to estimate the higher degree polynomial terms accurately. In other words, a fifth-degree polynomial overfits the data. You can confirm this by using the Analysis GUI to compute bounds for the functions themselves. The 95% prediction bounds for the fitted function using poly5 are shown below. As you can see, the uncertainty in predicting the function is large in the center of the data. Therefore, you would conclude that more data must
2-86
Fitting Data
be collected before you can make precise predictions using a fifth-degree polynomial.
In conclusion, you should examine all available goodness-of-fit measures before deciding on the fit that is best for your purposes. A graphical examination of the fit and residuals should always be your initial approach. However, some fit characteristics are revealed only through numerical fit results, statistics, and prediction bounds.
2-87
To get started, load the thermal expansion data from the file hahn1.mat, which is provided with the toolbox.
load hahn1
The workspace now contains two new variables, temp and thermex: temp is a vector of temperatures in degrees kelvin. thermex is a vector of thermal expansion coefficients for copper. Import these two variables into the Curve Fitting Tool and name the data set CuThermEx. For this data set, you will find the rational equation that produces the best fit. As described in Library Models on page 2-59, rational models are defined as a ratio of polynomials
where n is the degree of the numerator polynomial and m is the degree of the denominator polynomial. Note that the rational equations are not associated with physical parameters of the data. Instead, they provide a simple and flexible empirical model that you can use for interpolation and extrapolation.
2-88
Fitting Data
As you can see by examining the shape of the data, a reasonable initial choice for the rational model is quadratic/quadratic. The Fitting GUI configured for this equation is shown below.
2-89
The fit clearly misses the data for the smallest and largest predictor values. Additionally, the residuals show a strong pattern throughout the entire data set indicating that a better fit is possible.
2-90
Fitting Data
For the next fit, try a cubic/cubic equation. The data, fit, and residuals are shown below.
The numerical results shown below indicate that the fit did not converge.
2-91
Although the message in the Results window indicates that you might improve the fit if you increase the maximum number of iterations, a better choice at this stage of the fitting process is to use a different rational equation because the current fit contains several discontinuities. These discontinuities are due to the function blowing up at predictor values that correspond to the zeros of the denominator. As the next try, fit the data using a cubic/quadratic equation. The data, fit, and residuals are shown below.
The fit is well behaved over the entire data range, and the residuals are randomly scattered about zero. Therefore, you can confidently use this fit for further analysis.
2-92
Fitting Data
The data is collected by placing solid state detectors at values of from 10o to 240o in 10o increments.
ranging
2-93
It is sometimes useful to describe a variable expressed as a function of angle in terms of Legendre polynomials
where Pn(x) is a Legendre polynomial of degree n, x is cos( ), and an are the coefficients of the fit. Refer to the legendre function for information about generating Legendre polynomials. For the alpha-emission data, you can directly associate the coefficients with the nuclear dynamics by invoking a theoretical model, which is described in [8]. Additionally, the theoretical model introduces constraints for the infinite sum shown above. In particular, by considering the angular momentum of the reaction, a fourth-degree Legendre polynomial using only even terms should describe the data effectively. You can generate Legendre polynomials with Rodrigues formula:
The Legendre polynomials up to fourth degree are given below. Legendre Polynomials up to Fourth Degree n 0 1 2 3 4 Pn(x) 1 x (1/2)(3x2 1) (1/2)(5x3 3x) (1/8)(35x4 30x2 + 3)
The first step is to load the 12C alpha-emission data from the file carbon12alpha.mat, which is provided with the toolbox.
2-94
Fitting Data
load carbon12alpha
The workspace now contains two new variables, angle and counts: angle is a vector of angles (in radians) ranging from 10o to 240o in 10o increments. counts is a vector of raw alpha particle counts that correspond to the emission angles in angle. Import these two variables into the Curve Fitting Toolbox and name the data set C12Alpha. The Fit Editor for a custom equation fit type is shown below.
Fit the data using a fourth-degree Legendre polynomial with only even terms:
2-95
Because the Legendre polynomials depend only on the predictor variable and constants, you use the Linear Equations pane on the Create Custom Equation GUI. This pane is shown below for the model given by y1(x). Note that because angle is given in radians, the argument of the Legendre terms is given by cos( ).
2-96
Fitting Data
The fit and residuals are shown below. The fit appears to follow the trend of the data well, while the residuals appear to be randomly distributed and do not exhibit any systematic behavior.
The numerical fit results are shown below. The 95% confidence bounds indicate that the coefficients associated with P0(x) and P4(x) are known fairly accurately, but that the P2(x) coefficient has a relatively large uncertainty.
2-97
To confirm the theoretical argument that the alpha-emission data is best described by a fourth-degree Legendre polynomial with only even terms, fit the data using both even and odd terms:
The Linear Equations pane of the Create Custom Equation GUI is shown below for the model given by y2(x).
The numerical results indicate that the odd Legendre terms do not contribute significantly to the fit, and the even Legendre terms are essentially unchanged from the previous fit. This confirms that the initial model choice is the best one.
2-98
Fitting Data
Example: Custom Model (Fourier Series). This example fits the ENSO data using several custom nonlinear equations. The ENSO data consists of monthly averaged atmospheric pressure differences between Easter Island and Darwin, Australia. This difference drives the trade winds in the southern hemisphere. As shown in Example: Smoothing Data on page 2-22, the ENSO data is clearly periodic, which suggests it can be described by a Fourier series
where ai and bi are the amplitudes, and ci are the periods (cycles) of the data. The question to be answered in this example is how many cycles exist? As a first attempt, assume a single cycle and fit the data using one sine term and one cosine term.
If the fit does not describe the data well, add additional sine and cosine terms with unique period coefficients until a good fit is obtained. Because there is an unknown coefficient c1 included as part of the trigonometric function arguments, the equation is nonlinear. Therefore, you must specify the equation using the General Equations pane of the Create Custom Equation GUI.
2-99
Note that the toolbox includes the Fourier series as a nonlinear library equation. However, the library equation does not meet the needs of this example because its terms are defined as fixed multiples of the fundamental frequency w. Refer to Fourier Series on page 2-60 for more information. The numerical results shown below indicate that the fit does not describe the data well. In particular, the fitted value for c1 is unreasonably small. Because the starting points are randomly selected, your initial fit results might differ from the results shown here.
As you saw in Example: Smoothing Data on page 2-22, the data include a periodic component with a period of about 12 months. However, with c1 unconstrained and with a random starting point, this fit failed to find that cycle. To assist the fitting procedure, constrain c1 to a value between 10 and
2-100
Fitting Data
14. To define constraints for unknown coefficients, use the Fit Options GUI, which you open by clicking the Fit options button in the Fitting GUI.
2-101
The fit appears to be reasonable for some of the data points but clearly does not describe the entire data set very well. As predicted, the numerical results indicate a cycle of approximately 12 months. However, the residuals show a systematic periodic distribution indicating that there are additional cycles that you should include in the fit equation. Therefore, as a second attempt, add an additional sine and cosine term to y1(x)
2-102
Fitting Data
and constrain the upper and lower bounds of c2 to be roughly twice the bounds used for c1. The fit, residuals, and numerical results are shown below.
The fit appears to be reasonable for most of the data points. However, the residuals indicate that you should include another cycle to the fit equation. Therefore, as a third attempt, add an additional sine and cosine term to y2(x)
2-103
and constrain the lower bound of c3 to be roughly three times the value of c1. The fit, residuals, and numerical results are shown below.
2-104
Fitting Data
The fit is an improvement over the previous two fits, and appears to account for most of the cycles present in the ENSO data set. The residuals appear random for most of the data, although a pattern is still visible indicating that additional cycles may be present, or you can improve the fitted amplitudes. In conclusion, Fourier analysis of the data reveals three significant cycles. The annual cycle is the strongest, but cycles with periods of approximately 44 and 22 months are also present. These cycles correspond to El Nino and the Southern Oscillation (ENSO). Example: Custom Model (Gaussian with Exponential Background). This example fits two poorly resolved Gaussian peaks on a decaying exponential background using a general (nonlinear) custom model. To get started, load the data from the file gauss3.mat, which is provided with the toolbox.
load gauss3
The workspace now contains two new variables, xpeak and ypeak: xpeak is a vector of predictor values. ypeak is a vector of response values. Import these two variables into the Curve Fitting Toolbox and accept the default data set name ypeak vs. xpeak. You will fit the data with the following equation
where ai are the peak amplitudes, bi are the peak centroids, and ci are related to the peak widths. Because there are unknown coefficients included as part of the exponential function arguments, the equation is nonlinear. Therefore, you must specify the equation using the General Equations pane of the Create Custom Equation GUI. This pane is shown below for y(x).
2-105
2-106
Fitting Data
The data, fit, and numerical fit results are shown below. Clearly, the fit is poor.
Because the starting points are randomly selected, your initial fit results might differ from the results shown here. The results include this warning message.
2-107
Fit computation did not converge: Maximum number of function evaluations exceeded. Increasing MaxFunEvals (in fit options) may allow for a better fit, or the current equation may not be a good model for the data.
To improve the fit for this example, specify reasonable starting points for the coefficients. Deducing the starting points is particularly easy for the current model because the Gaussian coefficients have a straightforward interpretation and the exponential background is well defined. Additionally, as the peak amplitudes and widths cannot be negative, constrain a1, a2, c1, and c2 to be greater then zero. To define starting values and constraints for unknown coefficients, use the Fit Options GUI, which you open by clicking the Fit options button. The starting values and constraints are shown below.
2-108
Fitting Data
The data, fit, residuals, and numerical results are shown below.
2-109
In the 2000 presidential election, many residents of Palm Beach County, Florida, complained that the design of the election ballot was confusing, which they claim led them to vote for the Reform candidate Pat Buchanan instead of the Democratic candidate Al Gore. The so-called butterfly ballot was used only in Palm Beach County and only for the election-day ballots for the presidential race. As you will see, the number of Buchanan votes for Palm Beach is far removed from the bulk of data, which suggests that the data point should be treated as an outlier. To get started, load the Florida election result data from the file flvote2k.mat, which is provided with the toolbox.
load flvote2k
The workspace now contains these three new variables: buchanan is a vector of votes for the Reform Party candidate Pat Buchanan. bush is a vector of votes for the Republican Party candidate George Bush. gore is a vector of votes for the Democratic Party candidate Al Gore. Each variable contains 68 elements, which correspond to the 67 Florida counties plus the absentee ballots. The names of the counties are given in the variable counties. From these variables, create two data sets with the Buchanan votes as the response data: buchanan vs. bush and buchanan vs. gore. For this example, assume that the relationship between the response and predictor data is linear with an offset of zero. buchanan votes = (bush votes)(m1) buchanan votes = (gore votes)(m2) m1 is the number of Bush votes expected for each Buchanan vote, and m2 is the number of Gore votes expected for each Buchanan vote. To create a first-degree polynomial equation with zero offset, you must create a custom linear equation. As described in Fitting Custom Models on page 2-93, you can create a custom equation using the Fitting GUI by selecting
2-110
Fitting Data
Custom Equations from the Type of fit list, and then clicking the New Equation button. The Linear Equations pane of the Create Custom Equation GUI is shown below.
Before fitting, you should exclude the data point associated with the absentee ballots from each data set because these voters did not use the butterfly ballot. As described in Marking Outliers on page 2-28, you can exclude individual data points from a fit either graphically or numerically using the Exclude GUI. For this example, you should exclude the data numerically. The index of the absentee ballot data is given by
ind = find(strcmp(counties,'Absentee Ballots')) ind = 68
2-111
The exclusion rule is named AbsenteeVotes. You use the Fitting GUI to associate an exclusion rule with the data set to be fit. For each data set, perform a robust fit with bisquare weights using the
FlaElection equation defined above. For comparison purposes, also perform
a regular linear least squares fit. Refer to Robust Least Squares on page 2-55 for a description of the robust fitting methods provided by the toolbox. You can identify the Palm Beach County data in the scatter plot by using the data tips feature, and knowing the index number of the data point.
ind = find(strcmp(counties,'Palm Beach')) ind = 50
2-112
Fitting Data
The Fit Editor and the Fit Options GUI are shown below for a robust fit.
The data, robust and regular least squares fits, and residuals for the buchanan vs. bush data set are shown below.
2-113
The graphical results show that the linear model is reasonable for the majority of data points, and the residuals appear to be randomly scattered around zero. However, two residuals stand out. The largest residual corresponds to Palm Beach County. The other residual is at the largest predictor value, and corresponds to Miami/Dade County. The numerical results are shown below. The inverse slope of the robust fit indicates that Buchanan should receive one vote for every 197.4 Bush votes.
The data, robust and regular least squares fits, and residuals for the buchanan vs. gore data set are shown below.
2-114
Fitting Data
Again, the graphical results show that the linear model is reasonable for the majority of data points, and the residuals appear to be randomly scattered around zero. However, three residuals stand out. The largest residual corresponds to Palm Beach County. The other residuals are at the two largest predictor values, and correspond to Miami/Dade County and Broward County. The numerical results are shown below. The inverse slope of the robust fit indicates that Buchanan should receive one vote for every 189.3 Gore votes.
Using the fitted slope value, you can determine the expected number of votes that Buchanan should have received for each fit. For the Buchanan versus Bush data, you evaluate the fit at a predictor value of 152,951. For the Buchanan versus Gore data, you evaluate the fit at a predictor value of 269,732. These results are shown below for both data sets and both fits. Expected Buchanan Votes in Palm Beach County Expected Buchanan Votes 814 775 1246 1425
The robust results for the Buchanan versus Bush data suggest that Buchanan received 3411 775 = 2636 excess votes, while robust results for the Buchanan versus Gore data suggest that Buchanan received 3411 1425 = 1986 excess votes. The margin of victory for George Bush is given by
margin = sum(bush) sum(gore)
2-115
margin = 537
Therefore, the voter intention comes into play because in both cases, the margin of victory is less than the excess Buchanan votes. In conclusion, the analysis of the 2000 United States presidential election results for the state of Florida suggests that the Reform Party candidate received an excess number of votes in Palm Beach County, and that this excess number was a crucial factor in determining the election outcome. However, additional analysis is required before a final conclusion can be made.
Nonparametric Fitting
In some cases, you are not concerned about extracting or interpreting fitted parameters. Instead, you might simply want to draw a smooth curve through your data. Fitting of this type is called nonparametric fitting. The Curve Fitting Toolbox supports these nonparametric fitting methods: Interpolants Estimate values that lie between known data points. Smoothing spline Create a smooth curve through the data. You adjust the level of smoothness by varying a parameter that changes the curve from a least squares straight-line approximation to a cubic spline interpolant. For more information about interpolation, refer to Polynomials and Interpolation and the interp1 function in the MATLAB documentation.
Interpolants
Interpolation is a process for estimating values that lie between known data points. The supported interpolant methods are shown below.
2-116
Fitting Data
Interpolant Methods Method Linear Nearest neighbor Description Linear interpolation. This method fits a different linear polynomial between each pair of data points. Nearest neighbor interpolation. This method sets the value of an interpolated point to the value of the nearest data point. Therefore, this method does not generate any new data points. Cubic spline interpolation. This method fits a different cubic polynomial between each pair of data points. Piecewise cubic Hermite interpolation (PCHIP). This method preserves monotonicity and the shape of the data.
Cubic spline
Shape-preserving
The type of interpolant you should use depends on the characteristics of the data being fit, the required smoothness of the curve, speed considerations, postfit analysis requirements, and so on. The linear and nearest neighbor methods are fast, but the resulting curves are not very smooth. The cubic spline and shape-preserving methods are slower, but the resulting curves are often very smooth. For example, the nuclear reaction data from the file carbon12alpha.mat is shown below with a nearest neighbor interpolant fit and a shape-preserving (PCHIP) interpolant fit. Clearly, the nearest neighbor interpolant does not follow the data as well as the shape-preserving interpolant. The difference between these two fits can be important if you are interpolating. However, if you want to integrate the data to get a sense of the total unormalized strength of the reaction, then both fits provide nearly identical answers for reasonable integration bin widths.
2-117
Note Goodness-of-fit statistics, prediction bounds, and weights are not defined for interpolants. Additionally, the fit residuals are always zero (within computer precision) because interpolants pass through the data points.
Interpolants are defined as piecewise polynomials because the fitted curve is constructed from many pieces. For cubic spline and PCHIP interpolation, each piece is described by four coefficients, which are calculated using a cubic (third-degree) polynomial. Refer to the spline function for more information about cubic spline interpolation. Refer to the pchip function for more information about shape-preserving interpolation, and for a comparison of the two methods. It is possible to fit a single global polynomial interpolant to data, with a degree one less than the number of data points. However, such a fit can have wildly erratic behavior between data points. In contrast, the piecewise polynomials described here always produce a well-behaved fit, and thus they
2-118
Fitting Data
are more flexible than parametric polynomials and can be effectively used for a wider range of data sets.
Smoothing Spline
If your data is noisy, you might want to fit it using a smoothing spline. Alternatively, you can use one of the smoothing methods described in Smoothing Data on page 2-9. The smoothing spline s is constructed for the specified smoothing parameter p and the specified weights wi. The smoothing spline minimizes
If the weights are not specified, they are assumed to be 1 for all data points. p is defined between 0 and 1. p = 0 produces a least squares straight line fit to the data, while p = 1 produces a cubic spline interpolant. If you do not specify the smoothing parameter, it is automatically selected in the interesting range. The interesting range of p is often near 1/(1+h3/6) where h is the average spacing of the data points, and it is typically much smaller than the allowed range of the parameter. Because smoothing splines have an associated smoothing parameter, you might consider these fits to be parametric in that sense. However, smoothing splines are also piecewise polynomials like cubic spline or shape-preserving interpolants and are considered a nonparametric fit type in this guide.
Note The smoothing spline algorithm used by the Curve Fitting Toolbox is based on the csaps function included with the Spline Toolbox. Refer to the csaps reference pages for detailed information about smoothing splines.
The nuclear reaction data from the file carbon12alpha.mat is shown below with three smoothing spline fits. The default smoothing parameter (p = 0.99) produces the smoothest curve. The cubic spline curve (p = 1) goes through all the data points, but is not quite as smooth. The third curve (p = 0.95)
2-119
misses the data by wide margin and illustrates how small the interesting range of p can be.
As shown below, you can fit the data with a cubic spline interpolant by selecting Interpolant from the Type of fit list.
2-120
Fitting Data
The results shown below indicate that goodness-of-fit statistics are not defined for interpolants.
As described in Interpolants on page 2-116, cubic spline interpolation is defined as a piecewise polynomial that results in a structure of coefficients. The number of pieces in the structure is one less than the number of fitted data points, and the number of coefficients for each piece is four because the polynomial degree is three. The toolbox does not allow you to access the structure of coefficients. As shown below, you can fit the data with a smoothing spline by selecting Smoothing Spline in the Type of fit list.
2-121
The level of smoothness is given by the Smoothing Parameter. The default smoothing parameter value depends on the data set, and is automatically calculated by the toolbox after you click the Apply button. For this data set, the default smoothing parameter is close to 1, indicating that the smoothing spline is nearly cubic and comes very close to passing through each data point. Create a fit for the default smoothing parameter and name it Smooth1. If you do not like the level of smoothing produced by the default smoothing parameter, you can specify any value between 0 and 1. A value of 0 produces a linear polynomial fit, while a value of 1 produces a piecewise cubic polynomial fit that passes through all the data points. For comparison purposes, create another smoothing spline fit using a smoothing parameter of 0.5 and name the fit Smooth2. The numerical results for the smoothing spline fit Smooth1 are shown below.
The data and fits are shown below. The default abscissa scale was increased to show the fit behavior beyond the data limits. You change the axes limits with Tools > Axes Limit Control menu item.
2-122
Fitting Data
Note that the default smoothing parameter produces a curve that is smoother than the interpolant, but is a good fit to the data. In this case, decreasing the smoothing parameter from the default value produces a curve that is smoother still, but is not a good fit to the data. As the smoothing parameter increases beyond the default value, the associated curve approaches the cubic spline interpolant.
2-123
Selected Bibliography
[1] Draper, N.R and H. Smith, Applied Regression Analysis, 3rd Ed., John Wiley & Sons, New York, 1998. [2] Bevington, P.R. and D.K. Robinson, Data Reduction and Error Analysis for the Physical Sciences, 2nd Ed., WCB/McGraw-Hill, Boston, 1992. [3] Daniel, C. and F.S. Wood, Fitting Equations to Data, John Wiley & Sons, New York, 1980. [4] Branch, M.A., T.F. Coleman, and Y. Li, A Subspace, Interior, and Conjugate Gradient Method for Large-Scale Bound-Constrained Minimization Problems, SIAM Journal on Scientific Computing, Vol. 21, Number 1, pp. 1-23, 1999. [5] Levenberg, K., A Method for the Solution of Certain Problems in Least Squares, Quart. Appl. Math, Vol. 2, pp. 164-168, 1944. [6] Marquardt, D., An Algorithm for Least Squares Estimation of Nonlinear Parameters, SIAM J. Appl. Math, Vol. 11, pp. 431-441, 1963. [7] DuMouchel, W. and F. OBrien, Integrating a Robust Option into a Multiple Regression Computing Environment, in Computing Science and Statistics: Proceedings of the 21st Symposium on the Interface, (K. Berk and L. Malone, eds.), American Statistical Association, Alexandria, VA, pp. 297-301, 1989. [8] DeAngelis, D.J., J.R. Calarco, J.E. Wise, H.J. Emrich, R. Neuhausen, and H. Weyand, Multipole Strength in 12C from the (e,e) Reaction for Momentum Transfers up to 0.61 fm-1, Phys. Rev. C, Vol. 52, Number 1, pp. 61-75 (1995).
2-124
3
Programmatic Curve Fitting
This chapter describes how to use objects and methods in the Curve Fitting Toolbox for M-file programming. Curve Fitting Objects and Methods (p. 3-3) Interactive Code Generation (p. 3-24) Curve fitting outside of the Curve Fitting Tool Using code from the Curve Fitting Tool
Note Much of the basic statistical theory underlying curve fitting methods was presented in Chapter 2, Interactive Curve Fitting, and will not be repeated here. See, for example Moving Average Filtering on page 2-13 Lowess and Loess: Local Regression Smoothing on page 2-15 Savitzky-Golay Filtering on page 2-20 Transforming Response Data on page 2-41 Basic Assumptions About the Error on page 2-48 The Least Squares Fitting Method on page 2-49 Evaluating the Goodness of Fit on page 2-71 Interpolants on page 2-116 Smoothing Spline on page 2-119
3-2
Overview
In MATLAB programming, all workspace variables are objects of a particular class. Familiar examples of MATLAB classes are double, char, and function_handle. MATLAB also allows you to create custom classes, using object-oriented programming. Methods are functions that operate exclusively on objects of a particular class. Data types package together objects and methods so that the methods operate exclusively on objects of their own type, and not on objects of other types. A clearly defined encapsulation of objects and methods is the goal of object-oriented programming. The Curve Fitting Toolbox provides you with two new MATLAB data types for performing curve fitting: fittype Objects allow you to encapsulate information describing a parametric model for your data. Methods allow you to access and modify that information. cfit A subtype of fittype. Objects capture information from a particular fit by assigning values to coefficients, confidence intervals, fit statistics, etc. Methods allow you to post-process the fit, through plotting, extrapolation, integration, etc.
3-3
Because cfit is a subtype of fittype, cfit inherits all fittype methods. In other words, you can apply fittype methods to both fittype and cfit objects, but cfit methods are used exclusively with cfit objects. As an example, the fittype method islinear, which determines if a model is linear or nonlinear, would apply equally well before or after a fit; that is, to both fittype and cfit objects. On the other hand, the cfit methods coeffvalues and confint, which, respectively, return fit coefficients and their confidence intervals, would make no sense if applied to a general fittype object which describes a parametric model with undetermined coefficients.
3-4
Note that the display method for fittype objects returns only basic information, piecing together outputs from formula and indepnames.
Description Get input argument names Get fit category Get coefficient names Get dependent variable name Evaluate model at specified predictors Construct fittype object Get formula string Get independent variable name Determine if model is linear Get number of input arguments Get number of coefficients Get problem-dependent parameter names Get name of model
3-5
The methods listed in the following table are available exclusively forcfit objects. Curve Fit Method
cfit coeffvalues confint differentiate integrate plot predint probvalues
Description Construct cfit object Get coefficient values Get confidence intervals for fit coefficients Differentiate fit Integrate fit Plot fit Get prediction intervals Get problem-dependent parameter values
A complete list of methods for a curve fitting object can be obtained with the MATLAB methods command. For example,
f = fittype('a*x^2+b*exp(n*x)'); methods(f) Methods for class fittype: argnames cat category char clearhandles coeffnames constants dependnames derivexpr disp display exist feval fevalexpr fitoptions fittype formula getcoeffmatrix horzcat indepnames integexpr isempty islinear linearexprs linearterms loadobj nargin nargout nonlinearcoeffs numargs numcoeffs prettyname probnames saveobj setoptions startpt subsasgn subsref symvar type vertcat
3-6
Note that some of the methods listed by methods do not appear in the tables above, and do not have reference pages in the documentation for the Curve Fitting Toolbox. These additional methods are generally low-level operations used by the Curve Fitting Tool, and not of general interest when writing curve fitting applications. There are no global accessor methods, comparable to getfield and setfield, available for fittype objects. Access is limited to the methods listed above. This is because many of the properties of fittype objects are derived from other properties, for which you do have access. For example,
f = fittype('a*cos( b*x-c )') f = General model: f(a,b,c,x) = a*cos( b*x-c ) formula(f) ans = a*cos( b*x-c ) argnames(f) ans = 'a' 'b' 'c' 'x'
You construct the fittype object f by giving the formula, so you do have write access to that basic property of the object. You have read access to that property through the formula method. You also have read access to the argument names of the object, through the argnames method. You dont, however, have direct write access to the argument names, which are derived from the formula. If you want to set the argument names, set the formula.
3-7
has previously been stored in MATLAB variables) or any of the more specialized MATLAB functions for reading data from particular file types.
2 If your data is noisy, you might want to smooth it using the smooth
function. Smoothing is used to identify major trends in the data that can assist you in choosing an appropriate family of parametric models. If a parametric model is not evident or appropriate, smoothing can be an end in itself, providing a nonparametric fit of the data.
Note Smoothing estimates the center of the distribution of the response at each predictor. It invalidates the assumption that errors in the data are normally distributed, and so also invalidates the methods used to compute confidence and prediction intervals. Accordingly, once a parametric model is identified through smoothing, the original data should be passed to the fit function.
3-8
Fitting Toolbox or a custom model that you defineis specified as a fittype object using the fittype function. Library models can be displayed with the cflibhelp function.
4 A fit options structure can be created for the fit using the fitoptions
function. Fit options specify things like weights for the data, fitting methods, and low-level options for the fitting algorithm.
5 An exclusion rule can be created for the fit using the excludedata function.
Exclusion rules indicate which data values will be treated as outliers and excluded from the fit.
6 Data, a fittype object, and (optionally) a fit options structure and an
exclusion rule are all passed to the fit function to perform the fit. The fit function returns a cfit object that encapsulates the computed coefficients and the fit statistics.
7 cfit objects returned by the fit function can then be passed to a variety
Examples
The following examples illustrate the standard workflow outlined in Workflow for Object-Oriented Fitting on page 3-7. Further examples of programmatic fitting can be found in the reference pages for individual curve fitting methods. Example: Smoothing Data I (p. 3-10) Example: Smoothing Data II (p. 3-11) Example: Excluding Data (p. 3-12) Example: Specifying Fit Options (p. 3-15) Example: Robust Fitting (p. 3-16) Basic use of smooth Smoothing and robust smoothing Combining methods for robust fitting and residual analysis Using fitoptions with fit Robust fitting and outlier analysis
3-9
Example: Differentiating and Integrating a Fit (p. 3-18) Example: Prediction Intervals (p. 3-22)
The 24-by-3 array count contains traffic counts at three intersections for each hour of the day. First, use a moving average filter with a 5-hour span to smooth all of the data at once (by linear index) :
c = smooth(count(:)); C1 = reshape(c,24,3);
Second, use the same filter to smooth each column of the data separately:
C2 = zeros(24,3); for I = 1:3, C2(:,I) = smooth(count(:,I)); end
3-10
Smooth the data using the loess and rloess methods with a span of 10%:
yy1 = smooth(x,y,0.1,'loess'); yy2 = smooth(x,y,0.1,'rloess');
3-11
[xx,ind] = sort(x); subplot(2,1,1) plot(xx,y(ind),'b.',xx,yy1(ind),'r-') set(gca,'YLim',[-1.5 3.5]) legend('Original Data','Smoothed Data Using ''loess''',... 'Location','NW') subplot(2,1,2) plot(xx,y(ind),'b.',xx,yy2(ind),'r-') set(gca,'YLim',[-1.5 3.5]) legend('Original Data','Smoothed Data Using ''rloess''',... 'Location','NW')
Note that the outliers have less influence on the robust method.
3-12
Use the vote counts for the two major party candidates, Bush and Gore, as predictors for the vote counts for third-party candidate Buchanan, and plot the scatters:
plot(bush,buchanan,'rs') hold on plot(gore,buchanan,'bo') legend('Bush data','Gore data')
Assume a model where a fixed proportion of Bush or Gore voters choose to vote for Buchanan:
f = fittype({'x'}) f = Linear model: f(a,x) = a*x
Exclude the data from absentee voters, who did not use the controversial butterfly ballot:
absentee = find(strcmp(counties,'Absentee Ballots')); nobutterfly = excludedata(bush,buchanan,'indices',absentee);
3-13
Perform a bisquare weights robust fit of the model to the two data sets, excluding absentee voters:
bushfit = fit(bush,buchanan,f,'Exclude',nobutterfly,'Robust','on'); gorefit = fit(gore,buchanan,f,'Exclude',nobutterfly,'Robust','on');
Robust fits give outliers a low weight, so large residuals from a robust fit can be used to identify the outliers:
figure plot(bushfit,bush,buchanan,'rs','residuals') hold on plot(gorefit,gore,buchanan,'bo','residuals')
Large residuals can be identified as those outside the range [-500 500]:
bushoutliers = excludedata(bush,bushres,'range',[-500 500]);
3-14
The outliers for the two data sets correspond to the following counties:
counties(bushoutliers) ans = 'Miami-Dade' 'Palm Beach' counties(goreoutliers) ans = 'Broward' 'Miami-Dade' 'Palm Beach'
Miami-Dade and Broward counties correspond to the largest predictor values. Palm Beach county, the only county in the state to use the butterfly ballot, corresponds to the largest residual values.
Modifying the default fit options structure is useful when you want to set the Normalize, Exclude, or Weights fields, and then fit your data using the same options with different fitting methods. For example:
load f1 = f2 = f3 = census fit(cdate,pop,'poly3',options); fit(cdate,pop,'exp1',options); fit(cdate,pop,'cubicsp',options);
3-15
Data-dependent fit options are returned in the third output argument of the fit function. For example:
[f,gof,out] = fit(cdate,pop,'smooth'); smoothparam = out.p smoothparam = 0.0089
Identify outliers as points at a distance greater than 1.5 standard deviations from the baseline model, and refit the data with the outliers excluded:
fdata = feval(fit1,xdata);
3-16
Compare the effect of excluding the outliers with the effect of giving them lower bisquare weight in a robust fit:
fit3 = fit(xdata,ydata,f,'StartPoint',[1 1],'Robust','on');
Plot the data, the outliers, and the results of the fits:
plot(fit1,'r-',xdata,ydata,'k.',outliers,'m*') hold on plot(fit2,'c--') plot(fit3,'b:') xlim([0 2*pi])
3-17
hold on plot(fit3,xdata,ydata,'bx','residuals')
3-18
3-19
Note that derivatives can also be computed and plotted directly with the cfit plot method, as follows:
plot(fit1,xdata,ydata,{'fit','deriv1','deriv2'})
The plot method, however, does not return data on the derivatives. Find the integral of the fit at the predictors:
3-20
int = integrate(fit1,xdata,0);
Note that integrals can also be computed and plotted directly with the cfit
plot method, as follows: plot(fit1,xdata,ydata,{'fit','integral'})
The plot method, however, does not return data on the integral.
3-21
5])
5])
5])
5])
3-22
3-23
Overview
One way to quickly assemble curve fitting objects and methods into useful programs is to generate an M-file from a session in the Curve Fitting Tool. In this way, interactive analysis of a single data set is transformed into a reusable function for batch processing of multiple data sets. The generated M-file can be used without modification, or it can be edited and customized as needed. To generate an M-file from a session in the Curve Fitting Tool, select the menu item File > Generate M-file. The M-file captures the following information from the Curve Fitting Tool: Names of variables, fits, and residuals Fit options, such as whether the data should be normalized, initial values for the coefficients, and the fitting method Curve fitting objects and methods used to create the fit You can recreate your Curve Fitting Tool session by calling the M-file from the command line with your original data as input arguments. You can also call the M-file with new data, applying the assembled curve fitting methods to re-compute curve fitting objects.
3-24
% Data from dataset "census": % X = cdate: % Y = pop: % Unweighted % % This function was automatically generated on 22-Jul-2006 10:09:39 % Set up figure to receive datasets and fits f_ = clf; figure(f_); set(f_,'Units','Pixels','Position',[183.6 68.1 814.4 571.8]); legh_ = []; legt_ = {}; % handles and text for legend xlim_ = [Inf -Inf]; % limits of x axis ax_ = axes; set(ax_,'Units','normalized','OuterPosition',[0 0 1 1]);
3-25
% --- Plot data originally in dataset "census" cdate = cdate(:); pop = pop(:); h_ = line(cdate,pop,'Parent',ax_,'Color',[0.333333 0 0.666667],... 'LineStyle','none', 'LineWidth',1,... 'Marker','.', 'MarkerSize',12); xlim_(1) = min(xlim_(1),min(cdate)); xlim_(2) = max(xlim_(2),max(cdate)); legh_(end+1) = h_; legt_{end+1} = 'census'; % Nudge axis limits beyond data limits if all(isfinite(xlim_)) xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_); set(ax_,'XLim',xlim_) end
% --- Create fit "censusfit" fo_ = fitoptions('method','NonlinearLeastSquares','Lower',[0 0 ]); ok_ = ~(isnan(cdate) | isnan(pop)); st_ = [1 1 ]; set(fo_,'Startpoint',st_); ft_ = fittype('a*(x-b)^3',... 'dependent',{'y'},'independent',{'x'},... 'coefficients',{'a', 'b'}); % Fit this model using new data cf_ = fit(cdate(ok_),pop(ok_),ft_,fo_); % Or use coefficients from the original fit: if 0 cv_ = {1.359437793879e-005, 1724.696932124}; cf_ = cfit(ft_,cv_{:}); end
3-26
% Plot this fit h_ = plot(cf_,'fit',0.95); legend off; % turn off legend from plot method call set(h_(1),'Color',[1 0 0],... 'LineStyle','-', 'LineWidth',2,... 'Marker','none', 'MarkerSize',6); legh_(end+1) = h_(1); legt_{end+1} = 'censusfit'; % Done plotting data and fits. Now finish up loose ends. hold off; h_ = legend(ax_,legh_,legt_,'Location','NorthEast'); set(h_,'Interpreter','none'); xlabel(ax_,''); % remove x label ylabel(ax_,''); % remove y label
A quick look through the code shows that it has automatically assembled for you many of the curve fitting methods from the Curve Fitting Toolbox, such as fitoptions, fittype, fit, and plot.
3-27
To run the M-file without modification on new data, pass the new data to the function as input arguments:
newpop = pop + 50*randn(size(pop)); myfit(cdate,newpop)
3-28
The M-file recomputes the cfit object for the fit and displays the new data with the new fit.
The function accepts predictor and response data for a predefined fit type. The inputs are called cdate and pop because those were the predictor and response variables used in the Curve Fitting Tool session that produced the file. If you like, you can find and replace the input names here and elsewhere in the file to indicate a more generic application of the fit. Note that the file, as generated, returns no outputs. It simply applies the fit to the input data and displays the results. The next component of the M-file, after the help information, is the following:
% Set up figure to receive datasets and fits f_ = clf; figure(f_); set(f_,'Units','Pixels','Position',[183.6 68.1 814.4 571.8]); legh_ = []; legt_ = {}; % handles and text for legend xlim_ = [Inf -Inf]; % limits of x axis ax_ = axes; set(ax_,'Units','normalized','OuterPosition',[0 0 1 1]); set(ax_,'Box','on'); axes(ax_); hold on;
These are Handle Graphics methods, applied to Handle Graphics objects that encapsulate information on the display of the figure window, the legend, and the axes. This component of the M-file creates a figure for plotting that
3-29
mimics the Plotting GUI in the Curve Fitting Tool. Note that at the end of this component hold is toggled on. This allows the input data and the fit to be plotted together on the axes. The next component of the M-file plots the input data, using Handle Graphics methods to set properties of the line object, the axes, and the legend that mimic the plot in the Curve Fitting Tool:
% --- Plot data originally in dataset "census" cdate = cdate(:); pop = pop(:); h_ = line(cdate,pop,'Parent',ax_,'Color',[0.333333 0 0.666667],... 'LineStyle','none', 'LineWidth',1,... 'Marker','.', 'MarkerSize',12); xlim_(1) = min(xlim_(1),min(cdate)); xlim_(2) = max(xlim_(2),max(cdate)); legh_(end+1) = h_; legt_{end+1} = 'census';
The next component nudges the x-axis limits, leaving a space of 1% of the x data range between the data and the vertical axes. This gives a tight plot, while preventing data from being plotted directly onto the vertical axes, where it would be difficult to see.
% Nudge axis limits beyond data limits if all(isfinite(xlim_)) xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_); set(ax_,'XLim',xlim_) end
After all of the preliminaries, the M-file gets down to the business of fitting the data. The next component of the M-file uses fitoptions and fittype to create a fit options structure fo_ and a fittype object ft_ that encapsulate, respectively, information on the fitting method and the model. The inputs to fitoptions and fittype are read from the Fitting GUI in the Curve Fitting Tool.
% --fo_ = ok_ = st_ = Create fit "censusfit" fitoptions('method','NonlinearLeastSquares','Lower',[0 0 ]); ~(isnan(cdate) | isnan(pop)); [1 1 ];
3-30
The fit method is then called to fit the predefined fit type to the input data. Note that NaNs are removed from the data before the fit, using the logical vector ok_ defined in the previous component.
% Fit this model using new data cf_ = fit(cdate(ok_),pop(ok_),ft_,fo_);
The next component of the M-file is a little obscure, since it uses a conditional with a guard condition that is always false (0). This code is generated intentionally, to give you the option of plotting the new input data against a fit based on the old data (the data that was originally imported into the Curve Fitting Tool). To do so, simply change the 0 to true. The modified M-file then uses the cfit method to set the coefficients of the cfit object cf_ to the stored values computed with the old data. If you do not wish to do this, leave this component of the M-file alone, or delete it.
% Or use coefficients from the original fit: if 0 cv_ = {1.359437793879e-005, 1724.696932124}; cf_ = cfit(ft_,cv_{:}); end
With the fitting complete, the M-file calls the plot method to plot the cfit object cf_. Note that plot is called with the default plot type 'fit' (data and fit), but is also passed a confidence level of 0.95. To use this confidence level to plot prediction bounds for the fit or for new observations, change 'fit' to 'predfunc' or 'predobs', respectively. The rest of the code in this component of the M-file is more Handle Graphics, along the lines of previous components, setting Handle Graphics object properties that mimic the plot of the fit in the Curve Fitting Tool.
% Plot this fit h_ = plot(cf_,'fit',0.95); legend off; % turn off legend from plot method call set(h_(1),'Color',[1 0 0],...
3-31
Finally, the M-file takes care of loose ends: hold is toggled off to its default behavior, the legend is positioned, and the x and y labels ('x' and 'y' by default) are removed.
% Done plotting data and fits. Now finish up loose ends. hold off; h_ = legend(ax_,legh_,legt_,'Location','NorthEast'); set(h_,'Interpreter','none'); xlabel(ax_,''); % remove x label ylabel(ax_,''); % remove y label
Note the change in the function name from myfit to myfit2. The modified M-file should then be saved to a file named myfit2.m. You might also want to return goodness-of-fit statistics, which the M-file, by default, does not compute. You would have to modify both the call to fit:
[cf_,gof] = fit(cdate(ok_),pop(ok_),ft_,fo_);
You might also want to alter the call to plot, say to show prediction intervals for new observations:
3-32
h_ = plot(cf_,'predobs',0.95);
Running the M-file with the above modifications on the new data from Running the Generated M-file on page 3-27 produces the following output to the command line:
[c,g] = myfit2(cdate,newpop) c = General model: c(x) = a*(x-b)^3 Coefficients (with 95% confidence bounds): a = 7.211e-006 (-2.389e-006, 1.681e-005) b = 1670 (1548, 1792) g = sse: 5.5691e+004 rsquare: 0.6561 dfe: 19 adjrsquare: 0.6380 rmse: 54.1398
3-33
3-34
4
Functions By Category
Preprocessing Data (p. 4-2) Fitting Data (p. 4-2) Curve Fit Methods (p. 4-2) Fit Type Methods (p. 4-3) Postprocessing Fits (p. 4-4) Information and Help (p. 4-5) Prepare data for fitting Fit models to data Methods for cfit objects Methods for fittype objects Analyze fit results Information on models and objects
Functions By Category
Preprocessing Data
cftool excludedata smooth
Open Curve Fitting Tool Exclude data from fit Smooth response data
Fitting Data
cftool fit fitoptions fittype get set
Open Curve Fitting Tool Fit a model to data Create or modify fit options structure Constructor for fittype object Get fit options structure field names and values Assign values in fit options structure
Input argument names of cfit or fittype object Category of fit of cfit or fittype object Constructor for cfit object Coefficient names of cfit or fittype object Coefficient values of cfit object Confidence intervals for fit coefficients of cfit object
4-2
dependnames differentiate feval formula indepnames integrate islinear numargs numcoeffs plot predint probnames probvalues type
Dependent variable of cfit or fittype object Differentiate cfit object Evaluate cfit or fittype object Formula of cfit or fittype object Independent variable of cfit or fittype object Integrate cfit object Determine if cfit or fittype object is linear Number of input arguments of cfit or fittype object Number of coefficients of cfit or fittype object Plot cfit object Prediction intervals for cfit object Problem-dependent parameter names of cfit or fittype object Problem-dependent parameter values of cfit or fittype object Name of cfit or fittype object
Input argument names of cfit or fittype object Category of fit of cfit or fittype object Coefficient names of cfit or fittype object
4-3
Functions By Category
dependnames feval fittype formula indepnames islinear numargs numcoeffs probnames probvalues type
Dependent variable of cfit or fittype object Evaluate cfit or fittype object Constructor for fittype object Formula of cfit or fittype object Independent variable of cfit or fittype object Determine if cfit or fittype object is linear Number of input arguments of cfit or fittype object Number of coefficients of cfit or fittype object Problem-dependent parameter names of cfit or fittype object Problem-dependent parameter values of cfit or fittype object Name of cfit or fittype object
Postprocessing Fits
cfit cftool coeffvalues confint differentiate integrate plot
Constructor for cfit object Open Curve Fitting Tool Coefficient values of cfit object Confidence intervals for fit coefficients of cfit object Differentiate cfit object Integrate cfit object Plot cfit object
4-4
predint probvalues
Prediction intervals for cfit object Problem-dependent parameter values of cfit or fittype object
4-5
Functions By Category
4-6
5
Functions Alphabetical List
argnames
Example
See Also
5-2
category
Example
See Also
5-3
cfit
Note cfit is called by the fit function when fitting fittype objects to data. To create a cfit object that is the result of a regression, use fit. You should only call cfit directly if you want to assign values to coefficients and problem parameters of a fittype object without performing a fit.
Example
f = fittype('a*x^2+b*exp(n*x)') f = General model: f(a,b,n,x) = a*x^2+b*exp(n*x) c = cfit(f,1,10.3,-1e2) c = General model: c(x) = a*x^2+b*exp(n*x) Coefficients: a = 1 b = 10.3 n = -100 fit, fittype
See Also
5-4
cflibhelp
in the curve-fitting library. Library names are used as input arguments in the fit and fittype functions.
cflibhelp libtype restricts the display of names, equations, and descriptions to the subcategory of library models libtype. Recognized library types are listed in the table below. libtype distribution exponential fourier gaussian interpolant
Description Probability distribution models One-term and two-term exponential models Fourier polynomial models Sums of Gaussian models, up to eight terms Interpolating models, including linear, nearest neighbor, cubic spline, and shape-preserving cubic spline Polynomial models, up to ninth degree One-term and two-term power models Ratios of polynomial models, up to degree 5 in both the numerator and the denominator Sums of sinusoidal models, up to eight terms Cubic spline and smoothing spline models
For more information on library models, refer to the Library Models on page 2-59 section of the Users Guide.
5-5
cflibhelp
Example
cflibhelp polynomial POLYNOMIAL MODELS MODELNAME poly1 poly2 poly3 ... poly9 EQUATION Y = p1*x+p2 Y = p1*x^2+p2*x+p3 Y = p1*x^3+p2*x^2+...+p4 Y = p1*x^9+p2*x^8+...+p10
See Also
fit, fittype
5-6
cftool
Purpose Syntax
Description
xdata and response data ydata. xdata and ydata must be vectors of the same size. Infs, NaNs, and imaginary parts of complex numbers are
Fitting Tool for weighting data in subsequent fits. w must be the same length as xdata and ydata.
Remarks
The Curve Fitting Tool is an interactive environment presented in the form of a graphical user interface. It allows you to Import data from the MATLAB workspace Explore the data graphically Preprocess the data for fitting using exclusion rules and smoothing Fit a variety of library or custom models to the data Generate relevant regression statistics Postprocess the fit through interpolation, extrapolation, differentiation, and integration Export results back to the MATLAB workspace for further analysis and visualization The main Curve Fitting Tool interface is shown below.
5-7
cftool
Clicking the Data, Fitting, Exclude, Plotting, or Analysis buttons opens associated GUIs, described below. In the figure above, data was imported from the MAT-file census using the Data GUI and fit with a quadratic polynomial using the Fitting GUI. Residuals were displayed in the subplot by selecting View > Residuals > Line Plot. For a complete example that uses many of these GUIs, refer to Chapter 1, Getting Started. The Data GUI The Data GUI allows you to Import, name, preview, and delete data sets
5-8
cftool
Smooth data using a variety of methods The Data GUI is shown below with the census data loaded.
Refer to Importing, Viewing, and Preprocessing Data on page 2-2 for more information about the Data GUI. The Fitting GUI The Fitting GUI allows you to Fit your data using parametric or nonparametric models Set algorithm options for nonlinear fits Compare coefficients and goodness of fit statistics from different models Keep track of all data sets and fits in the current session
5-9
cftool
The Fitting GUI is shown below with the results of fitting the census data with a quadratic polynomial.
5-10
cftool
The Exclude GUI The Exclude GUI allows you to create exclusion rules for a data set. An exclusion rule identifies data to be excluded while fitting. The excluded data can be individual data points, or a section of predictor or response data. The Exclude GUI is shown below with the first two points of the census data marked for exclusion.
The Plotting GUI The Plotting GUI allows you to determine the data sets and fits displayed by the Curve Fitting Tool. The Plotting GUI is shown below with the census data and the fit poly2 checked for display.
5-11
cftool
The Analysis GUI The Analysis GUI allows you to Interpolate, extrapolate, differentiate, or integrate a fit Display the results of your analysis numerically or in a plot The Analysis GUI is shown below with a numerical display of the results of extrapolating the census data from the year 2000 to the year 2050 in 10-year increments.
5-12
cftool
Refer to Analyzing the Fit on page 1-18 for an example that uses the Analysis GUI.
5-13
coeffnames
Example
See Also
5-14
coeffvalues
Example
2.1130
See Also
5-15
confint
Purpose Syntax
Description
Remarks
To calculate confidence bounds, confint uses R-1 (the inverse R factor from QR decomposition of the Jacobian), the degrees of freedom for error, and the root mean squared error. This information is automatically returned by the fit function and contained within fitresult. If coefficients are bounded and one or more of the estimates are at their bounds, those estimates are regarded as fixed and do not have confidence bounds. Note that you cannot calculate confidence bounds if
category(fitresult) is 'spline' or 'interpolant'.
Example
load census fitresult = fit(cdate,pop,'poly2') fitresult = Linear model Poly2: fitresult(x) = p1*x^2 + p2*x + p3 Coefficients (with 95% confidence bounds): p1 = 0.006541 (0.006124, 0.006958) p2 = -23.51 (-25.09, -21.93)
5-16
confint
p3 =
2.113e+004
(1.964e+004, 2.262e+004)
Note that fit and confint display the confidence bounds in slightly different formats.
See Also
fit, predint
5-17
datastats
Data statistics
xds = datastats(xdata) [xds,yds] = datastats(xdata,ydata) xds = datastats(xdata) returns statistics for the column vector xdata to the structure xds. Fields in xds are listed in the table below.
Field
num max min mean median range std
Description The number of data values The maximum data value The minimum data value The mean value of the data The median value of the data The range of the data The standard deviation of the data
[xds,yds] = datastats(xdata,ydata) returns statistics for the column vectors xdata and ydata to the structures xds and yds, respectively. xds and yds contain the fields listed in the table above. xdata and ydata must be of the same size.
Remarks
If xdata or ydata contains complex values, only the real parts are used in computing the statistics. Data containing Inf or NaN are processed using the usual MATLAB rules. Compute statistics for the census data in census.mat:
load census [xds,yds] = datastats(cdate,pop) xds = num: 21 max: 1990
Example
5-18
datastats
min: mean: median: range: std: yds = num: max: min: mean: median: range: std:
1790 1890 1890 200 62.048 21 248.7 3.9 85.729 62.9 244.8 78.601
See Also
excludedata, smooth
5-19
dependnames
See Also
5-20
differentiate
Remarks
For library models with closed forms, derivatives are calculated analytically. For all other models, the first derivative is calculated using the centered difference quotient
y =
yx + h yx h 2h
where x is the value at which the derivative is calculated, h is a small number (on the order of the cube root of eps), yx+h is fun evaluated at x+h, and yxh is fun evaluated at x h. The second derivative is calculated using the expression
y =
yx + h + yx h 2 yx h2
Example
5-21
differentiate
5-22
differentiate
Note that derivatives can also be computed and plotted directly with the cfit plot method, as follows:
plot(fit1,xdata,ydata,{'fit','deriv1','deriv2'})
The plot method, however, does not return data on the derivatives.
5-23
differentiate
See Also
5-24
excludedata
below.
MethodName 'box' MethodValue
A four-element vector specifying the edges of a closed box in the xy-plane, outside of which data is to be excluded from a fit. The vector has the form [xmin xmax ymin ymax]. A two-element vector specifying the endpoints of a closed interval on the x-axis, outside of which data is to be excluded from a fit. The vector has the form [xmin xmax]. A vector of indices specifying the data points to be excluded. A two-element vector specifying the endpoints of a closed interval on the y-axis, outside of which data is to be excluded from a fit. The vector has the form [ymin ymax].
'domain'
'indices' 'range'
Remarks
You can combine data exclusion rules using logical operators. For example, to exclude data inside the box [-1 1 -1 1] or outside the domain [-2 2], use:
outliers1 = excludedata(xdata,ydata,'box',[-1 1 -1 1]); outliers2 = excludedata(xdata,ydata,'domain',[-2 2]); outliers = ~outliers1|outliers2;
5-25
excludedata
You can visualize the combined exclusion rule using random data:
xdata = -3 + 6*rand(1,1e4); ydata = -3 + 6*rand(1,1e4); plot(xdata(~outliers),ydata(~outliers),'.') axis ([-3 3 -3 3]) axis square
Example
Load the vote counts and county names for the state of Florida from the 2000 U.S. presidential election:
load flvote2k
Use the vote counts for the two major party candidates, Bush and Gore, as predictors for the vote counts for third-party candidate Buchanan, and plot the scatters:
plot(bush,buchanan,'rs') hold on plot(gore,buchanan,'bo')
5-26
excludedata
Assume a model where a fixed proportion of Bush or Gore voters choose to vote for Buchanan:
f = fittype({'x'}) f = Linear model: f(a,x) = a*x
Exclude the data from absentee voters, who did not use the controversial butterfly ballot:
absentee = find(strcmp(counties,'Absentee Ballots')); nobutterfly = excludedata(bush,buchanan,'indices',absentee);
Perform a bisquare weights robust fit of the model to the two data sets, excluding absentee voters:
bushfit = fit(bush,buchanan,f,...
5-27
excludedata
Robust fits give outliers a low weight, so large residuals from a robust fit can be used to identify the outliers:
figure plot(bushfit,bush,buchanan,'rs','residuals') hold on plot(gorefit,gore,buchanan,'bo','residuals')
Large residuals can be identified as those outside the range [-500 500]:
bushoutliers = excludedata(bush,bushres,'range',[-500 500]);
5-28
excludedata
The outliers for the two data sets correspond to the following counties:
counties(bushoutliers) ans = 'Miami-Dade' 'Palm Beach' counties(goreoutliers) ans = 'Broward' 'Miami-Dade' 'Palm Beach'
Miami-Dade and Broward counties correspond to the largest predictor values. Palm Beach county, the only county in the state to use the butterfly ballot, corresponds to the largest residual values.
See Also
fit, fitoptions
5-29
feval
Remarks
cfit or fittype objects fun can also be evaluated directly using the
following syntax.
f = cfun(x) % cfit objects; f = ffun(coef1,coef2,...,x) % fittype objects;
Example
f c X X
feval(f,1,10.3,-1e2,X) ans = 0.0349 0.6612 0.1245 3.8422 f(1,10.3,-1e2,X) ans = 0.0349 0.6612 0.1245 3.8422
5-30
feval
feval(c,X) ans = 0.0349 0.1245 0.6612 3.8422 c(X) ans = 0.0349 0.1245 0.6612 3.8422
See Also
fit, fittype
5-31
fit
Purpose Syntax
Description
data are used in the fit. You can display library model names with the cflibhelp function. The fit result is returned as a cfit object cfun.
cfun = fit(...,PropertyName,PropertyValue,...) fits the data
using specified property name/value pairs. You can display the supported property names and values for specific library models with the fitoptions function.
cfun = fit(xdata,ydata,libname,options) fits the data using the options specified by the fit options structure options. Fit options structures are created with the fitoptions function. cfun = fit(xdata,ydata,ffun,...) fits the data with the fittype object ffun. fittype objects are created with the fittype function. cfun = fit(...,'problem',values) assigns values to the problem-dependent parameters of the model before fitting. values is a scalar or a cell array with one element per parameter. [cfun,gof] = fit(...) returns goodness-of-fit statistics to the structure gof. The gof structure has the fields shown in the table below.
Field
sse rsquare
5-32
fit
Field
dfe adjrsquare rmse
Value Degrees of freedom Degree-of-freedom adjusted coefficient of determination Root mean squared error (standard error)
[cfun,gof,output] = fit(...) returns the structure output, which contains information associated with the fitting algorithm. Fields depend on the algorithm. For example, the output structure for nonlinear least squares algorithms has the fields shown in the table below.
Field
numobs numparam residuals Jacobian exitflag
Value Number of observations (response values) Number of unknown parameters (coefficients) to fit Vector of residuals Jacobian matrix Describes the exit condition of the algorithm. Positive flags indicate convergence, within tolerances. Zero flags indicate that the maximum number of function evaluations or iterations was exceeded. Negative flags indicate that the algorithm did not converge to a solution. Number of iterations Number of function evaluations Measure of first-order optimality (absolute maximum of gradient components) Fitting algorithm employed
5-33
fit
Remarks
For some nonlinear library models (rational and Weibull), and all custom nonlinear models, default initial values for coefficients are selected uniformly at random from the interval (0,1). As a result, multiple fits using the same data and model may lead to different fitted coefficients. To avoid this, initial values for coefficients can be specified through a fitoptions structure or a vector value for the StartPoint property. Alternatively, initial states for the random number generators rand and randn can be set before fitting. All other nonlinear library models automatically compute reasonable initial values. These initial values depend on the data, and are based on model-specific heuristics.
Example
5-34
fit
Create a fit options structure and a fittype object for the custom nonlinear model y = a(xb)n, where a and b are coefficients and n is a problem-dependent parameter:
s = fitoptions('Method','NonlinearLeastSquares',... 'Lower',[0,0],... 'Upper',[Inf,max(cdate)],... 'Startpoint',[1 1]); f = fittype('a*(x-b)^n','problem','n','options',s);
5-35
fit
See Also
5-36
fitoptions
Purpose Syntax
Description
Field Name
Normalize Exclude
Values Specifies whether the data is centered and scaled. Values are 'off' or 'on'. The default is 'off'. A logical vector indicating data points to exclude from the fit. The excludedata function can be used to create this vector. The default is empty. A vector of weights the same size as the response data. The default is empty. The fitting method. A complete list of supported fitting methods is given below. The default is 'None'.
Weights Method
options = fitoptions(model) creates the default fit options structure for the library or custom model specified by the string model. You can display library model names with the cflibhelp function.
5-37
fitoptions
options = fitoptions(model,fieldname1,value1,fieldname2,value2,...) creates a fit options structure for the specified model with the fields specified by the strings fieldname1, fieldname2, ... set to the values value1, value2, ... , respectively. options = fitoptions('Method',method) creates the default fit options structure for the fitting method specified by the string method.
Description Nearest neighbor interpolation Linear interpolation Piecewise cubic Hermite interpolation
options = fitoptions('Method',method,fieldname1,value1,fieldname2,value2,...)
creates the default fit options structure for the fitting method specified by the string method with the fields specified by the strings fieldname1, fieldname2, ... set to the values value1, value2, ... , respectively.
newoptions = fitoptions(options,fieldname1,value1,fieldname2,value2,...) modifies the existing fit options structure options by setting the fields specified by the strings fieldname1, fieldname2, ... to the values value1, value2, ... , respectively. The new options structure is returned in newoptions.
5-38
fitoptions
newoptions = fitoptions(options1,options2) combines the input fit options structures options1 and options2 to create the output fit options structure newoptions. If the input structures have Method
fields set to the same value, the nonempty values for the fields in options2 override the corresponding values in options1 in the output structure. If the input structures have Method fields set to different values, the output structure will have the same Method as options1, and only the values of the Normalize, Exclude, and Weights fields of options2 will override the corresponding values in options1.
Remarks
Field values in a fit options structure can be referenced with the get method and assigned with the set method. For example:
options = fitoptions('fourier1'); get(options,'Method') ans = NonlinearLeastSquares get(options,'MaxIter') ans = 400 set(options,'Maxiter',1e3); get(options,'MaxIter') ans = 1000
Field values can also be referenced and assigned using the dot notation. For example:
options.MaxIter ans = 1000 options.MaxIter = 500; options.MaxIter ans = 500
5-39
fitoptions
Additional Fit Options Additional fields in the fit options structure, beyond the default fields Normalize, Exclude, Weights, and Method, are available according to the fitting method. If the Method field has the value 'NearestInterpolant', 'LinearInterpolant', 'PchipInterpolant', or 'CubicSplineInterpolant', there are no additional fields in the fit options structure. If the Method field has the value SmoothingSpline, the SmoothingParam field is available to configure the smoothing parameter. Its value must be between 0 and 1. The default value depends on the data set. If the Method field has the value LinearLeastSquares, the additional fields available in the fit options structure are listed in the table below.
Field
Robust
Description Specifies the robust linear least squares fitting method to be used. Values are 'on', 'off', 'LAR', or 'Bisquare'. The default is 'off'. 'LAR' specifies the least absolute residual method and 'Bisquare' specifies the bisquare weights method. 'on' is equivalent to 'Bisquare', the default method.
5-40
fitoptions
Field
Lower
Description A vector of lower bounds on the coefficients to be fitted. The default value is an empty vector, indicating that the fit is unconstrained by lower bounds. If bounds are specified, the vector length must equal the number of coefficients. Individual unconstrained lower bounds can be specified by -Inf. A vector of upper bounds on the coefficients to be fitted. The default value is an empty vector, indicating that the fit is unconstrained by upper bounds. If bounds are specified, the vector length must equal the number of coefficients. Individual unconstrained upper bounds can be specified by Inf.
Upper
If the Method field has the value NonlinearLeastSquares, the additional fields available in the fit options structure are listed in the table below.
Property
Robust
Description Specifies the robust linear least squares fitting method to be used. Values are 'on', 'off', 'LAR', or 'Bisquare'. The default is 'off'. 'LAR' specifies the least absolute residual method and 'Bisquare' specifies the bisquare weights method. 'on' is equivalent to 'Bisquare', the default method.
5-41
fitoptions
Property
Lower
Description A vector of lower bounds on the coefficients to be fitted. The default value is an empty vector, indicating that the fit is unconstrained by lower bounds. If bounds are specified, the vector length must equal the number of coefficients. Individual unconstrained lower bounds can be specified by -Inf. A vector of upper bounds on the coefficients to be fitted. The default value is an empty vector, indicating that the fit is unconstrained by upper bounds. If bounds are specified, the vector length must equal the number of coefficients. Individual unconstrained upper bounds can be specified by Inf. A vector of initial values for the coefficients. The default value of StartPoint is an empty vector. If the default value is passed to the fit function, starting points for some library models are determined heuristically. For other models, the values are selected uniformly at random on the interval (0,1). The algorithm used for the fitting procedure. Values are 'Levenberg-Marquardt', 'Gauss-Newton', or 'Trust-Region'. The default is 'Trust-Region'. The maximum change in coefficients for finite difference gradients. The default is 0.1. The minimum change in coefficients for finite difference gradients. The default is 108.
Upper
StartPoint
Algorithm
DiffMaxChange DiffMinChange
5-42
fitoptions
Property
Display
Description Controls the display in the command window. 'notify', the default, displays output only if the fit does not converge. 'final' displays only the final output. 'iter' displays output at each iteration. 'off' displays no output. The maximum number of evaluations of the model allowed. The default is 600. The maximum number of iterations allowed for the fit. The default is 400. The termination tolerance on the model value. The default is 106. The termination tolerance on the coefficient values. The default is 106.
5-43
fitoptions
Note For the fields Upper, Lower, and StartPoint, the order of the entries in the vector value is the order of the coefficients returned by the coeffnames method. For example, if
f = fittype('b*x^2+c*x+a'); coeffnames(f) ans = 'a' 'b' 'c'
then setting
options.StartPoint = [1 3 5];
assigns initial values to the coefficients as follows: a = 1, b = 3, c = 5. Note that this is not the order of the coefficients in the expression used to create f with fittype.
Example
Create the default fit options structure and set the option to center and scale the data before fitting:
options = fitoptions; options.Normal = 'on'; options options = Normalize: 'on' Exclude: [1x0 double] Weights: [1x0 double] Method: 'None'
Modifying the default fit options structure is useful when you want to set the Normalize, Exclude, or Weights fields, and then fit your data using the same options with different fitting methods. For example:
load census
5-44
fitoptions
Data-dependent fit options are returned in the third output argument of the fit function. For example:
[f,gof,out] = fit(cdate,pop,'smooth'); smoothparam = out.p smoothparam = 0.0089
Example
Create a noisy sum of two Gaussian peaksone with a small width, and one with a large width:
a1 = 1; b1 = -1; c1 = 0.05; a2 = 1; b2 = 1; c2 = 50; x = (-10:0.02:10)'; gdata = a1*exp(-((x-b1)/c1).^2) + ... a2*exp(-((x-b2)/c2).^2) + ... 0.1*(rand(size(x))-.5); plot(x,gdata)
5-45
fitoptions
The algorithm is having difficulty, as indicated by the wide confidence intervals for some of the coefficients. To help the algorithm, we could specify lower bounds for the nonnegative amplitudes a1, a2 and widths c1, c2:
5-46
fitoptions
This is a much better fit. The fit can be further improved by assigning reasonable values to other fields in the fit options structure.
See Also
5-47
fittype
Purpose Syntax
Description
included in the expressions for the terms. If there is a constant term, use '1' as the corresponding expression in the cell array.
Note islinear assumes that all models specified with the syntax ffun = fittype(expr) are nonlinear models. To create a linear model with fittype that will be recognized as linear by islinear (and, importantly, by the algorithms of fit), use the syntax ffun = fittype({expr1,expr2,...,exprn}).
ffun = fittype(expr,PropertyName,PropertyValue,...) or ffun = fittype({expr1,expr2,...,exprn},PropertyName,PropertyValue,...) constructs the fittype object ffun using specified property
name/value pairs. Supported property names and values are given in the table below.
5-48
fittype
Name
'coefficients' 'dependent' 'independent' 'options' 'problem'
Value The coefficient names. Use a cell array if there are multiple names. The dependent (response) variable name The independent (predictor) variable name The default fit options for the object The problem-dependent (fixed) parameter names. Use a cell array if there are multiple names. The default is none.
Example
Construct a fittype object for a custom nonlinear model, designating n as a problem-dependent parameter and u as the independent variable:
g = fittype('a*u+b*exp(n*u)','problem','n','independent','u') g = General model: g(a,b,n,u) = a*u+b*exp(n*u)
Construct a fittype object for a custom linear model, specifying the names of the coefficients:
h = fittype({'cos(x)','1'},'coefficients',{'a1','a2'}) h = Linear model: h(a1,a2,x) = a1*cos(x) + a2
See Also
fit, cfit
5-49
formula
as a character array.
f = fittype('weibull'); formula(f) ans = a*b*x^(b-1)*exp(-a*x^b) g = fittype('cubicspline'); formula(g) ans = piecewise polynomial
See Also
5-50
get
Purpose Syntax
Description
Example
options = fitoptions('fourier1'); get(options,'Method') ans = NonlinearLeastSquares get(options,'MaxIter') ans = 400 set(options,'Maxiter',1e3); get(options,'MaxIter') ans = 1000
Field values can also be referenced and assigned using the dot notation. For example:
options.MaxIter ans = 1000 options.MaxIter = 500; options.MaxIter ans = 500
5-51
get
See Also
fitoptions, set
5-52
indepnames
Example
See Also
5-53
integrate
Example
5-54
integrate
Note that integrals can also be computed and plotted directly with the cfit plot method, as follows:
plot(fit1,xdata,ydata,{'fit','integral'})
The plot method, however, does not return data on the integral.
See Also
5-55
islinear
Note islinear assumes that all custom models specified by the fittype function using the syntax ftype = fittype('expr') are nonlinear models. To create a linear model with fittype that will be recognized as linear by islinear (and, importantly, by the algorithms of fit), use the syntax ftype = fittype({'expr1','expr2',...,'exprn'}).
Example
f = fittype('a*x+b') f = General model: f(a,b,x) = a*x+b g = fittype({'x','1'}) g = Linear model: g(a,b,x) = a*x + b h = fittype('poly1') h = Linear model Poly1: h(p1,p2,x) = p1*x + p2 islinear(f) ans = 0 islinear(g) ans =
5-56
islinear
1 islinear(h) ans = 1
See Also
fittype
5-57
numargs
See Also
5-58
numcoeffs
See Also
5-59
plot
Purpose Syntax
Description
response data using the color, marker symbol, and line style specified by the DataLineSpec formatting string. DataLineSpec strings take the same values as LineSpec strings used by the MATLAB plot function.
plot(fun,FitLineSpec,xdata,ydata,DataLineSpec) plots fun using the color, marker symbol, and line style specified by the FitLineSpec formatting string, and plots xdata and ydata using the color, marker symbol, and line style specified by the DataLineSpec formatting string. FitLineSpec and DataLineSpec strings take the same values as LineSpec strings used by the MATLAB plot function. plot(fun,xdata,ydata,outliers) plots data indicated by outliers in a different color. outliers is a logical array the same size as xdata and ydata. outliers can be computed with the excludedata function. plot(fun,xdata,ydata,outliers,OutlierLineSpec) plots outliers
using the color, marker symbol, and line style specified by the OutlierLineSpec. OutlierLineSpec strings take the same values as LineSpec strings used by the MATLAB plot function.
5-60
plot
plot types are: 'fit' Data and fit (default) 'predfunc' Data and fit with prediction bounds for the fit 'predobs' Data and fit with prediction bounds for new observations 'residuals' Residuals 'stresiduals' Standardized residuals (residuals divided by their standard deviation). 'deriv1' First derivative of the fit 'deriv2' Second derivative of the fit 'integral' Integral of the fit Plot types can be single or multiple, with multiple plot types specified as a cell array of strings. With a single plot type, plot draws to the current axes and can be used with commands like hold and subplot. With multiple plot types, plot creates subplots for each plot type.
plot(...,ptype,level) plots prediction intervals with a confidence level specified by level. level must be between 0 and 1. The default value of level is 0.95. h = plot(...) returns a vector of handles to the plotted objects.
Example
5-61
plot
% Salt-and-pepper noise spnoise = zeros(size(y0)); p = randperm(length(y0)); sppoints = p(1:round(length(p)/5)); spnoise(sppoints) = 5*sign(y0(sppoints)); ydata = y0 + gnoise + spnoise;
Identify outliers as points at a distance greater than 1.5 standard deviations from the baseline model, and refit the data with the outliers excluded:
fdata = feval(fit1,xdata); I = abs(fdata - ydata) > 1.5*std(ydata); outliers = excludedata(xdata,ydata,'indices',I); fit2 = fit(xdata,ydata,f,'StartPoint',[1 1],... 'Exclude',outliers);
Compare the effect of excluding the outliers with the effect of giving them lower bisquare weight in a robust fit:
fit3 = fit(xdata,ydata,f,'StartPoint',[1 1],'Robust','on');
Plot the data, the outliers, and the results of the fits:
plot(fit1,'r-',xdata,ydata,'k.',outliers,'m*') hold on plot(fit2,'c--') plot(fit3,'b:') xlim([0 2*pi])
5-62
plot
5-63
plot
See Also
5-64
predint
Purpose Syntax
Description
of bounds to compute.
intopt is one of
'observation' Bounds for a new observation (default) 'functional' Bounds for the fitted curve
simopt is one of
'off' Nonsimultaneous bounds (default) 'on' Simultaneous bounds Observation bounds are wider than functional bounds because they measure the uncertainty of predicting the fitted curve plus the random variation in the new observation. Nonsimultaneous bounds are for individual elements of x; simultaneous bounds are for all elements of x.
5-65
predint
[pi,y] = predint(...) returns the response values y predicted by fitresult at the predictors in x.
Example
5])
5])
5])
5])
5-66
predint
See Also
5-67
probnames
Example
See Also
5-68
probvalues
See Also
5-69
set
Purpose Syntax
Description
field does not have a finite list of possible string values, the value of the field in s is an empty cell array.
set(options,fieldname1,value1,fieldname2,value2,...) sets the fields specified by the strings fieldname1, fieldname2, ... to the values value1, value2, ... , respectively. set(options,fieldnames,values) sets the fields specified by the cell array of strings fieldnames to the corresponding values in the cell array values.
Example
Create a custom nonlinear model, and create a default fit options structure for the model:
f = fittype('a*x^2+b*exp(n*c*x)','problem','n'); options = fitoptions(f);
Set the Robust and Normalize fields of the fit options structure using field name/value pairs:
set(options,'Robust','LAR','Normalize','On')
Set the Display, Lower, and Algorithm fields of the fit options structure using cell arrays of field names/values:
5-70
set
set(opts,{'Disp','Low','Alg'},{'Final',[0 0 0],'Levenberg'})
See Also
fitoptions, get
5-71
smooth
Purpose Syntax
Description
yy = smooth(y) smooths the data in the column vector y using a moving average filter. Results are returned in the column vector yy. The default span for the moving average is 5.
Because of the way endpoints are handled, the result differs from the result returned by the filter function.
yy = smooth(y,span) sets the span of the moving average to span. span must be odd. yy = smooth(y,method) smooths the data in y using the method method and the default span. Supported values for method are listed in
Description Moving average (default). A lowpass filter with filter coefficients equal to the reciprocal of the span.
5-72
smooth
Description Local regression using weighted linear least squares and a 1st degree polynomial model Local regression using weighted linear least squares and a 2nd degree polynomial model Savitzky-Golay filter. A generalized moving average with filter coefficients determined by an unweighted linear least squares regression and a polynomial model of specified degree (default is 2). The method can accept nonuniform predictor data. A robust version of 'lowess' that assigns lower weight to outliers in the regression. The method assigns zero weight to data outside six mean absolute deviations. A robust version of 'loess' that assigns lower weight to outliers in the regression. The method assigns zero weight to data outside six mean absolute deviations.
'rlowess'
'rloess'
yy = smooth(y,span,method) sets the span of method to span. For the loess and lowess methods, span is a percentage of the total
number of data points, less than or equal to 1. For the moving average and Savitzky-Golay methods, span must be odd (an even span is automatically reduced by 1).
yy = smooth(y,'sgolay',degree) uses the Savitzky-Golay method with polynomial degree specified by degree. yy = smooth(y,span,'sgolay',degree) uses the number of data points specified by span in the Savitzky-Golay calculation. span must be odd and degree must be less than span. yy = smooth(x,y,...) additionally specifies x data. If x is not provided, methods that require x data assume x = 1:length(y). You should specify x data when it is not uniformly spaced or sorted.
5-73
smooth
If x is not uniform and you do not specify method, lowess is used. If the smoothing method requires x to be sorted, the sorting occurs automatically.
Remarks Example
Another way to generate smoothed data is to fit it with a smoothing spline. Refer to the fit function for more information. Load the data in count.dat:
load count.dat
The 24-by-3 array count contains traffic counts at three intersections for each hour of the day. First, use a moving average filter with a 5-hour span to smooth all of the data at once (by linear index) :
c = smooth(count(:)); C1 = reshape(c,24,3);
Second, use the same filter to smooth each column of the data separately:
C2 = zeros(24,3); for I = 1:3, C2(:,I) = smooth(count(:,I)); end
5-74
smooth
Example
5-75
smooth
Smooth the data using the loess and rloess methods with a span of 10%:
yy1 = smooth(x,y,0.1,'loess'); yy2 = smooth(x,y,0.1,'rloess');
Note that the outliers have less influence on the robust method.
5-76
smooth
See Also
fit, sort
5-77
type
See Also
5-78
Index
A
Index
adjusted R-square 2-76 adjusted residuals 2-55 algorithms 2-58 Analysis GUI census data example 1-18 description 5-12 argnames 5-2 axes limit control census data example 1-13 nonparametric fit example 2-122
B
backslash operator 2-52 best fit 1-11 bisquare weights robust fitting 2-55 robust smoothing 2-19 bounds confidence census data example 1-16 definition 2-77 prediction definition 2-77 goodness of fit example 2-83
C
carbon12alpha data set 2-93 category 5-3
census data example 1-5 center and scale 1-11 cfit 5-4 cflibhelp 5-5 cftool 5-7 coefficient confidence bounds 2-78
constraints Fit Options GUI 2-67 Fourier series example 2-100 Gaussian example 2-108 starting values Fit Options GUI 2-67 Gaussian example 2-108 structure piecewise polynomials 2-121 coefficient of multiple determination 2-75 coeffnames 5-14 coeffvalues 5-15 complex data importing 2-3 confidence bounds census data example 1-16 definition 2-77 Legendre polynomial example 2-97 confint 5-16 constraints Fit Options GUI 2-67 Fourier series example 2-100 Gaussian example 2-108 covariance matrix of coefficient estimates 2-80 Create Custom Equation GUI definition 2-64 Legendre polynomial example 2-93 cubic spline interpolation 2-116 curve fitting session saving custom equations 2-64 saving fit results 1-20 Curve Fitting Tool 5-7 description 5-7 Fourier series example 2-102 Gaussian example 2-107 Legendre polynomial example 2-97 nonparametric fit example 2-122 prediction bounds 2-85 rational example 2-90 residuals 2-84
Index-1
Index
robust fit example 2-113 starting 1-4 custom equations definition 2-64 general Fourier series example 2-99 Gaussian example 2-105 robust fit example 2-109 linear Legendre polynomial example 2-93 robust fitting example 2-109 saving 2-64
D
data 2-44 2-109 excluding 2-26 fitting procedure census data example 1-7 general steps 2-44 importing 1-5 quality 2-48 sectioning 2-26 smoothing 2-9 statistics 5-18 See also predictor data, response data Data GUI Data Sets pane 2-2 description 5-8 Smooth pane 2-11 data sets deleting 2-4 enso 2-99 flvote2k 2-109 gauss3 2-105 hahn1 2-87 importing 2-2 renaming 2-4 Data Sets pane census data example 1-6
description 2-2 data tips robust fit example 2-113 datastats 5-18 default coefficient parameters fit options 2-70 confidence level for bounds 2-79 smoothing parameter 2-119 degrees of freedom 2-76 deleting data sets 2-4 exclusion rules 2-27 dependnames 5-20 design matrix 2-51 determining the best fit 1-11 differentiate 3-18 5-21
E
enso data set 2-99
equations custom 2-64 library 2-59 error distribution 2-48 error sum of squares 2-75 evaluating the goodness of fit available measures 2-71 example 2-83 examples evaluating the goodness of fit 2-83 excluding and sectioning data 2-33 Fourier series fit 2-99 Gaussian fit 2-105 importing data 2-4 Legendre polynomial fit 2-93 nonparametric fit 2-120 rational fit 2-87 robust fit 2-109 sectioning periodic data 2-36
Index-2
Index
smoothing data 2-22 Exclude GUI description 2-27 5-11 example 2-35 robust fit example 2-112 excludedata 3-12 3-16 5-25 excluding data example 2-33 marking outliers 2-28 sectioning 2-31 exclusion rule definition 2-26 robust fitting example 2-112 exponentials fit type definition 2-59 extrapolation census data example 1-18
F
feval 3-12 5-30
filtering data moving average 2-13 Savitzky-Golay 2-20 finite differencing parameters 2-68 fit 3-12 3-15 to 3-16 5-32 fit convergence criteria 2-69 Fit Editor census data example 1-7 Legendre polynomial example 2-95 nonparametric fit example 2-120 robust fit example 2-113 Fit Options GUI description 2-67 Fourier series example 2-100 Gaussian example 2-108 fitoptions 3-15 5-37 fitting algorithms 2-58 fit options 2-67
least squares method definition 2-49 linear 2-50 nonlinear 2-57 nonparametric 2-116 numerical results 1-14 parametric 2-46 procedure census data example 1-7 general steps 2-44 visual results 1-11 Fitting GUI census data example 1-9 description 5-9 Fit Editor 1-7 fitting process 2-44 nonparametric fit example 2-120 numerical fit results 1-14 robust fit example 2-113 Table of Fits 1-7 fittype 3-12 3-16 5-48 flvote2k data set 2-109 formula 5-50 Fourier series example 2-99 fit type definition 2-60 functions argnames 5-2 category 5-3 cfit 5-4 cflibhelp 5-5 cftool 5-7 coeffnames 5-14 coeffvalues 5-15 confint 5-16 datastats 5-18 dependnames 5-20 differentiate 3-18 5-21 excludedata 3-12 3-16 5-25 feval 3-12 5-30
Index-3
Index
fit 3-12 3-15 to 3-16 5-32 fitoptions 3-15 5-37 fittype 3-12 3-16 5-48 formula 5-50 get 5-51 indepnames 5-53 integrate 3-18 5-54 islinear 5-56 numargs 5-58 numcoeffs 5-59 plot 3-16 3-22 5-60 predint 3-22 5-65 probnames 5-68 probvalues 5-69 set 5-70 smooth 3-10 to 3-11 5-72 type 5-78
G
Gauss-Newton algorithm 2-58 gauss3 data set 2-105 Gaussian error distribution 2-48 example 2-105 fit type definition 2-60 general equations 2-66 General Equations pane description 2-66 Fourier series example 2-100 Gaussian example 2-105 generating an M-file 1-21 3-24 get 5-51 goodness of fit available measures 2-71 census data example 1-11 statistics 2-74 graphically viewing data 2-6 GUI
Analysis census data example 1-18 description 5-12 Create Custom Equation definition 2-64 Legendre polynomial example 2-93 Curve Fitting Tool description 5-7 Data 2-2 description 5-8 Exclude 2-27 description 5-11 Fit Options description 2-67 Fourier series example 2-100 Gaussian example 2-108 Fitting census data example 1-9 description 5-9 Legendre polynomial example 2-95 nonparametric fit example 2-120 rational example 2-89 robust fit example 2-113 Plotting census data example 1-20 description 5-11 smoothing data example 2-25 Table Options census data example 1-14 goodness of fit evaluation 2-85
H
hahn1 data set 2-87
I
importing data 1-5 description 2-2
Index-4
Index
example 2-4
indepnames 5-53
influential data 2-30 Infs importing 2-3 removing 2-42 integrate 3-18 5-54 interpolants 2-116 islinear 5-56 iteratively reweighted least squares 2-55
linear least squares 2-50 loading the curve-fitting session 1-21 local regression smoothing 2-15 loess 2-15 lowess 2-15
M
M-file generation 1-21 3-24 MAD robust fitting 2-56 robust smoothing 2-19 marking outliers 2-28 median absolute deviation robust fitting 2-56 robust smoothing 2-19 methods cfit 5-2 to 5-4 5-14 to 5-16 5-20 to 5-21 5-30 5-50 5-53 to 5-54 5-56 5-58 to 5-60 5-65 5-68 to 5-69 5-78 fittype 5-2 to 5-3 5-14 to 5-15 5-20 5-30 5-48 5-50 5-53 5-56 5-58 to 5-59 5-68 to 5-69 5-78 models custom 2-64 library 2-59 moving average filtering 2-13 multiple correlation coefficient 2-75
J
Jacobian 2-58
L
LAR 2-55 least absolute residuals 2-55 least squares fitting definition 2-49 linear 2-50 nonlinear 2-57 robust 2-55 weighted linear 2-53 Legendre polynomials example 2-93 generating 2-94 Levenberg-Marquardt algorithm 2-58 leverages 2-56 library models 2-59 linear equations custom 2-65 fit options 2-67 fitting 2-50 Linear Equations pane description 2-65 Legendre polynomial example 2-96 robust fit example 2-111 linear interpolation 2-116
N
NaNs
importing 2-3 removing 2-42 nearest neighbor interpolation 2-116 nonlinear equations custom 2-66 fit options 2-67 fitting 2-57 nonlinear least squares 2-57
Index-5
Index
nonparametric fitting example 2-120 methods 2-116 normal distribution 2-48 normal equations 2-50 normalization 1-11 numargs 5-58 numcoeffs 5-59 numerically viewing data 2-8
O
objects
cfit 5-2 to 5-4 5-14 to 5-16 5-20 to 5-21
5-30 5-50 5-53 to 5-54 5-56 5-58 to 5-60 5-65 5-68 to 5-69 5-78 fittype 5-2 to 5-3 5-14 to 5-15 5-20 5-30 5-48 5-50 5-53 5-56 5-58 to 5-59 5-68 to 5-69 5-78 outliers definition 2-26 marking 2-28 removing 2-42 robust fit 2-55 overdetermined system of equations 2-50 overfitting census data example 1-16 goodness of fit evaluation 2-86
census data example 1-7 fit type definition 2-61 Legendre 2-94 piecewise 2-118 rational models 2-62 power series fit type definition 2-62 prediction bounds definition 2-77 goodness of fit example 2-83 predictor data census data example 1-6 importing 2-3 sectioning 2-28 viewing numerically 2-9 predint 3-22 5-65 preprocessing data 2-42 excluding and sectioning 2-26 smoothing 2-9 transforming the response data 2-41 Preview window 2-5 probnames 5-68 probvalues 5-69 projection matrix 2-53
Q
QR decomposition 2-52 quality of data definition 2-48 weighted linear least squares 2-53
P
parametric fitting 2-46 pchip 2-118 piecewise polynomials 2-118 plot 3-16 3-22 5-60 Plotting GUI census data example 1-20 description 5-11 smoothing data example 2-25 polynomials
R
R-square 2-75 adjusted 2-76 negative values 2-76 rationals example 2-87 fit type definition 2-62
Index-6
Index
regression sum of squares 2-75 weights least squares 2-53 smoothing 2-15 removing Infs, NaNs, and outliers 2-42 renaming data sets 2-4 exclusion rules 2-27 residual degrees of freedom 2-76 residuals adjusted 2-55 comparing multiple fits 1-11 definition 2-72 displaying census data example 1-10 goodness of fit evaluation 2-72 excluding data with 2-34 response data census data example 1-6 error distribution 2-48 importing 2-3 sectioning 2-28 transforming 2-41 viewing numerically 2-9 right-click menu 2-7 RMSE 2-77 robust fitting alternative to excluding data 2-30 example 2-109 regression schemes 2-55 smoothing 2-19 robust least squares 2-55 robust weights fitting 2-56 smoothing 2-19 Rodrigues formula 2-94 root mean squared error 2-77 rubber band selection 2-34
S
saving analysis results workspace variables 1-19 fit results curve fitting session 1-20 M-file 1-21 3-24 workspace variables 1-16 Savitzky-Golay filtering 2-20 scatter plot 2-6 scatter plot smooth 2-15 sectioning data definition 2-26 example 2-33 periodic data example 2-36 rules 2-31 session 1-20 set 5-70 shape-preserving interpolation 2-116 sine functions 2-63 smooth 3-10 to 3-11 5-72 Smooth pane description 2-11 example 2-23 smoothing data definition 2-9 example 2-22 local regression 2-15 moving average filtering 2-13 robust procedure 2-19 Savitzky-Golay filtering 2-20 smoothing spline 2-119 span 2-10 spline 2-118 cubic interpolant 2-116 smoothing 2-119 SSE, SSR, SST 2-75 standard error 2-77 starting values Fit Options GUI 2-67
Index-7
Index
Gaussian example 2-108 structure of coefficients 2-121 sum of sine functions fit type definition 2-63 sum of squares 2-49 error 2-75 regression 2-75 total 2-75
V
variances 2-54 viewing data graphically 2-6 numerically 2-8
W
Weibull distribution fit type definition 2-63 weighted linear least squares 2-53 weights 2-54 regression least squares 2-53 smoothing 2-15 robust least squares 2-55 smoothing 2-19 viewing numerically 2-9
T
Table of Fits 1-7 Table Options GUI census data example 1-14 goodness of fit evaluation 2-85 Tools menu 2-7 total sum of squares 2-75 transforming the response data 2-41 tricube weights 2-15 trust-region algorithm 2-58 type 5-78
Index-8