Functions > Data Analysis > Curve Fitting > Example: Power Regression
  
Example: Power Regression
Use the genfit and pwrfit functions to fit data to a power curve model.
Using the genfit Function
1. Define a data set.
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Define the fitting function where A and b are unknown.
Click to copy this expression
3. Define the function vector.
Click to copy this expression
Function ln0 is used here instead of ln because it can be evaluated approximately at 0, which is necessary for adjusting the parameter values while minimizing the error. Replacing ln0 with ln causes genfit to fail to converge.
4. Define a vector of guess values.
Click to copy this expression
5. Use genfit to find the coefficients of the power function.
Click to copy this expression
Click to copy this expression
Click to copy this expression
6. Define a function using coefficients A and b.
Click to copy this expression
7. Plot the data points and the fitting function.
Click to copy this expression
Click to copy this expression
Using the pwrfit Function
1. Define the fitting function where a, b and c are unknown.
Click to copy this expression
2. Define a vector of guess values.
Click to copy this expression
3. Use pwrfit to find the coefficients of the power function.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
4. Define a function using coefficients a, b and c.
Click to copy this expression
5. Plot the data points and the fitting function.
Click to copy this expression
Checking the Goodness of Fit
1. Use residual plots to check how well the models fit the data.
The largest residual values are:
Click to copy this expression
Click to copy this expression
The smallest residual values are:
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Use the corr function to calculate the correlation coefficients.
Click to copy this expression
Click to copy this expression
The fit seems much better than the residual plot might reveal.
For data that exists on a large scale, particularly power or exponential data that changes rapidly over larger and larger values of X, a residual plot is not necessarily the best tool for determining the goodness of a fit.
Weighted genfit
Use local functions to modify the vector of functions that you pass to genfit based on other parameters.
1. Define a function that weights data according to the data's standard deviations.
Click to copy this expression
Using the vector input form for parameters allows the weighted function to be easily used for other types of fitting functions with an arbitrary number of parameters.
2. Write a program that returns the genfit of data according to its standard deviations.
Click to copy this expression
3. Define a vector of standard deviations.
Click to copy this expression
4. Skew one of the data points from the original set.
Click to copy this expression
5. Call the program and pass it the vectors of standard deviations and guess values.
Click to copy this expression
Click to copy this expression
6. Call the genfit function without weights:
Click to copy this expression
Click to copy this expression
7. Plot, and compare, the data and the two fitting functions.
Click to copy this expression
The standard deviations based function provides a better fit.