趋势线的方法
在上一个练习中,您使用了 ggplot2 的默认 geom_smooth()。
在本练习中,您将改用 glm()。
这将让您可以通过 ggplot2 来"查看"逻辑回归。
具体来说,您将根据通勤距离绘制某人乘坐公交车的概率。
您需要告诉 geom_smooth() 使用 glm() 方法。
回忆第 2 章的内容:glm() 的默认族是 Gaussian,与 lm() 产生相同结果。
因此,您还需要为 glm() 指定方法参数。
上一练习中创建的 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(___))