plot regression line in R
Asked Answered
A

3

11

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

enter image description here

enter image description here

Avow answered 28/9, 2016 at 1:56 Comment(4)
Either way, OP is plotting a parabola, effectively. Tough to get a meaningful linear line of best fit with that.Willett
Right. I suppose more info is needed on behalf of OP, regarding whether the best-fit line they're looking for should be more parabolic, or regarding the typo you mentioned... That remains to be seen!Willett
In the first place, I just want to get help with the code, because I didn't know what was wrong with it, since it gave me the wrong plot. And after seeing this plot, I start to feel like that I might be doing it wrong. I didn't come here to ask for help with homework. I am posting this photo just for clarification.Avow
the y and x in your graph was reversed, that is why the line is low.Grefer
S
8

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)

enter image description here

Sensationalism answered 28/9, 2016 at 2:15 Comment(0)
W
9
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)

enter image description here

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

enter image description here

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

enter image description here

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

Willett answered 28/9, 2016 at 2:2 Comment(1)
This is the answer. **The original graph had the x-axis and y-axis reversed. ** The linear equation is y = 25.3 - 0.08x. A second order approximation would give you a parabola (quadradic approximation) Not shown above. A third order approximation was done above. But the original question was for the best linear approximation.Grefer
S
8

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)

enter image description here

Sensationalism answered 28/9, 2016 at 2:15 Comment(0)
G
2
  1. As I said above the graph in the original question switched the x-axis and y-axis
  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

original data dots,black linear abline, red quadratic curve, blue weighted curve

Grefer answered 19/6, 2020 at 3:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.