Categories &

Functions List

Function Reference: fitmnr

statistics: mdl = fitmnr (X, Y)

statistics: mdl = fitmnr (Tbl, Y)

statistics: mdl = fitmnr (Tbl, ResponseVarName)

statistics: mdl = fitmnr (Tbl, Formula)

statistics: mdl = fitmnr (…, name, value)

Fit a multinomial regression model.

mdl = fitmnr (X, Y) fits a multinomial regression of the response Y on the columns of the N-by-P numeric predictor matrix X, and returns a MultinomialRegression object. An intercept is always fitted, so X holds no column of ones.

Y is an N-by-1 numeric, logical, string or categorical vector, a cell array of character vectors, or a character matrix, each element or row the category of one observation; or an N-by-K matrix of counts, Y(i,j) observations of category j at the predictor values X(i,:), as mnrfit documents.

mdl = fitmnr (Tbl, Y) takes the predictors from the variables of the table Tbl, and the response from Y. mdl = fitmnr (Tbl, ResponseVarName) takes the response from the variable of Tbl that ResponseVarName names, a character vector or a string scalar, and the predictors from the others. mdl = fitmnr (Tbl, Formula) takes both from the Wilkinson formula Formula, such as 'y ~ x1 + x2*x3'. A numeric variable of a table is a continuous predictor and any other is categorical, coded as one indicator per level bar the first.

The following name, value pairs are accepted:

NameValue
'ModelType''nominal' (default), a model contrasting each category with the last; 'ordinal', a model of the cumulative probabilities of ordered categories; or 'hierarchical', a model of each category against the ones after it.
'Link'The link function of an ordinal or hierarchical model: 'logit' (default), 'probit', 'comploglog' or 'loglog'. A nominal model always uses the logit link.
'IncludeClassInteractions'true to give each category its own slopes, false for slopes shared by every category. The default is true for nominal and hierarchical models and false for ordinal ones. A nominal model without interactions fits its intercepts alone.
'EstimateDispersion'true to estimate the dispersion and scale the standard errors by it, false (default) for the theoretical dispersion of 1.
'Weights'An N-by-1 vector of nonnegative observation weights, each multiplying the counts of its observation; ones by default.
'CategoricalPredictors'The predictors to treat as categorical: a vector of indices, a logical vector with one element per predictor, a cell array or string array of names, or 'all'.
'PredictorNames'The names of the predictors: for a matrix X, one per column, x1, x2, … by default; for a table, the variables to use as predictors. A formula names its own.
'ResponseName'The name of the response, 'y' by default. A response variable name or a formula names its own.
'IterationLimit'The largest number of iterations of the fit, 100 by default.
'Tolerance'The convergence tolerance of the fit, the largest change of a coefficient relative to its value, 1e-6 by default.

A row holding a missing value of a predictor it uses, of the response, or of the weights is left out of the fit.

See also: MultinomialRegression, mnrfit, mnrval, fitglm

Source Code: fitmnr

A nominal model of the iris species on the length of the sepal, and the probabilities it gives three new flowers

 load fisheriris
 mdl = fitmnr (meas(:,1), species, 'PredictorNames', {'SepalLength'})
mdl =

  Multinomial regression with nominal responses

  4x4 table

                               Value         SE        tStat        pValue       
                              ________    ________    ________    ___________    

    (Intercept_setosa)          38.759     5.69068     6.81097    9.69448e-12    
    SepalLength_setosa         -6.8464     1.02222    -6.69756    2.11927e-11    
    (Intercept_versicolor)     12.6771     2.90634     4.36187    1.28955e-05    
    SepalLength_versicolor    -2.03071    0.465669    -4.36083    1.29567e-05    


150 observations, 296 error degrees of freedom
Dispersion: 1
Chi^2-statistic vs. constant model: 147.516, p-value = 9.27601e-33
 [label, probs] = predict (mdl, [5; 6; 7])
label =
  3x1 cell array

    {'setosa'    }    
    {'versicolor'}    
    {'virginica' }    

probs =

   8.7285e-01   1.1772e-01   9.4381e-03
   3.5950e-02   5.9845e-01   3.6560e-01
   8.6059e-05   1.7683e-01   8.2309e-01

An ordinal model, the species ordered by size, and the cumulative probabilities it gives three new flowers

 load fisheriris
 mdl = fitmnr (meas(:,1), species, 'ModelType', 'ordinal')
mdl =

  Multinomial regression with ordinal responses

  3x4 table

                               Value         SE        tStat       pValue       
                              ________    ________    _______    ___________    

    (Intercept_setosa)         18.5691     2.30164    8.06778    7.15903e-16    
    (Intercept_versicolor)     21.6898      2.5748    8.42386     3.6429e-17    
    x1                        -3.45235    0.417495    -8.2692    1.34859e-16    


150 observations, 297 error degrees of freedom
Dispersion: 1
Chi^2-statistic vs. constant model: 138.507, p-value = 5.64577e-32
 [~, cumprob] = predict (mdl, [5; 6; 7], 'ProbabilityType', 'cumulative')
cumprob =

   7.8707e-01   9.8820e-01
   1.0480e-01   7.2625e-01
   3.6940e-03   7.7510e-02