本文介绍了在R中绘制回归线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R中绘制一条简单的回归线.我已经输入了数据,但是回归线似乎并不正确.有人可以帮忙吗?

I want to plot a simple regression line in R. I've entered the data, but the regression line doesn't seem to be right. Can someone help?

x <- c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120)
y <- c(10, 18, 25, 29, 30, 28, 25, 22, 18, 15, 11, 8)
df <- data.frame(x,y)
plot(y,x)
abline(lm(y ~ x))

推荐答案

哦,@ GBR24的格式很好.然后,我将根据我的评论进行详细说明.

Oh, @GBR24 has nice formatted data. Then I'm going to elaborate a little bit based on my comment.

fit <- lm(y ~ poly(x, 3))   ## polynomial of degree 3
plot(x, y)  ## scatter plot (colour: black)

x0 <- seq(min(x), max(x), length = 20)  ## prediction grid
y0 <- predict.lm(fit, newdata = list(x = x0))  ## predicted values
lines(x0, y0, col = 2)  ## add regression curve (colour: red)

这篇关于在R中绘制回归线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 06:29