I know how to plot loess and spline regression with just one independent variable.
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))
What stuck me is that I have multiple independent variables such as x1, x2, x3, and I create a model like this:
y ~ x1 + x2 + x3, and I just want to plot the curve between y and x1 with loess or spline.
I tried but failed.
cyl and gear are covariates in the model and I just interested in drat and 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)
Any help will be highly appreciated.