LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Intermediate Regression in R

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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
  ___
Code bearbeiten und ausführen