Linear Regression for One Categorical Explanatory Variable

Perform linear regression for a categorical variable in R and learn the principles behind it.

We introduced simple linear regression that involves modeling the relationship between a numerical outcome variable 𝑦𝑦 and a numerical explanatory variable 𝑥𝑥. In our life expectancy example, we now instead have a categorical explanatory variable continent. Our model won’t yield a best-fitting regression line like it did previously, but rather offsets relative to a baseline for comparison.

As we did before when studying the relationship between teaching scores and beauty scores, let’s output the regression table for this model. Recall that this is done in two steps:

  1. We first fit the linear regression model using the lm(y ~ x, data) function and save it in lifeExp_model.

  2. We get the regression table as the code output by applying the get_regression_table() function from the moderndive package to lifeExp_model.

Get hands-on with 1200+ tech skills courses.