I have some x and y values which can be fitted nicely with a polynomial
> mysubx
[1] 0.05 0.10 0.20 0.50 1.00 2.00 5.00
[8] 9.00 12.30 18.30
> mysuby
[1] 1.008 1.019 1.039 1.091 1.165 1.258 1.402
[8] 1.447 1.421 1.278
> mymodel <- lm(mysuby ~ poly(mysubx,5))
The fit can be confirmed graphically.
> plot(mysubx, mysuby)
> lines(mysubx, mymodel$fitted.values, col = "red")
My problem happens when I try to use the coefficients returned by lm to determine a y value from a given x. So for example if I try to use the first value in mysubx this should give the mymodel$fitted.values1. From the graph it can be seen I should expect to see a number around 1.01.
> ansx = 0
> for(i in seq_along(mymodel$coefficients)){
+ ansx = ansx + mysubx[1]^(i-1)*mymodel$coefficients[[i]]
+ }
> ansx
[1] 1.229575
>
Where
> mysubx[1]
[1] 0.05
> mymodel$coefficients
(Intercept) poly(mysubx, 5)1 poly(mysubx, 5)2 poly(mysubx, 5)3
1.21280000 0.35310369 -0.35739878 0.10989141
poly(mysubx, 5)4 poly(mysubx, 5)5
-0.04608682 0.02054430
As can be seen an x value on the graph of 0.05 does not give 1.229575. Obviously I don't understand what is going on? Can someone explain how I can get the correct y value from any given x value using output of the lm function? Thank you.