Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Convert predictor matrix to design matrix

`D = x2fx(X,`

* model*)

D = x2fx(X,

`model`

D = x2fx(X,

`model`

`D = x2fx(X,`

converts
a matrix of predictors * model*)

`X`

to a design matrix `D`

for
regression analysis. Distinct predictor variables should appear in
different columns of `X`

.The optional input * model* controls
the regression model. By default,

`x2fx`

returns
the design matrix for a linear additive model with a constant term. `model`

`'linear'`

— Constant and linear terms. This is the default.`'interaction'`

— Constant, linear, and interaction terms`'quadratic'`

— Constant, linear, interaction, and squared terms`'purequadratic'`

— Constant, linear, and squared terms

If `X`

has *n* columns,
the order of the columns of `D`

for a full quadratic
model is:

The constant term

The linear terms (the columns of

`X`

, in order 1, 2, ...,*n*)The interaction terms (pairwise products of the columns of

`X`

, in order (1, 2), (1, 3), ..., (1,*n*), (2, 3), ..., (*n*–1,*n*))The squared terms (in order 1, 2, ...,

*n*)

Other models use a subset of these terms, in the same order.

Alternatively, * model* can be a matrix
specifying polynomial terms of arbitrary order. In this case,

`model`

`X`

and one row
for each term in the model. The entries in any row of `model`

`X`

. For
example, if `X`

has columns `X1`

, `X2`

,
and `X3`

, then a row `[0 1 2]`

in `model`

`(X1.^0).*(X2.^1).*(X3.^2)`

. A row of all
zeros in `model`

`D = x2fx(X,`

treats
columns with numbers listed in the vector * model*,categ)

`categ`

as
categorical variables. Terms involving categorical variables produce
dummy variable columns in `D`

. Dummy variables are
computed under the assumption that possible categorical levels are
completely enumerated by the unique values that appear in the corresponding
column of `X`

.`D = x2fx(X,`

accepts
a vector * model*,categ,catlevels)

`catlevels`

the same length as `categ`

,
specifying the number of levels in each categorical variable. In
this case, values in the corresponding column of `X`

must
be integers in the range from 1 to the specified number of levels.
Not all of the levels need to appear in `X`

.The following converts 2 predictors `X1`

and `X2`

(the
columns of `X`

) into a design matrix for a full quadratic
model with terms `constant`

, `X1`

, `X2`

, `X1.*X2`

, `X1.^2`

,
and `X2.^2`

.

X = [1 10 2 20 3 10 4 20 5 15 6 15]; D = x2fx(X,'quadratic') D = 1 1 10 10 1 100 1 2 20 40 4 400 1 3 10 30 9 100 1 4 20 80 16 400 1 5 15 75 25 225 1 6 15 90 36 225

The following converts 2 predictors `X1`

and `X2`

(the
columns of `X`

) into a design matrix for a quadratic
model with terms `constant`

, `X1`

, `X2`

, `X1.*X2`

,
and `X1.^2`

.

X = [1 10 2 20 3 10 4 20 5 15 6 15]; model = [0 0 1 0 0 1 1 1 2 0]; D = x2fx(X,model) D = 1 1 10 10 1 1 2 20 40 4 1 3 10 30 9 1 4 20 80 16 1 5 15 75 25 1 6 15 90 36