To get the result that you were expecting, use the following (which uses the
raw polynomial a + bx + cx^2 rather than the orthogonal polynomial of degree
2):
lm(y~poly(x,2, raw=TRUE))
Ravi.
----------------------------------------------------------------------------
-------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
Webpage:
http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h
tml
----------------------------------------------------------------------------
--------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of Gabor Grothendieck
Sent: Wednesday, June 10, 2009 6:37 AM
To: Ning Ma
Cc: r-help at r-project.org
Subject: Re: [R] poly regression
They have different coefficients because their model matrices are different
but they both lead to the same predictions:
> fitted(lm(y~1+x+I(x^2)))
1 2 3 4 5 6 7 8 9 10
1 4 9 16 25 36 49 64 81 100
> fitted(lm(y~poly(x,2)))
1 2 3 4 5 6 7 8 9 10
1 4 9 16 25 36 49 64 81 100
On Wed, Jun 10, 2009 at 1:41 AM, Ning Ma<pningma at gmail.com>
wrote:> hi,
>
> I want to do a polynomial regression of y on x of degree 2, as
> following
>
>> x<-1:10
>> y<-x^2
>> lm(y~poly(x,2))
>
> Call:
> lm(formula = y ~ poly(x, 2))
>
> Coefficients:
> (Intercept) ?poly(x, 2)1 ?poly(x, 2)2
> ? ? ?38.50 ? ? ? ?99.91 ? ? ? ?22.98
>
> Which is not what i had expected.
>
> If I wrote the expression in an explicit form, y~1+x+I(x^2), I could
> get the expected result:
>
>> lm(y~1+x+I(x^2))
>
> Call:
> lm(formula = y ~ 1 + x + I(x^2))
>
> Coefficients:
> (Intercept) ? ? ? ? ? ?x ? ? ? I(x^2)
> ? ? ? ? ?0 ? ? ? ? ? ?0 ? ? ? ? ? ?1
>
> What is the diff between them?
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.