# Documentation

## Interactive Fitting

### The Basic Fitting UI

The MATLAB® Basic Fitting UI allows you to interactively:

• Model data using a spline interpolant, a shape-preserving interpolant, or a polynomial up to the tenth degree

• Plot one or more fits together with data

• Plot the residuals of the fits

• Compute model coefficients

• Compute the norm of the residuals (a statistic you can use to analyze how well a model fits your data)

• Use the model to interpolate or extrapolate outside of the data

• Save coefficients and computed values to the MATLAB workspace for use outside of the dialog box

• Generate MATLAB code to recompute fits and reproduce plots with new data

 Note:   The Basic Fitting UI is only available for 2-D plots. For more advanced fitting and regression analysis, see the Curve Fitting Toolbox™ documentation and the Statistics and Machine Learning Toolbox™ documentation.

### Preparing for Basic Fitting

The Basic Fitting UI sorts your data in ascending order before fitting. If your data set is large and the values are not sorted in ascending order, it will take longer for the Basic Fitting UI to preprocess your data before fitting.

You can speed up the Basic Fitting UI by first sorting your data. To create sorted vectors x_sorted and y_sorted from data vectors x and y, use the MATLAB sort function:

[x_sorted, i] = sort(x);
y_sorted = y(i);

### Opening the Basic Fitting UI

To use the Basic Fitting UI, you must first plot your data in a figure window, using any MATLAB plotting command that produces (only) x and y data.

To open the Basic Fitting UI, select Tools > Basic Fitting from the menus at the top of the figure window.

When you fully expand it by twice clicking the arrow button in the lower right corner, the window displays three panels. Use these panels to:

• Select a model and plotting options

• Examine and export model coefficients and norms of residuals

• Examine and export interpolated and extrapolated values.

To expand or collapse panels one-by-one, click the arrow button in the lower right corner of the interface.

### Example: Using Basic Fitting UI

This example shows how to use the Basic Fitting UI to fit, visualize, analyze, save, and generate code for polynomial regressions.

#### Load and Plot Census Data

The file, census.mat, contains U.S. population data for the years 1790 through 1990 at 10 year intervals.

To load and plot the data, type the following commands at the MATLAB prompt:

plot(cdate,pop,'ro')

• cdate — A column vector containing the years from 1790 to 1990 in increments of 10. It is the predictor variable.

• pop — A column vector with U.S. population for each year in cdate. It is the response variable.

The data vectors are sorted in ascending order, by year. The plot shows the population as a function of year.

Now you are ready to fit an equation the data to model population growth over time.

#### Predict the Census Data with a Cubic Polynomial Fit

1. Open the Basic Fitting dialog box by selecting Tools > Basic Fitting in the Figure window.

2. In the Plot fits area of the Basic Fitting dialog box, select the cubic check box to fit a cubic polynomial to the data.

MATLAB uses your selection to fit the data, and adds the cubic regression line to the graph as follows.

In computing the fit, MATLAB encounters problems and issues the following warning:

Add points with distinct X values,
select a polynomial with a lower degree,
or select "Center and scale X data."

This warning indicates that the computed coefficients for the model are sensitive to random errors in the response (the measured population). It also suggests some things you can do to get a better fit.

3. Continue to use a cubic fit. As you cannot add new observations to the census data, improve the fit by transforming the values you have to z-scores before recomputing a fit. Select the Center and scale X data check box in the dialog box to make the Basic Fitting tool perform the transformation.

To learn how centering and scaling data works, see Learn How the Basic Fitting Tool Computes Fits.

4. Now view the equations and display residuals. In addition to selecting the Center and scale X data and cubic check boxes, select the following options:

• Show equations

• Plot residuals

• Show norm of residuals

Selecting Plot residuals creates a subplot of them as a bar graph. The following figure displays the results of the Basic Fitting UI options you selected.

The cubic fit is a poor predictor before the year 1790, where it indicates a decreasing population. The model seems to approximate the data reasonably well after 1790. However, a pattern in the residuals shows that the model does not meet the assumption of normal error, which is a basis for the least-squares fitting. The data 1 line identified in the legend are the observed x (cdate) and y (pop) data values. The cubic regression line presents the fit after centering and scaling data values. Notice that the figure shows the original data units, even though the tool computes the fit using transformed z-scores.

For comparison, try fitting another polynomial equation to the census data by selecting it in the Plot fits area.

 Tip   You can change the default plot settings and rename data series with the Property Editor.

#### View and Save the Cubic Fit Parameters

In the Basic Fitting dialog box, click the arrow button to display the estimated coefficients and the norm of the residuals in the Numerical results panel.

To view a specific fit, select it from the Fit list. This displays the coefficients in the Basic Fitting dialog box, but does not plot the fit in the figure window.

 Note:   If you also want to display a fit on the plot, you must select the corresponding Plot fits check box.

Save the fit data to the MATLAB workspace by clicking the Save to workspace button on the Numerical results panel. The Save Fit to Workspace dialog box opens.

With all check boxes selected, click OK to save the fit parameters as a MATLAB structure:

fit
fit =
type: 'polynomial degree 3'
coeff: [0.9210 25.1834 73.8598 61.7444]

Now, you can use the fit results in MATLAB programming, outside of the Basic Fitting UI.

#### Derive R2, the Coefficient of Determination

You can get an indication of how well a polynomial regression predicts your observed data by computing the coefficient of determination, or R-square (written as R2). The R2 statistic, which ranges from 0 to 1, measures how useful the independent variable is in predicting values of the dependent variable:

• An R2 value near 0 indicates that the fit is not much better than the model y = constant.

• An R2 value near 1 indicates that the independent variable explains most of the variability in the dependent variable.

To compute R2, first compute a fit, and then obtain residuals from it. A residual is the signed difference between an observed dependent value and the value your fit predicts for it.

residuals = yobserved - yfitted

The Basic Fitting tool can generate residuals for any fit it calculates. To view a graph of residuals, select the Plot residuals check box. You can view residuals as a bar, line or scatter plot.

After you have residual values, you can save them to the workspace, where you can compute R2. Complete the preceding part of this example to fit a cubic polynomial to the census data, and then perform these steps:

Compute Residual Data and R2 for a Cubic Fit.

1. Click the arrow button at the lower right to open the Numerical results tab if it is not already visible.

2. From the Fit drop-down menu, select cubic if it does not already show.

3. Save the fit coefficients, norm of residuals, and residuals by clicking Save to Workspace.

The Save Fit to Workspace dialog box opens with three check boxes and three text fields.

4. Select all three check boxes to save the fit coefficients, norm of residuals, and residual values.

5. Identify the saved variables as belonging to a cubic fit. Change the variable names by adding a 3 to each default name (for example, fit3, normresid3, and resids3). The dialog box should look like this figure.

6. Click OK. Basic Fitting saves residuals as a column vector of numbers, fit coefficients as a struct, and the norm of residuals as a scalar.

Notice that the value that Basic Fitting computes for norm of residuals is 12.2380. This number is the square root of the sum of squared residuals of the cubic fit.

7. Optionally, you can verify the norm-of-residuals value that the Basic Fitting tool provided. Compute the norm-of-residuals yourself from the resids3 array that you just saved:

mynormresid3 = sum(resids3.^2)^(1/2)

mynormresid3 =
12.2380

8. Compute the total sum of squares of the dependent variable, pop to compute R2. Total sum of squares is the sum of the squared differences of each value from the mean of the variable. For example, use this code:

SSpop = (length(pop)-1) * var(pop)

SSpop =
1.2356e+005

var(pop) computes the variance of the population vector. You multiply it by the number of observations after subtracting 1 to account for degrees of freedom. Both the total sum of squares and the norm of residuals are positive scalars.

9. Now, compute R2, using the square of normresid3 and SSpop:

rsqcubic = 1 - normresid3^2 / SSpop

rsqcubic =
0.9988

10. Finally, compute R2 for a linear fit and compare it with the cubic R2 value that you just derived. The Basic Fitting UI also provides you with the linear fit results. To obtain the linear results, repeat steps 2-6, modifying your actions as follows:

• To calculate least-squares linear regression coefficients and statistics, in the Fit drop-down on the Numerical results pane, select linear instead of cubic.

• In the Save to Workspace dialog, append 1 to each variable name to identify it as deriving from a linear fit, and click OK. The variables fit1, normresid1, and resids1 now exist in the workspace.

• Use the variable normresid1 (98.778) to compute R2 for the linear fit, as you did in step 9 for the cubic fit:

rsqlinear = 1 - normresid1^2 / SSpop

rsqlinear =
0.9210

This result indicates that a linear least-squares fit of the population data explains 92.1% of its variance. As the cubic fit of this data explains 99.9% of that variance, the latter seems to be a better predictor. However, because a cubic fit predicts using three variables (x, x2, and x3), a basic R2 value does not fully reflect how robust the fit is. A more appropriate measure for evaluating the goodness of multivariate fits is adjusted R2. For information about computing and using adjusted R2, see Residuals and Goodness of Fit.

 Caution   R2 measures how well your polynomial equation predicts the dependent variable, not how appropriate the polynomial model is for your data. When you analyze inherently unpredictable data, a small value of R2 indicates that the independent variable does not predict the dependent variable precisely. However, it does not necessarily mean that there is something wrong with the fit.

Compute Residual Data and R2 for a Linear Fit.  In this next example, use the Basic Fitting UI to perform a linear fit, save the results to the workspace, and compute R2 for the linear fit. You can then compare linear R2 with the cubic R2 value that you derive in the example Compute Residual Data and R2 for a Cubic Fit.

1. Click the arrow button at the lower right to open the Numerical results tab if it is not already visible.

2. Select the linear check box in the Plot fits area.

3. From the Fit drop-down menu, select linear if it does not already show. The Coefficients and norm of residuals area displays statistics for the linear fit.

4. Save the fit coefficients, norm of residuals, and residuals by clicking Save to Workspace.

The Save Fit to Workspace dialog box opens with three check boxes and three text fields.

5. Select all three check boxes to save the fit coefficients, norm of residuals, and residual values.

6. Identify the saved variables as belonging to a linear fit. Change the variable names by adding a 1 to each default name (for example, fit1, normresid1, and resids1).

7. Click OK. Basic Fitting saves residuals as a column vector of numbers, fit coefficients as a struct, and the norm of residuals as a scalar.

Notice that the value that Basic Fitting computes for norm of residuals is 98.778. This number is the square root of the sum of squared residuals of the linear fit.

8. Optionally, you can verify the norm-of-residuals value that the Basic Fitting tool provided. Compute the norm-of-residuals yourself from the resids3 array that you just saved:

mynormresid1 = sum(resids1.^2)^(1/2)

mynormresid3 =
98.7783

9. Compute the total sum of squares of the dependent variable, pop to compute R2. Total sum of squares is the sum of the squared differences of each value from the mean of the variable. For example, use this code:

SSpop = (length(pop)-1) * var(pop)

SSpop =
1.2356e+005

var(pop) computes the variance of the population vector. You multiply it by the number of observations after subtracting 1 to account for degrees of freedom. Both the total sum of squares and the norm of residuals are positive scalars.

10. Now, compute R2, using the square of normresid1 and SSpop:

rsqlinear = 1 - normresid1^2 / SSpop

rsqcubic =
0.9210

This result indicates that a linear least-squares fit of the population data explains 92.1% of its variance. As the cubic fit of this data explains 99.9% of that variance, the latter seems to be a better predictor. However, a cubic fit has four coefficients (x, x2, x3, and a constant), while a linear fit has two coefficients (x and a constant). A simple R2 statistic does not account for the different degrees of freedom. A more appropriate measure for evaluating polynomial fits is adjusted R2. For information about computing and using adjusted R2, see Residuals and Goodness of Fit.

 Caution   R2 measures how well your polynomial equation predicts the dependent variable, not how appropriate the polynomial model is for your data. When you analyze inherently unpredictable data, a small value of R2 indicates that the independent variable does not predict the dependent variable precisely. However, it does not necessarily mean that there is something wrong with the fit.

#### Interpolate and Extrapolate Population Values

Suppose you want to use the cubic model to interpolate the U.S. population in 1965 (a date not provided in the original data).

1. In the Basic Fitting dialog box, click the button to specify a vector of x values at which to evaluate the current fit.

2. In the Enter value(s)... field, type the following value:

1965

 Note:   Use unscaled and uncentered x values. You do not need to center and scale first, even though you selected to scale x values to obtain the coefficients in Predict the Census Data with a Cubic Polynomial Fit. The Basic Fitting tool makes the necessary adjustments behind the scenes.
3. Click Evaluate.

The x values and the corresponding values for f(x) computed from the fit and displayed in a table, as shown below:

4. Select the Plot evaluated results check box to display the interpolated value as a diamond marker:

5. Save the interpolated population in 1965 to the MATLAB workspace by clicking Save to workspace.

This opens the following dialog box, where you specify the variable names:

6. Click OK, but keep the Figure window open if you intend to follow the steps in the next section, Generate a Code File to Reproduce the Result.

#### Generate a Code File to Reproduce the Result

After completing a Basic Fitting session, you can generate MATLAB code that recomputes fits and reproduces plots with new data.

1. In the Figure window, select File > Generate Code.

This creates a function and displays it in the MATLAB Editor. The code shows you how to programmatically reproduce what you did interactively with the Basic Fitting dialog box.

2. Change the name of the function on the first line from createfigure to something more specific, like censusplot. Save the code file to your current folder with the file name censusplot.m The function begins with:

function censusplot(X1, Y1, valuesToEvaluate1)

3. Generate some new, randomly perturbed census data:

randpop = pop + 10*randn(size(pop));
4. Reproduce the plot with the new data and recompute the fit:

censusplot(cdate,randpop,1965)

You need three input arguments: x,y values (data 1) plotted in the original graph, plus an x-value for a marker.

The following figure displays the plot that the generated code produces. The new plot matches the appearance of the figure from which you generated code except for the y data values, the equation for the cubic fit, and the residual values in the bar graph, as expected.

#### Learn How the Basic Fitting Tool Computes Fits

The Basic Fitting tool calls the polyfit function to compute polynomial fits. It calls the polyval function to evaluate the fits. polyfit analyzes its inputs to determine if the data is well conditioned for the requested degree of fit.

When it finds badly conditioned data, polyfit computes a regression as well as it can, but it also returns a warning that the fit could be improved. The Basic Fitting example section Predict the Census Data with a Cubic Polynomial Fit displays this warning.

One way to improve model reliability is to add data points. However, adding observations to a data set is not always feasible. An alternative strategy is to transform the predictor variable to normalize its center and scale. (In the example, the predictor is the vector of census dates.)

The polyfit function normalizes by computing z-scores:

$z=\frac{x-\mu }{\sigma }$

where x is the predictor data, μ is the mean of x, and σ is the standard deviation of x. The z-scores give the data a mean of 0 and a standard deviation of 1. In the Basic Fitting UI, you transform the predictor data to z-scores by selecting the Center and scale x data check box.

After centering and scaling, model coefficients are computed for the y data as a function of z. These are different (and more robust) than the coefficients computed for y as a function of x. The form of the model and the norm of the residuals do not change. The Basic Fitting UI automatically rescales the z-scores so that the fit plots on the same scale as the original x data.

To understand the way in which the centered and scaled data is used as an intermediary to create the final plot, run the following code in the Command Window:

close
x = cdate;
y = pop;
z = (x-mean(x))/std(x); % Compute z-scores of x data

plot(x,y,'ro') % Plot data as red markers
hold on        % Prepare axes to accept new graph on top

zfit = linspace(z(1),z(end),100);
pz = polyfit(z,y,3); % Compute conditioned fit
yfit = polyval(pz,zfit);

xfit = linspace(x(1),x(end),100);
plot(xfit,yfit,'b-') % Plot conditioned fit vs. x data

The centered and scaled cubic polynomial plots as a blue line, as shown here:

In the code, computation of z illustrates how to normalize data. The polyfit function performs the transformation itself if you provide three return arguments when calling it:

[p,S,mu] = polyfit(x,y,n)
The returned regression parameters, p, now are based on normalized x. The returned vector, mu, contains the mean and standard deviation of x. For more information, see the polyfit reference page.