Main Content

NonLinearModel

Nonlinear regression model

Description

NonLinearModel is a fitted nonlinear regression model object. A regression model describes the relationship between a response and predictors. The nonlinearity in a nonlinear regression model refers to a nonlinear combination of predictors and predictor coefficients.

Use the properties of a NonLinearModel object to investigate a fitted nonlinear regression model. The object properties include information about coefficient estimates, summary statistics, fitting method, and input data. Use the object functions to predict responses and to evaluate and visualize the nonlinear regression model. You can predict model responses using the predict or feval object function.

Creation

Create a NonLinearModel object using fitnlm.

Properties

expand all

Coefficient Estimates

This property is read-only.

Covariance matrix of coefficient estimates, specified as a p-by-p matrix of numeric values. p is the number of coefficients in the fitted model, as given by NumCoefficients.

For details, see Coefficient Standard Errors and Confidence Intervals.

Data Types: single | double

This property is read-only.

Coefficient names, specified as a cell array of character vectors, each containing the name of the corresponding term.

Data Types: cell

This property is read-only.

Coefficient values, specified as a table. Coefficients contains one row for each coefficient and these columns:

  • Estimate — Estimated coefficient value

  • SE — Standard error of the estimate

  • tStatt-statistic for a two-sided test with the null hypothesis that the coefficient is zero

  • pValuep-value for the t-statistic

Use coefTest to perform other tests on the coefficients. Use coefCI to find the confidence intervals of the coefficient estimates.

To obtain any of these columns as a vector, index into the property using dot notation. For example, obtain the estimated coefficient vector in the model mdl:

beta = mdl.Coefficients.Estimate

Data Types: table

This property is read-only.

Number of coefficients in the fitted model, specified as a positive integer. NumCoefficients is the same as NumEstimatedCoefficients for NonLinearModel objects. NumCoefficients is equal to the degrees of freedom for regression.

Data Types: double

This property is read-only.

Number of estimated coefficients in the fitted model, specified as a positive integer. NumEstimatedCoefficients is the same as NumCoefficients for NonLinearModel objects. NumEstimatedCoefficients is equal to the degrees of freedom for regression.

Data Types: double

Summary Statistics

This property is read-only.

Degrees of freedom for the error (residuals), equal to the number of observations minus the number of estimated coefficients, specified as a positive integer.

Data Types: double

This property is read-only.

Diagnostic information for the model, specified as a table. Diagnostics can help identify outliers and influential observations. Diagnostics contains the following fields.

FieldMeaningUtility
LeverageDiagonal elements of HatMatrixLeverage for each observation indicates to what extent the fit is determined by the observed predictor values. A value close to 1 indicates that the fit is largely determined by that observation, with little contribution from the other observations. A value close to 0 indicates that the fit is largely determined by the other observations. For a model with P coefficients and N observations, the average value of Leverage is P/N. A Leverage value greater than 2*P/N indicates high leverage.
CooksDistanceCook's distanceCooksDistance is a measure of scaled change in fitted values. An observation with CooksDistance greater than three times the mean Cook's distance can be an outlier.
HatMatrixProjection matrix to compute fitted from observed responsesHatMatrix is an N-by-N matrix such that Fitted = HatMatrix*Y, where Y is the response vector and Fitted is the vector of fitted response values.

Diagnostics contains information that is helpful in finding outliers and influential observations. For more details, see Hat Matrix and Leverage and Cook’s Distance.

Use plotDiagnostics to plot observation diagnostics.

Rows not used in the fit because of missing values (in ObservationInfo.Missing) or excluded values (in ObservationInfo.Excluded) contain NaN values in the CooksDistance columns and zeros in the Leverage and HatMatrix columns.

To obtain any of these columns as an array, index into the property using dot notation. For example, obtain the Cook's distance vector in the model mdl:

CooksDistance = mdl.Diagnostics.CooksDistance;

Data Types: table

This property is read-only.

Fitted (predicted) response values based on input data, specified as an n-by-1 numeric vector. n is the number of observations in the input data. Use predict to compute predictions for other predictor values, or to compute confidence bounds on Fitted.

Data Types: single | double

This property is read-only.

Loglikelihood of the model distribution at the response values, specified as a numeric value. The mean is fitted from the model, and other parameters are estimated as part of the model fit.

Data Types: single | double

This property is read-only.

Criterion for model comparison, specified as a structure with these fields:

  • AIC — Akaike information criterion. AIC = –2*logL + 2*m, where logL is the loglikelihood and m is the number of estimated parameters.

  • AICc — Akaike information criterion corrected for the sample size. AICc = AIC + (2*m*(m + 1))/(n – m – 1), where n is the number of observations.

  • BIC — Bayesian information criterion. BIC = –2*logL + m*log(n).

  • CAIC — Consistent Akaike information criterion. CAIC = –2*logL + m*(log(n) + 1).

Information criteria are model selection tools that you can use to compare multiple models fit to the same data. These criteria are likelihood-based measures of model fit that include a penalty for complexity (specifically, the number of parameters). Different information criteria are distinguished by the form of the penalty.

When you compare multiple models, the model with the lowest information criterion value is the best-fitting model. The best-fitting model can vary depending on the criterion used for model comparison.

To obtain any of the criterion values as a scalar, index into the property using dot notation. For example, obtain the AIC value aic in the model mdl:

aic = mdl.ModelCriterion.AIC

Data Types: struct

This property is read-only.

F-statistic of the regression model, specified as a structure containing these fields:

  • FstatsF-statistic of the fitted model versus the null model

  • Pvaluep-value for the F-statistic

  • NullModel — Null model type, specified as 'constant' or 'zero'

If the fitted model contains an intercept term, NullModel is 'constant' (contains only an intercept term). Otherwise, NullModel is 'zero' (contains no terms).

Data Types: struct

This property is read-only.

Mean squared error, specified as a numeric value. The mean squared error is an estimate of the variance of the error term in the model.

Data Types: single | double

This property is read-only.

Residuals for the fitted model, specified as a table that contains one row for each observation and the columns described in this table.

ColumnDescription
RawObserved minus fitted values
PearsonRaw residuals divided by the root mean squared error (RMSE)
StandardizedRaw residuals divided by their estimated standard deviation
StudentizedRaw residual divided by an independent estimate of the residual standard deviation. The residual for observation i is divided by an estimate of the error standard deviation based on all observations except observation i.

Use plotResiduals to create a plot of the residuals. For details, see Residuals.

Rows not used in the fit because of missing values (in ObservationInfo.Missing) or excluded values (in ObservationInfo.Excluded) contain NaN values.

To obtain any of these columns as a vector, index into the property using dot notation. For example, obtain the raw residual vector r in the model mdl:

r = mdl.Residuals.Raw

Data Types: table

This property is read-only.

Root mean squared error (residuals), specified as a numeric value.

RMSE = sqrt(MSE),

where RMSE is the root mean squared error and MSE is the mean squared error. RMSE is an estimate of the standard deviation of the error term in the model.

Data Types: single | double

This property is read-only.

R-squared value for the model, specified as a structure with two fields:

  • Ordinary — Ordinary (unadjusted) R-squared

  • Adjusted — R-squared adjusted for the number of coefficients

The R-squared value is the proportion of the total sum of squares explained by the model. The ordinary R-squared value relates to the SSR and SST properties:

Rsquared = SSR/SST,

where SST is the total sum of squares, and SSR is the regression sum of squares.

For details, see Coefficient of Determination (R-Squared).

To obtain either of these values as a scalar, index into the property using dot notation. For example, obtain the adjusted R-squared value in the model mdl:

r2 = mdl.Rsquared.Adjusted

Data Types: struct

This property is read-only.

Sum of squared errors (residuals), specified as a numeric value. If the model was trained with observation weights, the sum of squares in the SSE calculation is the weighted sum of squares.

Data Types: single | double

This property is read-only.

Regression sum of squares, specified as a numeric value. SSR is equal to the sum of the squared deviations between the fitted values and the mean of the response. If the model was trained with observation weights, the sum of squares in the SSR calculation is the weighted sum of squares.

Data Types: single | double

This property is read-only.

Total sum of squares, specified as a numeric value. SST is equal to the sum of squared deviations of the response vector y from the mean(y). If the model was trained with observation weights, the sum of squares in the SST calculation is the weighted sum of squares.

Data Types: single | double

Fitting Method

This property is read-only.

Robust fit information, specified as a structure with the following fields:

FieldDescription
WgtFunRobust weighting function, such as 'bisquare' (see Robust Options)
TuneValue specified for tuning parameter (can be [])
WeightsVector of weights used in final iteration of robust fit

This structure is empty unless fitnlm constructed the model using robust regression.

Data Types: struct

Input Data

This property is read-only.

Model information, specified as a NonLinearFormula object.

Display the formula of the fitted model mdl by using dot notation.

mdl.Formula

This property is read-only.

Information about the fitting process, specified as a structure with the following fields:

  • InitialCoefs — Initial coefficient values (the beta0 vector)

  • IterOpts — Options included in the Options name-value pair argument for fitnlm.

Data Types: struct

This property is read-only.

Number of observations the fitting function used in fitting, specified as a positive integer. NumObservations is the number of observations supplied in the original table, dataset, or matrix, minus any excluded rows (set with the 'Exclude' name-value pair argument) or rows with missing values.

Data Types: double

This property is read-only.

Number of predictor variables used to fit the model, specified as a positive integer.

Data Types: double

This property is read-only.

Number of variables in the input data, specified as a positive integer. NumVariables is the number of variables in the original table or dataset, or the total number of columns in the predictor matrix and response vector.

NumVariables also includes any variables that are not used to fit the model as predictors or as the response.

Data Types: double

This property is read-only.

Observation information, specified as an n-by-4 table, where n is equal to the number of rows of input data. ObservationInfo contains the columns described in this table.

ColumnDescription
WeightsObservation weights, specified as a numeric value. The default value is 1.
ExcludedIndicator of excluded observations, specified as a logical value. The value is true if you exclude the observation from the fit by using the 'Exclude' name-value pair argument.
MissingIndicator of missing observations, specified as a logical value. The value is true if the observation is missing.
SubsetIndicator of whether or not the fitting function uses the observation, specified as a logical value. The value is true if the observation is not excluded or missing, meaning the fitting function uses the observation.

To obtain any of these columns as a vector, index into the property using dot notation. For example, obtain the weight vector w of the model mdl:

w = mdl.ObservationInfo.Weights

Data Types: table

This property is read-only.

Observation names, specified as a cell array of character vectors containing the names of the observations used in the fit.

  • If the fit is based on a table or dataset containing observation names, ObservationNames uses those names.

  • Otherwise, ObservationNames is an empty cell array.

Data Types: cell

This property is read-only.

Names of predictors used to fit the model, specified as a cell array of character vectors.

Data Types: cell

This property is read-only.

Response variable name, specified as a character vector.

Data Types: char

This property is read-only.

Information about variables contained in Variables, specified as a table with one row for each variable and the columns described in this table.

ColumnDescription
ClassVariable class, specified as a cell array of character vectors, such as 'double' and 'categorical'
Range

Variable range, specified as a cell array of vectors

  • Continuous variable — Two-element vector [min,max], the minimum and maximum values

  • Categorical variable — Vector of distinct variable values

InModelIndicator of which variables are in the fitted model, specified as a logical vector. The value is true if the model includes the variable.
IsCategoricalIndicator of categorical variables, specified as a logical vector. The value is true if the variable is categorical.

VariableInfo also includes any variables that are not used to fit the model as predictors or as the response.

Data Types: table

This property is read-only.

Names of variables, specified as a cell array of character vectors.

  • If the fit is based on a table or dataset, this property provides the names of the variables in the table or dataset.

  • If the fit is based on a predictor matrix and response vector, VariableNames contains the values specified by the 'VarNames' name-value pair argument of the fitting method. The default value of 'VarNames' is {'x1','x2',...,'xn','y'}.

VariableNames also includes any variables that are not used to fit the model as predictors or as the response.

Data Types: cell

This property is read-only.

Input data, specified as a table. Variables contains both predictor and response values. If the fit is based on a table or dataset array, Variables contains all the data from the table or dataset array. Otherwise, Variables is a table created from the input data matrix X and the response vector y.

Variables also includes any variables that are not used to fit the model as predictors or as the response.

Data Types: table

Object Functions

expand all

fevalEvaluate nonlinear regression model prediction
predictPredict response of nonlinear regression model
randomSimulate responses for nonlinear regression model
coefCIConfidence intervals of coefficient estimates of nonlinear regression model
coefTestLinear hypothesis test on nonlinear regression model coefficients
partialDependenceCompute partial dependence
plotDiagnosticsPlot diagnostics of nonlinear regression model
plotPartialDependenceCreate partial dependence plot (PDP) and individual conditional expectation (ICE) plots
plotResidualsPlot residuals of nonlinear regression model
plotSlicePlot of slices through fitted nonlinear regression surface

Examples

collapse all

Fit a nonlinear regression model for auto mileage based on the carbig data. Predict the mileage of an average car.

Load the sample data. Create a matrix X containing the measurements for the horsepower (Horsepower) and weight (Weight) of each car. Create a vector y containing the response values in miles per gallon (MPG).

load carbig
X = [Horsepower,Weight];
y = MPG;

Fit a nonlinear regression model.

modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + ...
    b(4)*x(:,2).^b(5);
beta0 = [-50 500 -1 500 -1];
mdl = fitnlm(X,y,modelfun,beta0)
mdl = 
Nonlinear regression model:
    y ~ b1 + b2*x1^b3 + b4*x2^b5

Estimated Coefficients:
          Estimate      SE        tStat       pValue 
          ________    _______    ________    ________

    b1     -49.383     119.97    -0.41164     0.68083
    b2      376.43     567.05     0.66384     0.50719
    b3    -0.78193    0.47168     -1.6578    0.098177
    b4      422.37     776.02     0.54428     0.58656
    b5    -0.24127    0.48325    -0.49926     0.61788


Number of observations: 392, Error degrees of freedom: 387
Root Mean Squared Error: 3.96
R-Squared: 0.745,  Adjusted R-Squared 0.743
F-statistic vs. constant model: 283, p-value = 1.79e-113

Find the predicted mileage of an average car. Because the sample data contains some missing (NaN) observations, compute the mean using mean with the 'omitnan' option.

Xnew = mean(X,'omitnan')  
Xnew = 1×2
103 ×

    0.1051    2.9794

MPGnew = predict(mdl,Xnew)
MPGnew = 
21.8073

More About

expand all

Version History

Introduced in R2012a