Visualizing parallel slopes
The two plots in the previous exercise gave very different predictions: one gave a predicted response that increased linearly with a numeric variable; the other gave a fixed response for each category. The only sensible way to reconcile these two conflicting predictions is to incorporate both explanatory variables in the model at once.
When it comes to a linear regression model with a numeric and a categorical explanatory variable, ggplot2
doesn't have an easy, "out of the box" way to show the predictions. Fortunately, the moderndive
package includes an extra geom, geom_parallel_slopes()
to make it simple.
taiwan_real_estate
is available; ggplot2
and moderndive
are loaded.
This exercise is part of the course
Intermediate Regression in R
Exercise instructions
- Using the
taiwan_real_estate
dataset, plot house prices versus the number of nearby convenience stores, colored by house age. - Make it a scatter plot.
- Add parallel slopes, without a standard error ribbon.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Using taiwan_real_estate, plot price_twd_msq vs. n_convenience colored by house_age_years
___ +
# Add a point layer
___ +
# Add parallel slopes, no ribbon
___