[R] Puzzling coefficients for linear fitting to polynom

(Ted Harding) Ted.Harding at manchester.ac.uk
Fri Mar 7 09:40:50 CET 2008


On 07-Mar-08 08:16:06, Firas Swidan, PhD wrote:
> Hi,
> I can not comprehend the linear fitting results of polynoms.
> For example, given the following data (representing y = x^2):
> 
>> x <- 1:3
>> y <- c(1, 4, 9)
> 
> performing a linear fit
> 
>> f <- lm(y ~ poly(x, 2))
> 
> gives weird coefficients:
> 
>> coefficients(f)
> (Intercept) poly(x, 2)1 poly(x, 2)2 
>   4.6666667   5.6568542   0.8164966 
> 
> However the fitted() result makes sense:
> 
>> fitted(f)
> 1 2 3 
> 1 4 9 
> 
> This is very confusing. How should one understand the result of
> coefficients()?
> 
> Thanks for any tips,
> Firas.

Have a look at the values returned by poly(x,2).
The coefficients you are getting are the results of fitting

  y = a + b1*poly(x,2)[,1] + b2*poly(x,2)[,2]

where

  poly(x, 2)[,1]
# [1] -7.071068e-01 -9.073264e-17  7.071068e-01
  poly(x, 2)[,2]
# [1]  0.4082483 -0.8164966  0.4082483

which is probably not what you may have thought you were doing!

It is certainly not the same as fitting

  y = a + b1*x + b2*(x^2)

though of course the fitted values will be the same.

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 07-Mar-08                                       Time: 08:40:46
------------------------------ XFMail ------------------------------



More information about the R-help mailing list