This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Class: LinearModel

Add terms to linear regression model


mdl1 = addTerms(mdl,terms)


mdl1 = addTerms(mdl,terms) returns a linear model mdl1 that is the same as the input model mdl, but with additional terms as specified by terms.

Input Arguments

expand all

Full, fitted linear regression model, specified as a LinearModel object constructed using fitlm or stepwiselm.

Terms to add to the regression model mdl, specified as one of the following:

  • Formula representing one or more terms to add. For details, see Wilkinson Notation.

  • Row or rows in the terms matrix (see the modelspec argument description in the fitting function fitlm). For example, if there are three variables A, B, and C:

    [0 0 0] represents a constant term or intercept
    [0 1 0] represents B; equivalently, A^0 * B^1 * C^0
    [1 0 1] represents A*C
    [2 0 0] represents A^2
    [0 1 2] represents B*(C^2)

Output Arguments

expand all

Regression model with additional terms, returned as a LinearModel object. mdl1 is the same as mdl but includes the additional terms specified in terms. To overwrite mdl, set mdl1 equal to mdl.


expand all

Create a model of the carsmall data without any interactions, then add an interaction term.

Load the carsmall data and make a model of the MPG as a function of weight and model year.

load carsmall
tbl = table(MPG,Weight);
tbl.Year = categorical(Model_Year);
mdl = fitlm(tbl,'MPG ~ Year + Weight^2');

Add an interaction term to mdl.

terms = 'Year*Weight';
mdl1 = addTerms(mdl,terms)
mdl1 = 

Linear regression model:
    MPG ~ 1 + Weight*Year + Weight^2

Estimated Coefficients:
                       Estimate          SE         tStat        pValue  
                      ___________    __________    ________    __________

    (Intercept)            48.045         6.779      7.0874    3.3967e-10
    Weight              -0.012624     0.0041455     -3.0454     0.0030751
    Year_76                2.7768        3.0538     0.90931        0.3657
    Year_82                16.416        4.9802      3.2962     0.0014196
    Weight:Year_76    -0.00020693    0.00092403    -0.22394       0.82333
    Weight:Year_82     -0.0032574     0.0018919     -1.7217      0.088673
    Weight^2           1.0121e-06      6.12e-07      1.6538       0.10177

Number of observations: 94, Error degrees of freedom: 87
Root Mean Squared Error: 2.76
R-squared: 0.89,  Adjusted R-Squared 0.882
F-statistic vs. constant model: 117, p-value = 1.88e-39


expand all


Use stepwiselm to select a model from a starting model, continuing until no single step is beneficial.

Use removeTerms to remove particular terms.

Use step to optimally improve the model by adding or removing terms.


[1] Wilkinson, G. N., and C. E. Rogers. Symbolic description of factorial models for analysis of variance. J. Royal Statistics Society 22, pp. 392–399, 1973.

Was this topic helpful?