ggplot2 and binomial GLM

Generalized Linear Models in R

Richard Erickson

Instructor

What can I see in my data?

Does commute distance change the probability of taking the bus?

ggplot(bus, aes(x = MilesOneWay, y = Bus)) + geom_point()

Plot with only points

Generalized Linear Models in R

geom_jitter()

gg_jitter <- ggplot(bus, aes(x = MilesOneWay, y = Bus)) +
             geom_jitter(width = 0, height = 0.05)
print(gg_jitter) 

Jittered point plot

Generalized Linear Models in R

geom_smooth()

gg_jitter + geom_smooth()

Previous jittered plot with a smoothed geom.

Generalized Linear Models in R

factor to numeric

str(bus)
bus$Bus2 <- as.numeric(bus$Bus) - 1 
Generalized Linear Models in R

geom_smooth()

gg_jitter + geom_smooth() 

Change data to be numeric rather than a character

Generalized Linear Models in R

linear models

gg_jitter + geom_smooth(method = 'glm')

Add lm to ggJitter

Generalized Linear Models in R

Logistic regressions

ggJitter + 
geom_smooth(method = 'glm',
            method.args = list(family = "binomial")) 

ggJitter with a logistic curve

Generalized Linear Models in R
gg_jitter + 
    geom_smooth(method = 'glm',
                method.args = list(family = binomial(link = 'logit')),
                se = FALSE, color = 'red') +
    geom_smooth(method = 'glm',
                method.args = list(family = binomial(link = 'probit')),
                se = FALSE, color = 'blue') 

ggJitter with both a logit and probit plot

Generalized Linear Models in R

Summary of steps

  • Plot as jitter to avoid overlap
  • Add a smoothed geom
  • Specify correct method and family
  • Polish your figure (not covered in this course)
Generalized Linear Models in R

Let's practice!

Generalized Linear Models in R

Preparing Video For Download...