【问题标题】:How to change the appearance of the best fit line in R如何更改R中最佳拟合线的外观
【发布时间】:2021-12-25 18:30:21
【问题描述】:

我正在尝试添加多项式最佳拟合线,但由于某种原因,它似乎在图上来回绘制。我想知道我应该怎么做才能修复这条线

yhat_pol = lm(age~poly(height,2), data = Loblolly)
yhat_lin = lm(age~height, data = Loblolly)

plot(x=Loblolly$height, y=Loblolly$age, pch=16, xlab = "Height (ft)", ylab = "Age (yr)", main = "Height vs Age of Loblloy Pine Trees")
lines(Loblolly$height, predict(yhat_pol), type="l", col="green", lwd=2)
lines(Loblolly$height, predict(yhat_lin), type="l", col="red", lwd=2)
lines(sort(Loblolly$height), fitted(yhat_lin)[order(Loblolly$height)])
legend("topleft", 
        legend = c("linear","polynomial"), 
        col = c("red","green"),
        lty = 1, lwd=3) 
height age Seed
1 4.51 3 301
15 10.89 5 301
29 28.72 10 301
43 41.74 15 301
57 52.70 20 301
71 60.92 25 301
2 4.55 3 303
16 10.92 5 303
30 29.07 10 303
44 42.83 15 303

【问题讨论】:

  • 您能分享一些您问题中使用的数据吗?试试dput(Loblolly)dput(head(Loblolly))
  • 我从 R 中的数据集包中获取了数据集,所以我认为只需输入 Loblolly 就可以了,但我在问题中添加了 head(Lobolly)。
  • 你是对的!忘记我的评论。

标签: r plot regression


【解决方案1】:

你几乎拥有它。

lines(sort(Loblolly$height), predict(yhat_pol)[order(Loblolly$height)], type="l", col="green", lwd=2)

如果您使用 ggplot2,我认为数据的顺序不会导致这些问题。

【讨论】:

    【解决方案2】:

    您没有要求它,但是正如 Brian Montgomery 所指出的那样,这是使用 ggplot2 的解决方案:

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    
    
    Loblolly %>%
      mutate(age_pred_pol = predict(yhat_pol),
             age_pred_lin = predict(yhat_lin)) %>% 
      ggplot(aes(x = height, y = age)) +
      geom_point() +
      geom_line(aes(y = age_pred_pol, color = "Polynomial Best Fit")) +
      geom_line(aes(y = age_pred_lin, color = "Linear Best Fit")) +
      labs(x = "Height (ft)", y = "Age (yr)", title = "Height vs Age of Loblloy Pine Trees") +
      scale_colour_manual(name = "Prediction",
                          breaks = c("Polynomial Best Fit", "Linear Best Fit"),
                          values = c("red", "green")) +
      theme_bw()
    

    返回

    【讨论】:

      猜你喜欢
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 2022-11-23
      • 1970-01-01
      • 2015-08-14
      相关资源
      最近更新 更多