7.8.1

7.8.2

library(splines)
library(ISLR)
attach(Wage)
agelims =range(age)
age.grid = seq(from=agelims[1],to=agelims[2])
fit = lm(wage~bs(age,knots=c(25,40,60)), data=Wage)
fit2 = lm(wage~bs(age,df=2,knots=NULL), data=Wage)
## Warning in bs(age, df = 2, knots = NULL): 'df' was too small; have used 3
fit3 = lm(wage~bs(age,df=6,knots=NULL), data=Wage)
pred = predict(fit, newdata=list(age=age.grid), se=T)
pred2 = predict(fit2, newdata=list(age=age.grid), se=T)
pred3 = predict(fit3, newdata=list(age=age.grid), se=T)


fit.sum = summary(fit)
fit2.sum = summary(fit2)
fit3.sum = summary(fit3)


fit.sum
## 
## Call:
## lm(formula = wage ~ bs(age, knots = c(25, 40, 60)), data = Wage)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -98.832 -24.537  -5.049  15.209 203.207 
## 
## Coefficients:
##                                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                       60.494      9.460   6.394 1.86e-10 ***
## bs(age, knots = c(25, 40, 60))1    3.980     12.538   0.317 0.750899    
## bs(age, knots = c(25, 40, 60))2   44.631      9.626   4.636 3.70e-06 ***
## bs(age, knots = c(25, 40, 60))3   62.839     10.755   5.843 5.69e-09 ***
## bs(age, knots = c(25, 40, 60))4   55.991     10.706   5.230 1.81e-07 ***
## bs(age, knots = c(25, 40, 60))5   50.688     14.402   3.520 0.000439 ***
## bs(age, knots = c(25, 40, 60))6   16.606     19.126   0.868 0.385338    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 39.92 on 2993 degrees of freedom
## Multiple R-squared:  0.08642,    Adjusted R-squared:  0.08459 
## F-statistic: 47.19 on 6 and 2993 DF,  p-value: < 2.2e-16
fit2.sum
## 
## Call:
## lm(formula = wage ~ bs(age, df = 2, knots = NULL), data = Wage)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -99.693 -24.562  -5.222  15.096 206.119 
## 
## Coefficients:
##                                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                      58.689      4.013  14.625  < 2e-16 ***
## bs(age, df = 2, knots = NULL)1  102.644     11.449   8.965  < 2e-16 ***
## bs(age, df = 2, knots = NULL)2   48.762      8.625   5.654 1.72e-08 ***
## bs(age, df = 2, knots = NULL)3   40.803     12.109   3.370 0.000762 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 39.93 on 2996 degrees of freedom
## Multiple R-squared:  0.0851, Adjusted R-squared:  0.08419 
## F-statistic: 92.89 on 3 and 2996 DF,  p-value: < 2.2e-16
fit3.sum
## 
## Call:
## lm(formula = wage ~ bs(age, df = 6, knots = NULL), data = Wage)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -99.681 -24.403  -5.202  15.441 201.413 
## 
## Coefficients:
##                                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                      56.314      7.258   7.759 1.17e-14 ***
## bs(age, df = 6, knots = NULL)1   27.824     12.435   2.238   0.0253 *  
## bs(age, df = 6, knots = NULL)2   54.063      7.127   7.585 4.41e-14 ***
## bs(age, df = 6, knots = NULL)3   65.828      8.323   7.909 3.62e-15 ***
## bs(age, df = 6, knots = NULL)4   55.813      8.724   6.398 1.83e-10 ***
## bs(age, df = 6, knots = NULL)5   72.131     13.745   5.248 1.65e-07 ***
## bs(age, df = 6, knots = NULL)6   14.751     16.209   0.910   0.3629    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 39.91 on 2993 degrees of freedom
## Multiple R-squared:  0.08729,    Adjusted R-squared:  0.08546 
## F-statistic: 47.71 on 6 and 2993 DF,  p-value: < 2.2e-16
anova(fit,fit2,fit3)
## Analysis of Variance Table
## 
## Model 1: wage ~ bs(age, knots = c(25, 40, 60))
## Model 2: wage ~ bs(age, df = 2, knots = NULL)
## Model 3: wage ~ bs(age, df = 6, knots = NULL)
##   Res.Df     RSS Df Sum of Sq      F  Pr(>F)  
## 1   2993 4770777                              
## 2   2996 4777674 -3   -6897.3 1.4424 0.22847  
## 3   2993 4766244  3   11429.9 2.3902 0.06687 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1