【问题标题】:Messy plot when plotting predictions of a polynomial regression using lm() in R在 R 中使用 lm() 绘制多项式回归的预测时的混乱图
【发布时间】:2016-12-06 14:47:12
【问题描述】:

我正在用 R 中的 lm 构建一个二次模型:

y <- data[[1]]
x <- data[[2]]
x2 <- x^2

quadratic.model = lm(y ~ x + x2)

现在我想在绘图上同时显示预测值和实际值。我试过这个:

par(las=1,bty="l")
plot(y~x)
P <- predict(quadratic.model)
lines(x, P)

但这条线总是曲折地出现。也许它与它是二次的事实有关?谢谢你的帮助。

【问题讨论】:

标签: r plot regression linear-regression lm


【解决方案1】:

你需要order():

P <- predict(quadratic.model)
plot(y~x)
reorder <- order(x)
lines(x[reorder], P[reorder])

我这里的回答是相关的:Problems displaying LOESS regression line and confidence interval

【讨论】:

    猜你喜欢
    • 2018-10-19
    • 2015-06-21
    • 2017-04-15
    • 2020-05-08
    • 2016-11-10
    • 2014-06-13
    相关资源
    最近更新 更多