lm produces different coefficients in r [duplicate]
I'm trying to using lm(poly) to get a polynomial regression for some points, but got some questions about the regression formula coefficients it returns.
sample like this:
x=seq(1,100) y=x^2+3*x+7 fit=lm(y~poly(x,2))
Results are:
lm(formula = y ~ poly(x, 2))
Coefficients:
(Intercept) poly(x, 2)1 poly(x, 2)2 3542 30021 7452
Why are the coefficients not 7,3,2?
Thank you very much!
You need to set the raw
argument to TRUE of you don't want to use orthogonal polynomial
which is the default
set.seed(101)
N <- 100
x <- rnorm(N, 10, 3)
epsilon <- rnorm(N)
y <- 7 + 3 * x + x^2 + epsilon
coef(lm(y ~ poly(x, 2, raw = TRUE)))
## (Intercept) poly(x, 2, raw = TRUE)1
## 7.8104 2.7538
## poly(x, 2, raw = TRUE)2
## 1.0150
From the help of the poly
function you have
Description:
Returns or evaluates orthogonal polynomials of degree 1 to ‘degree’ over the specified set of points ‘x’. These are all orthogonal to the constant polynomial of degree 0. Alternatively, evaluate raw polynomials.
And
raw: if true, use raw and not orthogonal polynomials.
But you can also use what Ferdinand proposed, it works.
coef(lm(y ~ x + I(x^2)))
## (Intercept) x I(x^2)
## 7.8104 2.7538 1.0150