ggplot2 และ binomial GLM

Generalized Linear Models ใน R

Richard Erickson

Instructor

มองเห็นอะไรในข้อมูลบ้าง?

ระยะทางเดินทางส่งผลต่อความน่าจะเป็นในการนั่งรถบัสหรือไม่?

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

กราฟที่แสดงเฉพาะจุดข้อมูล

Generalized Linear Models ใน R

geom_jitter()

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

กราฟจุดแบบ jitter

Generalized Linear Models ใน R

geom_smooth()

gg_jitter + geom_smooth()

กราฟ jitter เดิมพร้อมเส้น smooth

Generalized Linear Models ใน R

แปลง factor เป็น numeric

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

geom_smooth()

gg_jitter + geom_smooth() 

เปลี่ยนข้อมูลจาก character เป็น numeric

Generalized Linear Models ใน R

โมเดลเชิงเส้น

gg_jitter + geom_smooth(method = 'glm')

เพิ่ม lm ลงใน ggJitter

Generalized Linear Models ใน R

Logistic regression

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

ggJitter พร้อมเส้นโค้ง logistic

Generalized Linear Models ใน 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 แสดงทั้งเส้น logit และ probit

Generalized Linear Models ใน R

สรุปขั้นตอน

  • ใช้ jitter เพื่อหลีกเลี่ยงจุดข้อมูลทับกัน
  • เพิ่ม smooth geom
  • ระบุ method และ family ให้ถูกต้อง
  • ปรับแต่งกราฟ (ไม่ครอบคลุมในคอร์สนี้)
Generalized Linear Models ใน R

มาฝึกกันเถอะ!

Generalized Linear Models ใน R

Preparing Video For Download...