Team Project 3

Team Members: Esubalew Milam, Aubrey Cook, Nadia Shchetnikova, Daniel Zhang


Question 1:

NFL = read.csv("NFL Team Scoring.csv")
NFL = NFL[, -1]
summary(NFL)
      Year           PTS              TA              PAVG      
 Min.   :2009   Min.   :10.90   Min.   :0.9375   Min.   :5.100  
 1st Qu.:2009   1st Qu.:18.77   1st Qu.:1.3750   1st Qu.:6.500  
 Median :2010   Median :22.20   Median :1.6250   Median :7.000  
 Mean   :2010   Mean   :21.90   Mean   :1.6504   Mean   :7.042  
 3rd Qu.:2011   3rd Qu.:24.60   3rd Qu.:1.8906   3rd Qu.:7.700  
 Max.   :2011   Max.   :35.00   Max.   :2.5000   Max.   :9.300  
       TO             SCK              RAVG             FD       
 Min.   :0.625   Min.   :0.8125   Min.   :3.300   Min.   :14.10  
 1st Qu.:1.375   1st Qu.:1.7969   1st Qu.:4.000   1st Qu.:17.48  
 Median :1.594   Median :2.1875   Median :4.200   Median :18.95  
 Mean   :1.655   Mean   :2.2259   Mean   :4.231   Mean   :19.02  
 3rd Qu.:1.938   3rd Qu.:2.6875   3rd Qu.:4.400   3rd Qu.:20.70  
 Max.   :2.625   Max.   :3.5000   Max.   :5.400   Max.   :26.00  
      P20             R20              COMP            THRD      
 Min.   :1.562   Min.   :0.1875   Min.   :49.40   Min.   :25.80  
 1st Qu.:2.500   1st Qu.:0.4375   1st Qu.:57.58   1st Qu.:34.02  
 Median :2.812   Median :0.6875   Median :60.45   Median :37.65  
 Mean   :3.024   Mean   :0.7201   Mean   :60.36   Mean   :38.33  
 3rd Qu.:3.578   3rd Qu.:0.8750   3rd Qu.:62.95   3rd Qu.:41.73  
 Max.   :4.500   Max.   :1.6875   Max.   :71.30   Max.   :56.70  
       PA              RA              RY              PY       
 Min.   :24.60   Min.   :20.00   Min.   : 80.9   Min.   :129.8  
 1st Qu.:31.20   1st Qu.:25.20   1st Qu.:100.7   1st Qu.:192.3  
 Median :33.95   Median :27.00   Median :114.5   Median :219.0  
 Mean   :33.67   Mean   :27.33   Mean   :116.1   Mean   :223.2  
 3rd Qu.:36.02   3rd Qu.:29.10   3rd Qu.:127.0   3rd Qu.:253.9  
 Max.   :42.40   Max.   :37.90   Max.   :172.3   Max.   :334.2  
attach(NFL)

The NFL Team Scoring dataset contains 96 observations and 16 variables. After removing the team name column, the data includes PTS (points scored per game) as our response variable, along with offensive and defensive statistics such as passing yards (PY), rushing yards (RY), turnovers (TO), sacks (SCK), completion percentage (COMP), and others. Points per game range from 10.9 to 35.0, with a mean of 21.9.


Question 2:

library(glmnet)
Warning: package 'glmnet' was built under R version 4.5.3
Loading required package: Matrix
Loaded glmnet 5.0
x = model.matrix(PTS ~ .^2, NFL)[, -1]
y = PTS
grid = exp(1)^seq(10, -5, length = 100)

set.seed(300)
cv.out.r = cv.glmnet(x, y, lambda = grid, alpha = 0)
plot(cv.out.r)

bestlam.r = cv.out.r$lambda.min
bestlam.r
[1] 9.705835
min.r = which.min(cv.out.r$cvm)
cv.out.r$cvm[min.r]
[1] 4.476375
ridge.final = glmnet(x, y, alpha = 0, lambda = bestlam.r)
ridge.coef  = predict(ridge.final, type = "coefficients", s = bestlam.r)
sum(ridge.coef[-1] != 0)
[1] 120

Ridge regression was applied using 10-fold cross-validation across 100 lambda values. The best lambda was 9.71, producing a minimum CV MSE of 4.48. Because ridge regression never sets coefficients to exactly zero, all 120 predictors (main effects + two-way interactions) remained in the model. The plot shows MSE decreasing as lambda decreases from very large values, then stabilizing near the optimal point.


Question 3:

set.seed(300)
cv.out.l = cv.glmnet(x, y, lambda = grid, alpha = 1)
plot(cv.out.l)

bestlam.l = cv.out.l$lambda.min
bestlam.l
[1] 0.07609615
min.l = which.min(cv.out.l$cvm)
cv.out.l$cvm[min.l]
[1] 4.638889
lasso.final = glmnet(x, y, alpha = 1, lambda = bestlam.l)
lasso.coef  = predict(lasso.final, type = "coefficients", s = bestlam.l)
sum(lasso.coef[-1] != 0)
[1] 19

LASSO was applied with the same grid and 10-fold CV. The best lambda was 0.076, producing a minimum CV MSE of 4.64. Unlike ridge, LASSO shrinks some coefficients all the way to zero — it reduced the model from 120 predictors down to just 19, automatically selecting the most relevant variables. This makes LASSO much easier to interpret than ridge, though its MSE was slightly higher here.


Question 4:

library(pls)
Warning: package 'pls' was built under R version 4.5.3

Attaching package: 'pls'
The following object is masked from 'package:stats':

    loadings
set.seed(300)
pcr.fit = pcr(PTS ~ .^2, data = NFL, scale = T, validation = "CV")
summary(pcr.fit)
Data:   X dimension: 96 120 
    Y dimension: 96 1
Fit method: svdpc
Number of components considered: 85

VALIDATION: RMSEP
Cross-validated using 10 random segments.
       (Intercept)  1 comps  2 comps  3 comps  4 comps  5 comps  6 comps
CV           4.871    2.294    2.321    2.286    2.124    2.044    2.067
adjCV        4.871    2.287    2.315    2.292    2.114    2.036    2.058
       7 comps  8 comps  9 comps  10 comps  11 comps  12 comps  13 comps
CV       2.093    2.110    2.142     2.158     2.188     2.212     2.179
adjCV    2.083    2.099    2.130     2.148     2.171     2.195     2.161
       14 comps  15 comps  16 comps  17 comps  18 comps  19 comps  20 comps
CV        2.168     2.176     2.197     2.217     2.355     2.384     2.469
adjCV     2.150     2.156     2.175     2.194     2.324     2.351     2.407
       21 comps  22 comps  23 comps  24 comps  25 comps  26 comps  27 comps
CV        2.448     2.372     2.385     2.385     2.403     2.407     2.452
adjCV     2.397     2.325     2.341     2.344     2.361     2.364     2.405
       28 comps  29 comps  30 comps  31 comps  32 comps  33 comps  34 comps
CV        2.542     2.537     2.514     2.550     2.585     2.588     2.614
adjCV     2.490     2.486     2.460     2.491     2.528     2.535     2.557
       35 comps  36 comps  37 comps  38 comps  39 comps  40 comps  41 comps
CV        2.703     2.731     2.809     2.828     2.878     2.836     2.846
adjCV     2.642     2.668     2.743     2.760     2.807     2.774     2.765
       42 comps  43 comps  44 comps  45 comps  46 comps  47 comps  48 comps
CV        2.828     2.937     2.917     2.957     2.997     2.990     3.037
adjCV     2.749     2.849     2.833     2.874     2.910     2.913     2.951
       49 comps  50 comps  51 comps  52 comps  53 comps  54 comps  55 comps
CV        3.012     3.027     3.003     3.059     2.988     2.984     3.000
adjCV     2.935     2.943     2.914     2.957     2.898     2.889     2.903
       56 comps  57 comps  58 comps  59 comps  60 comps  61 comps  62 comps
CV        3.006     3.188     3.252     3.339     3.257     3.250     3.272
adjCV     2.910     3.084     3.146     3.229     3.157     3.159     3.187
       63 comps  64 comps  65 comps  66 comps  67 comps  68 comps  69 comps
CV        3.354     3.353     3.378     3.466     3.687     3.878     3.918
adjCV     3.237     3.236     3.254     3.340     3.549     3.729     3.767
       70 comps  71 comps  72 comps  73 comps  74 comps  75 comps  76 comps
CV        4.039     4.377     4.556     4.877     5.018     5.474     5.573
adjCV     3.876     4.197     4.367     4.672     4.808     5.241     5.337
       77 comps  78 comps  79 comps  80 comps  81 comps  82 comps  83 comps
CV        5.655     5.826     5.800     5.869     6.326     7.837     7.349
adjCV     5.418     5.580     5.554     5.626     6.057     7.470     7.007
       84 comps  85 comps
CV        7.945     11.52
adjCV     7.567     10.93

TRAINING: % variance explained
     1 comps  2 comps  3 comps  4 comps  5 comps  6 comps  7 comps  8 comps
X      34.63    56.23    67.26    77.52    85.26    89.54    92.63    94.79
PTS    78.27    78.48    80.15    82.50    84.27    84.27    84.27    84.32
     9 comps  10 comps  11 comps  12 comps  13 comps  14 comps  15 comps
X      96.38     97.34     98.22     98.83     99.16     99.34     99.47
PTS    84.33     84.33     84.63     84.65     85.26     85.53     85.87
     16 comps  17 comps  18 comps  19 comps  20 comps  21 comps  22 comps
X       99.56     99.63     99.69     99.73     99.76     99.79     99.81
PTS     85.88     86.00     86.02     86.13     87.41     87.59     87.97
     23 comps  24 comps  25 comps  26 comps  27 comps  28 comps  29 comps
X       99.83     99.85     99.87     99.88     99.90     99.91     99.91
PTS     87.98     87.98     88.07     88.38     88.54     88.70     88.79
     30 comps  31 comps  32 comps  33 comps  34 comps  35 comps  36 comps
X       99.92     99.93     99.94     99.94     99.95     99.95     99.96
PTS     89.05     89.28     89.30     89.32     89.47     89.57     89.67
     37 comps  38 comps  39 comps  40 comps  41 comps  42 comps  43 comps
X       99.96     99.97     99.97     99.97     99.97     99.98     99.98
PTS     89.75     89.86     90.04     90.20     90.78     90.85     90.99
     44 comps  45 comps  46 comps  47 comps  48 comps  49 comps  50 comps
X       99.98     99.98     99.99     99.99     99.99     99.99     99.99
PTS     90.99     91.02     91.14     91.14     91.52     91.57     91.95
     51 comps  52 comps  53 comps  54 comps  55 comps  56 comps  57 comps
X       99.99     99.99     99.99     99.99     99.99    100.00    100.00
PTS     92.28     92.69     92.70     92.91     93.00     93.09     93.14
     58 comps  59 comps  60 comps  61 comps  62 comps  63 comps  64 comps
X      100.00    100.00    100.00    100.00    100.00     100.0    100.00
PTS     93.14     93.24     93.28     93.35     93.36      94.2     94.32
     65 comps  66 comps  67 comps  68 comps  69 comps  70 comps  71 comps
X      100.00    100.00    100.00    100.00    100.00    100.00    100.00
PTS     94.53     94.53     94.59     94.71     94.74     94.97     94.99
     72 comps  73 comps  74 comps  75 comps  76 comps  77 comps  78 comps
X      100.00    100.00    100.00    100.00    100.00    100.00    100.00
PTS     95.02     95.14     95.14     95.18     95.29     95.48     95.48
     79 comps  80 comps  81 comps  82 comps  83 comps  84 comps  85 comps
X      100.00    100.00    100.00     100.0     100.0    100.00    100.00
PTS     96.03     96.05     96.53      97.6      97.6     97.79     98.21
validationplot(pcr.fit, ncomp = 1:85, val.type = "MSEP", legendpos = "topright")

msep.pcr     = MSEP(pcr.fit)$val[1, 1, ]
min.comp.pcr = which.min(msep.pcr) - 1
min.mse.pcr  = min(msep.pcr)
cat("PCR - Number of components at min MSE:", min.comp.pcr, "\n")
PCR - Number of components at min MSE: 5 
cat("PCR - Minimum CV MSE:", min.mse.pcr, "\n")
PCR - Minimum CV MSE: 4.179948 

PCR reduces the 120 predictors into a smaller set of uncorrelated principal components. The minimum CV MSE of 4.18 was achieved at just 5 components — meaning 5 linear combinations of the original predictors captured most of the useful information. This is the best MSE of all four methods. The validation plot shows MSE drops sharply in the first few components then rises again, confirming 5 is the sweet spot.


Question 5:

set.seed(300)
pls.fit = plsr(PTS ~ .^2, data = NFL, scale = T, validation = "CV")
summary(pls.fit)
Data:   X dimension: 96 120 
    Y dimension: 96 1
Fit method: kernelpls
Number of components considered: 85

VALIDATION: RMSEP
Cross-validated using 10 random segments.
       (Intercept)  1 comps  2 comps  3 comps  4 comps  5 comps  6 comps
CV           4.871    2.168    2.079    2.096    2.156    2.272    2.276
adjCV        4.871    2.164    2.070    2.087    2.142    2.241    2.250
       7 comps  8 comps  9 comps  10 comps  11 comps  12 comps  13 comps
CV       2.205    2.257    2.258     2.322     2.428     2.537     2.638
adjCV    2.179    2.227    2.231     2.272     2.372     2.486     2.568
       14 comps  15 comps  16 comps  17 comps  18 comps  19 comps  20 comps
CV        2.651     2.634     2.672     2.732      2.94     3.011     3.096
adjCV     2.582     2.557     2.609     2.662      2.85     2.915     2.993
       21 comps  22 comps  23 comps  24 comps  25 comps  26 comps  27 comps
CV        3.031     3.131     3.198     3.238     3.372     3.473     3.529
adjCV     2.932     3.027     3.090     3.127     3.248     3.342     3.397
       28 comps  29 comps  30 comps  31 comps  32 comps  33 comps  34 comps
CV        3.635     3.832     3.945     4.053     4.229     4.366     4.500
adjCV     3.496     3.680     3.786     3.887     4.053     4.182     4.307
       35 comps  36 comps  37 comps  38 comps  39 comps  40 comps  41 comps
CV        4.637     4.914     5.138     5.307     5.457     5.563     5.735
adjCV     4.437     4.697     4.905     5.063     5.201     5.298     5.459
       42 comps  43 comps  44 comps  45 comps  46 comps  47 comps  48 comps
CV        5.910     5.998     6.053     6.150     6.193     6.176     6.277
adjCV     5.622     5.705     5.756     5.847     5.887     5.870     5.966
       49 comps  50 comps  51 comps  52 comps  53 comps  54 comps  55 comps
CV        6.378     6.500     6.666      6.80     6.968     7.224     7.394
adjCV     6.061     6.177     6.334      6.46     6.619     6.861     7.022
       56 comps  57 comps  58 comps  59 comps  60 comps  61 comps  62 comps
CV        7.475     7.514     7.634     7.809     7.954     8.077     8.216
adjCV     7.098     7.135     7.248     7.414     7.551     7.667     7.799
       63 comps  64 comps  65 comps  66 comps  67 comps  68 comps  69 comps
CV        8.315     8.467     8.639     8.917     9.162     9.412     9.785
adjCV     7.893     8.036     8.200     8.463     8.693     8.929     9.282
       70 comps  71 comps  72 comps  73 comps  74 comps  75 comps  76 comps
CV       10.096    10.388     10.83     11.06     11.20     11.38     11.48
adjCV     9.575     9.851     10.27     10.49     10.62     10.79     10.88
       77 comps  78 comps  79 comps  80 comps  81 comps  82 comps  83 comps
CV        11.61     11.78     11.87     11.98     12.06     12.12     12.16
adjCV     11.01     11.17     11.25     11.36     11.43     11.49     11.52
       84 comps  85 comps
CV        12.16     12.16
adjCV     11.53     11.53

TRAINING: % variance explained
     1 comps  2 comps  3 comps  4 comps  5 comps  6 comps  7 comps  8 comps
X      34.48    45.12    63.28    73.26    76.19    84.99    87.85    90.18
PTS    81.30    84.36    84.55    84.83    85.65    85.85    86.32    86.78
     9 comps  10 comps  11 comps  12 comps  13 comps  14 comps  15 comps
X      92.77     93.63     94.84     97.49     97.98     98.38     98.55
PTS    87.10     87.92     88.50     88.66     89.39     89.80     90.26
     16 comps  17 comps  18 comps  19 comps  20 comps  21 comps  22 comps
X        99.4     99.53     99.58     99.63     99.68     99.72     99.75
PTS      90.4     91.17     91.86     92.33     92.77     93.15     93.46
     23 comps  24 comps  25 comps  26 comps  27 comps  28 comps  29 comps
X       99.78      99.8     99.81     99.82     99.84     99.86     99.87
PTS     93.79      94.1     94.51     94.75     94.91     95.11     95.34
     30 comps  31 comps  32 comps  33 comps  34 comps  35 comps  36 comps
X       99.88     99.89     99.90     99.91     99.92     99.93     99.94
PTS     95.53     95.70     95.84     95.99     96.12     96.28     96.47
     37 comps  38 comps  39 comps  40 comps  41 comps  42 comps  43 comps
X       99.94     99.94     99.95     99.95     99.95     99.96     99.96
PTS     96.80     97.05     97.34     97.62     97.82     97.98     98.18
     44 comps  45 comps  46 comps  47 comps  48 comps  49 comps  50 comps
X       99.96     99.97     99.97     99.97     99.98     99.98     99.98
PTS     98.33     98.45     98.61     98.71     98.77     98.83     98.90
     51 comps  52 comps  53 comps  54 comps  55 comps  56 comps  57 comps
X       99.98     99.98     99.99     99.99     99.99     99.99     99.99
PTS     98.96     99.05     99.11     99.16     99.21     99.27     99.31
     58 comps  59 comps  60 comps  61 comps  62 comps  63 comps  64 comps
X       99.99     99.99     99.99     99.99    100.00    100.00    100.00
PTS     99.34     99.36     99.41     99.42     99.44     99.47     99.48
     65 comps  66 comps  67 comps  68 comps  69 comps  70 comps  71 comps
X       100.0    100.00    100.00    100.00    100.00    100.00     100.0
PTS      99.5     99.51     99.52     99.54     99.56     99.57      99.6
     72 comps  73 comps  74 comps  75 comps  76 comps  77 comps  78 comps
X      100.00    100.00    100.00    100.00    100.00    100.00    100.00
PTS     99.61     99.65     99.67     99.69     99.71     99.74     99.75
     79 comps  80 comps  81 comps  82 comps  83 comps  84 comps  85 comps
X      100.00     100.0    100.00    100.00    100.00    100.00    100.00
PTS     99.79      99.8     99.81     99.83     99.86     99.86     99.88
validationplot(pls.fit, val.type = "MSEP", legendpos = "topright")

msep.pls     = MSEP(pls.fit)$val[1, 1, ]
min.comp.pls = which.min(msep.pls) - 1
min.mse.pls  = min(msep.pls)
cat("PLS - Number of components at min MSE:", min.comp.pls, "\n")
PLS - Number of components at min MSE: 2 
cat("PLS - Minimum CV MSE:", min.mse.pls, "\n")
PLS - Minimum CV MSE: 4.324006 

PLS is similar to PCR but constructs components using both the predictors and the response (PTS), making each component more directly useful for prediction. It achieved its minimum CV MSE at fewer components than PCR, demonstrating that response-guided dimension reduction is more efficient. The summary output shows RMSEP (root MSE) declining with added components before leveling off.


Summary Comparison

summary_table = data.frame(
  Method = c("Ridge", "LASSO", "PCR", "PLS"),
  Min_CV_MSE = c(cv.out.r$cvm[min.r],
                 cv.out.l$cvm[min.l],
                 min.mse.pcr,
                 min.mse.pls),
  Best_Tuning_Param = c(
    paste("lambda =", round(bestlam.r, 4)),
    paste("lambda =", round(bestlam.l, 4)),
    paste("components =", min.comp.pcr),
    paste("components =", min.comp.pls)
  )
)
knitr::kable(summary_table, caption = "Model Comparison: Minimum CV MSE")
Model Comparison: Minimum CV MSE
Method Min_CV_MSE Best_Tuning_Param
Ridge 4.476375 lambda = 9.7058
LASSO 4.638890 lambda = 0.0761
PCR 4.179948 components = 5
PLS 4.324006 components = 2

Across all four methods, PCR achieved the lowest CV MSE (4.18) using only 5 components. Ridge and LASSO performed similarly in terms of MSE, but LASSO is far more interpretable by retaining only 19 of 120 predictors. PLS offers a balance, fewer components than PCR with competitive accuracy. For prediction accuracy, PCR wins; for interpretability, LASSO wins.

Question 6:

library(randomForest)
Warning: package 'randomForest' was built under R version 4.5.3
randomForest 4.7-1.2
Type rfNews() to see new features/changes/bug fixes.
set.seed(300)
bag.fit=randomForest(PTS~., NFL, mtry=15, ntree=5000, importance=T)
bag.fit

Call:
 randomForest(formula = PTS ~ ., data = NFL, mtry = 15, ntree = 5000,      importance = T) 
               Type of random forest: regression
                     Number of trees: 5000
No. of variables tried at each split: 15

          Mean of squared residuals: 6.610999
                    % Var explained: 71.55
plot(bag.fit$mse)

plot(bag.fit$mse[1000:5000])

5,000 trees are not sufficient to reach a plateu; the graph shows high fluctuation even at 5,000 trees.

The resulting MSE of the model is 6.611.

bag.fit$mse[5000]
[1] 6.610999

Question 7:

varImpPlot(bag.fit)

partialPlot(bag.fit,NFL,FD)

Question 8:

set.seed(300)
bag.fit1=randomForest(PTS~., NFL, mtry=5, ntree=5000, importance=T)
bag.fit1

Call:
 randomForest(formula = PTS ~ ., data = NFL, mtry = 5, ntree = 5000,      importance = T) 
               Type of random forest: regression
                     Number of trees: 5000
No. of variables tried at each split: 5

          Mean of squared residuals: 6.108013
                    % Var explained: 73.71
plot(bag.fit1$mse)

plot(bag.fit1$mse[1000:5000])

5,000 trees are not sufficient to reach a plateau; the graph shows high fluctuation even at 5,000 trees.

The resulting MSE of the model is 6.108 (lower than the previous model with mtry = 15).

bag.fit1$mse[5000]
[1] 6.108013

Question 9:

library(gbm)
Warning: package 'gbm' was built under R version 4.5.3
Loaded gbm 2.2.3
This version of gbm is no longer under development. Consider transitioning to gbm3, https://github.com/gbm-developers/gbm3
set.seed(300)
gbm.fit=gbm(PTS~.,data=NFL,distribution="gaussian",n.trees=10000,interaction.depth=1,shrinkage=0.01,cv.folds=5)
plot(gbm.fit$cv.error)

plot(gbm.fit$cv.error[1000:10000])

The y-axis range of this plot makes it difficult to differentiate the minimum mse point from other points close to the minimum.

The minimum test MSE occurs at 4,718 trees and has a value of 5.05 (the lowest MSE so far).

min.n=which.min(gbm.fit$cv.error)
min.n
[1] 4718
gbm.fit$cv.error[min.n]
[1] 5.04952

Question 10:

summary(gbm.fit, min.n, las=1)

      var    rel.inf
FD     FD 22.8020050
PAVG PAVG 13.2670156
TA     TA 10.5385469
PY     PY  7.6143880
THRD THRD  7.3123765
COMP COMP  6.6261006
TO     TO  5.3845372
RY     RY  5.1841922
P20   P20  5.1549510
RA     RA  4.2577190
SCK   SCK  4.1261189
PA     PA  3.3306449
R20   R20  2.0824099
RAVG RAVG  1.4335399
Year Year  0.8854545
plot(gbm.fit, min.n, i="FD")

Question 11:

library(gbm)
set.seed(300)
gbm.fit2 = gbm(PTS~., data=NFL, distribution="gaussian", n.trees=10000,
               interaction.depth=2, shrinkage=0.01, cv.folds=5)
plot(gbm.fit2$cv.error)

plot(gbm.fit2$cv.error[1000:10000])

min.n2 = which.min(gbm.fit2$cv.error)
min.n2
[1] 2448
gbm.fit2$cv.error[min.n2]
[1] 5.583418
summary(gbm.fit2, min.n2, las=1)

      var    rel.inf
FD     FD 23.9004916
PAVG PAVG 13.0800572
TA     TA 10.6390082
PY     PY  9.2795725
THRD THRD  6.8806183
COMP COMP  6.8023852
TO     TO  5.1214795
P20   P20  4.9194088
RY     RY  4.3321599
RA     RA  4.2624045
SCK   SCK  4.0033664
PA     PA  2.8855717
R20   R20  1.9881341
RAVG RAVG  1.2167642
Year Year  0.6885778
plot(gbm.fit2, min.n2, i="FD")

The test MSE is 5.583.

gbm.fit2$cv.error[min.n2]
[1] 5.583418

Question 12:

pr.pass = prcomp(NFL[,c(3,8,12,15)], scale=TRUE)
pr.pass$rotation
             PC1         PC2        PC3         PC4
TA   -0.04786066 -0.81732521 -0.5737121  0.02330899
FD   -0.69284264  0.08729075 -0.0953797 -0.70940264
THRD -0.68715713  0.14024955 -0.1138890  0.70368633
RY   -0.21329209 -0.55198723  0.8054728  0.03209562
dim(pr.pass$x)
[1] 96  4
pr.rush = prcomp(NFL[,c(6,9,13,14)], scale=TRUE)
pr.rush$rotation
           PC1          PC2        PC3        PC4
SCK -0.2683435  0.817308475 -0.3504730  0.3703611
P20  0.4565423 -0.151198262 -0.8633829 -0.1525718
PA   0.6673093 -0.004232434  0.2283297  0.7089047
RA  -0.5236976 -0.555992831 -0.2821421  0.5805244
dim(pr.rush$x)
[1] 96  4
NFL2 = cbind(NFL[,-c(3,6,8,9,12:15)], pr.pass$x[,1:2], pr.rush$x[,1:2])
colnames(NFL2) = c("PTS","TA","TO","SCK","FD","COMP","THRD","P1","P2","R1","R2")
names(NFL2)
 [1] "PTS"  "TA"   "TO"   "SCK"  "FD"   "COMP" "THRD" "P1"   "P2"   "R1"  
[11] "R2"   NA    
attach(NFL2)
The following objects are masked from NFL:

    COMP, FD, PTS, SCK, TA, THRD, TO

Question 13:

library(leaps)
Warning: package 'leaps' was built under R version 4.5.3
regfit.full = regsubsets(PTS~., NFL2, nvmax=10)
reg.summary = summary(regfit.full)

plot(reg.summary$cp, xlab="Number of Variables", ylab="Cp", type="l")

which.min(reg.summary$cp)
[1] 3
reg.summary$cp[which.min(reg.summary$cp)]
[1] -0.4103878
coef(regfit.full, which.min(reg.summary$cp))
  (Intercept)           SCK          THRD            P1 
2014.28375791   -0.30050381   -0.09993133    0.01005953 

The minimum Cp is -0.41 at 3 variables.

The best set of variables selected is: SCK, THRD, P1.

Question 14:

regfit.full2 = regsubsets(PTS~(TA+TO+SCK+FD+P1+P2+R1)^2, NFL2, nvmax=28)
reg.summary2 = summary(regfit.full2)

plot(reg.summary2$cp, xlab="Number of Variables", ylab="Cp", type="l")

which.min(reg.summary2$cp)
[1] 2
reg.summary2$cp[which.min(reg.summary2$cp)]
[1] -4.585289
coef(regfit.full2, which.min(reg.summary2$cp))
 (Intercept)           R1        FD:R1 
2010.0656918   -1.6696941    0.4075313 

Minimum Cp is -4.585 at 2 variables.

Best set of variables: R1, FD:R1.

Question 15:

lm.fit0 = lm(PTS ~ FD + R1 + FD:R1, data = NFL2)
summary(lm.fit0)

Call:
lm(formula = PTS ~ FD + R1 + FD:R1, data = NFL2)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.30783 -0.83634 -0.00817  0.88128  1.30003 

Coefficients:
             Estimate Std. Error  t value Pr(>|t|)    
(Intercept) 2009.2699     0.9306 2159.219   <2e-16 ***
FD             0.1875     0.2182    0.859   0.3926    
R1            -1.5718     0.8281   -1.898   0.0608 .  
FD:R1          0.3916     0.1907    2.054   0.0428 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.8065 on 92 degrees of freedom
Multiple R-squared:  0.06493,   Adjusted R-squared:  0.03444 
F-statistic:  2.13 on 3 and 92 DF,  p-value: 0.1018

No predictors can be removed from the model since FD:R1 is significant. The terms involved (FD and R1) can’t be removed even if they’re individually insignificant due to the hierarchical principle.

Question 16:

mean(lm.fit0$residuals^2)
[1] 0.623379
  • Ridge: 4.48

  • LASSO: 4.64

  • PCR: 4.18

  • PLS: 4.32

  • Bagging (mtry=15): 6.61

  • Random Forest (mtry=5): 6.11

  • Boosting depth=1: 5.05

  • Boosting depth=2: 5.58

  • Q15 model: 0.62

  • Team project 1 mode: 4.15

    The model from Q15 technically has the lowest test MSE, but it isn’t very comparable since it uses scaled data. The model from team project 1 is still the best model, but PCR is the best among the newer models.