R Tutorial : Visualizing parallel slopes models

Learn to visualize a parallel slopes regression model in R using car data. The model shows fuel efficiency declining 3.611 mpg per liter of engine displacement, with 2008 cars starting 1.4 mpg higher than 1999 models. Use broom's augment function and ggplot2 to plot fitted lines over your scatter plot.
Chapters
In this video, you'll learn more about visualizing parallel slopes models. In this scatter plot, we use color to differentiate the cars from 2008 from those from 1999. Do you notice anything about the green points relative to the red points? In this manner, we have depicted three variables, two numeric and one categorical, on the same scatter plot. Thus, this plot will enable us to visualize our parallel slopes model in the data space. We're going to make use of a little high school algebra to inform our understanding of the geometry of our model. First, since our categorical explanatory variable year only has two levels, we're going to define a binary variable called newer that takes on the value 1 for cars from 2008 and is 0 otherwise. Then we can express our model mathematically using this equation. We compute the fitted coefficients using lm. So what happens when the cars are newer? Plugging in and simplifying reveals the equation for a line. Note that since displacement is our numeric explanatory variable, the slope of this line is negative 3.611 miles per gallon per liter and the Intercept is 36.678 miles per gallon. The sum of the other two coefficients. What about the older cars from 1999? In their case, the value of newer is zero and plugging that into our equation also results in the equation for our line. This line also has a slope of -3.611 miles per gallon per liter, but now the intercept is just 35.276 miles per gallon. Thus, our model consists of two parallel lines, one for newer cars from 2008 and one for older cars from 1999. The two lines are parallel because they have the same slope, but they're not the same line because they have different intercepts. This is why models with 1 numeric explanatory variable and 1 categorical explanatory variable are called parallel slopes models. Notice how the geometry was informed by the mathematics. In order to visualize our model, we need to extract the necessary information about our model that was created by lm. Conceptually, the three fitted coefficients will give us that information. However, it's easier in ggplot to simply plot the fitted values and connect them with a line. This process is streamlined by the augment function from the broom package. Applying augment to our model will return a dataframe with the fitted values attached, like the one you see here. Note that the name for the variable that contains the fitted values is fitted, and the name of the categorical variable is factor year. Finally, we can use the geomline function to put the two lines on the scatter plot that we created previously. We need to tell GEOMLINE to plot the fitted values rather than the observed values, and those values only exist in the augmented model object that we created previously. Now it's your turn to build some visualizations.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







