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.
2. Define the fitting function where A and b are unknown.
3. Define the function vector.
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.
5. Use genfit to find the coefficients of the power function.
6. Define a function using coefficients A and b.
7. Plot the data points and the fitting function.
Using the pwrfit Function
1. Define the fitting function where a, b and c are unknown.
2. Define a vector of guess values.
3. Use pwrfit to find the coefficients of the power function.
4. Define a function using coefficients a, b and c.
5. Plot the data points and the fitting function.
Checking the Goodness of Fit
1. Use residual plots to check how well the models fit the data.
The largest residual values are:
The smallest residual values are:
2. Use the corr function to calculate the correlation coefficients.
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.
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.
3. Define a vector of standard deviations.
4. Skew one of the data points from the original set.
5. Call the program and pass it the vectors of standard deviations and guess values.
6. Call the genfit function without weights:
7. Plot, and compare, the data and the two fitting functions.
The standard deviations based function provides a better fit.