({{, (RefLink[DesignMatrix, paclet:ref/DesignMatrix])[{{(Subscript[x, 11]),(Subscript[x, 12]),…,(Subscript[y, 1])},{(Subscript[x, 21]),(Subscript[x, 22]),…,(Subscript[y, 2])},…},{(Subscript[f, 1]),(Subscript[f, 2]),…},{(Subscript[x, 1]),(Subscript[x, 2]),…}]
constructs the design matrix for the linear model (Subscript[β, 0])+(Subscript[β, 1]) (Subscript[f, 1])+(Subscript[β, 2]) (Subscript[f, 2])+….}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[DesignMatrix, paclet:ref/DesignMatrix])[{(Subscript[y, 1]),(Subscript[y, 2]),…},{(Subscript[f, 1]),(Subscript[f, 2]),…},x] assumes data of the form {{1,(Subscript[y, 1])},{2,(Subscript[y, 2])},…}. »

With data in the form {{(Subscript[x, 11]),(Subscript[x, 12]),…,(Subscript[y, 1])},{(Subscript[x, 21]),(Subscript[x, 22]),…,(Subscript[y, 2])},…}, the number of coordinates (Subscript[x, i1]), (Subscript[x, i2]), … should equal the number of variables (Subscript[x, i]).

The design matrix m is formed from the values of basis functions (Subscript[f, i]) at data points in the form

(({{(Subscript[f, 1])[(Subscript[x, 11]),(Subscript[x, 12]),…], (Subscript[f, 2])[(Subscript[x, 11]),(Subscript[x, 12]),…], ⋯}, {(Subscript[f, 1])[(Subscript[x, 21]),(Subscript[x, 22]),…], (Subscript[f, 2])[(Subscript[x, 21]),(Subscript[x, 22]),…], ⋯}, {⋮, ⋮,  }}))

(RefLink[DesignMatrix, paclet:ref/DesignMatrix]) takes the following options:

With the setting (RefLink[IncludeConstantBasis, paclet:ref/IncludeConstantBasis])->(RefLink[False, paclet:ref/False]), the design matrix for a model of form (Subscript[β, 1]) (Subscript[f, 1])+(Subscript[β, 2]) (Subscript[f, 2])+⋯ is constructed. »

OpenersPrimaryExamplesSection(Spacer1)Examples  (9)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Design matrix for a linear model:

data=Table[{i,i^(3/2)+RandomReal[]},{i,5}]

DesignMatrix[data,x,x]//MatrixForm

Add a quadratic term:

DesignMatrix[data,{x,x^2},x]//MatrixForm

Leave out the constant term:

DesignMatrix[data,{x,x^2},x,IncludeConstantBasis->False]//MatrixForm

Design matrix with two predictor variables:

data2=Table[{RandomInteger[10],RandomInteger[10],RandomReal[]},{10}]

DesignMatrix[data2,{x,y},{x,y}]//MatrixForm

Include a product term:

DesignMatrix[data2,{x,y,x*y},{x,y}]//MatrixForm

Assume predictor values 1, 2, …:

DesignMatrix[RandomReal[10,5],x,x]//MatrixForm

OpenersExampleSection1(Spacer1)Scope  (2)

Use any numeric functions of the predictors:

data=Table[{i,RandomReal[]},{i,5}]

DesignMatrix[data,{Sin[x],Sqrt[x]},x]//MatrixForm

Get the design matrix using exact arithmetic:

data = RandomInteger[99, {5,2}]

DesignMatrix[data,{x,Sin[x]},x] //MatrixForm

Use machine arithmetic:

DesignMatrix[N[data],{x,Sin[x]},x] //MatrixForm

Use arbitrary-precision arithmetic:

DesignMatrix[N[data,24],{x,Sin[x]},x] //MatrixForm

Use fixed 24-digit precision arithmetic:

DesignMatrix[data,{x,Sin[x]},x, WorkingPrecision->24] //MatrixForm

OpenersExampleSection2(Spacer1)Options  (3)

OpenersExampleSubsection0(Spacer1)IncludeConstantBasis  (1)

A constant term is included by default:

data=Table[{i,RandomReal[]},{i,10}]

DesignMatrix[data,x,x]//MatrixForm

Construct a design matrix without a constant term:

DesignMatrix[data,x,x,IncludeConstantBasis->False]//MatrixForm

OpenersExampleSubsection1(Spacer1)NominalVariables  (2)

Treat x as a numeric variable:

data={{0,1.5},{2,2.3},{2,1.8},{0,2.5}};

DesignMatrix[data,x,x]//MatrixForm

Treat x as nominal:

DesignMatrix[data,x,x,NominalVariables->x]//MatrixForm

Use nominal variables that are not numeric:

DesignMatrix[{{a,1.5},{b,2.3},{b,1.8},{a,2.5}},x,x,NominalVariables->x]//MatrixForm

Treat only x as nominal:

DesignMatrix[{{a,0,1.5},{b,2,2.3},{a,2,1.8},{b,0,2.5}},{x,y},{x,y},NominalVariables->x]//MatrixForm

Treat all predictors as nominal:

DesignMatrix[{{a,0,1.5},{b,2,2.3},{a,2,1.8},{b,0,2.5}},{x,y},{x,y},NominalVariables->All]//MatrixForm

OpenersExampleSection3(Spacer1)Properties & Relations  (1)

(RefLink[DesignMatrix, paclet:ref/DesignMatrix]) constructs the design matrix used by (RefLink[LinearModelFit, paclet:ref/LinearModelFit]):

data=Table[{i,RandomReal[]},{i,5}]

DesignMatrix[data,x,x]//MatrixForm

lm=LinearModelFit[data,x,x];

lm["DesignMatrix"]//MatrixForm

The matrix is the same for (RefLink[GeneralizedLinearModelFit, paclet:ref/GeneralizedLinearModelFit]):

glm=GeneralizedLinearModelFit[data,x,x];

glm["DesignMatrix"]//MatrixForm

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[LinearModelFit, paclet:ref/LinearModelFit])  ▪  (RefLink[GeneralizedLinearModelFit, paclet:ref/GeneralizedLinearModelFit])  ▪  (RefLink[LogitModelFit, paclet:ref/LogitModelFit])  ▪  (RefLink[ProbitModelFit, paclet:ref/ProbitModelFit])  ▪  (RefLink[LeastSquares, paclet:ref/LeastSquares])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Statistical Model Analysis, paclet:guide/StatisticalModelAnalysis])}})}})