Nonlinear Regression

If you need to fit your data to a function more complicated than a polynomial series, such as a function with a Sin or Cos of a parameter, you need to use the nonlinear regression fitting function.

Starting Up

Off[General::spell]; Off[General::spell1];

Initialize and load the needed package for the nonlinear fitting.

Needs["Statistics`NonlinearFit`"]

Generating some sample data

We will generate some sample data of the form α + β cos(ω t + ϕ)  with some added random noise.

α  =  0.34;
β =  1.17;
ω =   .223;
ϕ =  0.2;
time  =  Table[tt, {tt, 0, 100, 1}]; 
    ydata  =  
      Table[α + β Cos[ω tt  +  ϕ] +  
          0.1 Random[Real, {-1, 1}], {tt, 0, 100, 1}];

sampledata = Transpose[{time, ydata}];

Here is a plot of the sample data:

dataplot = ListPlot[sampledata]

Fitting the data

We need to define the function we are fitting, and the starting values for the fit routine to use:

model = a  +  b Cos[2 Pi f t + phi];   
startvals = {{a, 0}, {b, 1}, {f,  .1}, {phi, 
            0}};
fit = 
      NonlinearRegress[sampledata, model, t, startvals, 
        Method ->  Newton, 
        RegressionReport  ->  {BestFit, ParameterCITable}]

Plot the fit and the data to see how it looks - displays fit  and fit+data:

\(\(\(
\)\(Show[dataplot, 
        Plot[\((BestFit /. fit)\), {t, 0, 100}, 
          PlotStyle -> RGBColor[1, 0, 0]]];\)\)\)

Our fit results were clearly not correct... one always has to check, because the fitting routine can find a local chi-squared minimum.  Most likely, we used starting values too far from the correct ones, and the fit routine got lost.  Try again, changing the starting frequency:

startvals = {{a, 0}, {b, 1}, {f,  .03}, {phi, 0}};
fit = 
      NonlinearRegress[sampledata, model, t, startvals, 
        Method ->  Newton, 
        RegressionReport  ->  {BestFit, ParameterCITable}]

\(\(\(
\)\(Show[dataplot, 
        Plot[\((BestFit /. fit)\), {t, 0, 100}, 
          PlotStyle -> RGBColor[1, 0, 0]]];\)\)\)

This is a much better fit!       There are some things to note:  the value for β came out negative, but this is OK, because shifting the phase by π is equivalent to changing the sign of β.  Also note that ϕ was  different from our input, but remember that ϕ is only defined modulo 2π and there can be a π offset due to the sign of β.