Useless plot for building polynomial regression forecasts using lm () in R

I am building a quadratic model with lm in R:

y <- data[[1]] x <- data[[2]] x2 <- x^2 quadratic.model = lm(y ~ x + x2) 

Now I want to display both the predicted values ​​and the actual values ​​on the graph. I tried this:

 par(las=1,bty="l") plot(y~x) P <- predict(quadratic.model) lines(x, P) 

but the line rises all excitedly. Maybe this is due to the fact that it is quadratic? Thanks for any help.

enter image description here

0
source share
1 answer

You need order() :

 P <- predict(quadratic.model) plot(y~x) reorder <- order(x) lines(x[reorder], P[reorder]) 

My answer here is related: Problems with displaying string and LOESS confidence interval

+1
source

Source: https://habr.com/ru/post/1259864/


All Articles