MATLAB: How to get the confidence intervals of regression coefficients in nlinfit

confidence intervalMATLABnlinfitnonlinear

I used nlinfit or lsqcurvefit to do non-linear fit.
[beta,R,J,CovB,MSE,ErrorModelInfo] = nlinfit(X,Y,@myfun,beta0)
How to get the confidence intervals of regression coefficients 'beta'?
I'm sorry it might be just a math problem, but I really want to implement it in matlab code.
Thank you very much!

Best Answer

I looked into this somewhat recently and found two main strategies for computing confidence intervals of the coefficients.
  1. using nlparci()
  2. computing them directly by calculating the covariance matrix
Both methods use the jacobian and residual outputs and for my data there are very tiny differences in the results between the two methods.
Here are some sources I stored within the footnotes of my code and I recommend browsing through them to determine the best method for your problem.
Approximation of covar matrix of the params
Both covar matrix and nlparci methods
How covar matrix is used to get CI
One method to approximate CIs using linear example, explains how to do with nonlinearly.
nlparci can be used with lsqnonlin outputs (my project uses lsqnonlin but the outputs are similar for your project)
If you go with the nlparci() method, it's as easy as pluggin in your outputs...
CI = nlparci(betaEstimates,residuals,'jacobian',jacobian);
If you go with the covar matrix method, here a snippet from my code that should be fairly self-explanatory.
df = length(residuals) - numel(betaEstimates); %degrees of freedom
alpha = 0.05; %for 95% conf int
crit = tinv(1-alpha/2,df); %critical value
covm = inv(jacobian'*jacobian) * var(residuals); %covariance matrix
covmIdx = sub2ind(size(covm),1:size(covm,1),1:size(covm,2)); %indices of the diag of covm
CI = nan(numel(betaEstimates),2);
I'm refraining for making a recommendation partially due to what @SK mentioned; there isn't a standard solution to getting CIs for the beta estimates, and also because I'm exploring these methods as a novice (and welcome feedback).