LinearModel.fit

(Not Recommended) Create linear regression model

LinearModel.fit is not recommended. Use fitlm instead.

Syntax

mdl = LinearModel.fit(tbl)
mdl = LinearModel.fit(X,y)
mdl = LinearModel.fit(___,modelspec)
mdl = LinearModel.fit(___,Name,Value)
mdl = LinearModel.fit(___,modelspec,Name,Value)

Description

mdl = LinearModel.fit(tbl) creates a linear model of a table or dataset array tbl.

mdl = LinearModel.fit(X,y) creates a linear model of the responses y to a data matrix X.

mdl = LinearModel.fit(___,modelspec) creates a linear model of the type specified by modelspec, using any of the previous syntaxes.

mdl = LinearModel.fit(___,Name,Value) or mdl = LinearModel.fit(___,modelspec,Name,Value) creates a linear model with additional options specified by one or more Name,Value pair arguments. For example, you can specify which predictor variables to include in the fit or include observation weights.

Input Arguments

expand all

Input data including predictor and response variables, specified as a table or dataset array. The predictor variables can be numeric, logical, categorical, character, or string. The response variable must be numeric or logical.

  • By default, LinearModel.fit takes the last variable as the response variable and the others as the predictor variables.

  • To set a different column as the response variable, use the ResponseVar name-value pair argument.

  • To use a subset of the columns as predictors, use the PredictorVars name-value pair argument.

  • To define a model specification, set the modelspec argument using a formula or terms matrix. The formula or terms matrix specifies which columns to use as the predictor or response variables.

The variable names in a table do not have to be valid MATLAB® identifiers. However, if the names are not valid, you cannot use a formula when you fit or adjust a model; for example:

  • You cannot specify modelspec using a formula.

  • You cannot use a formula to specify the terms to add or remove when you use the addTerms function or the removeTerms function, respectively.

  • You cannot use a formula to specify the lower and upper bounds of the model when you use the step or stepwiselm function with the name-value pair arguments 'Lower' and 'Upper', respectively.

You can verify the variable names in tbl by using the isvarname function. The following code returns logical 1 (true) for each variable that has a valid variable name.

cellfun(@isvarname,tbl.Properties.VariableNames)
If the variable names in tbl are not valid, then convert them by using the matlab.lang.makeValidName function.
tbl.Properties.VariableNames = matlab.lang.makeValidName(tbl.Properties.VariableNames);

Predictor variables, specified as an n-by-p matrix, where n is the number of observations and p is the number of predictor variables. Each column of X represents one variable, and each row represents one observation.

By default, there is a constant term in the model, unless you explicitly remove it, so do not include a column of 1s in X.

Data Types: single | double

Response variable, specified as an n-by-1 vector, where n is the number of observations. Each entry in y is the response for the corresponding row of X.

Data Types: single | double | logical

Model specification, specified as one of the following.

  • A character vector or string scalar naming the model.

    ValueModel Type
    'constant'Model contains only a constant (intercept) term.
    'linear'Model contains an intercept and linear term for each predictor.
    'interactions'Model contains an intercept, linear term for each predictor, and all products of pairs of distinct predictors (no squared terms).
    'purequadratic'Model contains an intercept term and linear and squared terms for each predictor.
    'quadratic'Model contains an intercept term, linear and squared terms for each predictor, and all products of pairs of distinct predictors.
    'polyijk'Model is a polynomial with all terms up to degree i in the first predictor, degree j in the second predictor, and so on. Specify the maximum degree for each predictor by using numerals 0 though 9. The model contains interaction terms, but the degree of each interaction term does not exceed the maximum value of the specified degrees. For example, 'poly13' has an intercept and x1, x2, x22, x23, x1*x2, and x1*x22 terms, where x1 and x2 are the first and second predictors, respectively.
  • t-by-(p + 1) matrix, namely terms matrix, specifying terms to include in the model, where t is the number of terms and p is the number of predictor variables, and plus 1 is for the response variable.

  • A character vector or string scalar representing a formula in the form

    'Y ~ terms',

    where the terms are specified using Wilkinson Notation.

Example: 'quadratic'

Example: 'y ~ X1 + X2^2 + X1:X2'

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Categorical variable list, specified as the comma-separated pair consisting of 'CategoricalVars' and either a string array or cell array of character vectors containing categorical variable names in the table or dataset array tbl, or a logical or numeric index vector indicating which columns are categorical.

  • If data is in a table or dataset array tbl, then, by default, LinearModel.fit treats all categorical values, logical values, character arrays, string arrays, and cell arrays of character vectors as categorical variables.

  • If data is in matrix X, then the default value of 'CategoricalVars' is an empty matrix []. That is, no variable is categorical unless you specify it as categorical.

For example, you can specify the observations 2 and 3 out of 6 as categorical using either of the following examples.

Example: 'CategoricalVars',[2,3]

Example: 'CategoricalVars',logical([0 1 1 0 0 0])

Data Types: single | double | logical | string | cell

Observations to exclude from the fit, specified as the comma-separated pair consisting of 'Exclude' and a logical or numeric index vector indicating which observations to exclude from the fit.

For example, you can exclude observations 2 and 3 out of 6 using either of the following examples.

Example: 'Exclude',[2,3]

Example: 'Exclude',logical([0 1 1 0 0 0])

Data Types: single | double | logical

Indicator for the constant term (intercept) in the fit, specified as the comma-separated pair consisting of 'Intercept' and either true to include or false to remove the constant term from the model.

Use 'Intercept' only when specifying the model using a character vector or string scalar, not a formula or matrix.

Example: 'Intercept',false

Predictor variables to use in the fit, specified as the comma-separated pair consisting of 'PredictorVars' and either a string array or cell array of character vectors of the variable names in the table or dataset array tbl, or a logical or numeric index vector indicating which columns are predictor variables.

The string values or character vectors should be among the names in tbl, or the names you specify using the 'VarNames' name-value pair argument.

The default is all variables in X, or all variables in tbl except for ResponseVar.

For example, you can specify the second and third variables as the predictor variables using either of the following examples.

Example: 'PredictorVars',[2,3]

Example: 'PredictorVars',logical([0 1 1 0 0 0])

Data Types: single | double | logical | string | cell

Response variable to use in the fit, specified as the comma-separated pair consisting of 'ResponseVar' and either a character vector or string scalar containing the variable name in the table or dataset array tbl, or a logical or numeric index vector indicating which column is the response variable. You typically need to use 'ResponseVar' when fitting a table or dataset array tbl.

For example, you can specify the fourth variable, say yield, as the response out of six variables, in one of the following ways.

Example: 'ResponseVar','yield'

Example: 'ResponseVar',[4]

Example: 'ResponseVar',logical([0 0 0 1 0 0])

Data Types: single | double | logical | char | string

Indicator of the robust fitting type to use, specified as the comma-separated pair consisting of 'RobustOpts' and one of these values.

  • 'off' — No robust fitting. LinearModel.fit uses ordinary least squares.

  • 'on' — Robust fitting using the 'bisquare' weight function with the default tuning constant.

  • Character vector or string scalar — Name of a robust fitting weight function from the following table. LinearModel.fit uses the corresponding default tuning constant specified in the table.

  • Structure with the two fields RobustWgtFun and Tune.

    • The RobustWgtFun field contains the name of a robust fitting weight function from the following table or a function handle of a custom weight function.

    • The Tune field contains a tuning constant. If you do not set the Tune field, LinearModel.fit uses the corresponding default tuning constant.

    Weight FunctionDescriptionDefault Tuning Constant
    'andrews'w = (abs(r)<pi) .* sin(r) ./ r1.339
    'bisquare'w = (abs(r)<1) .* (1 - r.^2).^2 (also called biweight)4.685
    'cauchy'w = 1 ./ (1 + r.^2)2.385
    'fair'w = 1 ./ (1 + abs(r))1.400
    'huber'w = 1 ./ max(1, abs(r))1.345
    'logistic'w = tanh(r) ./ r1.205
    'ols'Ordinary least squares (no weighting function)None
    'talwar'w = 1 * (abs(r)<1)2.795
    'welsch'w = exp(-(r.^2))2.985
    function handleCustom weight function that accepts a vector r of scaled residuals, and returns a vector of weights the same size as r1
    • The default tuning constants of built-in weight functions give coefficient estimates that are approximately 95% as statistically efficient as the ordinary least-squares estimates, provided the response has a normal distribution with no outliers. Decreasing the tuning constant increases the downweight assigned to large residuals; increasing the tuning constant decreases the downweight assigned to large residuals.

    • The value r in the weight functions is

      r = resid/(tune*s*sqrt(1–h)),

      where resid is the vector of residuals from the previous iteration, tune is the tuning constant, h is the vector of leverage values from a least-squares fit, and s is an estimate of the standard deviation of the error term given by

      s = MAD/0.6745.

      MAD is the median absolute deviation of the residuals from their median. The constant 0.6745 makes the estimate unbiased for the normal distribution. If X has p columns, the software excludes the smallest p absolute deviations when computing the median.

For robust fitting, LinearModel.fit uses M-estimation to formulate estimating equations and solves them using the method of iterative reweighted least squares (IRLS).

Example: 'RobustOpts','andrews'

Names of variables, specified as the comma-separated pair consisting of 'VarNames' and a string array or cell array of character vectors including the names for the columns of X first, and the name for the response variable y last.

'VarNames' is not applicable to variables in a table or dataset array, because those variables already have names.

The variable names do not have to be valid MATLAB identifiers. However, if the names are not valid, you cannot use a formula when you fit or adjust a model; for example:

  • You cannot use a formula to specify the terms to add or remove when you use the addTerms function or the removeTerms function, respectively.

  • You cannot use a formula to specify the lower and upper bounds of the model when you use the step or stepwiselm function with the name-value pair arguments 'Lower' and 'Upper', respectively.

Before specifying 'VarNames',varNames, you can verify the variable names in varNames by using the isvarname function. The following code returns logical 1 (true) for each variable that has a valid variable name.

cellfun(@isvarname,varNames)
If the variable names in varNames are not valid, then convert them by using the matlab.lang.makeValidName function.
varNames = matlab.lang.makeValidName(varNames);

Example: 'VarNames',{'Horsepower','Acceleration','Model_Year','MPG'}

Data Types: string | cell

Observation weights, specified as the comma-separated pair consisting of 'Weights' and an n-by-1 vector of nonnegative scalar values, where n is the number of observations.

Data Types: single | double

Output Arguments

expand all

Linear model representing a least-squares fit of the response to the data, returned as a LinearModel object.

If the value of the 'RobustOpts' name-value pair is not [] or 'ols', the model is not a least-squares fit, but uses the robust fitting function.

For properties and methods of the linear model object, see the LinearModel class page.

Examples

expand all

Fit a linear regression model using a matrix input data set.

Load the carsmall data set, a matrix input data set.

load carsmall
X = [Weight,Horsepower,Acceleration];

Fit a linear regression model by using fitlm.

mdl = fitlm(X,MPG)
mdl = 
Linear regression model:
    y ~ 1 + x1 + x2 + x3

Estimated Coefficients:
                    Estimate        SE          tStat        pValue  
                   __________    _________    _________    __________

    (Intercept)        47.977       3.8785        12.37    4.8957e-21
    x1             -0.0065416    0.0011274      -5.8023    9.8742e-08
    x2              -0.042943     0.024313      -1.7663       0.08078
    x3              -0.011583      0.19333    -0.059913       0.95236


Number of observations: 93, Error degrees of freedom: 89
Root Mean Squared Error: 4.09
R-squared: 0.752,  Adjusted R-Squared: 0.744
F-statistic vs. constant model: 90, p-value = 7.38e-27

The model display includes the model formula, estimated coefficients, and model summary statistics.

The model formula in the display, y ~ 1 + x1 + x2 + x3, corresponds to y=β0+β1X1+β2X2+β3X3+ϵ.

The model display also shows the estimated coefficient information, which is stored in the Coefficients property. Display the Coefficients property.

mdl.Coefficients
ans=4×4 table
                    Estimate        SE          tStat        pValue  
                   __________    _________    _________    __________

    (Intercept)        47.977       3.8785        12.37    4.8957e-21
    x1             -0.0065416    0.0011274      -5.8023    9.8742e-08
    x2              -0.042943     0.024313      -1.7663       0.08078
    x3              -0.011583      0.19333    -0.059913       0.95236

The Coefficient property includes these columns:

  • Estimate — Coefficient estimates for each corresponding term in the model. For example, the estimate for the constant term (intercept) is 47.977.

  • SE — Standard error of the coefficients.

  • tStatt-statistic for each coefficient to test the null hypothesis that the corresponding coefficient is zero against the alternative that it is different from zero, given the other predictors in the model. Note that tStat = Estimate/SE. For example, the t-statistic for the intercept is 47.977/3.8785 = 12.37.

  • pValuep-value for the t-statistic of the hypothesis test that the corresponding coefficient is equal to zero or not. For example, the p-value of the t-statistic for x2 is greater than 0.05, so this term is not significant at the 5% significance level given the other terms in the model.

The summary statistics of the model are:

  • Number of observations — Number of rows without any NaN values. For example, Number of observations is 93 because the MPG data vector has six NaN values and the Horsepower data vector has one NaN value for a different observation, where the number of rows in X and MPG is 100.

  • Error degrees of freedomn p, where n is the number of observations, and p is the number of coefficients in the model, including the intercept. For example, the model has four predictors, so the Error degrees of freedom is 93 – 4 = 89.

  • Root mean squared error — Square root of the mean squared error, which estimates the standard deviation of the error distribution.

  • R-squared and Adjusted R-squared — Coefficient of determination and adjusted coefficient of determination, respectively. For example, the R-squared value suggests that the model explains approximately 75% of the variability in the response variable MPG.

  • F-statistic vs. constant model — Test statistic for the F-test on the regression model, which tests whether the model fits significantly better than a degenerate model consisting of only a constant term.

  • p-valuep-value for the F-test on the model. For example, the model is significant with a p-value of 7.3816e-27.

You can find these statistics in the model properties (NumObservations, DFE, RMSE, and Rsquared) and by using the anova function.

anova(mdl,'summary')
ans=3×5 table
                SumSq     DF    MeanSq      F         pValue  
                ______    __    ______    ______    __________

    Total       6004.8    92    65.269                        
    Model         4516     3    1505.3    89.987    7.3816e-27
    Residual    1488.8    89    16.728                        

Fit a linear regression model that contains a categorical predictor. Reorder the categories of the categorical predictor to control the reference level in the model. Then, use anova to test the significance of the categorical variable.

Model with Categorical Predictor

Load the carsmall data set and create a linear regression model of MPG as a function of Model_Year. To treat the numeric vector Model_Year as a categorical variable, identify the predictor using the 'CategoricalVars' name-value pair argument.

load carsmall
mdl = fitlm(Model_Year,MPG,'CategoricalVars',1,'VarNames',{'Model_Year','MPG'})
mdl = 
Linear regression model:
    MPG ~ 1 + Model_Year

Estimated Coefficients:
                     Estimate      SE      tStat       pValue  
                     ________    ______    ______    __________

    (Intercept)        17.69     1.0328    17.127    3.2371e-30
    Model_Year_76     3.8839     1.4059    2.7625     0.0069402
    Model_Year_82      14.02     1.4369    9.7571    8.2164e-16


Number of observations: 94, Error degrees of freedom: 91
Root Mean Squared Error: 5.56
R-squared: 0.531,  Adjusted R-Squared: 0.521
F-statistic vs. constant model: 51.6, p-value = 1.07e-15

The model formula in the display, MPG ~ 1 + Model_Year, corresponds to

MPG=β0+β1ΙYear=76+β2ΙYear=82+ϵ,

where ΙYear=76 and ΙYear=82 are indicator variables whose value is one if the value of Model_Year is 76 and 82, respectively. The Model_Year variable includes three distinct values, which you can check by using the unique function.

unique(Model_Year)
ans = 3×1

    70
    76
    82

fitlm chooses the smallest value in Model_Year as a reference level ('70') and creates two indicator variables ΙYear=76 and ΙYear=82. The model includes only two indicator variables because the design matrix becomes rank deficient if the model includes three indicator variables (one for each level) and an intercept term.

Model with Full Indicator Variables

You can interpret the model formula of mdl as a model that has three indicator variables without an intercept term:

y=β0Ιx1=70+(β0+β1)Ιx1=76+(β0+β2)Ιx2=82+ϵ.

Alternatively, you can create a model that has three indicator variables without an intercept term by manually creating indicator variables and specifying the model formula.

temp_Year = dummyvar(categorical(Model_Year));
Model_Year_70 = temp_Year(:,1);
Model_Year_76 = temp_Year(:,2);
Model_Year_82 = temp_Year(:,3);
tbl = table(Model_Year_70,Model_Year_76,Model_Year_82,MPG);
mdl = fitlm(tbl,'MPG ~ Model_Year_70 + Model_Year_76 + Model_Year_82 - 1')
mdl = 
Linear regression model:
    MPG ~ Model_Year_70 + Model_Year_76 + Model_Year_82

Estimated Coefficients:
                     Estimate      SE       tStat       pValue  
                     ________    _______    ______    __________

    Model_Year_70      17.69      1.0328    17.127    3.2371e-30
    Model_Year_76     21.574     0.95387    22.617    4.0156e-39
    Model_Year_82      31.71     0.99896    31.743    5.2234e-51


Number of observations: 94, Error degrees of freedom: 91
Root Mean Squared Error: 5.56

Choose Reference Level in Model

You can choose a reference level by modifying the order of categories in a categorical variable. First, create a categorical variable Year.

Year = categorical(Model_Year);

Check the order of categories by using the categories function.

categories(Year)
ans = 3x1 cell array
    {'70'}
    {'76'}
    {'82'}

If you use Year as a predictor variable, then fitlm chooses the first category '70' as a reference level. Reorder Year by using the reordercats function.

Year_reordered = reordercats(Year,{'76','70','82'});
categories(Year_reordered)
ans = 3x1 cell array
    {'76'}
    {'70'}
    {'82'}

The first category of Year_reordered is '76'. Create a linear regression model of MPG as a function of Year_reordered.

mdl2 = fitlm(Year_reordered,MPG,'VarNames',{'Model_Year','MPG'})
mdl2 = 
Linear regression model:
    MPG ~ 1 + Model_Year

Estimated Coefficients:
                     Estimate      SE        tStat       pValue  
                     ________    _______    _______    __________

    (Intercept)       21.574     0.95387     22.617    4.0156e-39
    Model_Year_70    -3.8839      1.4059    -2.7625     0.0069402
    Model_Year_82     10.136      1.3812     7.3385    8.7634e-11


Number of observations: 94, Error degrees of freedom: 91
Root Mean Squared Error: 5.56
R-squared: 0.531,  Adjusted R-Squared: 0.521
F-statistic vs. constant model: 51.6, p-value = 1.07e-15

mdl2 uses '76' as a reference level and includes two indicator variables ΙYear=70 and ΙYear=82.

Evaluate Categorical Predictor

The model display of mdl2 includes a p-value of each term to test whether or not the corresponding coefficient is equal to zero. Each p-value examines each indicator variable. To examine the categorical variable Model_Year as a group of indicator variables, use anova. Use the 'components'(default) option to return a component ANOVA table that includes ANOVA statistics for each variable in the model except the constant term.

anova(mdl2,'components')
ans=2×5 table
                  SumSq     DF    MeanSq      F        pValue  
                  ______    __    ______    _____    __________

    Model_Year    3190.1     2    1595.1    51.56    1.0694e-15
    Error         2815.2    91    30.936                       

The component ANOVA table includes the p-value of the Model_Year variable, which is smaller than the p-values of the indicator variables.

Fit a linear regression model to sample data. Specify the response and predictor variables, and include only pairwise interaction terms in the model.

Load sample data.

load hospital

Fit a linear model with interaction terms to the data. Specify weight as the response variable, and sex, age, and smoking status as the predictor variables. Also, specify that sex and smoking status are categorical variables.

mdl = fitlm(hospital,'interactions','ResponseVar','Weight',...
    'PredictorVars',{'Sex','Age','Smoker'},...
    'CategoricalVar',{'Sex','Smoker'})
mdl = 
Linear regression model:
    Weight ~ 1 + Sex*Age + Sex*Smoker + Age*Smoker

Estimated Coefficients:
                         Estimate      SE        tStat        pValue  
                         ________    _______    ________    __________

    (Intercept)             118.7     7.0718      16.785     6.821e-30
    Sex_Male               68.336     9.7153      7.0339    3.3386e-10
    Age                   0.31068    0.18531      1.6765      0.096991
    Smoker_1               3.0425     10.446     0.29127       0.77149
    Sex_Male:Age         -0.49094    0.24764     -1.9825      0.050377
    Sex_Male:Smoker_1      0.9509     3.8031     0.25003       0.80312
    Age:Smoker_1         -0.07288    0.26275    -0.27737       0.78211


Number of observations: 100, Error degrees of freedom: 93
Root Mean Squared Error: 8.75
R-squared: 0.898,  Adjusted R-Squared: 0.892
F-statistic vs. constant model: 137, p-value = 6.91e-44

The weight of the patients do not seem to differ significantly according to age, or the status of smoking, or interaction of these factors with patient sex at the 5% significance level.

Load the hald data set, which measures the effect of cement composition on its hardening heat.

load hald

This data set includes the variables ingredients and heat. The matrix ingredients contains the percent composition of four chemicals present in the cement. The vector heat contains the values for the heat hardening after 180 days for each cement sample.

Fit a robust linear regression model to the data.

mdl = fitlm(ingredients,heat,'RobustOpts','on')
mdl = 
Linear regression model (robust fit):
    y ~ 1 + x1 + x2 + x3 + x4

Estimated Coefficients:
                   Estimate      SE        tStat       pValue 
                   ________    _______    ________    ________

    (Intercept)       60.09     75.818     0.79256      0.4509
    x1               1.5753    0.80585      1.9548    0.086346
    x2               0.5322    0.78315     0.67957     0.51596
    x3              0.13346     0.8166     0.16343     0.87424
    x4             -0.12052     0.7672    -0.15709     0.87906


Number of observations: 13, Error degrees of freedom: 8
Root Mean Squared Error: 2.65
R-squared: 0.979,  Adjusted R-Squared: 0.969
F-statistic vs. constant model: 94.6, p-value = 9.03e-07

For more details, see the topic Robust Regression — Reduce Outlier Effects, which compares the results of a robust fit to a standard least-squares fit.

More About

expand all

Tips

  • Use robust fitting (RobustOpts name-value pair) to reduce the effect of outliers automatically.

  • Do not use robust fitting when you want to subsequently adjust a model using step.

  • For other methods or properties of the LinearModel object, see LinearModel.

Algorithms

The main fitting algorithm is QR decomposition. For robust fitting, the algorithm is robustfit.

Alternatives

You can also construct a linear model using fitlm.

You can construct a model in a range of possible models using stepwiselm. However, you cannot use robust regression and stepwise regression together.