Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using lm(poly) to get formula coeff [duplicate]

Tags:

r

regression

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!

like image 599
user2548246 Avatar asked Jul 03 '13 20:07

user2548246


People also ask

What does Poly () do in R?

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 .

What is r squared in polynomial regression?

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.


1 Answers

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 
like image 106
dickoa Avatar answered Nov 15 '22 07:11

dickoa