I'm trying to understanding polynomial fitting with R. From my research on the internet, there apparently seems to be two methods. Assuming I want to fit a cubic curve ax^3 + bx^2 + cx + d
into some dataset, I can either use:
lm(dataset, formula = y ~ poly(x, 3))
or
lm(dataset, formula = y ~ x + I(x^2) + I(x^3))
However, as I try them in R, I ended up with two different curves with complete different intercepts and coefficients. Is there anything about polynomial I'm not getting right here?
This comes down to what the different functions do. poly
generates orthonormal polynomials. Compare the values of poly(dataset$x, 3)
to I(dataset$x^3)
. Your coefficients will be different because the values being passed directly into the linear model (as opposed to indirectly, through either the I
or poly
function) are different.
As 42 pointed out, your predicted values will be fairly similar. If a
is your first linear model and b
is your second, b$fitted.values - a$fitted.value
should be fairly close to 0 at all points.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With