0

Here is some data and a model. It consists of a linear and quadratic predictor (a and a2) and a linear control variable (b).

library(data.table)
library(ggplot2)

d <- as.data.table(cbind(a = rnorm(50), b = rnorm(50), y = rnorm(50)))
d$a2 <- (d$a)^2

m <- lm(y ~ a + a2 + b, data = d)

I would like to plot the linear and quadratic effects, while also controlling for b.

I have found how to do this if I want only the effects of a and a2:

ggplot(d,
       aes(x = a, y = y)) +
  geom_point() +
  geom_smooth(method = "lm",
              formula = y ~ x,
              aes(color = "linear"),
              se = FALSE) +
  geom_smooth(method = "lm",
              formula = y ~ x + I(x^2),
              aes(color = "quadratic"),
              se = FALSE) +
  theme_bw()

But how can I plot this while also controlling for b?

Dave
  • 232
  • 1
  • 2
  • 14
  • What exactly do you mean by "controlling for b"? What precisely do you want to plot? If you are just trying to make a 2D plot there are only so many continuous variables you can plot at once. But just asking for plotting recommendations is a better fit for [stats.se] where questions about data visualization are on topic. This doesn't seem to be a specific programming question that's appropriate for Stack Overflow in it's current form. – MrFlick Apr 20 '20 at 18:49
  • I was under the impression that coding questions went here rather than crossvalidated? I could easily be wrong though. To answer your question, I would like to plot a and a^2 as they exist in a model that also controls for b. – Dave Apr 20 '20 at 18:51
  • True, coding questions do go here. You just need to define exactly what you mean by "controls for b". That doesn't sound like a programming problem. That sounds like a data modeling question. It's like asking for a plot that "shows the patterns in my data." That's too general a request. If you are more specific about what you mean then we can help you find the code to solve your problem. – MrFlick Apr 20 '20 at 18:54
  • Ah, I guess it exists somewhere in the middle! I'll post on crossvalidated as well. I'm not sure how else to word it, other than I would like to plot the predictors in model m, and currently, I'm only able to plot a model that goes in the ggplot code that omits predictor b. – Dave Apr 20 '20 at 18:58
  • Perhaps what you are looking for is to plot y and the columns of `model.matrix(m) %*% diag(coef(m))` except the first column. – G. Grothendieck Apr 20 '20 at 19:08
  • Do you want `stat_function(fun = mean, aes(colour = "adjusted"))` after the two `geom_smooth`? Don't worry about the warning, it works. – Rui Barradas Apr 20 '20 at 20:04
  • is using an offset along the correct lines: after fitting your model `m` we can get similar using `lm(y ~ a + a2, offset=(coef(m)['b']*b), data = d)` or `lm(y - coef(m)['b']*b ~ a + a2, data=d)`. So using this idea we could create an updated `y` in the ggplot call. i.e. `ggplot(d, aes(x = a, y = (y-coef(m)['b']*b))) + ...` – user20650 Apr 20 '20 at 21:15

0 Answers0