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

NameTypeRequiredDefaultDescription
tblOrXAnyYesInput table or predictor matrix.
yOrSpecAnyNoResponse vector, response variable name, model formula, or model specification.
optionsAnyVariadicModel specification and name-value options such as Intercept, ResponseVar, PredictorVars, VarNames, Exclude, and Weights.

Returns

NameTypeDescription
mdlAnyLinearModel object containing coefficients, fitted values, residuals, and summary statistics.

Errors

IdentifierWhenMessage
RunMat:fitlm:InvalidArgumentInputs, model specifications, dimensions, or name-value options are malformed or unsupported.fitlm: invalid argument
RunMat:fitlm:NumericalThe linear model cannot be fit numerically.fitlm: numerical failure
RunMat:fitlm:InternalRunMat 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 ResponseVar option.
  • Additive table formulas such as Y ~ A + B are supported for numeric predictors.
  • Matrix model specifications constant, linear, interactions, purequadratic, and quadratic are supported.
  • Rows containing NaN in predictors, response, or zero weights are omitted before fitting; Inf values are rejected.
  • Exclude accepts logical masks or one-based numeric row indices. Weights accepts 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, and ResponseName properties.
  • The Coefficients table contains Estimate, SE, tStat, and pValue variables with coefficient names in Properties.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.Estimate

Expected 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.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fitlm 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.