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!
The poly() command allows us to avoid having to write out a long formula with powers of age . The function returns a matrix whose columns are a basis of orthogonal polynomials, which essentially means that each column is a linear combination of the variables age , age^2 , age^3 and age^4 .
R-squared is a statistical measure of how close the data are to the fitted regression line. It is also known as the coefficient of determination, or the coefficient of multiple determination for multiple regression. 0% indicates that the model explains none of the variability of the response data around its mean.
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
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