【发布时间】:2020-10-02 07:03:35
【问题描述】:
我知道如何用一个自变量绘制黄土和样条回归。
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,我只想用黄土或样条曲线绘制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)
我们将不胜感激。
【问题讨论】:
标签: r ggplot2 model regression