Categorical predictors can be incorporated into regression analysis, provided that they are properly prepared and interpreted. This tutorial will explore how categorical variables can be handled in R.
Tutorial FilesBefore we begin, you may want to download the sample data (.csv) used in this tutorial. Be sure to right-click and save the file to your R working directory. Note that all code samples in this tutorial assume that this data has already been read into an R variable and has been attached. This dataset contains variables for the following information related to NFL quarterback and team salaries in 1991.
- TEAM: Name of team
- QB: Starting quarterback salary in thousands of dollars
- TOTAL: team salary in thousands of dollars
- CONF: conference (NFC or AFC)
Dummy CodingTo be able to perform regression with a categorical variable, it must first be coded. Here, I will use the as.numeric(VAR) function, where VAR is the categorical variable, to dummy code the CONF predictor. As a result, CONF will represent NFC as 1 and AFC as 0. The sample code below demonstrates this process.
Note that the -1 that comes after the as.numeric(CONF) function causes the variables to read 1 and 0 rather than 2 and 1, which is the default behavior.
- > #represent a categorical variable numerically using as.numeric(VAR)
- > #dummy code the CONF variable into NFC = 1 and AFC = 0
- > dCONF <- as.numeric(CONF) - 1
VisualOne useful way to visualize the relationship between a categorical and continuous variable is through a box plot. When dealing with categorical variables, R automatically creates such a graph via the plot() function (see Scatterplots). The CONF variable is graphically compared to TOTAL in the following sample code.
The resulting box plot is show below.
- > #use the plot() function to create a box plot
- > #what does the relationship between conference and team salary look like?
- > plot(CONF, TOTAL, main=”Team Salary by Conference”, xlab=”Conference”, ylab=”Salary ($1,000s)”)
From a box plot, we can derive many useful insights, such as the minimum, maximum, and median values. Our box plot of total team salary on conference suggests that, compared to AFC teams, NFC teams have slightly higher salaries on average and the range of these salaries is larger.
Routine AnalysisOnce a categorical variable has been quantified, it can be used in routine analyses, such as descriptive statistics and correlations. The following code depicts a few examples.
The correlation between total team salary and conference indicates that there is little to no linear relationship between the variables.
- > #what are the mean and standard deviation of conference?
- > mean(dCONF)
- >  0.5
- > sd(dCONF)
- >  0.5091751
- > #this makes sense… there are an even number of teams in both conferences and they are coded as either 0 or 1!
- > #what is the correlation between total team salary and conference?
- > cor(dCONF, TOTAL)
- > 0.007019319
Linear RegressionLet’s return to our original question of how well quarterback salary and conference predict team salary. With the categorical predictor quantified, we can create a regression model for this relationship, as demonstrated below.
The model summary is pictured below.
- > #create a linear model using lm(FORMULA, DATAVAR)
- > #predict team salary using quarterback salary and conference
- linearModel <- lm(TOTAL ~ QB + dCONF, datavar)
- #generate model summary
Considering both the counterintuitive and statistically insignificant results of this model, our analysis of the conference variable would likely end or change directions at this point. However, there is one more interpretation method that is worth mentioning for future reference.
Split ModelWith a dummy coded predictor, a regression model can be split into two halves by substituting in the possible values for the categorical variable. For example, we can think of our model as a regression of total salary on quarterback salary for two states of the world – teams in the AFC and teams in the NFC. These derivative models are covered in the following sample code.
Based only on what we have modeled, we can further infer that conference was not a significant predictor of total team salaries in the NFL in 1991. The difference between the team salaries based on conference is less than one-half of one percent on average! Of course, only using quarterback salary and conference to predict an NFL team’s overall salary is neglecting quite a few potentially significant predictors. Nonetheless, split model interpretation is a useful way to break down the perspectives captured by a categorical regression model.
- > #input the categorical values to split the linear model into two representations
- > #the original model: TOTAL = 19099 + 2.5 * QB – 103 * dCONF
- > #substitute 0 for dCONF to derive the AFC model: TOTAL = 19099 + 2.5 * QB
- > #substitute 1 for dCONF to derive the NFC model: TOTAL = 18996 + 2.5 * QB
- #what is the predicted salary for a team with a quarterback salary of $2,000,000 in the AFC and NFC conferences?
- #AFC prediction
- 19099 + 2.5 * 2000
-  24099
- #NFC prediction
- 18996 + 2.5 * 2000
-  23996