mnrfit — Fit nominal multinomial logistic regression coefficients.

mnrfit(X,Y) fits nominal multinomial logistic regression coefficients for predictor matrix X and response Y. Rows of X are observations, columns are predictors, and columns of B correspond to all response classes except the final sorted reference class.

Syntax

B = mnrfit(X, Y)
B = mnrfit(X, Y, Name, Value)
[B, dev, stats] = mnrfit(X, Y)
[B, dev, stats] = mnrfit(X, Y, Name, Value)

Inputs

NameTypeRequiredDefaultDescription
XNumericArrayYesPredictor matrix with observations in rows.
YAnyYesResponse class vector or two-column binomial counts matrix.
NameValueAnyVariadicName-value options such as Model, Link, Constant, Weights, IterationLimit, Tolerance, and Options.

Returns

NameTypeDescription
BNumericArrayCoefficient matrix with one column per non-reference response category.
devNumericScalarModel deviance.
statsAnyFit statistics structure containing beta, se, t, p, covb, coeffcorr, and dfe.

Returned values from mnrfit depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:mnrfit:InvalidArgumentInputs, response values, dimensions, or options are malformed.mnrfit: invalid argument
RunMat:mnrfit:ConvergenceThe multinomial logistic solver cannot make numerical progress.mnrfit: convergence failure
RunMat:mnrfit:InternalRunMat cannot allocate or construct mnrfit outputs.mnrfit: internal error

How mnrfit works

  • X must be a finite real numeric matrix with observations in rows and predictors in columns.
  • Y may be a numeric, logical, string, character, cellstr, or categorical class vector with one response per row of X. Classes are sorted, and the final class is used as the reference category.
  • Y may also be an N-by-K numeric grouped-counts matrix. Columns are treated as response categories, and row totals become multinomial observation weights.
  • B = mnrfit(...) returns a coefficient matrix with one row per model term and one column per non-reference class. A constant term is included by default.
  • [B,dev,stats] = mnrfit(...) returns model deviance and a stats structure containing beta, se, t, p, covb, coeffcorr, dfe, s, estdisp, and classNames.
  • Model currently supports "nominal"; Link supports "logit"; Constant accepts logical values or "on"/"off"; Interactions accepts "off".
  • Weights accepts a nonnegative finite observation-weight vector with positive total weight. IterationLimit and Tolerance control Newton solver convergence and may also be supplied through a statset-style Options struct. MaxIter and TolX are accepted as compatibility aliases.
  • Rows with NaN in X, numeric Y, or grouped counts are omitted before fitting.
  • Display is accepted for compatibility. Overdispersion estimation is not implemented, so EstDisp must be "off".

Examples

Fit a binary nominal logistic model

X = [0; 1; 2; 3; 4];
y = [1; 2; 1; 2; 2];
[B,dev,stats] = mnrfit(X, y)

Expected output:

B has two rows, one intercept and one predictor coefficient. stats.se has the same shape as B.

Fit a three-class nominal model

X = [0; 1; 0; 1; 2; 3];
y = [1; 1; 2; 2; 3; 3];
B = mnrfit(X, y, "Model", "nominal")

Expected output:

B has two columns because the final sorted class is the reference category.

Fit binomial count responses

X = [0; 1; 2; 3];
Y = [8 1 1; 5 3 2; 2 5 3; 1 6 4];
B = mnrfit(X, Y, "IterationLimit", 200)

Expected output:

Y columns contribute grouped response counts, and B has one column per non-reference category.

Fit text response labels

X = [0; 1; 2; 3; 4];
y = ["low"; "high"; "low"; "high"; "high"];
[B,dev,stats] = mnrfit(X, y, "Tolerance", 1e-7)

Expected output:

stats.classNames is returned as a string array containing the sorted response classes.

Using mnrfit with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how mnrfit changes the result.

Run a small mnrfit example, explain the result, then change one input and compare the output.

FAQ

Which response class is the reference class?

RunMat sorts numeric or logical response classes and uses the final sorted class as the reference, matching the default nominal coding used by MATLAB-style multinomial fits.

Does RunMat implement ordinal models?

No. The current implementation is for nominal multinomial logistic regression. Model values other than "nominal" return a compatibility error.

Does RunMat estimate overdispersion?

No. EstDisp is accepted only as "off"; stats.s is returned as 1.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how mnrfit is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.