Skip to Main Content Skip to Search
Product Documentation

mnrfit - Multinomial logistic regression

Syntax

B = mnrfit(X,Y)
B = mnrfit(X,Y,param1,val1,param2,val2,...)
[B,dev] = mnrfit(...)
[B,dev,stats] = mnrfit(...)

Description

B = mnrfit(X,Y) returns a matrix B of coefficient estimates for a multinomial logistic regression of the responses in Y on the predictors in X. X is an n-by-p matrix of p predictors at each of n observations. Y is an n-by-k matrix, where Y(i,j) is the number of outcomes of the multinomial category j for the predictor combinations given by X(i,:). The sample sizes for each observation are given by the row sums sum(Y,2).

Alternatively, Y can be an n-by-1 column vector of scalar integers from 1 to k indicating the value of the response for each observation, and all sample sizes are taken to be 1.

The result B is a (p+1)-by-(k–1) matrix of estimates, where each column corresponds to the estimated intercept term and predictor coefficients, one for each of the first k–1 multinomial categories. The estimates for the kth category are taken to be zero.

mnrfit treats NaNs in either X or Y as missing values, and ignores them.

B = mnrfit(X,Y,param1,val1,param2,val2,...) allows you to specify optional parameter name/value pairs to control the model fit. Parameters are:

[B,dev] = mnrfit(...) returns the deviance of the fit dev.

[B,dev,stats] = mnrfit(...) returns a structure stats that contains the following fields:

Examples

Fit multinomial logistic regression models to data with one predictor variable and three categories in the response variable:

x = [-3 -2 -1 0 1 2 3]';
Y = [1 11 13; 2 9 14; 6 14 5; 5 10 10; 5 14 6; 7 13 5;...
    8 11 6];
bar(x,Y,'stacked'); ylim([0 25]);

% Now fit a nominal model for the individual response 
% category probabilities, with separate slopes on the
% single predictor variable, x, for each
% category:

% The first row of betaHatNom contains the intercept terms 
% for the first two response categories.  The second row 
% contains the slopes.
betaHatNom = mnrfit(x,Y,'model','nominal',...
'interactions','on')

% Compute the predicted probabilities for the three 
% response categories:
xx = linspace(-4,4)';
pHatNom = mnrval(betaHatNom,xx,'model','nominal',...
'interactions','on');
line(xx,cumsum(25*pHatNom,2),'LineWidth',2);

 
 

Fit a "parallel" ordinal model for the cumulative response category probabilities, with a common slope on the single predictor variable, x, across all categories:

% The first two elements of betaHatOrd are the
% intercept terms for the first two response categories.
% The last element of betaHatOrd is the common slope.
betaHatOrd = mnrfit(x,Y,'model','ordinal',...
	'interactions','off')

% Compute the predicted cumulative probabilities for the
% first two response categories.  The cumulative
% probability for the third category is always 1.
pHatOrd = mnrval(betaHatOrd,xx,'type','cumulative',...
'model','ordinal','interactions','off');
bar(x,cumsum(Y,2),'grouped'); ylim([0 25]);
line(xx,25*pHatOrd,'LineWidth',2);

References

[1] McCullagh, P., and J. A. Nelder. Generalized Linear Models. New York: Chapman & Hall, 1990.

See Also

glmfit | glmval | mnrval | regress | regstats

How To

  


 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS