NonlinearRegress and ND

There are situations for which you need to use NonlinearRegress where symbolic derivatives cannot be calculated. In such cases you cannot obtain a full Regression Report. There is a technique, illustrated in this notebook, for getting around this which is fairly simple and gives the desired results.

Loading the necessary packages

To make this work we need two packages. Statistics`NonlinearFit` and NumericalMath`NLLimit.

<<Statistics`

<<NumericalMath`

Statement of the problem

Suppose we want to fit the following data

data={{1,1},{2,2},{3,1},{4,2},{5,3},{6,4}};

to the model

b Abs[c x]

where b and c are parameters to be determined in the fitting process. If we try to use NonlinearRegress with this it does not evaluate.

NonlinearRegress[data,b Abs[c x],x,{b,c}]

Redefining derivatives

We need to add to the definition of the D function so that it will switch to numerical differentiation if symbolic differentiation cannot be carried out. We begin by Unprotecting D.

Unprotect[D]

Next we create a two level definition for the derivative. Note that the first argument is the model function.

D[b_ Abs[c_ x],c_]:=dc[x,b,c]

Now we define dc where we put conditions on the arguments so it is only used for cases where the arguments are numerical values.

dc[x_?NumericQ,b_?NumericQ,c_?NumericQ]:=ND[b Abs[cc x],cc,c]

It is always a good idea to reprotect unprotected functions to avoid unwanted changes to the functions.

Protect[D]

A second try

Our approach seems to work as the following shows.

NonlinearRegress[data,b Abs[c x],x,{b,c}]

The reason the FitCurvatureTable fails is that it requires second derivatives. Definitions similar to the above can be given for second derivatives and then the FitCurvatureTable will also evaluate. This will require three definitions in most cases and is left as an exercise.

Thanks to David Withoff for providing the above solution.