開始使用免費開始

趨勢線的方法

在前一個練習中,你使用了 ggplot2 的預設 geom_smooth()。 在這個練習中,你將改用 glm()。 這能讓你用 ggplot2「看見」羅吉斯迴歸。 具體來說,你要繪製在不同通勤距離下,某人選擇搭公車的機率。

你需要告訴 geom_smooth() 使用 glm() 方法。 回想第 2 章,glm() 的預設 family 是 Gaussian,會產生與 lm() 相同的結果。 因此,你也需要從 glm() 指定 method 的參數。 前一個練習中你建立的 gg_jitter 程式碼已提供給你。

本練習屬於課程

R 的廣義線性模型

檢視課程

練習說明

  • geom_smooth() 中使用 "glm" 方法。
  • 使用 method.args 將 family 設為 'binomial'

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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(___))
編輯並執行程式碼