Add fitted quadratic curve
Instead of using abline
, use fitted
, which gives you a vector the same length as your input of the predictions:
fitted(lm(data~factor+I(factor^2)))
# 1 2 3 4 5 6 7
# 0.1248016 0.2395833 0.3699405 0.5158730 0.6773810 0.8544643 1.0471230
Thus, something like:
plot(factor, fitted(lm(data~factor+I(factor^2))), type="l")
I couldn't get answers so far to work, as dataset I used has x-values which are not increasing (as stated by David Robinson above). Here's how I solved it...
require(ISLR)
plot(mpg~horsepower, data=Auto)
# fit the model
glm.fit = glm(mpg~poly(horsepower,2), data=Auto)
# create 100 x-values based on min/max of plotted values
minMax = range(Auto$horsepower)
xVals = seq(minMax[1], minMax[2], len = 100)
# Use predict based on a dataframe containing 'horsepower'
yVals = predict(glm.fit, newdata = data.frame(horsepower = xVals))
lines(xVals, yVals)