Add terms to linear regression model
mdl1 = addTerms(mdl,terms)
terms— Terms to add to regression model
Terms to add to the regression model
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
description in the fitting function
For example, if there are three variables
[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)
Create a model of the
carsmall data without any interactions, then add an interaction term.
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
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
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 Notation||Factors in Standard Notation|
|Constant (intercept) term|
|Do not include |
Statistics and Machine
Learning Toolbox™ notation always includes a constant term
unless you explicitly remove the term using
For details, see Wilkinson and Rogers .
stepwiselm to select
a model from a starting model, continuing until no single step is
removeTerms to remove particular terms.
optimally improve the model by adding or removing terms.
 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.