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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | NumericArray | Yes | — | Predictor matrix with observations in rows. |
Y | Any | Yes | — | Response class vector or two-column binomial counts matrix. |
NameValue | Any | Variadic | — | Name-value options such as Model, Link, Constant, Weights, IterationLimit, Tolerance, and Options. |
Returns
| Name | Type | Description |
|---|---|---|
B | NumericArray | Coefficient matrix with one column per non-reference response category. |
dev | NumericScalar | Model deviance. |
stats | Any | Fit statistics structure containing beta, se, t, p, covb, coeffcorr, and dfe. |
Returned values from mnrfit depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:mnrfit:InvalidArgument | Inputs, response values, dimensions, or options are malformed. | mnrfit: invalid argument |
RunMat:mnrfit:Convergence | The multinomial logistic solver cannot make numerical progress. | mnrfit: convergence failure |
RunMat:mnrfit:Internal | RunMat cannot allocate or construct mnrfit outputs. | mnrfit: internal error |
How mnrfit works
Xmust be a finite real numeric matrix with observations in rows and predictors in columns.Ymay be a numeric, logical, string, character, cellstr, or categorical class vector with one response per row ofX. Classes are sorted, and the final class is used as the reference category.Ymay also be anN-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 containingbeta,se,t,p,covb,coeffcorr,dfe,s,estdisp, andclassNames.Modelcurrently supports"nominal";Linksupports"logit";Constantaccepts logical values or"on"/"off";Interactionsaccepts"off".Weightsaccepts a nonnegative finite observation-weight vector with positive total weight.IterationLimitandTolerancecontrol Newton solver convergence and may also be supplied through astatset-styleOptionsstruct.MaxIterandTolXare accepted as compatibility aliases.- Rows with
NaNinX, numericY, or grouped counts are omitted before fitting. Displayis accepted for compatibility. Overdispersion estimation is not implemented, soEstDispmust 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.
Related Stats functions
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Summary
binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how mnrfit is executed, line by line, in Rust.
- View the source for mnrfit in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.