Documentation Center

  • Trial Software
  • Product Updates


Class: LinearModel

Add terms to linear regression model


mdl1 = addTerms(mdl,terms)


mdl1 = addTerms(mdl,terms) returns a linear model the same as mdl but with additional terms.

Input Arguments


Linear model, as constructed by fitlm or stepwiselm.


Terms to add to the mdl regression model. Specify as either a:

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

  • Row or rows in the terms matrix (see modelspec in 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


Linear model, the same as mdl but with additional terms given in terms. You can set mdl1 equal to mdl to overwrite mdl.


Wilkinson Notation

Wilkinson notation describes the factors present in models. The notation relates to factors present in models, not to the multipliers (coefficients) of those factors.

Wilkinson NotationFactors in Standard Notation
1Constant (intercept) term
A^k, where k is a positive integerA, A2, ..., Ak
A + BA, B
A*BA, B, A*B
A:BA*B only
-BDo not include B
A*B + CA, B, C, A*B
A + B + C + A:BA, B, C, A*B
A*B*C - A:B:CA, B, C, A*B, A*C, B*C
A*(B + C)A, B, C, A*B, A*C

Statistics Toolbox™ notation always includes a constant term unless you explicitly remove the term using -1.

For details, see Wilkinson and Rogers [1].


expand all

Add a Term to a Model

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
ds = dataset(MPG,Weight);
ds.Year = ordinal(Model_Year);
mdl = fitlm(ds,'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


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


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.

See Also

| | |

How To

Was this topic helpful?