开始使用免费开始使用

非线性逻辑回归

在第 3 章中,您探讨了通勤者的出行距离,以及该距离对乘坐公交车概率的线性影响。 但是,如果这种关系是非线性且非单调的呢?

probitVsLogit

例如,通勤距离最短和最长的人是否更不可能乘坐公交? 在 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')
编辑并运行代码