Methods for trend lines

During the previous exercise, you used ggplot2's default geom_smooth(). During this exercise, you will use a glm() instead. This will allow you to "see" a logistic regression with ggplot2. Specifically, you plot the probability someone takes the bus given their commute distance.

You will need to tell geom_smooth() to use the glm() method. Recall from Chapter 2 that the default family for a glm() is a Gaussian family, which produces the same results as a lm(). Thus, you will also need to specify the method argument from glm().
The code for creating gg_jitter, which you built in the last exercise, has been provided for you.

This exercise is part of the course

Generalized Linear Models in R

View Course

Exercise instructions

  • Use the "glm" method with geom_smooth().
  • With the method.args, set the family to 'binomial'.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a jittered plot of MilesOneWay vs Bus2 using the bus dataset
gg_jitter <- ggplot(data = bus, aes(x = MilesOneWay, y = Bus2)) + 
	geom_jitter(width = 0, height = 0.05) +
	ylab("Probability of riding the bus") +
	xlab("One-way commute trip (in miles)")

# Add a geom_smooth() that uses a GLM method to your plot
gg_jitter + geom_smooth(method =  ___ , method.args = list(___))