compare fits in curve fitter app -凯发k8网页登录

main content

compare fits in curve fitter app

interactive curve fitter workflow

the next topics fit census data using polynomial equations up to the sixth degree, and a single-term exponential equation. the steps demonstrate how to:

  • load data and explore various fits using different library models.

  • search for the best fit by:

    • comparing graphical fit results

    • comparing numerical fit results including the fitted coefficients and goodness-of-fit statistics

  • export your best fit results to the matlab® workspace to analyze the model at the command line.

  • save the session and generate matlab code for all fits and plots.

loading data and creating fits

you must load the data variables into the matlab workspace before you can fit data using the curve fitter app. for this example, the data is stored in the matlab file census.mat.

  1. load the data.

    load census
    

    the workspace contains two new variables.

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

  2. open the curve fitter app.

    curvefitter
  3. on the curve fitter tab, in the data section, click select data. in the select fitting data dialog box, select the variable names cdate and pop from the x data and y data lists, respectively.

    the curve fitter app creates and plots a default fit to the x input (or predictor) data and the y output (or response) data. the default fit is a linear polynomial fit type. observe the fit settings display in the fit options pane. the fit is a first-degree polynomial.

  4. in the fit options pane, change the fit to a second-degree polynomial by selecting 2 from the degree list.

    the curve fitter app plots the new fit. the curve fitter app calculates a new fit when you change fit settings because auto is selected by default. if refitting is time consuming, as is sometimes the case for large data sets, you can turn off the automatic behavior. on the curve fitter tab, in the fit section, select manual.

    the curve fitter app displays results of fitting the census data with a quadratic polynomial in the results pane, where you can view the library model, fitted coefficients, and goodness-of-fit statistics.

  5. change the name of the fit. in the table of fits pane, double-click untitled fit 1 in the fit name column and type poly2.

  6. display the residuals. on the curve fitter tab, in the visualization section, click residuals plot.

    the residuals indicate that a better fit might be possible. therefore, continue exploring various fits to the census data set.

    polynomial fit with residuals in curve fitter app

  7. add new fits to try the other library equations.

    1. right-click the fit in the table of fits pane, and select duplicate "poly2". alternatively, on the curve fitter tab, in the file section, click duplicate.

      tip

      for fits of a given type (for example, polynomials), duplicate a fit instead of creating a new fit because copying a fit requires fewer steps. the duplicated fit contains the same data selections and fit settings.

    2. change the polynomial degree to 3 and rename the fit poly3.

    3. when you fit higher degree polynomials, the results pane displays this warning.

      equation is badly conditioned. remove repeated data points
      or try centering and scaling.

      normalize the data by selecting the center and scale check box in the fit options pane.

    4. repeat steps a and b to add polynomial fits up to the sixth degree. then add an exponential fit. on the curve fitter tab, in the file section, click new and select new fit. in the fit type section, click the arrow to open the gallery, and click exponential in the regression models section.

    5. for each new fit, look at the results pane information, and the residuals plot in the app.

      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.

about scaling

the warning about scaling 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 a scaling problem. to address this problem, you can normalize the cdate data. normalization scales the predictor data to improve the accuracy of the subsequent numeric computations. for example, you can normalize cdate by centering and scaling the data to have zero mean and unit standard deviation.

(cdate - mean(cdate))./std(cdate)

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 fitter app plots using the original scale.

determining the best fit

to determine the best fit, you should examine both the graphical and numerical fit results.

examine the graphical fit results

  1. determine the best fit by examining the graphs of the fits and residuals. to view plots for each fit in turn, click the fit in the table of fits pane. the graphical fit results indicate that:

    • the fits and residuals for the polynomial equations are all similar, making it difficult to choose the best one.

    • the fit and residuals for the single-term exponential equation indicate it is a poor fit overall. therefore, it is a poor choice and you can remove the exponential fit from the candidates for best fit.

  2. examine the behavior of the fits after the year 2000. the goal of fitting the census data is to extrapolate the best fit to predict future population values.

    1. click the sixth-degree polynomial fit in the table of fits pane to view the plots for this fit.

    2. in the fit plot, click the pan button in the axes toolbar and pan until the fit is visible for several years after the year 2000. the axes limits of the residuals plot adjust accordingly.

    3. examine the fit plot. the behavior of the sixth-degree polynomial fit beyond the data range makes it a poor choice for extrapolation and you can reject this fit.

      plot of sixth-degree polynomial fit extended to years after the year 2000

evaluate the numerical fit results

when you can no longer eliminate fits by examining them graphically, you should examine the numerical fit results. the curve fitter app displays two types of numerical fit results:

  • goodness-of-fit statistics

  • confidence bounds on the fitted coefficients

the goodness-of-fit statistics help you determine how well the curve fits the data. the confidence bounds on the coefficients determine their accuracy.

examine the numerical fit results.

  1. for each fit, view the goodness-of-fit statistics in the results pane.

    results pane showing statistics for third-degree polynomial fit

  2. compare all fits simultaneously in the table of fits pane. click the column headings to sort by statistics results.

    table of fits pane displaying five polynomial fits and one exponential fit. the fits are sorted by the sum of squares due to error.

  3. examine the sum of squares due to error (sse) and the adjusted r-square statistics to help determine the best fit. 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.

    the largest sse for exp indicates it is a poor fit, which you 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, so you already rejected this fit by examining the plots with new axes limits.

    the next best sse value is associated with the fifth-degree polynomial fit, poly5, suggesting it might be the best fit. however, the sse and adjusted r-square values for the remaining polynomial fits are all very close to each other.

  4. resolve the best fit issue by examining the confidence bounds for the remaining fits in the results pane. click a fit in the table of fits pane to open the fit figure (or select it, if the figure is already open), and view the results pane. each fit figure displays the plots for a single fit.

    display the fifth-degree polynomial poly5 and the second-degree polynomial poly2 fit figures side by side. examining results side by side can help you assess fits.

    1. to show two fit figures simultaneously, you can drag and drop the fit figure tabs in the fits pane. alternatively, you can click the document actions arrow located to the far right of the fit figure tabs. select the tile all option and specify a 1-by-2 or 2-by-1 layout.

    2. compare the coefficients and bounds (p1, p2, and so on) in the results pane for both fits, poly5 and poly2. the toolbox calculates 95% confidence bounds on coefficients. the confidence bounds on the coefficients determine their accuracy. check the equations in the results pane (f(x)=p1*x p2*x...) to see the model terms for each coefficient. note that p2 refers to the p2*x term in poly2 and the p2*x^4 term in poly5. do not compare normalized coefficients directly with non-normalized coefficients.

      tip

      if you want more space to view and compare plots and results, as shown next, drag down the table of fits pane. you can also hide the results pane to show only plots.

      comparison of poly2 and poly5 fits in the curve fitter app

      the bounds cross zero on the p1, p2, and p3 coefficients for the fifth-degree polynomial. this means you cannot be sure that these coefficients differ from zero. if the higher order model terms might have coefficients of zero, they are not helping with the fit, which suggests that this model overfits the census data.

      results pane showing statistics for fifth-degree polynomial fit

      however, the small confidence bounds do not cross zero on p1, p2, and p3 for the quadratic fit poly2, indicating that the fitted coefficients are known fairly accurately.

      results pane showing statistics for second-degree polynomial fit

      therefore, after examining both the graphical and numerical fit results, you should select poly2 as the best fit to extrapolate the census data.

note

the fitted coefficients associated with the constant, linear, and quadratic terms are nearly identical for each normalized polynomial equation. however, as the polynomial degree increases, the coefficient bounds associated with the higher degree terms cross zero, which suggests overfitting.

analyzing best fit in the workspace

you can export the selected fit and the associated fit results to the matlab workspace. on the curve fitter tab, in the export section, click export and select export to workspace. the fit is saved as a matlab object and the associated fit results are saved as structures.

  1. right-click the poly2 fit in the table of fits pane, and select save "poly2" to workspace. alternatively, click export and select export to workspace. the app opens a dialog box.

    save fit to matlab workspace dialog box

  2. click ok to save the fit with the default names.

    fittedmodel is saved as a curve fitting toolbox™ cfit object.

    whos fittedmodel
      name             size            bytes  class    attributes
      fittedmodel      1x1               925  cfit               

examine the cfit object fittedmodel to display the model, the fitted coefficients, and the confidence bounds for the fitted coefficients.

fittedmodel
     linear model poly2:
     fittedmodel(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 04  (1.964e 04, 2.262e 04)

examine the goodness structure to display goodness-of-fit results.

goodness
goodness = 
  struct with fields:
           sse: 159.0293
       rsquare: 0.9987
           dfe: 18
    adjrsquare: 0.9986
          rmse: 2.9724

examine the output structure to display additional information associated with the fit, such as the residuals.

output
output = 
  struct with fields:
        numobs: 21
      numparam: 3
     residuals: [21×1 double]
      jacobian: [21×3 double]
      exitflag: 1
     algorithm: 'qr factorization and solve'
    iterations: 1

you can evaluate (interpolate or extrapolate), differentiate, or integrate a fit over a specified data range with various postprocessing functions.

for example, evaluate fittedmodel at a vector of values to extrapolate to the year 2050.

x = 2000:10:2050;
y = fittedmodel(x)
y =
  274.6221
  301.8240
  330.3341
  360.1524
  391.2790
  423.7137
plot the fit to the census data and the extrapolated fit values.
plot(fittedmodel,cdate,pop)
hold on
plot(fittedmodel,x,y,"k ")
hold off
legend(["data","","extrapolated data","fitted curve"], ...
  "location","northwest")

fit plot using census data and extrapolated values

for more examples and instructions for interactive and command-line fit analysis, and a list of all postprocessing functions, see fit postprocessing.

for an example reproducing this interactive census data analysis using the command line, see polynomial curve fitting.

saving your work

the curve fitter app provides several options for saving your work. you can save one or more fits and the associated fit results as variables to the matlab workspace. you can then use this saved information for documentation purposes, or to extend your data exploration and analysis. in addition to saving your work to matlab workspace variables, you can:

  • save the current curve fitting session. on the curve fitter tab, in the file section, click save and select save session. the session file contains all the fits and variables in your session and remembers your layout. see save and reopen sessions.

  • generate matlab code to recreate a fit and its associated plots. in the export section, click export and select generate code. the curve fitter app generates code for the currently selected fit and displays the file in the matlab editor.

    you can recreate your fit and plots by calling the file at the command line with your original data as input arguments. you can also call the file with new data, and automate the process of fitting multiple data sets. for more information, see generating code from the curve fitter app.

related topics

网站地图