在 R 中绘制多项式回归曲线 [英] Plot polynomial regression curve in R

查看:23
本文介绍了在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆