www.gusucode.com > stats 源码程序 matlab案例代码 > stats/MultinomialRegressionforOrdinalResponsesExample.m

    %% Multinomial Regression for Ordinal Responses  
% Fit a multinomial regression model for categorical responses with natural
% ordering among categories.   

% Copyright 2015 The MathWorks, Inc.


%% 
% Load the sample data and define the predictor variables. 
load carbig
X = [Acceleration Displacement Horsepower Weight]; 

%%
% The predictor variables are the acceleration, engine displacement, horsepower,
% and weight of the cars. The response variable is miles per gallon (mpg).  

%% 
% Create an ordinal response variable categorizing |MPG| into four levels
% from 9 to 48 mpg by labeling the response values in the range 9-19 as
% 1, 20-29 as 2, 30-39 as 3, and 40-48 as 4. 
miles = ordinal(MPG,{'1','2','3','4'},[],[9,19,29,39,48]);  

%% 
% Fit an ordinal response model for the response variable |miles|. 
[B,dev,stats] = mnrfit(X,miles,'model','ordinal');
B 

%%
% The first three elements of |B| are the intercept terms for the models,
% and the last four elements of |B| are the coefficients of the covariates,
% assumed common across all categories. This model corresponds to _parallel
% regression_, which is also called the _proportional odds_ model, where
% there is a different intercept but common slopes among categories. You
% can specify this using the |'interactions','off'| name-value pair argument,
% which is the default for ordinal models.  

%%  
[B(1:3)'; repmat(B(4:end),1,3)]  

%% 
% The link function in the model is logit (|'link','logit'|), which is the
% default for an ordinal model. The coefficients express the relative risk
% or log odds of the mpg of a car being less than or equal to one value
% versus greater than that value. 
%
% The proportional odds model in this example is

%%
% $\begin{array}{l}
% \ln \left( {\frac{{P\left( {mpg \le 19} \right)}}{{P\left( {mpg > 19} \right)}}} \right) =  - 16.6895 + 0.1048{X_A} + 0.0103{X_D} + 0.0645{X_H} + 0.0017{X_W}\\
% \\
% \ln \left( {\frac{{P\left( {mpg \le 29} \right)}}{{P\left( {mpg > 29} \right)}}} \right) =  - 11.7208 + 0.1048{X_A} + 0.0103{X_D} + 0.0645{X_H} + 0.0017{X_W}\\
% \\
% \ln \left( {\frac{{P\left( {mpg \le 39} \right)}}{{P\left( {mpg > 39} \right)}}} \right) =  - 8.0606 + 0.1048{X_A} + 0.0103{X_D} + 0.0645{X_H} + 0.0017{X_W}
% \end{array}$

%%
% For example, the coefficient estimate of 0.1048 indicates that a unit
% change in acceleration would impact the odds of the mpg of a car being
% less than or equal to 19 versus more than 19, or being less than or equal
% to 29 versus greater than 29, or being less than or equal to 39 versus
% greater than 39, by a factor of exp(0.01048) given all else is equal.  

%% 
% Assess the significance of the coefficients. 
stats.p 

%%
% The $p$-values of 0.035, 0.0000, and 0.0118 for engine displacement, horsepower,
% and weight of a car, respectively, indicate that these factors are significant
% on the odds of mpg of a car being less than or equal to a certain value
% versus being greater than that value.