As regards the scientific interpretability of the fitted polynomial coefficients: In my experience polynomial fits are mainly used to set up empirical nonlinear models where you either don't know the actual functional relationship or it is too complicated to model. And in those situations numerical stability matters.
Let me give you a concrete example, it is about the pH dependence of the activity of a certain enzyme. The scientific details don't matter; suffice to say that the predictor variable is the pH value that measures the "acidity"/"basicity" of the solution, and the response is the activity which measures to what extent the enzyme "speeds up" a biochemical reaction. The relationship between the two is nonlinear and there's no good physicochemical theory to describe it, so biochemists resort to empirical models. For teaching purposes I took Equation 5 from a paper (Becker et al, Biochem. J. (2001) 356, 19–30) and used it to generate some synthetic data:
# Activity of the cellobiohydrolase enzyme as a function of pH
# Empirical formula from Becker et al, Biochem. J. (2001) 356, 19–30
# Equation 5
act.from.ph <- function(ph) {
LOGK <- 2.89 # log(kcat/Km) where kcat/Km is maximal
KA1 <- 6.08e-3 # KA1, KA2 fitted by the authors
KA2 <- 1.02e-6
hconc <- 10.0^(-ph)
return(LOGK - log10(1.0 + hconc/KA1 + KA2/hconc))
}
# generate synthetic data with some Normal noise added
set.seed(42)
ph <- seq(from=2.5, to=7.0, by=0.25)
act <- act.from.ph(ph) + rnorm(length(ph), mean=0.0, sd=0.05)
data <- data.frame(x=ph, y=act)
Now let's fit a 5-degree raw polynomial to this dataset:
raw.fit <- lm(y ~ poly(x, degree=5, raw=T), data)
print(summary(raw.fit))
which gives us the following result (I dropped the formula and residuals output):
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.834e+00 8.351e+00 0.220 0.830
poly(x, degree = 5, raw = T)1 7.837e-01 9.816e+00 0.080 0.938
poly(x, degree = 5, raw = T)2 -2.709e-01 4.477e+00 -0.061 0.953
poly(x, degree = 5, raw = T)3 5.261e-02 9.916e-01 0.053 0.958
poly(x, degree = 5, raw = T)4 -4.110e-03 1.069e-01 -0.038 0.970
poly(x, degree = 5, raw = T)5 -3.145e-05 4.496e-03 -0.007 0.995
Residual standard error: 0.05243 on 13 degrees of freedom
Multiple R-squared: 0.984, Adjusted R-squared: 0.9779
F-statistic: 160.1 on 5 and 13 DF, p-value: 3.34e-11
What we get is a seemingly excellent fit (high $R^2$, significant F-stats etc.), but none of the coefficients seem to be significantly different from zero. If you try higher degrees [an exercise left to the reader], eventually the whole estimation breaks down, with NaN estimates. This is your punishment for using highly correlated predictors ($x, x^2, x^3, \ldots$) that make the columns of your design matrix almost linearly dependent, and the whole problem ill-conditioned.
Now let's perform the same estimation with orthogonal polynomials:
orth.fit <- lm(y ~ poly(x, degree=5, raw=F), data)
print(summary(orth.fit))
which gives us
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.6758702 0.0120294 222.445 < 2e-16 ***
poly(x, degree = 5, raw = F)1 -1.0769949 0.0524348 -20.540 2.71e-11 ***
poly(x, degree = 5, raw = F)2 -0.9789274 0.0524348 -18.669 9.04e-11 ***
poly(x, degree = 5, raw = F)3 -0.2836844 0.0524348 -5.410 0.000119 ***
poly(x, degree = 5, raw = F)4 -0.0491978 0.0524348 -0.938 0.365213
poly(x, degree = 5, raw = F)5 -0.0003668 0.0524348 -0.007 0.994525
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.05243 on 13 degrees of freedom
Multiple R-squared: 0.984, Adjusted R-squared: 0.9779
F-statistic: 160.1 on 5 and 13 DF, p-value: 3.34e-11
Points to note:
- The residual standard error, the $R^2$ values, the F-stats are exactly the same as before. We are essentially "fitting the same thing"...
- ...but differently. Look at the coefficients! The first 3 are now "significant", the rest aren't. So: what is the maximal degree of the polynomials to be used here? [Hint: 3 :-) ]
If you run the estimation with the 3-degree polynomials, you are in for another pleasant surprise:
orth.fit.3 <- lm(y ~ poly(x, degree=3, raw=F), data)
print(summary(orth.fit.3))
Check the coefficients in particular:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.67587 0.01157 231.242 < 2e-16 ***
poly(x, degree = 3, raw = F)1 -1.07699 0.05044 -21.352 1.22e-12 ***
poly(x, degree = 3, raw = F)2 -0.97893 0.05044 -19.408 4.88e-12 ***
poly(x, degree = 3, raw = F)3 -0.28368 0.05044 -5.624 4.84e-05 ***
and you'll see that they are (practically) the same as in the degree=5
case. Which is neat, because you can build an empirical model now very easily: just run an orthogonal polynomial estimation, and keep the coefficients up to the highest that is still "significant".
As regards the model quality:
Residual standard error: 0.05044 on 15 degrees of freedom
Multiple R-squared: 0.9829, Adjusted R-squared: 0.9795
F-statistic: 288.1 on 3 and 15 DF, p-value: 1.768e-13
These numbers differ slightly from the degree=5
case because the degrees of freedom is higher (15 vs 13).
Now you have your 3-degree polynomial empirical model and you can predict enzyme activity as a function of the pH to your heart's content.
Long story short: you are almost always better off by fitting orthogonal polynomials because the procedure is numerically better-conditioned. Should you ever need the raw polynomial coefficients, they can be calculated back from the orthogonal coefficients.
poly
has something to do with orthogonal polynomials and I(x^2) doesn't (though I don't know the details) - but still, why would the authors of ISLR then recommend a method that does not work ? It seems very misleading if both command seem to do the same, but only one actually is ok. $\endgroup$poly
and spent already a while with this problem, but I can't figure out why poly(x,2) and x+I(x^2) make a difference? Could you please enlighten me here in the comments, if the question is offtopic? $\endgroup$R
technicality, that I didn't understand, but now it seems to be a fullblown stat problem that hinders me of doing coding a regression that should not be that difficult to code. $\endgroup$