Design Matrix
Study guide: Exam MAS-I
A Design Matrix is the matrix \mathbf{X} that holds the values of a regression's predictors, with one row per observation and one column per coefficient. For a model with an intercept, n observations and p predictors, the design matrix is the n \times (p + 1) matrix
\mathbf{X} = \begin{bmatrix} 1 & x_{11} & \cdots & x_{1p} \\ 1 & x_{21} & \cdots & x_{2p} \\ \vdots & \vdots & & \vdots \\ 1 & x_{n1} & \cdots & x_{np} \end{bmatrix}
x_{ij}is the value of predictorjfor observationi.- The first column, in which every entry is 1, is the intercept column.
The model for all n observations is \mathbf{y} = \mathbf{X}\boldsymbol\beta + \boldsymbol\varepsilon, and the linear predictor of a generalized linear model is \boldsymbol\eta = \mathbf{X}\boldsymbol\beta. The intercept is an estimated coefficient, so the number of parameters is the number of columns of \mathbf{X}, p + 1. The least squares estimate \hat{\boldsymbol\beta} = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y} has one entry for each column of \mathbf{X}.
Five policies have vehicle ages 2, 5, 8, 1 and 4 and territories A, B, C, B and A. What is the design matrix of a model with an intercept, vehicle age and territory, with territory A as the base level?
- The columns are the intercept, vehicle age, an indicator for territory B and an indicator for territory C.
- Each row holds 1, the policy's vehicle age, and a 1 in the indicator of its territory, if that territory is not A.
\mathbf{X} = \begin{bmatrix} 1 & 2 & 0 & 0 \\ 1 & 5 & 1 & 0 \\ 1 & 8 & 0 & 1 \\ 1 & 1 & 1 & 0 \\ 1 & 4 & 0 & 0 \end{bmatrix}
- The matrix is
5 \times 4, so the model has 4 parameters from 2 predictors.
Indicator columns for a categorical predictor
A categorical predictor with J levels enters a model with an intercept through J - 1 indicator columns, one for each level except a base level. The indicator for level j is
I_j = \begin{cases} 1 & \text{if the observation is at level } j \\ 0 & \text{otherwise} \end{cases}
The base level has no indicator, so its effect is absorbed into the intercept. The linear predictor of an observation is the intercept plus the coefficient of its own level, and the base level contributes zero. With one categorical predictor, the intercept estimate is the mean response of the base level, and each indicator's coefficient is the mean response of its level minus the mean response of the base level.
Rural claims of 2 and 4 and urban claims of 7 and 9 are fitted by
y = \beta_0 + \beta_1 I_} + \varepsilon. What are the least squares estimates?
- The rows of
\mathbf{X}are(1, 0),(1, 0),(1, 1)and(1, 1), and\mathbf{y}has entries2, 4, 7, 9.\mathbf{X}^T\mathbf{X}has rows(4, 2)and(2, 2), and\mathbf{X}^T\mathbf{y}has entries22and16.\begin{aligned} (\mathbf{X}^T\mathbf{X})^{-1} &= \frac{1}{4}\begin{bmatrix} 2 & -2 \\ -2 & 4 \end{bmatrix} \\ \hat{\boldsymbol\beta} &= \frac{1}{4}\begin{bmatrix} 2(22) - 2(16) \\ -2(22) + 4(16) \end{bmatrix} \\ &= \begin{bmatrix} 3 \\ 5 \end{bmatrix} \end{aligned}
- The intercept
\hat\beta_0 = 3is the rural mean, and\hat\beta_1 = 5is the urban mean, 8, minus the rural mean.
Interaction and polynomial columns
An interaction between predictors x_1 and x_2 enters the design matrix as a column of their products, so the linear predictor contains the term
\beta_3\, x_1 x_2
\beta_3is the interaction coefficient.
When x_2 is an indicator, the term applies only when x_2 = 1, and it changes the slope of x_1 for that level by \beta_3. A quadratic term enters the same way, as a column of x^2.
A model has linear predictor
\eta = 1.0 + 0.2x + 0.5I + 0.1xI, whereIindicates an urban policy. What is\etaatx = 3for a rural and for an urban policy?
- For a rural policy,
I = 0, so only the intercept and thexterm apply:\eta = 1.0 + 0.2(3) = 1.6.- For an urban policy,
I = 1, so the indicator and interaction terms apply as well:\eta = 1.0 + 0.6 + 0.5 + 0.1(3) = 2.4.- The slope of
xis0.2for rural policies and0.2 + 0.1 = 0.3for urban ones.
Linearly dependent columns
If one column of \mathbf{X} is a linear combination of the others, \mathbf{X}^T\mathbf{X} is singular and the coefficients are not uniquely determined. This is called aliasing: any amount can be added to one coefficient and offset in others without changing the fit, which causes convergence problems or arbitrary estimates. The usual case is an intercept together with an indicator for every level of a categorical predictor, because the indicators then sum to the intercept column. Software that finds such a column drops it and reports its coefficient as NA, and that level becomes the base level.
Dropping the intercept and keeping the indicators of all J levels instead is a reparameterization: the columns span the same space, so the fitted values are unchanged and each level has its own intercept. Columns that are nearly, but not exactly, dependent cause multicollinearity.
In the vehicle age and territory model above, what goes wrong if an indicator for territory A is added as a fifth column?
- Every policy is in exactly one territory, so
I_A + I_B + I_C = 1in every row.- The three indicators sum to the intercept column, so the five columns have rank 4.
\mathbf{X}^T\mathbf{X}is singular and the coefficients have no unique estimates.- Dropping one indicator, or the intercept, restores a unique fit.