非線性羅吉斯迴歸
在第 3 章中,你探討了通勤者的通勤距離,以及它對搭公車機率的線性影響。 不過,如果這個關係是非線性、而且也非單調呢?

例如,通勤距離最短與最長的人,是否反而比較不會搭公車?
你可以在 R 的公式中使用 I(..) 來加入非線性項。
例如,y~I(x^2) 會讓你為 x*x 估計一個係數。
在這個練習中,你會進一步檢視公車資料。
本練習屬於課程
R 的廣義線性模型
練習說明
- 在第二次呼叫
geom_smooth()時,把公式y ~ I(x^2)加到formula參數中。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Plot linear effect of travel distance on probability of taking the bus
gg_jitter <-
ggplot(data = bus, aes(x = MilesOneWay, y = Bus2)) +
geom_jitter(width = 0, height = 0.05) +
geom_smooth(method = 'glm',
method.args = list(family = 'binomial'))
# Add a non-linear equation to a geom_smooth()
gg_jitter +
geom_smooth(method = 'glm',
method.args = list(family = 'binomial'),
formula = ___,
color = 'red')