fitlm — Fit an ordinary least-squares linear regression model.
fitlm fits an ordinary least-squares linear regression model from a numeric predictor matrix or table and returns a LinearModel object.
Syntax
mdl = fitlm(tbl)
mdl = fitlm(tbl, modelspec)
mdl = fitlm(X, y)
mdl = fitlm(___, Name, Value)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
tblOrX | Any | Yes | — | Input table or predictor matrix. |
yOrSpec | Any | No | — | Response vector, response variable name, model formula, or model specification. |
options | Any | Variadic | — | Model specification and name-value options such as Intercept, ResponseVar, PredictorVars, VarNames, Exclude, and Weights. |
Returns
| Name | Type | Description |
|---|---|---|
mdl | Any | LinearModel object containing coefficients, fitted values, residuals, and summary statistics. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:fitlm:InvalidArgument | Inputs, model specifications, dimensions, or name-value options are malformed or unsupported. | fitlm: invalid argument |
RunMat:fitlm:Numerical | The linear model cannot be fit numerically. | fitlm: numerical failure |
RunMat:fitlm:Internal | RunMat cannot construct a LinearModel result. | fitlm: internal error |
How fitlm works
fitlm(X,y)fits a model with an intercept and one linear term per predictor column.- Table input uses the last variable as the response by default, or the response from a response name, formula, or
ResponseVaroption. - Additive table formulas such as
Y ~ A + Bare supported for numeric predictors. - Matrix model specifications
constant,linear,interactions,purequadratic, andquadraticare supported. - Rows containing
NaNin predictors, response, or zero weights are omitted before fitting;Infvalues are rejected. Excludeaccepts logical masks or one-based numeric row indices.Weightsaccepts a numeric vector, and table input also accepts a weights variable name.- The returned model exposes
Coefficients,Fitted,Residuals.Raw,NumObservations,DFE,RMSE,Rsquared,Formula,PredictorNames, andResponseNameproperties. - The
Coefficientstable containsEstimate,SE,tStat, andpValuevariables with coefficient names inProperties.RowNames. - Categorical predictors, robust regression, and Wilkinson interaction formula terms are not supported yet and raise explicit errors.
Examples
Fit a matrix-input linear model
X = [0; 1; 2; 3];
y = [1; 3; 5; 7];
mdl = fitlm(X, y);
mdl.Coefficients.EstimateExpected output:
The coefficient estimates are approximately [1; 2].Fit and predict
mdl = fitlm(X, y);
yhat = predict(mdl, [4; 5])Expected output:
yhat is approximately [9; 11].Fit a named table formula
T = table(A, B, Y, 'VariableNames', {'A','B','Y'});
mdl = fitlm(T, 'Y ~ A + B')Expected output:
mdl uses A and B as predictors and Y as the response.Using fitlm with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how fitlm changes the result.
Run a small fitlm example, explain the result, then change one input and compare the output.
FAQ
Does fitlm add an intercept?⌄
Yes. The default model includes an intercept. Use the Intercept option or a no-intercept formula to omit it.
How are missing rows handled?⌄
Rows with NaN in any active predictor or response are omitted before fitting, matching MATLAB's complete-observation behavior.
Related Stats functions
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · 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 fitlm is executed, line by line, in Rust.
- View the source for fitlm 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.