如何在回归模型中绘制线性和二次预测变量,同时控制其他变量?

时间:2020-04-20 18:37:51

标签: r plot regression polynomials quadratic

这是一些数据和模型。它由一个线性和二次预测变量(a和a2)和一个线性控制变量(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)

我想绘制线性和二次效应,同时还要控制b。

我已经找到了只需要a和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()

但是在控制b的同时又如何绘制呢?

0 个答案:

没有答案