zhiwei li
zhiwei li

Reputation: 1711

How to add covariates in loess and spline regression and then plot it in r with ggplot2

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.

Upvotes: 3

Views: 779

Answers (1)

Ryan SY Kwan
Ryan SY Kwan

Reputation: 496

One way is to fit loess separately and draw the fitted line with confidence intervals.

fit = predict(loess(drat ~ mpg + cyl + gear, data=mtcars, span=0.5), se=T)
#tem = predict(loess(drat~ mpg,data=mtcars), se=T)

dat = mtcars
dat$loess = fit$fit
dat$ymax  = fit$fit + fit$se.fit * abs(qnorm((1-0.95)/2))
dat$ymin  = fit$fit - fit$se.fit * abs(qnorm((1-0.95)/2))

ggplot(dat, aes(x = mpg, y = drat)) + 
  geom_point() + 
  geom_line(aes(y=loess), color='blue', size=1) +
  geom_ribbon(aes(ymin=ymin, ymax=ymax), alpha=0.2)

Upvotes: 1

Related Questions