Recently I was asked about the design matrix (or model matrix) for a regression model and why it is important. In a nutshell it is a matrix usually denoted of size where is the number of observations and is the number of parameters to be estimated. In simple linear regression i.e. a parameter for the intercept and a parameter for the slope. The typical model formulation is: where The interpretation of the slope is, as increases by 1 changes by . Consider a short example using the Boston housing data set. This won’t be the best fit for the data, it’s just for explanation.

The interpretation of the lstat coefficient is, for each increase in lstat (lower status of the population) the median value of homes (in \$1000’s) decreases by 0.95.

It’s somewhat more interesting when considering categorical variables. Each category needs to be converted to a numerical representation, this means expanding the matrix out into a number of columns depending on the number of categories. For example, let’s convert the lstat variable from the model above to categories high, medium and low.

With 3 levels R will automatically set the contrasts to be

What this means is the ‘high’ category is the reference group and ‘low’ and ‘med’ are modelled against ‘high’. To see this algebraically and We now consider all cases where observation is either in the high, med or low category and estimate their respective ‘medv’ values using the model. From the above it is shown is the average ‘medv’ for the high category. For the low and med categories the average ‘medv’ value is given by and respectively. This is important since it shows that and are the differences with repect to the high category. To show this with our example we get There are other ways to set up the design matrix which alters the interpretation of the coefficients. Another popular design is the Helmert contrasts. In R the design matrix can be changed to the Helmert design with the following commands.

Under this design the estimates of high, med and low now become And in the example The estimates of are the same for each category however the interpretation of the coefficients is completely different. The first model has much more coherent interpretation than the second. Under a multivariate regression model the same applies although the design matrix will be wider to accommodate all other variables, continuous or categorical. Finally, if you want to view the full model matrix you can do so by the command

This demonstrates the role of the design matrix and its importance. If it isn’t well understood it could completely change the interpretation of the model. If you’re interested try fitting the model

lm4 <- lm(medv ~ lstat_cat-1, data = bost.df)

And see what happens. It should look familiar.