ggplot2 and binomial GLM

Model Linear Tergeneralisasi di 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

Model Linear Tergeneralisasi di 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

Model Linear Tergeneralisasi di R

geom_smooth()

gg_jitter + geom_smooth()

Previous jittered plot with a smoothed geom.

Model Linear Tergeneralisasi di R

factor to numeric

str(bus)
bus$Bus2 <- as.numeric(bus$Bus) - 1 
Model Linear Tergeneralisasi di R

geom_smooth()

gg_jitter + geom_smooth() 

Change data to be numeric rather than a character

Model Linear Tergeneralisasi di R

linear models

gg_jitter + geom_smooth(method = 'glm')

Add lm to ggJitter

Model Linear Tergeneralisasi di R

Logistic regressions

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

ggJitter with a logistic curve

Model Linear Tergeneralisasi di 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

Model Linear Tergeneralisasi di 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)
Model Linear Tergeneralisasi di R

Let's practice!

Model Linear Tergeneralisasi di R

Preparing Video For Download...