在R中绘制多项式回归曲线 [英] Plot polynomial regression curve in R
本文介绍了在R中绘制多项式回归曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的多项式回归,其操作如下
I have a simple polynomial regression which I do as follows
attach(mtcars)
fit <- lm(mpg ~ hp + I(hp^2))
现在,我绘制如下
> plot(mpg~hp)
> points(hp, fitted(fit), col='red', pch=20)
这给了我以下内容
我想将这些点连接成一条平滑的曲线,使用直线可以看到以下内容
I want to connect these points into a smooth curve, using lines gives me the following
> lines(hp, fitted(fit), col='red', type='b')
我在这里想念什么.我希望输出为连接点的平滑曲线
What am I missing here. I want the output to be a smooth curve which connects the points
推荐答案
尝试:
lines(sort(hp), fitted(fit)[order(hp)], col='red', type='b')
由于数据集中的统计单位没有排序,因此,当您使用lines
时,情况一团糟.
Because your statistical units in the dataset are not ordered, thus, when you use lines
it's a mess.
这篇关于在R中绘制多项式回归曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文