MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# How can I calculate the confidence intervall of parameter using lsqcurvefit

Asked by Martin on 28 Aug 2012

Dear All,

I am trying to calculate the 95% confidence interval on parameters for my model fitted using lsqcurvefit. The curvefit is working great overall and the fitted parameters seem correct. But I am having trouble calculating the CIs.

First of all, is nlparci the correct function to use for this purpose? If it is not, can someone please guide me on how to get the CIs for the fitted parameters? If it is, then I have a big problem since I get the Error message: "??? Undefined function or method 'nlparci' for input arguments of type 'double'."

Seems that there is something wrong with the jacobian matrix, but I cannot solve the problem.

Has anyone a full example of calculating the confidence intervall of a 3 dimensional function in connection with lsqcurvefit? I have spent hours on the internet but could not get it to work.

## Products

Answer by Star Strider on 28 Aug 2012

You shouldn't be getting an ‘undefined function’ error with nlparci if everything is working as it should. To be sure you have it and you don't have another function shadowing it, I suggest entering from the Command Line:

```which nlparci -all
```

The lsqcurvefit function is part of the Optimization Toolbox, and nlparci is part of the Statistics Toolbox, so you have to have the Statistics Toolbox to use nlparci. If you supplied your own analytic Jacobian to lsqcurvefit, you may have to transpose it to make it work with nlparci. I had this problem in the past with an analytic Jacobian, however in that situation, nlparci should be throwing an error telling you that the dimensions of the parameter, residual, and Jacobian don't match.

Answer by Martin on 29 Aug 2012
Edited by Martin on 29 Aug 2012

Seems that I don't have the statistics toolbox since the "which nlparci -all" already gives a " 'nlparci' not found. " . Does this mean I cannot derive the confidence intervalls for my parameters?

Or is there any way to calculate the confidence intervals manually? I must admit my mathematical knowledge in this area is not really profound. I have read that the jacobi matrix can be used to calculate them but I didn't understand how.

Do you by any chance have a script that determines the confidence intervalls manually?

So far my fitting command looks like this: [p,resnorm,residual,exitflag,output,lambda,jacobian]=lsqcurvefit(@esx_2dfunc_col,p0,r,dat(:),[0 0 0 wmin wmin 0 0],[10 50 50 wmax wmax 12 3],... optimset('MaxFunEvals',4000,'MaxIter',1000));

the function esx_2dfunc_col is quite sick, but that shouldn't play a role for the determination of the confidence intervalls as far as I understood the problem.

## 1 Comment

Star Strider on 29 Aug 2012

I've been working on a function to post to the File Exchange for this purpose but I haven't gotten it working quite as I would like it. I refer you to The Levenberg-Marquardt method for nonlinear least squares curve-fitting problems by Henri Gavin (Duke University, 2011). Since you don't have the Statistics Toolbox, I'll include these anonymous functions for calculating the probability from a z-statistic:

```P = @(z) erfc(-z/sqrt(2))/2;
```

and the z-statistic (or ‘critical value’) from a probability:

```CV = @(alpha) -sqrt(2) * erfcinv(2*alpha/2);
```

Those will help in calculating specific confidence intervals from the covariance matrix if you want to write your own routine.