简体   繁体   English

在 R 中绘制回归线

[英]plot regression line in 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.我想在 R 中绘制一条简单的回归线。我已经输入了数据,但回归线似乎不正确。 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))

在此处输入图片说明

在此处输入图片说明

Oh, @GBR24 has nice formatted data.哦,@GBR24 有很好的格式化数据。 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)

在此处输入图片说明

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.看看哲元的答案以获得更好的拟合函数。

  1. As I said above the graph in the original question switched the x-axis and y-axis正如我上面所说,原始问题中的图表切换了 x 轴和 y 轴
  2. The linear model answer is the best for the question since that is what was asked.线性模型答案最适合该问题,因为这就是所问的问题。
  3. The other answers address further modeling choices such as best cubic model above or best quadratic below.其他答案解决了进一步的建模选择,例如上面的最佳三次模型或下面的最佳二次模型。 This just combines the reasoning above.这只是结合了上面的推理。
    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)
    summary(lm(y~x))
    plot(x,y)
    abline(lm(y ~ x)) # black answer 1
    ########################
    fit <- lm(y ~ poly(x, 2))   ## polynomial of degree 2
    y0 <- predict.lm(fit)  ## predicted values
    lines(x, y0, col = 2)  ##  predicted poly red color
    #y1 <- predict(fit, interval = "prediction")
    [![#lines(x, y1\[,1\], col = 3)  same as y1 green color   # answer 2
    #########################
    w <- 1 + (x-1)^2  # with weights
    wfit <- lm(y ~ poly(x,2), weights = w)
    y2 <- predict(wfit, interval = "prediction")
    lines(x, y2\[,1\], col = 4) # blue    # answer 3

原始数据点,黑色线性曲线,红色二次曲线,蓝色加权曲线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM