[R] Odp: fitting a quadratic function - poly?

Petr PIKAL petr.pikal at precheza.cz
Wed Apr 14 17:50:58 CEST 2010


Hi

r-help-bounces at r-project.org napsal dne 14.04.2010 17:12:51:

> Hi List,
> 
> I can not get my head around the following problem. I want to fit a 
> quadratic function to some data and stumbled across poly(). What exactly 

> does it, i.e. why are there different results for fit1 and fit2?
> 
> x = seq(-10, 10)
> y = x^2
> 
> fit1 = lm(y ~ x + I(x^2))
> fit2 = lm(y ~ poly(x, 2))
> 
> plot(x,y)
> lines(x, fit1$fitted.values, col = 2)
> lines(x, fit2$fitted.values, col = 3)

> round(fitted(fit1)-fitted(fit2),5)
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 
 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 

results are same.

> 
> round(fit1$coefficients, 2)
> round(fit2$coefficients, 2)

Coefficients are different as you fit different values. See

?poly

poly(-10:10,2)

I believe that others give you better explanation. So you can not use 
coefficients evaluated by lm(.~poly(...)) directly.

Regards
Petr

> 
> Thanks in advance,
> Stefan
> 
> ______________________________________________
> 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.



More information about the R-help mailing list