Linear models explain the values of the dependent variable by means of a linear combination of the explanatory variables, the linear predictor. Determining a linear relationship with numerical explanatory variables is easy to derive and understand. But how should categorical variables be considered?
Consider the example of investigating the relationship of weight to height among a population of school children and its dependence on the gender of the child. Gender is unlike height as it is not measured numerically. However, we can relate the child’s gender to a Boolean TRUE/FALSE variable by asking the question: Is the child male?
We can represent the Boolean variables numerically as if TRUE or if FALSE. This numerical representation are called indicator variables. For the school example, let be the gender indicator variable for the th child where if the child is male or if the child is female. Combining this with the height explanatory variable (denoted by ) obtains the linear predictor for the expected weight of child :
Exercise 6.48
Write the linear predictor for a male and a female child. Explain the effect has on the linear predictor.
A qualitative variable that takes a finite number of non-numerical values is categorical. The values are sometimes called levels. The subspace spanned by the indicator vectors for the levels is a factor. (Sometimes, though loosely, we take factor to refer directly to the variable.)
Returning to the school example, the categorical variable gender has two levels, either male or female.
Categorical variables may consist of more than two variables, such as blood type where a patient is one of the levels . The factor for this variable is defined by four indicator variables, e.g.:
The distinction between factor and numerical variables is usually straightforward but some variables can fall in the middle. For instance, a survey on household bills may ask how many hours in a day do you use a mobile phone with options ‘0hr-1hr’, ‘1hr-2hr’, ‘2hr-3hr’ or ‘3hr+’. To add to the confusion, the data could be encoded in the database as , , , and for the four levels respectively.
Exercise 6.49
What assumptions are being made about the relationship between phone usage categories if is treated as a numerical variable?
Suppose we have three categorical variables: with four levels, with 2 levels and also with two levels. Measurements from units were collected as follows:
Each unit takes one and only one level of each factor. This information can be converted into indicators
Here, indicates level of factor and occurs for units 2 and 3. Also, indicates level of factor and occurs on units 2, 4 and 6. It follows that the span for each categorical variable is:
This procedure generalizes to an arbitrary number of factors with arbitrary numbers of levels in the obvious way.
However, since each unit must fall in exactly one level for each categorical variable, we note the relationships:
Evidently, there is a linear relationship between the columns in the above table, e.g. . This is not an ideal property of the design matrix as there will be dependence between the co-efficients.
From the above relationship, we are able to write one of the levels in terms of the others for each of the categorical variables. For example, . This means we are able to provide an alternative span for each categorical variable:
The linear predictor is therefore an element of the space spanned by the sum of these subspaces:
These indicator vectors are linearly independent and so provide an ideal basis for defining the design matrix. For the example above, the design matrix is:
Exercise 6.50
The linear predictor corresponding to the design matrix is
Explain the meaning of each co-efficient.