First, the best way to get help is to provide a reproducible example [reprex]; there are references for how to do it in the help here in the RStudio community. FAQ: What's a reproducible example (`reprex`) and how do I create one?
Second, I am pretty sure the problem is the lm
call. Try modifying your lm
to use the formula and data form.
mod = lm(Life.expectancy ~ poly(Adult.Mortality, 2), data=D)
You may also need to name the input, so alter the definition of x4
x4 = data.frame(Adult.Mortality = seq(min(D$Adult.Mortality), max(D$Adult.Mortality),
length.out=length(D$Adult.Mortality)))
And remove the duplicate data.frame() call.
y4 = predict(mod, x4, interval = "confidence")
That should fix it.