Categories &

Functions List

Function Reference: nlinfit

statistics: beta = nlinfit (X, y, modelfun, beta0)

statistics: beta = nlinfit (…, options)

statistics: beta = nlinfit (…, Name, Value)

statistics: [beta, R, J, CovB, MSE, ErrorModelInfo] = nlinfit (…)

Fit a nonlinear regression model.

beta = nlinfit (X, y, modelfun, beta0) estimates the coefficients of the nonlinear regression model y = modelfun (beta, X) by iteratively minimizing the (possibly weighted) sum of squared residuals, starting from the initial coefficient vector beta0. The fit uses the Levenberg-Marquardt algorithm with a numerically computed Jacobian.

  • X is a matrix of predictor values. nlinfit does not interpret the columns of X; the array is passed unchanged as the second argument of modelfun, so its shape is whatever modelfun expects.
  • y is a numeric vector of responses, one element per observation.
  • modelfun is a function handle @(b, X) returning a vector of fitted responses the same size as y.
  • beta0 is a numeric vector of initial values for the coefficients.

Additional options are given either as a statset-style options structure or as Name/Value pairs (or both). The supported options are:

NameValue
'Weights'A vector of nonnegative observation weights, or a function handle @(yhat) returning such a vector. Weighted least squares is used.
'ErrorModel'The form of the error variance: 'constant' (default), 'proportional', or 'combined'.
'ErrorParameters'Initial values for the error-model parameters.
'RobustWgtFun'The name of a robust weight function ('andrews', 'bisquare', 'cauchy', 'fair', 'huber', 'logistic', 'talwar', or 'welsch'), enabling robust iteratively reweighted least squares. MATLAB accepts this name only inside an 'Options' structure; taking it as a Name/Value pair as well is an Octave extension.
'Tune'The tuning constant for the robust weight function.
'Options'A statset-style structure whose MaxIter, TolFun, TolX, and DerivStep fields override the corresponding defaults, and whose RobustWgtFun, Robust, WgtFun and Tune fields select a robust fit. RobustWgtFun names the weight function on its own and takes precedence over the other two; the older WgtFun is read only when Robust is 'on'. The structure statset ('nlinfit') returns carries WgtFun 'bisquare' beside Robust 'off', and so leaves the fit unweighted.

The remaining outputs describe the converged fit: R is the vector of raw residuals y - modelfun (beta, X), J is the Jacobian of modelfun with respect to beta at the solution, CovB is the estimated covariance matrix of the coefficients, MSE is the mean squared error, and ErrorModelInfo is a structure describing the fitted error model.

Algorithm

The coefficients are estimated by the Levenberg-Marquardt algorithm using a numerically computed (forward-difference) Jacobian. For an ordinary or weighted fit the coefficient covariance is CovB = MSE * inv (J' * W * J), where W is the diagonal matrix of observation weights and MSE is the weighted residual sum of squares divided by the error degrees of freedom n - p (with p coefficients). A non-constant 'ErrorModel' is fitted by generalized least squares, re-deriving the observation weights from the fitted values each iteration; the 'proportional' model weights each observation by the inverse squared fitted value, and MSE then estimates the proportionality constant of the variance.

For a robust fit ('RobustWgtFun') the coefficients are found by iteratively reweighted least squares applied to leverage-adjusted residuals (the leverage is taken from the ordinary fit and held fixed). The robust coefficient covariance follows the Street-Carroll-Ruppert convention, the same one used by robustfit: CovB = s^2 * inv (J' * J) and MSE = s^2, where the scale s blends the ordinary-fit scale ols_s with the robust scale robust_s at the solution as s^2 = (p^2 × ols_s^2 + n × robust_s^2) / (n + p^2), taken to be at least robust_s.

The robust MSE and CovB differ from MATLAB’s by up to a few tenths of a percent, because the shared robust scale does; robustfit documents that difference and why it is left in place. The coefficients themselves agree to about 1e-8.

See also: fitnlm, nlparci, nlpredci, NonLinearModel, robustfit

Source Code: nlinfit

Fit an exponential growth model y = b1 exp (b2 x).

 x = [1:10]';
 y = [2.1;2.9;4.2;5.3;7.1;9.4;12.8;16.5;22.1;29.8];
 modelfun = @(b, x) b(1) .* exp (b(2) .* x);
 beta = nlinfit (x, y, modelfun, [1; 0.3])
beta =

   1.6837
   0.2869

Robust fitting downweights a gross outlier (5th point corrupted).

 x = [1:10]';
 y = [2.1;2.9;4.2;5.3;7.1;9.4;12.8;16.5;22.1;29.8];
 y(5) = 30;
 modelfun = @(b, x) b(1) .* exp (b(2) .* x);
 beta_ols = nlinfit (x, y, modelfun, [1; 0.3]);
 beta_rob = nlinfit (x, y, modelfun, [1; 0.3], 'RobustWgtFun', 'bisquare');
 [beta_ols, beta_rob]
ans =

   3.9711   1.6792
   0.1963   0.2872