[R] fitting a quadratic function - poly?

Stefan Uhmann stefan.uhmann at googlemail.com
Wed Apr 14 17:12:51 CEST 2010


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(fit1$coefficients, 2)
round(fit2$coefficients, 2)

Thanks in advance,
Stefan



More information about the R-help mailing list