I have the following data:
Which looks quadratic. If I fit a quadratic curve using I()
I get coefficients that make sense:
> modelI = lm(
formula = Y ~ I(X^2) + I(X),
data = df
)
> modelI
Call:
lm(formula = Y ~ I(X^2) + I(X), data = df)
Coefficients:
(Intercept) I(X^2) I(X)
4.822e-02 -4.426e-05 3.229e-05
I fitted a quadratic curve using the same data in Excel, and got exactly the same coefficients, so this is probably correct. However, when I do the same using poly
, I get completely wrong coefficients:
> modelPoly = lm(
formula = Y ~ poly(X, degree = 2),
data = df
)
> modelPoly
Call:
lm(formula = Y ~ poly(X, degree = 2), data = df)
Coefficients:
(Intercept) poly(X, degree = 2)1 poly(X, degree = 2)2
0.048067 0.002358 -0.004766
Why is this please?
As @Limey comments on the post, the problem was orthagonal polynomials. Using
raw = TRUE
inpoly()
fixed the issue.