Categories &

Functions List

Function Reference: mnrfit

statistics: B = mnrfit (X, Y)

statistics: B = mnrfit (X, Y, name, value)

statistics: [B, dev] = mnrfit (…)

statistics: [B, dev, stats] = mnrfit (…)

Fit a multinomial logistic regression model.

Nominal models are fitted with a baseline-category multinomial logit, using the last category of Y as the reference. Ordinal models are fitted with a cumulative link model and hierarchical models with a sequential (continuation-ratio) link model, both honouring the 'link' option below. Nominal models always use the logit link.

B = mnrfit (X, Y) returns a matrix, B, of coefficient estimates for a multinomial logistic regression of the nominal responses in Y on the predictors in X. X is an N×P numeric matrix the observations on predictor variables, where N corresponds to the number of observations and P corresponds to predictor variables. Y contains the response category labels and it can be an N×1 vector of positive integers, logical values, character vectors in a cell array, strings, or categorical values, each element the category of one observation. A cell array, string, or categorical Y can also be a row vector. Y can also be a character matrix with each row holding the label of an observation of X.

Y can also be an N×K numeric or logical matrix of counts, with a column for each of the K categories: Y(i,j) is the number of observations in category j at the predictor values in X(i,:), so that a row of X stands for the sum (Y(i,:)) observations it was seen with. The counts must be nonnegative integers. A row of zeros stands for no observations and takes no part in the estimates, though it counts in 'dfe', as in MATLAB. A matrix of 1s and 0s with a single 1 in each row is the same as a vector of one category per observation.

The categories of a numeric or logical Y are taken in sorted order, those of a cell array, string, or character matrix in order of first appearance, and those of a categorical Y in the order of its categories. A category without observations is ignored wherever it lies among the categories of a categorical Y; MATLAB ignores only a last one and fits coefficients for any other. Likewise the categories of a numeric Y are the values it holds, where MATLAB numbers them from 1 to the largest and fits coefficients for any value missing below it, and a column of zeros in a matrix Y is no category, where MATLAB fits coefficients for it. The last category is the reference category of a nominal model. An observation whose label is missing (an empty character vector, <missing>, or <undefined>), whose row of a matrix Y holds a NaN, or whose predictors hold a NaN, is left out of the fit, and its rows of the residuals in stats are NaN.

A Y with a single category leaves nothing to fit: a nominal or hierarchical model returns a B with no columns, a deviance of 0 and residuals of 0, the raw and Pearson residuals of a hierarchical model having no columns, and an ordinal model is an error.

B = mnrfit (X, Y, name, value) returns a matrix, B, of coefficient estimates for a multinomial model fit with additional parameters specified Name-Value pair arguments.

NameValue
'model'The type of model to fit: 'nominal' (default) for a baseline-category model, 'ordinal' for a cumulative model, or 'hierarchical' for a sequential (continuation-ratio) model.
'link'The link function for ordinal and hierarchical models: 'logit' (default), 'probit', 'comploglog', or 'loglog'. Nominal models always use the logit link.
'interactions''on' to give each category its own slopes, or 'off' for slopes shared by every category, the intercepts always differing. The default is 'on' for nominal and hierarchical models and 'off' for ordinal ones. With 'on', B is a (P+1)×(K-1) matrix whose column j holds the intercept and slopes of category j; with 'off', B is a (K-1+P)×1 vector of the K-1 intercepts followed by the P shared slopes. A nominal model without interactions has no slopes to share, so it fits the K-1 intercepts alone, with a warning.
'weights'An N×1 vector of nonnegative observation weights, each multiplying the counts of its row of Y. The default is a vector of ones. A row whose weight is NaN is left out of the fit.
'iterationlimit'The largest number of iterations of the fit, a positive integer. The default is 100.
'tolerance'The convergence tolerance of the fit, a positive scalar: the fit stops once no coefficient changes by more than this fraction of its value. The default is 1e-6.
'estdisp''on' to estimate a dispersion parameter, scaling the coefficient standard errors by it and testing the coefficients against the t distribution, or 'off' (default) for the theoretical dispersion of 1.

[B, dev, stats] = mnrfit (…) also returns the deviance of the fit, dev, and a structure stats with the fitted coefficients 'beta' (same as B), their standard errors 'se', covariance matrix 'covb', correlation matrix 'coeffcorr', error degrees of freedom 'dfe', the coefficient t statistics 't' and p-values 'p', the dispersion parameters 's', 'sfit', and 'estdisp', and the raw, Pearson, and deviance residuals 'resid', 'residp', and 'residd'.

The raw and Pearson residuals are taken on what the model fits: an N×K matrix over the categories of a nominal model, and an N×(K-1) matrix over the cumulative probabilities of an ordinal model or over the stages of a hierarchical one, where a row is NaN at the stages that none of its observations reach. A raw residual is the count minus its fitted value, the count times the fitted probability, and a Pearson residual divides it by the binomial standard deviation of the count; a weight multiplies both counts. A row of zeros has a Pearson residual of NaN. The deviance residuals are an N×1 vector holding the contribution of each row to the deviance, so they sum to dev; for an observation of one category it is -2 times the log of the fitted probability of that category. For a hierarchical model MATLAB sums the signed deviance residuals of the stages instead, which is NaN for every observation in the first category. 'sfit' is the square root of the Pearson statistic over the categories divided by 'dfe', and it differs from MATLAB’s, which for ordinal and hierarchical models is taken over the residuals in 'residp'.

'dfe' is the number of rows of X times K-1, less the number of coefficients; for a hierarchical model each stage counts only the rows with observations that reach it.

See also: mnrval, logistic_regression

Source Code: mnrfit