Andreas Neumann
2005-Jun-29 16:19 UTC
[R] poly() in lm() leads to wrong coefficients (but correct residuals)
Dear all,
I am using poly() in lm() in the following form.
1> DelsDPWOS.lm3 <- lm(DelsPDWOS[,1] ~ poly(DelsPDWOS[,4],3))
2> DelsDPWOS.I.lm3 <- lm(DelsPDWOS[,1] ~ poly(I(DelsPDWOS[,4]),3))
3> DelsDPWOS.2.lm3 <-
lm(DelsPDWOS[,1]~DelsPDWOS[,4]+I(DelsPDWOS[,4]^2)+I(DelsPDWOS[,4]^3))
1 and 2 lead to identical but wrong results. 3 is correct. Surprisingly
(to me) the residuals are the same (correct) in all cases although the
coefficients of 1 (and 2) are wrong and do not in any way match the
stated regression polynomial. (summaries below)
QUESTION:
Is there a correct way to use poly() in lm() since version 3 becomes quite
tedious if used more often especially with higher order polynomials?
I am using R Version 1.9.0 (2004-04-12).
Thank you for your time,
Andreas Neumann
SUMMARIES:> summary(DelsDPWOS.lm3)
Call:
lm(formula = DelsPDWOS[, 1] ~ poly(DelsPDWOS[, 4], 3))
Residuals:
1 2 3 4 5 6 7 8
-0.11414 0.12756 -0.21060 0.04636 -0.03244 0.16030 0.04290 -0.03944
9
0.01949
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.33333 0.04861 27.430 1.21e-06 ***
poly(DelsPDWOS[, 4], 3)1 -1.27464 0.14583 -8.741 0.000325 ***
poly(DelsPDWOS[, 4], 3)2 0.27483 0.14583 1.885 0.118175
poly(DelsPDWOS[, 4], 3)3 0.11590 0.14583 0.795 0.462774
---
Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` '
1
Residual standard error: 0.1458 on 5 degrees of freedom
Multiple R-Squared: 0.9416, Adjusted R-squared: 0.9065
F-statistic: 26.86 on 3 and 5 DF, p-value: 0.001645
> summary(DelsDPWOS.2.lm3)
Call:
lm(formula = DelsPDWOS[, 1] ~ DelsPDWOS[, 4] + I(DelsPDWOS[,
4]^2) + I(DelsPDWOS[, 4]^3))
Residuals:
1 2 3 4 5 6 7 8
-0.11414 0.12756 -0.21060 0.04636 -0.03244 0.16030 0.04290 -0.03944
9
0.01949
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.486 7.242 -0.205 0.846
DelsPDWOS[, 4] 8.640 14.161 0.610 0.568
I(DelsPDWOS[, 4]^2) -6.526 8.799 -0.742 0.492
I(DelsPDWOS[, 4]^3) 1.375 1.730 0.795 0.463
Residual standard error: 0.1458 on 5 degrees of freedom
Multiple R-Squared: 0.9416, Adjusted R-squared: 0.9065
F-statistic: 26.86 on 3 and 5 DF, p-value: 0.001645
Markus Jäntti
2005-Jun-29 16:43 UTC
[R] poly() in lm() leads to wrong coefficients (but correct residuals)
On Wed, 2005-06-29 at 18:19 +0200, Andreas Neumann wrote:> Dear all, > > I am using poly() in lm() in the following form. > > 1> DelsDPWOS.lm3 <- lm(DelsPDWOS[,1] ~ poly(DelsPDWOS[,4],3)) > > 2> DelsDPWOS.I.lm3 <- lm(DelsPDWOS[,1] ~ poly(I(DelsPDWOS[,4]),3)) > > 3> DelsDPWOS.2.lm3 <- > lm(DelsPDWOS[,1]~DelsPDWOS[,4]+I(DelsPDWOS[,4]^2)+I(DelsPDWOS[,4]^3)) > > 1 and 2 lead to identical but wrong results. 3 is correct. Surprisingly > (to me) the residuals are the same (correct) in all cases although the > coefficients of 1 (and 2) are wrong and do not in any way match the > stated regression polynomial. (summaries below) > > QUESTION: > Is there a correct way to use poly() in lm() since version 3 becomes quite > tedious if used more often especially with higher order polynomials? >The coefficients using 1 and 2 are not incorrect. poly() defines orthogonal polynomials, whereas your DelsPDWOS[,4]+I(DelsPDWOS[,4]^2)+I(DelsPDWOS[,4]^3 contruct defines an ordinary polynomial. You should be able to recover version 3 coefficients from 1 and 2. See help(poly)> x <- runif(10) > x[1] 0.1878 0.2415 0.5834 0.6556 0.4112 0.3399 0.8144 0.1134 0.7360 0.0463> model.matrix(~ poly(x, 2))(Intercept) poly(x, 2)1 poly(x, 2)2 1 1 -0.27648 -0.0452 2 1 -0.21052 -0.1899 3 1 0.20937 -0.2708 4 1 0.29799 -0.1021 5 1 -0.00212 -0.4117 6 1 -0.08970 -0.3621 7 1 0.49297 0.4968 8 1 -0.36790 0.2148 9 1 0.39672 0.1620 10 1 -0.45033 0.5082 attr(,"assign") [1] 0 1 1> model.matrix(~ x + I(x^2))(Intercept) x I(x^2) 1 1 0.1878 0.03528 2 1 0.2415 0.05834 3 1 0.5834 0.34040 4 1 0.6556 0.42982 5 1 0.4112 0.16911 6 1 0.3399 0.11554 7 1 0.8144 0.66320 8 1 0.1134 0.01286 9 1 0.7360 0.54169 10 1 0.0463 0.00214 attr(,"assign") [1] 0 1 2>Regards, -- Markus Jantti Abo Akademi University markus.jantti at iki.fi http://www.iki.fi/~mjantti