First, load libraries.
1 2 |
##### Load Goodies ##### library(tidyverse) |
We will use diamonds dataset as an example.
1 2 |
##### Data ##### data <- select(diamonds, price, carat) |
Manual
Let’s say we want to fit a quadratic polynomial. We can manually fit by adding \(carat^2\) in the data.
1 2 3 4 5 |
##### Fit 1 ##### data <- mutate(data, carat_2 = carat^2) fit_manual <- lm(price ~ ., data = data) fit_manual |
1 2 3 4 5 6 7 8 |
> fit_manual Call: lm(formula = price ~ ., data = data) Coefficients: (Intercept) carat carat_2 -1832.6 6677.0 507.9 |
I() Function
But manually adding a new variable may not be the best. Suppose we want to fit 50 predictors manually adding would be a bit too much work. We can use
I() within
lm() .
1 2 3 4 |
##### Fit 2 ##### fit_I <- lm(price ~ carat + I(carat^2), data = data) fit_I |
1 2 3 4 5 6 7 8 |
> fit_I Call: lm(formula = price ~ carat + I(carat^2), data = data) Coefficients: (Intercept) carat I(carat^2) -1832.6 6677.0 507.9 |
Please notice that the results from fit_manual and fit_I are the same.
The semi-manual method is better. But if we want to fit the cubic term for ten variables, that will still be too much work.
Poly() Function
1 2 3 4 |
##### Fit 3 ##### fit_poly <- lm(price ~ poly(carat,2,raw = TRUE), data = data) fit_poly |
1 2 3 4 5 6 7 8 |
> fit_poly Call: lm(formula = price ~ poly(carat, 2, raw = TRUE), data = data) Coefficients: (Intercept) poly(carat, 2, raw = TRUE)1 poly(carat, 2, raw = TRUE)2 -1832.6 6677.0 507.9 |
Yep, the results of those methods are the same.
Raw VS Orthogonal
Using
poly() without
raw = TRUE will yield a different result. The default value is
FALSE , which will calculate orthogonal polynomial.
1 2 3 4 |
##### Orthogonal ##### fit_poly_o <- lm(price ~ poly(carat,2,raw = FALSE), data = data) fit_poly_o |
1 2 3 4 5 6 7 8 |
> fit_poly Call: lm(formula = price ~ poly(carat, 2, raw = TRUE), data = data) Coefficients: (Intercept) poly(carat, 2, raw = TRUE)1 poly(carat, 2, raw = TRUE)2 -1832.6 6677.0 507.9 |
Long story short, an orthogonal polynomial is better. To interpret the orthogonal, polynomial doesn’t need all the predictors and can unveil subtle relationship between predictors. Professor Archim Zeileis has well explained the difference here.