Messy plot when plotting predictions of a polynomial regression using lm() in R -


i building quadratic model lm in r:

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

now want display both predicted values , actual values on plot. tried this:

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

but line comes squiggely. maybe has fact it's quadratic? help.

enter image description here

you need order():

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

my answer here related: problems displaying loess regression line , confidence interval


Comments