WOLFRAM

Enable JavaScript to interact with content and submit forms on Wolfram websites. Learn how
Wolfram Language & System Documentation Center

LinearModelFit [{{x1,y1},{x2,y2},},{f1,f2,},x]

constructs a linear model of the form that fits the yi for successive xi values.

LinearModelFit [data,{f1,f2,},{x1,x2,}]

constructs a linear model where the fi depend on the variables xk.

LinearModelFit [{m,v}]

constructs a linear model from the design matrix m and response vector v.

Details and Options
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Data  
Model  
Properties  
Data & Fitted Functions  
Residuals  
Sums of Squares  
Parameter Estimation Diagnostics  
Influence Measures  
Prediction Values  
Goodness-of-Fit Measures  
Generalizations & Extensions  
Options  
ConfidenceLevel  
IncludeConstantBasis  
LinearOffsetFunction  
Show More Show More
NominalVariables  
VarianceEstimatorFunction  
Weights  
WorkingPrecision  
Applications  
Properties & Relations  
See Also
Tech Notes
Related Guides
History
Cite this Page

LinearModelFit

LinearModelFit [{{x1,y1},{x2,y2},},{f1,f2,},x]

constructs a linear model of the form that fits the yi for successive xi values.

LinearModelFit [data,{f1,f2,},{x1,x2,}]

constructs a linear model where the fi depend on the variables xk.

LinearModelFit [{m,v}]

constructs a linear model from the design matrix m and response vector v.

Details and Options

  • LinearModelFit attempts to model the input data using a linear combination of functions.
  • LinearModelFit produces a linear model of the form under the assumption that the original are independent normally distributed with mean and common standard deviation.
  • LinearModelFit returns a symbolic FittedModel object to represent the linear model it constructs.
  • The value of the best-fit function from LinearModelFit at a particular point x1, can be found from model[x1,].
  • Data
  • Possible forms of data are:
  • {y1,y2,} equivalent to the form {{1,y1},{2,y2},}
    {{x11,x12,,y1},} a list of independent values xij and the responses yi
    {{x11,x12,}y1,} a list of rules between input values and response
    {{x11,x12,},}{y1,y2,} a rule between a list of input values and responses
    {{x11,,y1,},}n fit the n^(th) column of a matrix
    Tabular []name fit the column name in a tabular object
  • With multivariate data such as {{x_(11),x_(12),... ,y_(1)},{x_(21),x_(22),... ,y_(2)},...}, the number of coordinates xi1, xi2, should equal the number of variables xi.
  • The data points can be approximate real numbers. Uncertainty can be specified using Around .
  • Additionally, data can be specified using a design matrix without specifying functions and variables:
  • {m,v} a design matrix m and response vector v
  • In LinearModelFit [{m,v }], the design matrix m is formed from the values of basis functions fi at data points in the form {{f1,f2,},{f1,f2,},}. The response vector v is the list of responses {y1,y2,}.
  • When a design matrix is used, the basis functions fi can be specified using the form LinearModelFit [{m,v},{f1,f2,}].
  • For a design matrix m and response vector v, the model is , where is the vector of parameters to be estimated.
  • Options
  • LinearModelFit takes the following options:
  • ConfidenceLevel 95/100 confidence level to use for parameters and predictions
    IncludeConstantBasis True whether to include a constant basis function
    LinearOffsetFunction None known offset in the linear predictor
    NominalVariables None variables considered as nominal or categorical
    VarianceEstimatorFunction Automatic function for estimating the error variance
    Weights Automatic weights for data elements
    WorkingPrecision Automatic precision used in internal computations
  • With the setting IncludeConstantBasis ->False , a model of the form is fitted. The option IncludeConstantBasis is ignored if the design matrix is specified in the input.
  • With the setting LinearOffsetFunction ->h, a model of the form is fitted.
  • With ConfidenceLevel ->p, probability-p confidence intervals are computed for parameter and prediction intervals.
  • With the setting Weights ->{w1,w2,}, the error variance for yi is assumed proportional to .
  • With the setting Weights ->Automatic , the weights will be set to 1 if the data contains exact values. If the data contains Around values, the weights will be set to with the total response variance.
  • The total response variance is a function of the initial response variance si2 and the independent values variance .
  • The are propagated through the model using AroundReplace and the resulting variance is added to response variance si2. The function FindRoot is used internally to find a self-consistent solution according to the Fasano and Vio method.
  • With the setting VarianceEstimatorFunction ->f, the variance is estimated by f[res,w], where res={y1-,y2-,} is the list of residuals and w={w1,w2,} is the list of weights for the measurements yi.
  • Using VarianceEstimatorFunction ->(1&) and Weights ->{1/Δy12,1/Δy22,}, Δyi is treated as the known uncertainty of measurement yi, and parameter standard errors are effectively computed only from the weights.
  • Properties
  • The properties and diagnostics of the FittedModel can be obtained from model["property"].
  • Properties related to data and the fitted function obtained using model["property"] include:
  • "BasisFunctions" list of basis functions
    "BestFit" fitted function
    "BestFitAround" fitted function and mean uncertainty
    "BestFitDataAround" fitted function and data uncertainty
    "BestFitParameters" parameter estimates
    "Data" the input data or design matrix and response vector
    "DesignMatrix" design matrix for the model
    "Function" best fit pure function
    "Response" response values in the input data
    "Weights" weights used to fit the data
  • Types of residuals include:
  • "FitResiduals" difference between actual and predicted responses
    "StandardizedResiduals" fit residuals divided by the standard error for each residual
    "StudentizedResiduals" fit residuals divided by single deletion error estimates
  • Properties related to the sum of squared errors include:
  • "ANOVA" analysis of variance data
    "CoefficientOfVariation" estimated standard deviation divided by the response mean
    "EstimatedVariance" estimate of the error variance
    "PartialSumOfSquares" changes in model sum of squares as nonconstant basis functions are removed
    "SequentialSumOfSquares" the model sum of squares partitioned componentwise
  • Properties and diagnostics for parameter estimates include:
  • "CorrelationMatrix" parameter correlation matrix
    "CovarianceMatrix" parameter covariance matrix
    "Eigenstructure" eigenstructure of the parameter correlation matrix
    "ParameterEstimates" table of fitted parameter information
    "VarianceInflationFactors" list of inflation factors for the estimated parameters
  • Properties related to influence measures include:
  • "BetaDifferences" DFBETAS measures of influence on parameter values
    "CatcherMatrix" catcher matrix
    "CookDistances" list of Cook distances
    "CovarianceRatios" COVRATIO measures of observation influence
    "DurbinWatsonD" DurbinWatson statistic for autocorrelation
    "FitDifferences" DFFITS measures of influence on predicted values
    "FVarianceRatios" FVARATIO measures of observation influence
    "HatDiagonal" diagonal elements of the hat matrix
    "SingleDeletionVariances" list of variance estimates with the ^(th) data point omitted
  • Properties of predicted values include:
  • "MeanPredictionBands" confidence bands for mean predictions
    "MeanPredictions" confidence intervals for the mean predictions
    "PredictedResponse" fitted values for the data
    "SinglePredictionBands" confidence bands based on single observations
    "SinglePredictions" confidence intervals for the predicted response of single observations
  • Properties that measure goodness of fit include:
  • "AdjustedRSquared" adjusted for the number of model parameters
    "AIC" Akaike Information Criterion
    "AICc" finite sample corrected AIC
    "BIC" Bayesian Information Criterion
    "RSquared" coefficient of determination
  • The properties "BestFit", "BestFitAround", "BestFitDataAround", "SinglePredictionBands" and "MeanPredictionBands" can also be called as {"prop",x} or {"prop",{x1,x2,}} to evaluate these properties at specific independent values.
  • For the properties "RSquared" and "AdjustedRSquared", the computation of the total sum of squares is mean adjusted only when the constant basis is included.

Examples

open all close all

Basic Examples  (2)

Fit a linear model to some data:

Wolfram Language code: lm = LinearModelFit[{...}, x, x]

Evaluate the model at a point:

Wolfram Language code: lm[2.3]

Visualize the fitted function with the data:

Wolfram Language code: Show[ListPlot[lm["Data"]], Plot[lm[x], {x, 0, 5}], Frame -> True]

Fit a model of 2 variables with an interaction term:

Wolfram Language code: lm = LinearModelFit[{...}, {x, y, x y}, {x, y}]

Obtain the functional form as an expression:

Wolfram Language code: Normal[lm]

Extract information about the fitting:

Wolfram Language code: lm["FitResiduals"]

Scope  (18)

Data  (8)

Fit a model of one variable assuming increasing integer independent values:

Wolfram Language code: LinearModelFit[{-0.8, -0.2, 0.8, 2.2}, x, x]

This is equivalent to:

Wolfram Language code: LinearModelFit[{{1, -0.8}, {2, -0.2}, {3, 0.8}, {4, 2.2}}, x, x]

Fit a model of more than one variable, assuming the response is the last one:

Wolfram Language code: LinearModelFit[{{1, 1, Sin[2]}, {1, 2, Sin[3]}, {1, 3, Sin[4]}, {2, 1, Sin[3]}, {2, 2, Sin[4]}, {2, 3, Sin[5]}}, {x, y}, {x, y}]

This is equivalent to:

Wolfram Language code: LinearModelFit[{{1, 1, Sin[2]}, {1, 2, Sin[3]}, {1, 3, Sin[4]}, {2, 1, Sin[3]}, {2, 2, Sin[4]}, {2, 3, Sin[5]}} -> -1, {x, y}, {x, y}]

Specify a column as the response:

Wolfram Language code: LinearModelFit[{{1, 1, Sin[2]}, {1, 2, Sin[3]}, {1, 3, Sin[4]}, {2, 1, Sin[3]}, {2, 2, Sin[4]}, {2, 3, Sin[5]}} -> 2, {x, y}, {x, y}]

Fit a list of rules:

Wolfram Language code: LinearModelFit[{{1, 1} -> 2, {2, 2} -> 3, {3, 1} -> 6, {3, 2} -> 5}, {x, y}, {x, y}]

Fit a rule of input values and responses:

Wolfram Language code: LinearModelFit[{1, 2, 3, 4, 5} -> {4, 4, 2, -2, -8}, {1, x ^ 2}, {x}]

Fit a model with categorical predictor variables:

Wolfram Language code: nom = LinearModelFit[{{a, 1}, {b, 2}, {a, 1.8}, {b, 2.5}}, x, x, NominalVariables -> x]

Fit a model given a design matrix and response vector:

Wolfram Language code: LinearModelFit[{{{1, 1}, {1, 2}, {1, 3}, {1, 4}}, {1, 3, 6, 10}}]

Fit the model referring to the basis functions as x and y:

Wolfram Language code: LinearModelFit[{{{1, 1}, {1, 2}, {1, 3}, {1, 4}}, {1, 3, 6, 10}}, {x, y}]

Fit a Tabular object by specifying the response column:

Wolfram Language code: LinearModelFit[Tabular[Association["RawSchema" -> Association["ColumnProperties" -> Association["a" -> Association["ElementType" -> "Real64"], "b" -> Association["ElementType" -> "Real64"], "c" -> Association["ElementType" -> "Real64"]], "KeyColumns" -> None, "Backend" -> "WolframKernel"], "BackendData" -> Association["ColumnData" -> DataStructure["ColumnTable", {{TabularColumn[Association["Data" -> {{1., 1., 1., 2., 2., 2.}, {}, None}, "ElementType" -> "Real64"]], TabularColumn[Association[ "Data" -> {{1., 2., 3., 1., 2., 3.}, {}, None}, "ElementType" -> "Real64"]], TabularColumn[Association["Data" -> {{0.9092974268256817, 0.1411200080598672, -0.7568024953079283, 0.1411200080598672, -0.7568024953079283, -0.9589242746631385}, {}, None}, "ElementType" -> "Real64"]]}}]]]] -> "c", {x, Cos[y]}, {x, y}]

Model  (3)

Find the best fit linear coefficient for a function:

Wolfram Language code: data = Table[{x, .3Tanh[x]}, {x, 10}];
Wolfram Language code: LinearModelFit[data, Tanh[x], x, IncludeConstantBasis -> False]

Fit data to a linear combination of linear functions of independent variables:

Wolfram Language code: data = Flatten[Table[{x, y, 3x - .2y + .5}, {x, 5}, {y, 5}], 1];
Wolfram Language code: LinearModelFit[data, {x, y}, {x, y}]

This is equivalent to explicitly specifying a constant function:

Wolfram Language code: LinearModelFit[data, {1, x, y}, {x, y}]

Fit data to a linear combination of nonlinear functions of independent variables:

Wolfram Language code: data = Flatten[Table[{x, y, Sin[x + y]}, {x, 5}, {y, 5}], 1];
Wolfram Language code: LinearModelFit[data, {1, Sin[x], Cos[y]}, {x, y}]

Properties  (7)

Data & Fitted Functions  (1)

Fit a linear model:

Wolfram Language code: data = Block[{i, j}, Table[{i = RandomReal[10], j = RandomReal[10], 4i + 2j + RandomReal[]}, {10}]]; lm = LinearModelFit[data, {x, y}, {x, y}]

Obtain a list of available properties for a linear model:

Wolfram Language code: lm["Properties"]

Extract the original data:

Wolfram Language code: lm["Data"]

Obtain and plot the best fit:

Wolfram Language code: fit = lm["BestFit"]
Wolfram Language code: Show[Plot3D[fit, {x, 0, 10}, {y, 0, 10}, PlotRange -> All], Graphics3D[{PointSize[0.025], Point[data]}]]

Obtain the best fit at a specific point:

Wolfram Language code: lm[{"BestFit", {2., 5.}}]

Obtain the fitted function as a pure function:

Wolfram Language code: lm["Function"]

Get the design matrix and response vector for the fitting:

Wolfram Language code: MatrixForm /@ lm[{"DesignMatrix", "Response"}]

Residuals  (1)

Examine residuals for a fit:

Wolfram Language code: lm = LinearModelFit[RandomReal[10, {100, 3}], {x, y}, {x, y}]
Wolfram Language code: {fr, sr1, sr2} = lm[{"FitResiduals", "StandardizedResiduals", "StudentizedResiduals"}];

Visualize the raw fit residuals:

Wolfram Language code: ListPlot[fr]

Visualize scaled residuals in stem plots:

Wolfram Language code: Map[ListPlot[#, Filling -> 0]&, {sr1, sr2}]//Row

Plot the absolute differences between the standardized and Studentized residuals:

Wolfram Language code: ListPlot[Abs[sr1 - sr2]]

Sums of Squares  (1)

Fit a linear model to some data:

Wolfram Language code: SeedRandom[1]; data = Flatten[Table[{x, y, .2 + .3x + .1y + RandomReal[{-1, 1}]}, {x, RandomReal[5, 10]}, {y, RandomReal[5, 10]}], 1];lm = LinearModelFit[data, {x, y}, {x, y}]

Extract the estimated error variance and coefficient of variation:

Wolfram Language code: lm[{"EstimatedVariance", "CoefficientOfVariation"}]

Obtain an analysis of variance table for the model:

Wolfram Language code: lm["ANOVA"]

Get the F-statistics from the table:

Wolfram Language code: lm["ANOVA"][All, "FStatistics"]//DeleteMissing//Normal

Parameter Estimation Diagnostics  (1)

Obtain a formatted table of parameter information:

Wolfram Language code: SeedRandom[1]; data = Table[{x, .2 + .3x + .1Sin[x] + RandomReal[{-.2, .2}]}, {x, RandomReal[5, 100]}];lm = LinearModelFit[data, {x, Sin[x], Cos[x]}, x]
Wolfram Language code: lm["ParameterEstimates"]

Obtain the -statistics of fitted parameters:

Wolfram Language code: lm["ParameterEstimates"][All, "TStatistic"]//Normal

Influence Measures  (1)

Fit some data containing extreme values to a linear model:

Wolfram Language code: SeedRandom[1]; data = Table[{i, 2 + 4i - Log[i] + RandomReal[]}, {i, RandomReal[{1, 10}, 20]}]; data[[{3, 8}, -1]] = data[[{3, 8}, -1]] + 5; lm = LinearModelFit[data, {x, Log[x]}, x]

Use single deletion variances to check the impact on the error variance of removing each point:

Wolfram Language code: ListPlot[lm["SingleDeletionVariances"], PlotRange -> {0, All}, Filling -> 0]

Check Cook distances to identify highly influential points:

Wolfram Language code: ListPlot[lm["CookDistances"], PlotRange -> {0, All}, Filling -> 0]

Use DFFITS values to assess the influence of each point on the fitted values:

Wolfram Language code: ListPlot[lm["FitDifferences"], PlotRange -> All, Filling -> 0]

Use DFBETAS values to assess the influence of each point on each estimated parameter:

Wolfram Language code: MapThread[ListPlot[#1, PlotRange -> {-.5, 1}, Filling -> 0, PlotLabel -> #2]&, {Transpose[lm["BetaDifferences"]], {"SubscriptBox[β, 1]", "SubscriptBox[β, 2]", "SubscriptBox[β, 3]"}}]//Row

Prediction Values  (1)

Fit a linear model:

Wolfram Language code: SeedRandom[1]; data = Flatten[Table[{x, y, 3Exp[-x] + 1.2y + RandomReal[{-1, 1}]}, {x, RandomReal[5, 3]}, {y, RandomReal[5, 3]}], 1];lm = LinearModelFit[data, {Exp[-x], y}, {x, y}]

Plot the predicted values against the observed values:

Wolfram Language code: ListPlot[Transpose[lm[{"Response", "PredictedResponse"}]], FrameLabel -> {"observed", "predicted"}, Frame -> True, Axes -> False]

Obtain tabular results for the mean prediction confidence intervals:

Wolfram Language code: lm["MeanPredictions"]

Obtain tabular results for the single prediction confidence intervals:

Wolfram Language code: lm["SinglePredictions"]

Get the single prediction intervals from the table:

Wolfram Language code: lm["SinglePredictions"][All, "ConfidenceInterval"]//Normal

Extract 99% mean prediction bands:

Wolfram Language code: lm["MeanPredictionBands", ConfidenceLevel -> .99]

Compute the 99% mean prediction bands at a specific location:

Wolfram Language code: lm[{"MeanPredictionBands", {4., 1.}}, ConfidenceLevel -> .99]

Goodness-of-Fit Measures  (1)

Obtain a table of goodness-of-fit measures for a linear model:

Wolfram Language code: data = Flatten[Table[{i, j, RandomReal[10], 30i - 10j + RandomReal[5]}, {i, RandomReal[10, 5]}, {j, RandomReal[10, 5]}], 1];
Wolfram Language code: lm = LinearModelFit[data, {x, y, z}, {x, y, z}]
Wolfram Language code: Grid[Transpose[{#, lm[#]}&[{"AdjustedRSquared", "AIC", "BIC", "RSquared"}]], Alignment -> Left]

Compute goodness-of-fit measures for all possible linear submodels:

Wolfram Language code: sub = Table[Join[{i}, LinearModelFit[data, i, {x, y, z}][{"AdjustedRSquared", "RSquared", "AIC", "BIC"}]], {i, Subsets[{x, y, z}]}]

Rank the models by :

Wolfram Language code: Grid[Join[{{"Model", "AdjustedRSquared", "RSquared", "AIC", "BIC"}}, SortBy[sub, -#[[3]]&]]]

Rank the models by adjusted , which penalizes for adding terms:

Wolfram Language code: Grid[Join[{{"Model", "AdjustedRSquared", "RSquared", "AIC", "BIC"}}, SortBy[sub, -#[[2]]&]]]

Generalizations & Extensions  (1)

Perform other mathematical operations on the functional form of the model:

Wolfram Language code: lm = LinearModelFit[RandomReal[10, 20], {x, Log[x]}, x]

Integrate symbolically and numerically:

Wolfram Language code: Integrate[lm[x], x]
Wolfram Language code: NIntegrate[lm[x], {x, 1, 5}]

Find a predictor value that gives a particular value for the model:

Wolfram Language code: FindRoot[lm[x] == 5.5, {x, 10}]

Options  (11)

ConfidenceLevel  (1)

The default gives 95% confidence intervals:

Wolfram Language code: data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}};
Wolfram Language code: lm = LinearModelFit[data, x, x]
Wolfram Language code: lm["ParameterEstimates"][All, "ConfidenceInterval"]//Normal

Use 99% intervals instead:

Wolfram Language code: lm = LinearModelFit[data, x, x, ConfidenceLevel -> .99]
Wolfram Language code: lm["ParameterEstimates"][All, "ConfidenceInterval"]//Normal

Set the level to 90% within FittedModel :

Wolfram Language code: lm["ParameterEstimates", ConfidenceLevel -> .9][All, "ConfidenceInterval"]//Normal

IncludeConstantBasis  (1)

Fit a simple linear regression model:

Wolfram Language code: data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}};
Wolfram Language code: LinearModelFit[data, x, x]//Normal

Fit the linear model with intercept zero:

Wolfram Language code: LinearModelFit[data, x, x, IncludeConstantBasis -> False]//Normal

LinearOffsetFunction  (1)

Fit data to a linear model:

Wolfram Language code: data = {{0, 1}, {1, 1.5}, {3, 2}, {5, 4}};
Wolfram Language code: LinearModelFit[data, x, x]//Normal

Fit data to a linear model with a known Sqrt [x] term:

Wolfram Language code: LinearModelFit[data, x, x, LinearOffsetFunction -> (Sqrt[#]&)]//Normal

NominalVariables  (1)

Fit data treating the first variable as a nominal variable:

Wolfram Language code: data = {{a, 0, 1}, {b, 2, 2}, {a, 2, 1.8}, {b, 0, 2.5}};nom = LinearModelFit[data, {x, y}, {x, y}, NominalVariables -> x]
Wolfram Language code: Normal[nom]

Treat both variables as nominal:

Wolfram Language code: LinearModelFit[data, {x, y}, {x, y}, NominalVariables -> All]//Normal

VarianceEstimatorFunction  (1)

Use the default unbiased estimate of error variance:

Wolfram Language code: lm = LinearModelFit[Range[10] ^ 2, {x}, {x}]
Wolfram Language code: lm["EstimatedVariance"]

Assume a known error variance:

Wolfram Language code: lm["EstimatedVariance", VarianceEstimatorFunction -> (20&)]

Estimate the variance by the mean squared error:

Wolfram Language code: lm["EstimatedVariance", VarianceEstimatorFunction -> (Mean[# ^ 2]&)]

Weights  (5)

Fit a model using equal weights:

Wolfram Language code: LinearModelFit[Range[10] ^ 2, x, x]//Normal

Give explicit weights for the data points:

Wolfram Language code: LinearModelFit[Range[10] ^ 2, x, x, Weights -> 1 / Range[10]]//Normal

Use Around values to give different weights to data points:

Wolfram Language code: data = {Around[1, 0.2], Around[2, 0.1], Around[4, 2]}; fit = LinearModelFit[data, x, x, Weights -> Automatic]
Wolfram Language code: Show[Plot[fit[x], {x, 0, 3}], ListPlot[data], PlotRange -> All]

Find the weights that were used to account for the uncertainty in the data:

Wolfram Language code: fit["Weights"]

Use Around values in both the independent values and responses:

Wolfram Language code: SeedRandom[1]; data = Table[ {Around[x, RandomReal[0.25]], Around[2x ^ 2 - x + 0.3 + RandomReal[{-0.1, 0.1}], RandomReal[0.25]]}, {x, -1, 1, 0.2} ]; fit = LinearModelFit[data, {1, x, x ^ 2}, x, Weights -> Automatic]//Normal
Wolfram Language code: Show[Plot[fit, {x, -1, 1}], ListPlot[data], PlotRange -> All, AxesOrigin -> 0]

Fit a model of more than one variable with Around values:

Wolfram Language code: SeedRandom[1]; data = Join@@Table[ {Around[x, RandomReal[0.25]], Around[y, RandomReal[0.25]], Around[2x ^ 2 - x + x y + y ^ 2 + RandomReal[{-0.1, 0.1}], RandomReal[0.25]]}, {x, -1, 1, 0.2}, {y, -1, 1, 0.2} ]; fit = LinearModelFit[data, {1, x, x ^ 2, y, y ^ 2, x y}, {x, y}, Weights -> Automatic]//Normal

Try the FixedPoint algorithm to find the weights for the model:

Wolfram Language code: data = {...}; LinearModelFit[data, {1, x, x ^ 2, y, y ^ 2, x y}, {x, y}, Weights -> {Automatic, Method -> FixedPoint}]

Reduce the damping factor and increase the MaxIterations to reach convergence:

Wolfram Language code: LinearModelFit[data, {1, x, x ^ 2, y, y ^ 2, x y}, {x, y}, Weights -> {Automatic, Method -> {FixedPoint, DampingFactor -> 0.25, MaxIterations -> 200, Tolerance -> 10 ^ (-10) } } ]

WorkingPrecision  (1)

Use WorkingPrecision to get higher precision in parameter estimates:

Wolfram Language code: data = Table[{x, x ^ 3}, {x, 10}]
Wolfram Language code: lm = LinearModelFit[data, {x, x ^ 2}, x, WorkingPrecision -> 50]

Obtain the fitted function:

Wolfram Language code: lm["BestFit"]

Reduce the precision in property computations after the fitting:

Wolfram Language code: lm["BestFit", WorkingPrecision -> 20]

Applications  (6)

Fit the first 100 primes to a linear model:

Wolfram Language code: lm = LinearModelFit[Array[Prime, 100], x, x]

Visualize the fit:

Wolfram Language code: Show[ListPlot[lm["Data"], PlotStyle -> Orange], Plot[lm[x], {x, 0, 100}]]

The systematic trend in the residuals violates the assumption of independent normal errors:

Wolfram Language code: ListPlot[lm["FitResiduals"]]

Fit a linear model of multiple variables:

Wolfram Language code: data = Map[{#[[1]], #[[2]], #[[3]], 1.2 + (3.7 + RandomReal[{-1, 1}])#[[1]] - 2#[[2]] + 23.4#[[3]]}&, RandomReal[10, {100, 3}]];
Wolfram Language code: lm = LinearModelFit[data, {x, y, z}, {x, y, z}]

Visually inspect the residuals by data point:

Wolfram Language code: ListPlot[lm["FitResiduals"], Frame -> True, PlotRange -> All]

Plot the residuals against each predictor variable:

Wolfram Language code: Table[ListPlot[Transpose[{data[[All, i]], lm["FitResiduals"]}], Frame -> True, FrameLabel -> {{x, y, z}[[i]], "Residual"}], {i, 3}]//Row

Plot Cook's distances to diagnose leverage:

Wolfram Language code: cd = lm["CookDistances"];
Wolfram Language code: ListPlot[cd, Filling -> 0, Frame -> True, AxesOrigin -> {0, 0}, PlotRange -> All, PlotLabel -> "Cook's Distances"]

Find the positions of distances above a given cutoff value:

Wolfram Language code: Position[cd, _ ? (# > .05&)]

Extract the associated data points:

Wolfram Language code: Extract[data, %]

Use - plots to check the assumption of normal errors:

Wolfram Language code: lm = LinearModelFit[RandomReal[10, {50, 4}], {x, y, z}, {x, y, z}]

Compare standardized residuals to standard normal values:

Wolfram Language code: QuantilePlot[lm["StandardizedResiduals"], Table[InverseCDF[NormalDistribution[], q], {q, 1 / 100, 99 / 100, 1 / 50}]]

Do the comparison with Studentized residuals:

Wolfram Language code: QuantilePlot[lm["StudentizedResiduals"], Table[InverseCDF[NormalDistribution[], q], {q, 1 / 100, 99 / 100, 1 / 50}]]

Simulate some data with a continuous and a nominal variable:

Wolfram Language code: groups = {"control", "treatment 1", "treatment 2", "treatment 3"}; data = BlockRandom[SeedRandom[123]; Block[{vals, times, rand}, vals = RandomChoice[groups, 100]; times = RandomInteger[10, 100]; rand = RandomReal[1, 100]; Transpose[{vals, times, (vals /. Thread[Rule[groups, {.16, .34, .57, 1.1}]]) - .05 times + rand}]]];

Fit an analysis of covariance model to the data:

Wolfram Language code: lm = LinearModelFit[data, {treatment, time}, {treatment, time}, NominalVariables -> treatment];

Obtain an analysis of variance table for the model:

Wolfram Language code: lm["ANOVA"]

Group the data by treatment:

Wolfram Language code: grps = Drop[GatherBy[Sort[data], First], None, None, 1];

Visualize the grouped data and associated curves:

Wolfram Language code: Show[ListPlot[grps, PlotRange -> All], Plot[Evaluate[Map[lm[#, t]&, groups]], {t, 0, 10}, PlotLegends -> LineLegend[groups, LegendLayout -> "ReversedColumn"]]]

Use properties to compute additional results:

Wolfram Language code: SeedRandom[1]; data = Flatten[Table[{x, y, z, 1.2x - 3.4y + 10z + RandomReal[10]}, {x, RandomReal[10, 3]}, {y, RandomReal[10, 3]}, {z, RandomReal[10, 3]}], 2];
Wolfram Language code: lm = LinearModelFit[data, {x, y, z}, {x, y, z}]

Extract the design matrix and residuals:

Wolfram Language code: {desmat, resids} = lm[{"DesignMatrix", "FitResiduals"}];

Compute White's heteroskedasticity-consistent covariance estimate:

Wolfram Language code: (wc = With[{inv = Inverse[Transpose[desmat].desmat], xresid = resids * desmat}, inv.Transpose[xresid].xresid.inv])//MatrixForm

Compare with the covariance assuming homoskedasticity:

Wolfram Language code: lm["CovarianceMatrix"]//MatrixForm

Compare standard errors based on the two covariance estimates:

Wolfram Language code: Sqrt[Diagonal[wc]]
Wolfram Language code: lm["ParameterEstimates"][All, "StandardError"]//Normal

Perform a BreuschPagan test:

Wolfram Language code: data = Flatten[Table[{x, y, z, 1.2x - 3.4y + 10z + RandomReal[10]}, {x, RandomReal[10, 3]}, {y, RandomReal[10, 3]}, {z, RandomReal[10, 3]}], 2];

Fit a model:

Wolfram Language code: lm1 = LinearModelFit[data, {x, y, z}, {x, y, z}]

Fit the squared errors to a model with the same predictors:

Wolfram Language code: lm2 = LinearModelFit[Block[{newdata = data}, newdata[[All, -1]] = lm1["FitResiduals"] ^ 2;newdata], {x, y, z}, {x, y, z}]

Compute the BreuschPagan test statistic:

Wolfram Language code: bp = With[{sqResids = lm1["FitResiduals"] ^ 2}, (Variance[sqResids](Length[data] - 1) - Total[lm2["FitResiduals"] ^ 2]) / 2 / (Total[sqResids] / Length[data]) ^ 2]

Compute the -value:

Wolfram Language code: 1 - CDF[ChiSquareDistribution[Length[lm1["BestFitParameters"]] - 1], bp]

Properties & Relations  (10)

DesignMatrix constructs the design matrix used by LinearModelFit :

Wolfram Language code: data = Table[{i, RandomReal[]}, {i, 5}]
Wolfram Language code: DesignMatrix[data, x, x]//MatrixForm
Wolfram Language code: lm = LinearModelFit[data, x, x];
Wolfram Language code: lm["DesignMatrix"]//MatrixForm

By default, LinearModelFit and GeneralizedLinearModelFit fit equivalent models:

Wolfram Language code: data = Table[{i, RandomReal[{i - 1, i}]}, {i, 10}];
Wolfram Language code: LinearModelFit[data, x, x]
Wolfram Language code: GeneralizedLinearModelFit[data, x, x]

LinearModelFit fits linear models assuming normally distributed errors:

Wolfram Language code: data = Table[{i, RandomReal[{i - 1, i}]}, {i, 10}];
Wolfram Language code: LinearModelFit[data, x ^ 2, x]

NonlinearModelFit fits nonlinear models assuming normally distributed errors:

Wolfram Language code: NonlinearModelFit[data, a Exp[b x], {a, b}, x]

Fit and LinearModelFit fit equivalent models:

Wolfram Language code: data = Table[{i, RandomReal[{i - 1, i}]}, {i, 10}];
Wolfram Language code: Fit[data, {1, x ^ 2}, x]
Wolfram Language code: lm = LinearModelFit[data, x ^ 2, x]

LinearModelFit allows for extraction of additional information about the fitting:

Wolfram Language code: lm["FitResiduals"]

Fit a linear model to data:

Wolfram Language code: data = Flatten[Table[{i, j, i + j + Exp[i - j] + RandomReal[2]}, {i, 5}, {j, 5}], 1];
Wolfram Language code: LinearModelFit[data, {x, y, Exp[x - y]}, {x, y}]["BestFitParameters"]

Perform the same fitting using a design matrix and response vector:

Wolfram Language code: dm = DesignMatrix[data, {x, y, Exp[x - y]}, {x, y}]; resp = data[[All, -1]];
Wolfram Language code: LinearModelFit[{dm, resp}]["BestFitParameters"]

Obtain the parameter estimates via LeastSquares :

Wolfram Language code: LeastSquares[dm, resp]

LinearModelFit fits linear models:

Wolfram Language code: data = Table[{i, i}, {i, 10}];
Wolfram Language code: LinearModelFit[data, {x ^ 2, Sin[x]}, x]["BestFitParameters"]

FindFit gives parameter estimates for linear and nonlinear models:

Wolfram Language code: FindFit[data, a + b x ^ 2 + c Sin[x], {a, b, c}, x]

LinearModelFit will use the time stamps of a TimeSeries as variables:

Wolfram Language code: ts1 = TemporalData[TimeSeries, {{{1.102448341846048, 6.331833897009389, 7.5843632999043455, 44.59969789743589, 98.76273258351092, 150.00729051003992, 644.7367359379259, 1724.3968817536402, 5903.697723461762, 19468.09340765025}}, {{0, 9, 1}}, 1, {"Continuous", 1}, {"Discrete", 1}, 1, {ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}}}, False, 10.1];
Wolfram Language code: ts1["Times"]
Wolfram Language code: base = {1, x, Exp[x]};
Wolfram Language code: LinearModelFit[ts1, base, x]

Rescale the time stamps and fit again:

Wolfram Language code: ts2 = TimeSeriesRescale[ts1, {.1, 2}]
Wolfram Language code: ts2["Times"]
Wolfram Language code: LinearModelFit[ts2, base, x]

Find fit for the values:

Wolfram Language code: LinearModelFit[ts1["Values"], base, x]

LinearModelFit acts pathwise on a multipath TemporalData :

Wolfram Language code: LinearModelFit[TemporalData[Automatic, {{{1.102448341846048, 6.331833897009389, 7.5843632999043455, 44.59969789743589, 98.76273258351092, 150.00729051003992, 644.7367359379259, 1724.3968817536402, 5903.697723461762, 19468.09340765025}, {1.1024483418 ... 7359379259, 1724.3968817536402, 5903.697723461762, 19468.09340765025}}, {{0, 9, 1}, {0.1, 2., 0.2111111111111111}}, 2, {"Continuous", 2}, {"Discrete", 2}, 1, {ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}}}, False, 10.1], {1, x, Exp[x]}, x]

Do a simple linear model fit:

Wolfram Language code: LinearModelFit[{{0, 1}, {1, 0}, {3, 2}, {5, 4}}, x, x]
Wolfram Language code: %[2.3]

Do the same fit using a neural net with a single linear layer:

Wolfram Language code: NetTrain[LinearLayer[], Rule@@@{{0, 1}, {1, 0}, {3, 2}, {5, 4}}]
Wolfram Language code: %[2.3]

Compute the AIC from first principles:

Wolfram Language code: data = {{0, 1}, {1, 0}, {2, 1}, {3, 2}, {5, 4}}; n = Length[data]; basis = {1, x}; k = Length[basis] + 1; (* +1 for the estimation of the variance *) lm = LinearModelFit[data, basis, x]; lm["AIC"]
Wolfram Language code: errorSumOfSquares = Total[lm["FitResiduals"] ^ 2]; varianceMLE = errorSumOfSquares / n; logLike = (-1 / 2) * (n * Log[2Pi * varianceMLE] + errorSumOfSquares / varianceMLE); aic = 2 * k - 2 * logLike

Check the "AICc" property:

Wolfram Language code: lm["AICc"] == aic + (2k * (k + 1)) / (n - k - 1)

Check the "BIC" property:

Wolfram Language code: lm["BIC"] == Log[n] * k - 2 * logLike

Compute the from first principles:

Wolfram Language code: responses = data[[All, 2]]; lm["RSquared"]
Wolfram Language code: 1 - Total[lm["FitResiduals"] ^ 2] / Total[(responses - Mean[responses]) ^ 2]

If the model does not include a constant basis, the denominator is not mean adjusted:

Wolfram Language code: lm2 = LinearModelFit[data, {x}, x, IncludeConstantBasis -> False]; lm2["RSquared"]
Wolfram Language code: 1 - Total[lm2["FitResiduals"] ^ 2] / Total[responses ^ 2]
Wolfram Research (2008), LinearModelFit, Wolfram Language function, https://reference.wolfram.com/language/ref/LinearModelFit.html (updated 2025).

Text

Wolfram Research (2008), LinearModelFit, Wolfram Language function, https://reference.wolfram.com/language/ref/LinearModelFit.html (updated 2025).

CMS

Wolfram Language. 2008. "LinearModelFit." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/LinearModelFit.html.

APA

Wolfram Language. (2008). LinearModelFit. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LinearModelFit.html

BibTeX

@misc{reference.wolfram_2026_linearmodelfit, author="Wolfram Research", title="{LinearModelFit}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/LinearModelFit.html}", note=[Accessed: 13-August-2026]}

BibLaTeX

@online{reference.wolfram_2026_linearmodelfit, organization={Wolfram Research}, title={LinearModelFit}, year={2025}, url={https://reference.wolfram.com/language/ref/LinearModelFit.html}, note=[Accessed: 13-August-2026]}

Top [フレーム]

AltStyle によって変換されたページ (->オリジナル) /