Page 354 - Applied Statistics with R
P. 354

354                              CHAPTER 14. TRANSFORMATIONS


                                 ## Model 1: y ~ poly(x, 2)
                                 ## Model 2: y ~ poly(x, 4)
                                 ##   Res.Df     RSS Df Sum of Sq       F    Pr(>F)
                                 ## 1     247 2334.1
                                 ## 2     245 1912.6  2    421.51 26.997 2.536e-11 ***
                                 ## ---
                                 ## Signif. codes:   0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

                                 fit_6 = lm(y ~ poly(x, 6), data = data_higher)

                                 anova(fit_4, fit_6)


                                 ## Analysis of Variance Table
                                 ##
                                 ## Model 1: y ~ poly(x, 4)
                                 ## Model 2: y ~ poly(x, 6)
                                 ##   Res.Df     RSS Df Sum of Sq       F Pr(>F)
                                 ## 1     245 1912.6
                                 ## 2     243 1904.4  2    8.1889 0.5224 0.5937


                                 14.2.5   poly() Function and Orthogonal Polynomials

                                                                                 4
                                                                          3
                                                                    2
                                                    =    +       +       +       +       +      
                                                       0
                                                                  2   
                                                           1   
                                                                               4   
                                                     
                                                                        3   
                                 fit_4a = lm(y ~ poly(x, degree = 4), data = data_higher)
                                 fit_4b = lm(y ~ poly(x, degree = 4, raw = TRUE), data = data_higher)
                                 fit_4c = lm(y ~ x + I(x^2) + I(x^3) + I(x^4), data = data_higher)
                                 coef(fit_4a)
                                 ##           (Intercept) poly(x, degree = 4)1 poly(x, degree = 4)2
                                 ##             -1.980036             -2.053929            -49.344752
                                 ## poly(x, degree = 4)3 poly(x, degree = 4)4
                                 ##              0.669874             20.519759

                                 coef(fit_4b)


                                 ##                       (Intercept) poly(x, degree = 4, raw = TRUE)1
                                 ##                         2.9996256                         -0.3880250
                                 ## poly(x, degree = 4, raw = TRUE)2 poly(x, degree = 4, raw = TRUE)3
                                 ##                        -6.1511166                          0.1269046
                                 ## poly(x, degree = 4, raw = TRUE)4
                                 ##                         1.0282139
   349   350   351   352   353   354   355   356   357   358   359