我知道如何使用只有一个独立变量的loess和spline回归进行绘图。
library(tidyverse)
# loess
ggplot(mtcars, aes(x = mpg, y = drat)) +
geom_point() +
geom_smooth(method = 'loess', formula = y ~ x)
# splines
ggplot(mtcars, aes(x = mpg, y = drat)) +
geom_point() +
geom_smooth(method = 'lm', formula = y ~ splines::bs(x, 8))
引起我的注意的是,我有多个独立变量,例如x1
、x2
和x3
,我创建了一个模型,如下所示:y ~ x1 + x2 + x3
,我只想用loess或spline绘制y
和x1
之间的曲线。
我尝试了但失败了。
cyl
和gear
是模型中的协变量,我只对drat
和mpg
感兴趣。
# loess
ggplot(mtcars, aes(x = mpg, y = drat)) +
geom_point() +
geom_smooth(method = 'loess', formula = y ~ x + cyl + gear)
# splines
ggplot(mtcars, aes(x = mpg, y = drat)) +
geom_point() +
geom_smooth(method = 'lm', formula = y ~ splines::bs(x, 8)+ cyl + gear)
非常感谢您的帮助。