Main Content

x2fx

Convert predictor matrix to design matrix

Syntax

D = x2fx(X,model)
D = x2fx(X,model,categ)
D = x2fx(X,model,categ,catlevels)

Description

D = x2fx(X,model) converts a matrix of predictors 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 is one of the following:

  • '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:

  1. The constant term

  2. The linear terms (the columns of X, in order 1, 2, ..., n)

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

  4. 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 should have one column for each column in X and one row for each term in the model. The entries in any row of model are powers for the corresponding columns of X. For example, if X has columns X1, X2, and X3, then a row [0 1 2] in model specifies the term (X1.^0).*(X2.^1).*(X3.^2). A row of all zeros in model specifies a constant term, which can be omitted.

D = x2fx(X,model,categ) treats columns with numbers listed in the vector 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,model,categ,catlevels) accepts a vector 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.

Examples

collapse all

Convert two 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 = 6×6

     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

Convert two 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 = 6×5

     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

Version History

Introduced before R2006a