plot regression line in R

被刻印的时光 ゝ 提交于 2019-12-20 06:09:15

问题


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))


回答1:


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)




回答2:


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, df)
model <- lm(y ~ x, df)

You're trying to fit a linear function to parabolic data. As such, you won't end up with a pretty line of best fit.

Something like this might work:

model <- lm(y ~ I(x^2), df)

plot(y ~ x, df)
lines(df$x, predict(model), col = 'blue')

Although that doesn't really fit well, we could try 3rd- or 4th-order polynomial models:

model <- lm(y ~ I(x^3), df)
lines(df$x, predict(model), col = 'red')
model <- lm(y ~ I(x^4), df)
lines(df$x, predict(model), col = 'green')

Although those don't fit very well, either. Look to Zheyuan's answer for a better-fitting function.



来源:https://stackoverflow.com/questions/39736847/plot-regression-line-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!