caret
)library(ISLR2)
df <- Credit
str(df)
## 'data.frame': 400 obs. of 11 variables:
## $ Income : num 14.9 106 104.6 148.9 55.9 ...
## $ Limit : num 3606 6645 7075 9504 4897 ...
## $ Rating : num 283 483 514 681 357 569 259 512 266 491 ...
## $ Cards : num 2 3 4 3 2 4 2 2 5 3 ...
## $ Age : num 34 82 71 36 68 77 37 87 66 41 ...
## $ Education: num 11 15 11 11 16 10 12 9 13 19 ...
## $ Own : Factor w/ 2 levels "No","Yes": 1 2 1 2 1 1 2 1 2 2 ...
## $ Student : Factor w/ 2 levels "No","Yes": 1 2 1 1 1 1 1 1 1 2 ...
## $ Married : Factor w/ 2 levels "No","Yes": 2 2 1 1 2 1 1 1 1 2 ...
## $ Region : Factor w/ 3 levels "East","South",..: 2 3 3 3 2 2 1 3 2 1 ...
## $ Balance : num 333 903 580 964 331 ...
head(df)
## Income Limit Rating Cards Age Education Own Student Married Region Balance
## 1 14.891 3606 283 2 34 11 No No Yes South 333
## 2 106.025 6645 483 3 82 15 Yes Yes Yes West 903
## 3 104.593 7075 514 4 71 11 No No No West 580
## 4 148.924 9504 681 3 36 11 Yes No No West 964
## 5 55.882 4897 357 2 68 16 No No Yes South 331
## 6 80.180 8047 569 4 77 10 No No No South 1151
set.seed(1234)
sp <- sample(1:nrow(df), 300)
df.train <- df[sp, ]
df.test <- df[-sp, ]
dim(df.train)
## [1] 300 11
dim(df.test)
## [1] 100 11
gbm.grid <- expand.grid(
interaction.depth = c(2, 3, 4, 5),
n.trees = (5:20) * 10,
shrinkage = (1:5) * 0.1,
n.minobsinnode = 20
)
The code provided creates a grid of hyperparameters for a GBM model
using the expand.grid()
function in R.
The gbm.grid
object is a data frame that contains all
possible combinations of the following hyperparameters:
interaction.depth
: the maximum depth of each tree (2,
3, 4, or 5)n.trees
: the number of trees in the model (50 to 200 in
increments of 10)shrinkage
: the shrinkage parameter used to control the
contribution of each tree to the model (0.1 to 0.5 in increments of
0.1)n.minobsinnode
: the minimum number of observations
allowed in each terminal node (20)By using expand.grid()
, we create a grid of all possible
combinations of the hyperparameters. The resulting gbm.grid
object will have a total of 800 rows (4 x 16 x 5), with each row
representing a different combination of hyperparameters.
This grid can be used to train multiple GBM models with different hyperparameter combinations, and then select the best model based on a performance metric such as accuracy or mean squared error. Grid search is a common technique used in machine learning to find the optimal hyperparameters for a model.
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
ctrl <- trainControl(method = "cv", number = 10)
set.seed(425)
gbm.credit <-
train(
Balance ~ .,
data = df.train,
method = "gbm",
metric = "RMSE",
verbose = FALSE,
trControl = ctrl,
tuneGrid = gbm.grid
)
gbm.credit
## Stochastic Gradient Boosting
##
## 300 samples
## 10 predictor
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 271, 269, 271, 271, 269, 272, ...
## Resampling results across tuning parameters:
##
## shrinkage interaction.depth n.trees RMSE Rsquared MAE
## 0.1 2 50 189.8320 0.8365213 127.1961
## 0.1 2 60 185.7044 0.8432616 123.9282
## 0.1 2 70 183.1703 0.8471708 121.2262
## 0.1 2 80 179.7367 0.8512123 119.0688
## 0.1 2 90 180.0730 0.8503387 118.4947
## 0.1 2 100 179.2083 0.8518389 118.0485
## 0.1 2 110 179.0971 0.8529287 118.6411
## 0.1 2 120 177.9066 0.8547971 117.5102
## 0.1 2 130 175.8188 0.8575044 116.2724
## 0.1 2 140 175.8146 0.8581993 116.5942
## 0.1 2 150 177.0744 0.8556376 116.8971
## 0.1 2 160 176.3989 0.8565998 117.1652
## 0.1 2 170 174.9173 0.8589836 115.9087
## 0.1 2 180 173.7194 0.8611196 115.4743
## 0.1 2 190 173.2503 0.8618840 115.4255
## 0.1 2 200 174.0124 0.8609874 115.7237
## 0.1 3 50 182.5488 0.8457632 118.1936
## 0.1 3 60 179.6602 0.8507843 115.4142
## 0.1 3 70 177.6025 0.8529356 114.6084
## 0.1 3 80 177.2615 0.8541054 114.6695
## 0.1 3 90 176.9735 0.8541626 114.8934
## 0.1 3 100 176.2829 0.8550070 114.4629
## 0.1 3 110 174.4554 0.8574076 114.2332
## 0.1 3 120 173.2556 0.8601219 114.0214
## 0.1 3 130 173.1905 0.8602664 114.1271
## 0.1 3 140 173.6635 0.8594557 114.7711
## 0.1 3 150 174.2027 0.8589309 115.8216
## 0.1 3 160 174.3063 0.8594904 115.0274
## 0.1 3 170 174.5272 0.8583303 114.9515
## 0.1 3 180 174.9117 0.8580970 115.4733
## 0.1 3 190 174.8490 0.8588174 115.8959
## 0.1 3 200 174.4506 0.8599979 116.5055
## 0.1 4 50 184.7839 0.8410740 118.1732
## 0.1 4 60 181.3610 0.8474669 116.6057
## 0.1 4 70 178.0072 0.8537532 115.7390
## 0.1 4 80 177.4597 0.8548989 116.6646
## 0.1 4 90 176.6032 0.8544743 116.6410
## 0.1 4 100 176.4926 0.8543792 116.7894
## 0.1 4 110 175.7178 0.8546516 116.8876
## 0.1 4 120 175.7773 0.8553028 116.9440
## 0.1 4 130 175.2039 0.8559282 116.0793
## 0.1 4 140 175.4137 0.8546437 116.0194
## 0.1 4 150 174.8441 0.8557051 116.3535
## 0.1 4 160 174.7531 0.8557931 116.2098
## 0.1 4 170 175.0248 0.8556866 116.4236
## 0.1 4 180 175.7841 0.8543333 116.8949
## 0.1 4 190 176.6736 0.8541393 117.2414
## 0.1 4 200 174.8949 0.8565507 116.5551
## 0.1 5 50 182.4990 0.8448215 117.1225
## 0.1 5 60 180.9413 0.8479785 116.5334
## 0.1 5 70 180.6068 0.8483359 116.7792
## 0.1 5 80 178.2919 0.8528411 116.2469
## 0.1 5 90 178.6858 0.8522803 116.8494
## 0.1 5 100 176.5918 0.8560241 115.8135
## 0.1 5 110 174.9114 0.8566129 114.5765
## 0.1 5 120 173.9506 0.8582325 114.3627
## 0.1 5 130 173.8228 0.8587811 114.2268
## 0.1 5 140 174.4583 0.8592259 114.4183
## 0.1 5 150 175.7090 0.8577915 115.5971
## 0.1 5 160 174.6815 0.8590130 114.8766
## 0.1 5 170 173.3600 0.8601285 114.6582
## 0.1 5 180 174.5557 0.8587682 115.7802
## 0.1 5 190 174.5413 0.8584344 115.4287
## 0.1 5 200 173.6176 0.8593905 115.5582
## 0.2 2 50 184.9925 0.8424518 123.3805
## 0.2 2 60 181.8724 0.8476367 121.2069
## 0.2 2 70 181.6186 0.8470401 121.9794
## 0.2 2 80 182.5637 0.8468165 123.5639
## 0.2 2 90 179.2995 0.8518411 120.8421
## 0.2 2 100 177.5710 0.8552835 121.7212
## 0.2 2 110 178.2606 0.8534321 121.2557
## 0.2 2 120 178.1325 0.8555039 121.7107
## 0.2 2 130 178.6640 0.8538746 121.5169
## 0.2 2 140 176.9375 0.8549612 120.2876
## 0.2 2 150 174.8824 0.8594099 119.5981
## 0.2 2 160 176.8341 0.8575628 122.0296
## 0.2 2 170 176.4583 0.8574541 120.9594
## 0.2 2 180 176.6724 0.8584429 122.1728
## 0.2 2 190 174.6669 0.8620364 121.4773
## 0.2 2 200 174.5101 0.8623414 121.1291
## 0.2 3 50 180.5433 0.8513586 119.8921
## 0.2 3 60 179.1503 0.8557743 118.6895
## 0.2 3 70 181.6600 0.8518483 121.4981
## 0.2 3 80 179.6088 0.8547197 120.5963
## 0.2 3 90 180.9482 0.8537088 121.7983
## 0.2 3 100 180.5342 0.8541370 122.4983
## 0.2 3 110 179.7305 0.8546336 123.4239
## 0.2 3 120 178.8502 0.8563656 123.3123
## 0.2 3 130 177.2689 0.8590375 122.0864
## 0.2 3 140 177.2995 0.8582059 122.1869
## 0.2 3 150 180.6721 0.8547357 122.6718
## 0.2 3 160 178.4210 0.8565553 121.8149
## 0.2 3 170 177.9761 0.8570512 122.5976
## 0.2 3 180 176.9076 0.8581718 121.0775
## 0.2 3 190 179.3071 0.8553338 123.0448
## 0.2 3 200 178.3149 0.8561287 122.5743
## 0.2 4 50 176.9280 0.8584895 118.0734
## 0.2 4 60 177.3136 0.8576515 119.0308
## 0.2 4 70 177.2056 0.8570916 119.1220
## 0.2 4 80 176.6260 0.8589508 120.0635
## 0.2 4 90 177.5233 0.8557677 120.3248
## 0.2 4 100 175.5521 0.8592071 120.0652
## 0.2 4 110 174.5595 0.8593787 119.6815
## 0.2 4 120 175.9431 0.8589837 120.8916
## 0.2 4 130 175.6819 0.8580279 120.2558
## 0.2 4 140 174.2812 0.8602310 120.1140
## 0.2 4 150 175.6104 0.8578394 121.9320
## 0.2 4 160 176.4373 0.8580512 122.5013
## 0.2 4 170 177.3384 0.8568976 123.5143
## 0.2 4 180 177.1006 0.8567239 123.8475
## 0.2 4 190 177.0482 0.8565277 123.7055
## 0.2 4 200 176.4134 0.8574508 123.6367
## 0.2 5 50 187.0212 0.8389284 123.4033
## 0.2 5 60 181.9325 0.8449545 119.9459
## 0.2 5 70 179.4006 0.8494421 118.7397
## 0.2 5 80 180.4630 0.8491327 120.0773
## 0.2 5 90 180.1867 0.8492514 120.3550
## 0.2 5 100 184.5064 0.8447805 124.2050
## 0.2 5 110 183.6514 0.8447929 123.1723
## 0.2 5 120 183.4170 0.8440176 124.7663
## 0.2 5 130 181.2283 0.8488926 123.5865
## 0.2 5 140 181.7294 0.8489062 124.6633
## 0.2 5 150 181.1803 0.8502417 124.9138
## 0.2 5 160 182.1984 0.8486948 126.1787
## 0.2 5 170 182.7028 0.8473936 126.1350
## 0.2 5 180 182.8954 0.8467530 127.6162
## 0.2 5 190 183.2754 0.8466692 127.9939
## 0.2 5 200 183.9753 0.8454310 128.5809
## 0.3 2 50 182.3428 0.8486601 124.7731
## 0.3 2 60 182.1427 0.8507622 125.3209
## 0.3 2 70 182.5893 0.8520100 123.7312
## 0.3 2 80 185.3726 0.8470163 127.2449
## 0.3 2 90 183.6077 0.8502428 125.2890
## 0.3 2 100 180.2053 0.8551588 122.4544
## 0.3 2 110 183.9351 0.8485568 125.9275
## 0.3 2 120 181.0348 0.8537958 123.9707
## 0.3 2 130 182.6376 0.8512411 125.1608
## 0.3 2 140 182.8555 0.8512171 125.2171
## 0.3 2 150 180.9013 0.8537279 123.8030
## 0.3 2 160 182.5301 0.8544809 124.2799
## 0.3 2 170 180.6298 0.8551648 124.5415
## 0.3 2 180 181.4438 0.8543789 125.4669
## 0.3 2 190 180.4788 0.8568388 125.1936
## 0.3 2 200 183.2355 0.8521478 127.6458
## 0.3 3 50 188.3084 0.8380573 127.4298
## 0.3 3 60 187.1048 0.8379808 128.0059
## 0.3 3 70 188.3692 0.8377338 128.7087
## 0.3 3 80 188.2043 0.8392638 129.2129
## 0.3 3 90 188.3017 0.8376771 128.5012
## 0.3 3 100 189.3033 0.8373671 131.0384
## 0.3 3 110 187.2410 0.8419609 130.0917
## 0.3 3 120 186.8538 0.8415029 129.6962
## 0.3 3 130 188.2200 0.8372621 131.3572
## 0.3 3 140 187.9723 0.8389418 131.7574
## 0.3 3 150 187.1955 0.8393284 130.8484
## 0.3 3 160 187.7270 0.8398844 132.6807
## 0.3 3 170 186.6859 0.8412267 131.4002
## 0.3 3 180 187.1143 0.8406383 131.9049
## 0.3 3 190 187.8395 0.8396806 132.1102
## 0.3 3 200 189.2401 0.8380435 133.7965
## 0.3 4 50 189.8677 0.8379829 128.5410
## 0.3 4 60 187.3331 0.8405686 128.3320
## 0.3 4 70 189.7111 0.8359963 131.1729
## 0.3 4 80 191.8167 0.8344235 133.9231
## 0.3 4 90 192.4529 0.8351622 134.6814
## 0.3 4 100 191.4200 0.8358518 134.0701
## 0.3 4 110 190.2813 0.8393578 134.6609
## 0.3 4 120 188.5301 0.8403330 133.0386
## 0.3 4 130 190.3798 0.8386859 135.3929
## 0.3 4 140 191.7726 0.8376864 135.7403
## 0.3 4 150 192.6112 0.8366243 137.4903
## 0.3 4 160 193.6128 0.8351959 137.7584
## 0.3 4 170 191.7225 0.8382155 137.0265
## 0.3 4 180 196.1832 0.8326291 139.4932
## 0.3 4 190 192.5743 0.8392095 137.8844
## 0.3 4 200 194.0096 0.8362616 139.3807
## 0.3 5 50 177.9365 0.8553693 125.3915
## 0.3 5 60 175.7964 0.8608727 125.3691
## 0.3 5 70 176.4694 0.8604106 126.0578
## 0.3 5 80 176.4990 0.8614532 128.0836
## 0.3 5 90 179.5844 0.8564193 129.1529
## 0.3 5 100 180.6255 0.8535846 130.3961
## 0.3 5 110 183.3294 0.8524704 132.7713
## 0.3 5 120 183.4210 0.8507625 134.2418
## 0.3 5 130 185.6340 0.8489286 134.9052
## 0.3 5 140 186.1211 0.8481909 135.0714
## 0.3 5 150 185.0196 0.8507721 135.0162
## 0.3 5 160 183.5864 0.8519753 136.3586
## 0.3 5 170 186.8425 0.8472290 138.9994
## 0.3 5 180 187.0430 0.8471082 139.8108
## 0.3 5 190 187.4865 0.8464322 139.8311
## 0.3 5 200 188.2556 0.8447583 139.7396
## 0.4 2 50 183.9855 0.8452558 127.2257
## 0.4 2 60 184.1163 0.8456530 125.3293
## 0.4 2 70 186.1429 0.8390443 127.1747
## 0.4 2 80 187.2242 0.8393500 129.2029
## 0.4 2 90 183.3207 0.8446093 127.6583
## 0.4 2 100 184.7211 0.8440139 127.3423
## 0.4 2 110 189.5717 0.8363197 131.9434
## 0.4 2 120 189.5090 0.8378774 132.2739
## 0.4 2 130 186.5132 0.8416937 130.3847
## 0.4 2 140 188.9851 0.8390336 131.6551
## 0.4 2 150 190.1555 0.8372908 132.3152
## 0.4 2 160 192.1499 0.8349209 136.6173
## 0.4 2 170 190.8667 0.8371238 134.5214
## 0.4 2 180 190.5590 0.8363807 134.3264
## 0.4 2 190 189.8628 0.8393258 134.2588
## 0.4 2 200 192.6425 0.8348418 137.1129
## 0.4 3 50 190.6533 0.8344948 133.4804
## 0.4 3 60 187.9124 0.8379250 134.0772
## 0.4 3 70 189.4449 0.8359142 135.2827
## 0.4 3 80 189.0073 0.8381538 135.2778
## 0.4 3 90 189.5973 0.8370261 136.2981
## 0.4 3 100 194.7865 0.8297549 140.6280
## 0.4 3 110 191.3677 0.8354339 137.4970
## 0.4 3 120 187.8281 0.8416551 136.7739
## 0.4 3 130 190.0409 0.8386247 137.3377
## 0.4 3 140 189.8862 0.8396984 136.4253
## 0.4 3 150 190.6696 0.8390186 136.8831
## 0.4 3 160 189.5581 0.8400722 136.4488
## 0.4 3 170 191.1736 0.8387349 139.3195
## 0.4 3 180 189.9070 0.8417226 139.3216
## 0.4 3 190 192.4178 0.8404984 141.1562
## 0.4 3 200 192.8313 0.8382272 141.9877
## 0.4 4 50 189.2993 0.8387383 130.1617
## 0.4 4 60 195.3147 0.8281720 134.0865
## 0.4 4 70 193.0814 0.8329687 133.2453
## 0.4 4 80 194.2711 0.8308257 135.8416
## 0.4 4 90 196.9183 0.8267587 138.0598
## 0.4 4 100 195.0489 0.8300310 136.3301
## 0.4 4 110 197.0603 0.8267848 137.6779
## 0.4 4 120 194.6697 0.8311022 135.9321
## 0.4 4 130 194.5337 0.8310717 137.5186
## 0.4 4 140 196.7503 0.8263218 138.5193
## 0.4 4 150 197.2204 0.8276621 139.0806
## 0.4 4 160 198.4715 0.8255451 140.0450
## 0.4 4 170 197.7574 0.8271536 140.6258
## 0.4 4 180 197.5051 0.8270786 139.9126
## 0.4 4 190 199.2531 0.8249129 142.4121
## 0.4 4 200 200.7825 0.8218512 143.6861
## 0.4 5 50 196.1275 0.8273695 137.0792
## 0.4 5 60 193.1564 0.8310002 134.7601
## 0.4 5 70 194.9901 0.8273364 136.7143
## 0.4 5 80 193.7955 0.8305523 137.6063
## 0.4 5 90 196.8929 0.8274687 138.1996
## 0.4 5 100 195.4734 0.8292155 139.5293
## 0.4 5 110 196.8283 0.8276468 139.5617
## 0.4 5 120 199.1045 0.8237028 140.5106
## 0.4 5 130 199.3151 0.8226462 138.7014
## 0.4 5 140 198.3001 0.8243121 138.5855
## 0.4 5 150 198.7977 0.8261121 138.4098
## 0.4 5 160 199.9244 0.8230064 139.9519
## 0.4 5 170 199.0083 0.8244113 139.0274
## 0.4 5 180 198.7724 0.8248488 139.5626
## 0.4 5 190 199.8818 0.8238693 141.8972
## 0.4 5 200 198.4655 0.8260262 140.5233
## 0.5 2 50 185.5514 0.8419377 132.1470
## 0.5 2 60 186.3082 0.8432601 132.5419
## 0.5 2 70 184.6047 0.8445685 133.7869
## 0.5 2 80 188.3517 0.8393940 135.8344
## 0.5 2 90 187.9570 0.8391125 136.6429
## 0.5 2 100 188.2320 0.8395853 135.9616
## 0.5 2 110 190.1369 0.8398311 137.6297
## 0.5 2 120 190.5107 0.8378188 138.4868
## 0.5 2 130 193.3843 0.8355052 140.1955
## 0.5 2 140 190.0853 0.8398647 140.6112
## 0.5 2 150 191.0198 0.8395063 140.2990
## 0.5 2 160 193.4190 0.8368379 143.0161
## 0.5 2 170 193.7293 0.8350382 142.1675
## 0.5 2 180 196.7220 0.8308381 144.0901
## 0.5 2 190 195.3545 0.8335562 143.3336
## 0.5 2 200 194.6354 0.8356882 144.1153
## 0.5 3 50 194.2035 0.8260776 135.7130
## 0.5 3 60 196.1929 0.8265648 137.3698
## 0.5 3 70 190.5051 0.8369516 134.9859
## 0.5 3 80 193.6891 0.8339489 138.1934
## 0.5 3 90 189.8656 0.8407512 136.1424
## 0.5 3 100 192.7655 0.8359200 138.0203
## 0.5 3 110 193.7559 0.8358763 138.7791
## 0.5 3 120 196.0068 0.8296508 139.3794
## 0.5 3 130 195.5606 0.8308319 138.1859
## 0.5 3 140 195.1163 0.8305333 139.2883
## 0.5 3 150 197.6770 0.8268831 140.4535
## 0.5 3 160 199.3216 0.8243362 142.0672
## 0.5 3 170 197.7789 0.8286669 142.4533
## 0.5 3 180 199.4022 0.8275821 145.0926
## 0.5 3 190 201.1385 0.8239128 144.7842
## 0.5 3 200 200.7386 0.8248501 144.5474
## 0.5 4 50 195.9184 0.8256480 140.4099
## 0.5 4 60 198.8903 0.8231441 143.8934
## 0.5 4 70 195.2917 0.8290983 140.8804
## 0.5 4 80 198.0103 0.8254323 142.5801
## 0.5 4 90 197.7038 0.8251273 143.0140
## 0.5 4 100 197.0726 0.8267804 141.8672
## 0.5 4 110 198.4617 0.8250812 144.4848
## 0.5 4 120 195.8690 0.8302284 141.6702
## 0.5 4 130 201.2487 0.8217403 144.0490
## 0.5 4 140 198.6885 0.8268019 143.2121
## 0.5 4 150 197.8162 0.8264497 143.1531
## 0.5 4 160 196.8804 0.8281616 143.2180
## 0.5 4 170 199.0976 0.8258583 141.9580
## 0.5 4 180 200.5986 0.8220563 142.6655
## 0.5 4 190 199.6753 0.8253083 142.1420
## 0.5 4 200 202.6323 0.8228625 145.6615
## 0.5 5 50 186.0457 0.8444262 131.8366
## 0.5 5 60 188.7618 0.8392964 133.1566
## 0.5 5 70 190.7450 0.8350425 137.8583
## 0.5 5 80 192.1486 0.8345392 138.9646
## 0.5 5 90 187.7986 0.8419154 138.0338
## 0.5 5 100 190.0456 0.8383912 140.0224
## 0.5 5 110 189.5167 0.8395804 140.9283
## 0.5 5 120 192.8875 0.8341605 144.0698
## 0.5 5 130 191.4997 0.8368002 145.1516
## 0.5 5 140 190.3428 0.8364342 143.1112
## 0.5 5 150 192.5123 0.8352507 145.2868
## 0.5 5 160 194.2087 0.8325877 147.3640
## 0.5 5 170 196.4477 0.8305800 148.5757
## 0.5 5 180 195.6409 0.8306026 147.5649
## 0.5 5 190 196.3954 0.8306234 147.8939
## 0.5 5 200 195.3331 0.8324527 146.5717
##
## Tuning parameter 'n.minobsinnode' was held constant at a value of 20
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were n.trees = 130, interaction.depth =
## 3, shrinkage = 0.1 and n.minobsinnode = 20.