LinearFormula
statistics: LinearFormula
Model formula of a linear or generalized linear regression.
A LinearFormula object describes the terms of a fitted model: which
variables the model draws on, how they combine into terms, and how the whole
thing reads back as a formula. It is the class of the Formula
property of a LinearModel and of a GeneralizedLinearModel, and
is normally obtained from a fitted model rather than built directly.
The object is defined by its terms matrix and the names of the variables that
matrix is written over; every other property is derived from those two. Each
row of Terms is one term of the model and each column is one variable,
the entry giving the power that variable carries in that term. An all-zero
row is the intercept. The response variable occupies a column of its own,
which is always zero.
Converting the object with char renders the whole formula, response
included, as "y ~ 1 + x1 + x2"; the LinearPredictor property
holds the right-hand side on its own. For a generalized linear model the
response carries its link function, as in "logit(y) ~ 1 + x1".
Source Code: LinearFormula
The LinearFormula class contains the following properties:
A character vector naming the variable on the left-hand side of the formula. This property is read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A cell array of character vectors naming every variable the model was given, whether or not it is used, in the order the data lists them. The response is included. This property is read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A cell array of character vectors holding those elements of
VariableNames that appear in at least one term. This property is
read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A column cell array of character vectors, one per row of Terms,
naming the term over the model’s variables: the intercept is
"(Intercept)", an interaction joins its factors with a colon, and
a power is written with a caret. A categorical variable contributes one
term under its own name however many indicator columns it expands to, so
these are not the coefficient names. This property is read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A numeric matrix with one row per term and one column per variable of
VariableNames, each entry giving the power that variable carries
in that term. An all-zero row is the intercept. This property is
read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A logical row vector with one element per variable of
VariableNames, true where that variable appears in at least one
term. The response is false. This property is read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A logical scalar, true when Terms holds an all-zero row. This
property is read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A character vector rendering the model’s terms, such as
"1 + x1 + x2". A pair of variables appearing both on their own
and as an interaction is written as a product, so that
"x1 + x2 + x1:x2" reads "x1*x2". This property is
read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
The link of a generalized linear model, given as its name, its numeric
exponent, or a structure of function handles. It is "identity"
for a linear model. This property is read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A function handle taking the coefficient vector and the design matrix. This property is read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A cell array of character vectors, empty unless the formula applies a function to a variable. This property is read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A non-negative integer, the number of rows of Terms. This
property is read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A non-negative integer, the number of elements of
VariableNames. This property is read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
A non-negative integer, the number of true elements of InModel.
This property is read-only.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
The LinearFormula class offers the following public methods:
LinearFormula: obj = LinearFormula ()
LinearFormula: obj = LinearFormula (terms, varnames)
LinearFormula: obj = LinearFormula (terms, varnames, name, value, …)
obj = LinearFormula () returns an empty formula.
obj = LinearFormula (terms, varnames) builds a
formula whose terms matrix is terms and whose variables are named
by the cell array of character vectors varnames. terms must
have one column per element of varnames; each row is one term and
each entry the power its variable carries in that term. An all-zero row
is the intercept.
The remaining properties are derived from these two arguments, except those given as name-value pairs:
| name | value |
|---|---|
"ResponseName" | A character vector naming the response variable. It must be one of varnames. |
"Link" | The link function applied to the response, as a
name, a numeric exponent, or a structure of function handles. It
defaults to "identity". |
"ModelFun" | A function handle computing the linear predictor from the coefficients and the design matrix. |
"FunctionCalls" | A cell array of character vectors naming functions the formula applies to its variables. |
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
LinearFormula: str = char (obj)
str = char (obj) returns the whole formula, response
included, as in "y ~ 1 + x1 + x2". The response carries the
link function of a generalized linear model, as in
"logit(y) ~ 1 + x1".
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
LinearFormula: str = string (obj)
str = string (obj) is the string counterpart of
char.
The Formula property of a fitted model is a LinearFormula object.
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
A formula can also be built directly from a terms matrix. Each column is a variable and each entry the power it carries in that term; the all-zero row is the intercept and the response keeps a column of its own.
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0
x1 = [1 2 3 4 5 6 7 8]'; x2 = [2 1 4 3 6 5 8 7]'; y = [3.1 4.2 5.3 6.4 7.5 8.6 9.7 10.8]'; mdl = fitlm ([x1, x2], y, 'interactions'); f = mdl.Formula
f = y ~ 1 + x1*x2
Rendering it as text gives back the whole formula, response included, while the LinearPredictor property holds the right-hand side alone.
char (f)
ans = y ~ 1 + x1*x2
f.LinearPredictor
ans = 1 + x1*x2
A pair of variables present both on their own and as their interaction is written as a product.
f.TermNames
ans =
4x1 cell array
{'(Intercept)'}
{'x1' }
{'x2' }
{'x1:x2' }
f.Terms
ans = 0 0 0 1 0 0 0 1 0 1 1 0
terms = [0 0 0; 1 0 0; 0 1 0; 1 1 0];
f = LinearFormula (terms, {'dose', 'age', 'score'}, ...
'ResponseName', 'score')
f = score ~ 1 + dose*age
f.PredictorNames
ans =
1x2 cell array
{'dose'} {'age'}
f.InModel
ans = 1 1 0