【问题标题】:Adding multiple regression lines to a line plot R将多条回归线添加到折线图 R
【发布时间】:2021-01-21 20:27:58
【问题描述】:

这是我正在使用的线性模型:

blk.lm <- lm(formula = YEARS ~ AGE + AGE2, data = BLKFRIDAY)

我想用 ggplot2 创建一个图,将YEARSAGEYEARSAGE2 进行比较,其中AGE2 只是AGE^2。我的目标是查看线性模型还是二次模型最适合我的数据。

如何设置一个图,其中有两条回归线以Y ~ X1Y ~ X2 进行比较?

【问题讨论】:

  • 您能否发布您的BLKFRIDAY 数据样本或分享它的来源?

标签: r ggplot2 regression


【解决方案1】:

这是来自ggplot2diamonds 数据集的示例:

library(ggplot2)
ggplot(diamonds, aes(x = carat, y =  price)) + 
  geom_point() +
  geom_smooth(formula = y ~ x, se = FALSE, method = "lm") +
  geom_smooth(formula = y ~ I(x^2), se = FALSE, method = "lm", color = "red")
# I(x^2) is needed because x^2 is interpreted as x * X - the interaction of x with x

请注意,红线是弯曲的,因为它相对于carat 是非线性的。

【讨论】:

    猜你喜欢
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多