caret
package documentation on githublibrary(dplyr)
library(ggplot2)
library(mlbench) # sonar data
library(caret)
library(caTools)
source('create_datasets.R')
caret
package in the courselm
funciton in RIn-sample error example:
# Fit a model to the mtcars data
data(mtcars)
model <- lm(mpg ~ hp, mtcars[1:20, ])
model
##
## Call:
## lm(formula = mpg ~ hp, data = mtcars[1:20, ])
##
## Coefficients:
## (Intercept) hp
## 32.79788 -0.09301
# Predict in-sample
predicted <- predict(model, mtcars[1:20, ], type = "response")
# Calculate RMSE
actual <- mtcars[1:20,"mpg"]
as.numeric(predicted)
## [1] 22.56685 22.56685 24.14800 22.56685 16.52124 23.03189 10.01058
## [8] 27.03129 23.96199 21.35772 21.35772 16.05619 16.05619 16.05619
## [15] 13.73096 12.80086 11.40572 26.65926 27.96139 26.75227
actual
## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2
## [15] 10.4 10.4 14.7 32.4 30.4 33.9
sqrt(mean((predicted - actual)^2))
## [1] 3.172132
data(diamonds)
glimpse(diamonds)
## Observations: 53,940
## Variables: 10
## $ carat <dbl> 0.23, 0.21, 0.23, 0.29, 0.31, 0.24, 0.24, 0.26, 0.22, ...
## $ cut <ord> Ideal, Premium, Good, Premium, Good, Very Good, Very G...
## $ color <ord> E, E, E, I, J, J, I, H, E, H, J, J, F, J, E, E, I, J, ...
## $ clarity <ord> SI2, SI1, VS1, VS2, SI2, VVS2, VVS1, SI1, VS2, VS1, SI...
## $ depth <dbl> 61.5, 59.8, 56.9, 62.4, 63.3, 62.8, 62.3, 61.9, 65.1, ...
## $ table <dbl> 55, 61, 65, 58, 58, 57, 57, 55, 61, 61, 55, 56, 61, 54...
## $ price <int> 326, 326, 327, 334, 335, 336, 336, 337, 337, 338, 339,...
## $ x <dbl> 3.95, 3.89, 4.05, 4.20, 4.34, 3.94, 3.95, 4.07, 3.87, ...
## $ y <dbl> 3.98, 3.84, 4.07, 4.23, 4.35, 3.96, 3.98, 4.11, 3.78, ...
## $ z <dbl> 2.43, 2.31, 2.31, 2.63, 2.75, 2.48, 2.47, 2.53, 2.49, ...
# Fit lm model: model
model <- lm(price ~ ., diamonds)
# Predict on full data: p
p <- predict(model, diamonds)
# Compute errors: error
error <- p - diamonds$price
# Calculate RMSE
sqrt(mean(error^2))
## [1] 1129.843
caret
package and this course: don’t overfit.Example of out-of-sample RMSE:
caret
we can use createResamples()
or createFolds()
functions# Fit model to the mtcars data (first 20 rows)
model <- lm(mpg ~ hp, mtcars[1:20, ])
# Predict out-of-sample
predicted <- predict(model, mtcars[21:32, ], type = "response")
# Evaluate error
actual <- mtcars[21:32, "mpg"]
sqrt(mean((predicted - actual)^2))
## [1] 5.507236
# Set seed
set.seed(42)
# Shuffle row indices: rows
rows <- sample(nrow(diamonds))
head(rows)
## [1] 49345 50545 15434 44792 34614 27998
# Randomly order data
diamonds <- diamonds[rows, ]
# Determine row to split on: split
split <- round(nrow(diamonds) * .80)
split
## [1] 43152
# Create train
train <- diamonds[1:split,]
# Create test
test <- diamonds[(split + 1):nrow(diamonds),]
dim(diamonds)
## [1] 53940 10
dim(train)
## [1] 43152 10
dim(test)
## [1] 10788 10
# Fit lm model on train: model
model <- lm(price ~ ., train)
model
##
## Call:
## lm(formula = price ~ ., data = train)
##
## Coefficients:
## (Intercept) carat cut.L cut.Q cut.C
## 5179.769 11237.590 602.695 -307.809 151.088
## cut^4 color.L color.Q color.C color^4
## -25.812 -1941.427 -660.511 -160.640 43.022
## color^5 color^6 clarity.L clarity.Q clarity.C
## -96.375 -47.526 4087.131 -1937.609 977.749
## clarity^4 clarity^5 clarity^6 clarity^7 depth
## -383.520 231.186 7.064 94.505 -56.218
## table x y z
## -24.767 -967.581 9.207 -110.286
# Predict on test: p
p <- predict(model, test)
# Compute errors: error
error <- p - test$price
# Calculate RMSE
sqrt(mean(error^2))
## [1] 1136.596
Cross Validation Example:
train
function formula interface is exactly like the lm
function in R
caret
package behind the cross validation of models.trControl
arugment controls the parameters used for cross validation
number
argumentverboseIter
so we can see the progress of each model and know if we have time to get coffee.# the caret library is loaded
set.seed(42)
# Fit linear regression model using caret
model <- train(mpg ~ hp, mtcars,
method = "lm",
trControl = trainControl(
method = "cv",
number = 10,
verboseIter = T
))
## + Fold01: intercept=TRUE
## - Fold01: intercept=TRUE
## + Fold02: intercept=TRUE
## - Fold02: intercept=TRUE
## + Fold03: intercept=TRUE
## - Fold03: intercept=TRUE
## + Fold04: intercept=TRUE
## - Fold04: intercept=TRUE
## + Fold05: intercept=TRUE
## - Fold05: intercept=TRUE
## + Fold06: intercept=TRUE
## - Fold06: intercept=TRUE
## + Fold07: intercept=TRUE
## - Fold07: intercept=TRUE
## + Fold08: intercept=TRUE
## - Fold08: intercept=TRUE
## + Fold09: intercept=TRUE
## - Fold09: intercept=TRUE
## + Fold10: intercept=TRUE
## - Fold10: intercept=TRUE
## Aggregating results
## Fitting final model on full training set
model
## Linear Regression
##
## 32 samples
## 1 predictor
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 30, 29, 29, 28, 29, 28, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 3.957996 0.9252153 3.349958
##
## Tuning parameter 'intercept' was held constant at a value of TRUE
# Fit lm model using 10-fold CV: model
model <- train(
price ~ ., diamonds,
method = "lm",
trControl = trainControl(
method = "cv", number = 10,
verboseIter = TRUE
)
)
## + Fold01: intercept=TRUE
## - Fold01: intercept=TRUE
## + Fold02: intercept=TRUE
## - Fold02: intercept=TRUE
## + Fold03: intercept=TRUE
## - Fold03: intercept=TRUE
## + Fold04: intercept=TRUE
## - Fold04: intercept=TRUE
## + Fold05: intercept=TRUE
## - Fold05: intercept=TRUE
## + Fold06: intercept=TRUE
## - Fold06: intercept=TRUE
## + Fold07: intercept=TRUE
## - Fold07: intercept=TRUE
## + Fold08: intercept=TRUE
## - Fold08: intercept=TRUE
## + Fold09: intercept=TRUE
## - Fold09: intercept=TRUE
## + Fold10: intercept=TRUE
## - Fold10: intercept=TRUE
## Aggregating results
## Fitting final model on full training set
# Print model to console
model
## Linear Regression
##
## 53940 samples
## 9 predictor
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 48546, 48546, 48545, 48547, 48547, 48546, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 1130.765 0.9196636 740.4743
##
## Tuning parameter 'intercept' was held constant at a value of TRUE
# Fit lm model using 5-fold CV: model
model <- train(
medv ~ . , Boston,
method = "lm",
trControl = trainControl(
method = "cv", number = 5,
verboseIter = TRUE
)
)
## + Fold1: intercept=TRUE
## - Fold1: intercept=TRUE
## + Fold2: intercept=TRUE
## - Fold2: intercept=TRUE
## + Fold3: intercept=TRUE
## - Fold3: intercept=TRUE
## + Fold4: intercept=TRUE
## - Fold4: intercept=TRUE
## + Fold5: intercept=TRUE
## - Fold5: intercept=TRUE
## Aggregating results
## Fitting final model on full training set
# Print model to console
model
## Linear Regression
##
## 506 samples
## 13 predictor
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 404, 405, 405, 405, 405
## Resampling results:
##
## RMSE Rsquared MAE
## 4.816486 0.7249456 3.404306
##
## Tuning parameter 'intercept' was held constant at a value of TRUE
# Fit lm model using 5 x 5-fold CV: model
model <- train(
medv ~ ., Boston,
method = "lm",
trControl = trainControl(
method = "cv",
number = 5,
repeats = 5,
verboseIter = TRUE
)
)
## + Fold1: intercept=TRUE
## - Fold1: intercept=TRUE
## + Fold2: intercept=TRUE
## - Fold2: intercept=TRUE
## + Fold3: intercept=TRUE
## - Fold3: intercept=TRUE
## + Fold4: intercept=TRUE
## - Fold4: intercept=TRUE
## + Fold5: intercept=TRUE
## - Fold5: intercept=TRUE
## Aggregating results
## Fitting final model on full training set
# Print model to console
model
## Linear Regression
##
## 506 samples
## 13 predictor
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 404, 405, 405, 405, 405
## Resampling results:
##
## RMSE Rsquared MAE
## 4.896398 0.7254017 3.423548
##
## Tuning parameter 'intercept' was held constant at a value of TRUE
# Predict on full Boston dataset
p <- predict(model, Boston)
head(p)
## 1 2 3 4 5 6
## 30.00384 25.02556 30.56760 28.60704 27.94352 25.25628
data(Sonar)
# Shuffle row indices: rows
rows <- sample(nrow(Sonar))
# Randomly order data: Sonar
Sonar <- Sonar[rows,]
# Identify row to split on: split
split <- round(nrow(Sonar) * .60)
# Create train
train <- Sonar[1:split,]
# Create test
test <- Sonar[(split + 1):nrow(Sonar), ]
nrow(train)/nrow(Sonar)
## [1] 0.6009615
glm.fit: algorithm did not converge
orglm.fit: fitted probabilities numerically 0 or 1 occurred
glimpse(Sonar)
## Observations: 208
## Variables: 61
## $ V1 <dbl> 0.0086, 0.0209, 0.0491, 0.0131, 0.0201, 0.0187, 0.0664, ...
## $ V2 <dbl> 0.0215, 0.0261, 0.0279, 0.0068, 0.0116, 0.0346, 0.0575, ...
## $ V3 <dbl> 0.0242, 0.0120, 0.0592, 0.0308, 0.0123, 0.0168, 0.0842, ...
## $ V4 <dbl> 0.0445, 0.0768, 0.1270, 0.0311, 0.0245, 0.0177, 0.0372, ...
## $ V5 <dbl> 0.0667, 0.1064, 0.1772, 0.0085, 0.0547, 0.0393, 0.0458, ...
## $ V6 <dbl> 0.0771, 0.1680, 0.1908, 0.0767, 0.0208, 0.1630, 0.0771, ...
## $ V7 <dbl> 0.0499, 0.3016, 0.2217, 0.0771, 0.0891, 0.2028, 0.0771, ...
## $ V8 <dbl> 0.0906, 0.3460, 0.0768, 0.0640, 0.0836, 0.1694, 0.1130, ...
## $ V9 <dbl> 0.1229, 0.3314, 0.1246, 0.0726, 0.1335, 0.2328, 0.2353, ...
## $ V10 <dbl> 0.1185, 0.4125, 0.2028, 0.0901, 0.1199, 0.2684, 0.1838, ...
## $ V11 <dbl> 0.0775, 0.3943, 0.0947, 0.0750, 0.1742, 0.3108, 0.2869, ...
## $ V12 <dbl> 0.1101, 0.1334, 0.2497, 0.0844, 0.1387, 0.2933, 0.4129, ...
## $ V13 <dbl> 0.1042, 0.4622, 0.2209, 0.1226, 0.2042, 0.2275, 0.3647, ...
## $ V14 <dbl> 0.0853, 0.9970, 0.3195, 0.1619, 0.2580, 0.0994, 0.1984, ...
## $ V15 <dbl> 0.0456, 0.9137, 0.3340, 0.2317, 0.2616, 0.1801, 0.2840, ...
## $ V16 <dbl> 0.1304, 0.8292, 0.3323, 0.2934, 0.2097, 0.2200, 0.4039, ...
## $ V17 <dbl> 0.2690, 0.6994, 0.2780, 0.3526, 0.2532, 0.2732, 0.5837, ...
## $ V18 <dbl> 0.2947, 0.7825, 0.2975, 0.3657, 0.3213, 0.2862, 0.6792, ...
## $ V19 <dbl> 0.3669, 0.8789, 0.2948, 0.3221, 0.4327, 0.2034, 0.6086, ...
## $ V20 <dbl> 0.4948, 0.8501, 0.1729, 0.3093, 0.4760, 0.1740, 0.4858, ...
## $ V21 <dbl> 0.6275, 0.8920, 0.3264, 0.4084, 0.5328, 0.4130, 0.3246, ...
## $ V22 <dbl> 0.8162, 0.9473, 0.3834, 0.4285, 0.6057, 0.6879, 0.2013, ...
## $ V23 <dbl> 0.9237, 1.0000, 0.3523, 0.4663, 0.6696, 0.8120, 0.2082, ...
## $ V24 <dbl> 0.8710, 0.8975, 0.5410, 0.5956, 0.7476, 0.8453, 0.1686, ...
## $ V25 <dbl> 0.8052, 0.7806, 0.5228, 0.6948, 0.8930, 0.8919, 0.2484, ...
## $ V26 <dbl> 0.8756, 0.8321, 0.4475, 0.8386, 0.9405, 0.9300, 0.2736, ...
## $ V27 <dbl> 1.0000, 0.6502, 0.5340, 0.8875, 1.0000, 0.9987, 0.2984, ...
## $ V28 <dbl> 0.9858, 0.4548, 0.5323, 0.6404, 0.9785, 1.0000, 0.4655, ...
## $ V29 <dbl> 0.9427, 0.4732, 0.3907, 0.3308, 0.8473, 0.8104, 0.6990, ...
## $ V30 <dbl> 0.8114, 0.3391, 0.3456, 0.3425, 0.7639, 0.6199, 0.7474, ...
## $ V31 <dbl> 0.6987, 0.2747, 0.4091, 0.4920, 0.6701, 0.6041, 0.7956, ...
## $ V32 <dbl> 0.6810, 0.0978, 0.4639, 0.4592, 0.4989, 0.5547, 0.7981, ...
## $ V33 <dbl> 0.6591, 0.0477, 0.5580, 0.3034, 0.3718, 0.4160, 0.6715, ...
## $ V34 <dbl> 0.6954, 0.1403, 0.5727, 0.4366, 0.2196, 0.1472, 0.6942, ...
## $ V35 <dbl> 0.7290, 0.1834, 0.6355, 0.5175, 0.1416, 0.0849, 0.7440, ...
## $ V36 <dbl> 0.6680, 0.2148, 0.7563, 0.5122, 0.2680, 0.0608, 0.8169, ...
## $ V37 <dbl> 0.5917, 0.1271, 0.6903, 0.4746, 0.2630, 0.0969, 0.8912, ...
## $ V38 <dbl> 0.4899, 0.1912, 0.6176, 0.4902, 0.3104, 0.1411, 1.0000, ...
## $ V39 <dbl> 0.3439, 0.3391, 0.5379, 0.4603, 0.3392, 0.1676, 0.8753, ...
## $ V40 <dbl> 0.2366, 0.3444, 0.5622, 0.4460, 0.2123, 0.1200, 0.7061, ...
## $ V41 <dbl> 0.1716, 0.2369, 0.6508, 0.4196, 0.1170, 0.1201, 0.6803, ...
## $ V42 <dbl> 0.1013, 0.1195, 0.4797, 0.2873, 0.2655, 0.1036, 0.5898, ...
## $ V43 <dbl> 0.0766, 0.2665, 0.3736, 0.2296, 0.2203, 0.1977, 0.4618, ...
## $ V44 <dbl> 0.0845, 0.2587, 0.2804, 0.0949, 0.1541, 0.1339, 0.3639, ...
## $ V45 <dbl> 0.0260, 0.1393, 0.1982, 0.0095, 0.1464, 0.0902, 0.1492, ...
## $ V46 <dbl> 0.0333, 0.1083, 0.2438, 0.0527, 0.1044, 0.1085, 0.1216, ...
## $ V47 <dbl> 0.0205, 0.1383, 0.1789, 0.0383, 0.1225, 0.1521, 0.1306, ...
## $ V48 <dbl> 0.0309, 0.1321, 0.1706, 0.0107, 0.0745, 0.1363, 0.1198, ...
## $ V49 <dbl> 0.0101, 0.1069, 0.0762, 0.0108, 0.0490, 0.0858, 0.0578, ...
## $ V50 <dbl> 0.0095, 0.0325, 0.0238, 0.0077, 0.0224, 0.0290, 0.0235, ...
## $ V51 <dbl> 0.0047, 0.0316, 0.0268, 0.0109, 0.0032, 0.0203, 0.0135, ...
## $ V52 <dbl> 0.0072, 0.0057, 0.0081, 0.0062, 0.0076, 0.0116, 0.0141, ...
## $ V53 <dbl> 0.0054, 0.0159, 0.0129, 0.0028, 0.0045, 0.0098, 0.0190, ...
## $ V54 <dbl> 0.0022, 0.0085, 0.0161, 0.0040, 0.0056, 0.0199, 0.0043, ...
## $ V55 <dbl> 0.0016, 0.0372, 0.0063, 0.0075, 0.0075, 0.0033, 0.0036, ...
## $ V56 <dbl> 0.0029, 0.0101, 0.0119, 0.0039, 0.0037, 0.0101, 0.0026, ...
## $ V57 <dbl> 0.0058, 0.0127, 0.0194, 0.0053, 0.0045, 0.0065, 0.0024, ...
## $ V58 <dbl> 0.0050, 0.0288, 0.0140, 0.0013, 0.0029, 0.0115, 0.0162, ...
## $ V59 <dbl> 0.0024, 0.0129, 0.0332, 0.0052, 0.0008, 0.0193, 0.0109, ...
## $ V60 <dbl> 0.0030, 0.0023, 0.0439, 0.0023, 0.0018, 0.0157, 0.0079, ...
## $ Class <fctr> R, M, M, R, R, M, R, R, R, R, M, R, M, R, M, M, M, R, M...
# Fit glm model: model
model <- glm(Class ~ ., family = "binomial", train)
model
##
## Call: glm(formula = Class ~ ., family = "binomial", data = train)
##
## Coefficients:
## (Intercept) V1 V2 V3 V4
## 145.95 351.31 -456.22 359.50 -524.19
## V5 V6 V7 V8 V9
## 84.67 39.10 -158.13 440.59 -512.82
## V10 V11 V12 V13 V14
## -18.64 315.68 -295.58 39.11 -198.35
## V15 V16 V17 V18 V19
## 137.16 -16.54 186.89 -165.70 -10.90
## V20 V21 V22 V23 V24
## -77.54 227.15 -320.07 202.39 -176.36
## V25 V26 V27 V28 V29
## -100.99 101.45 31.31 -10.64 37.27
## V30 V31 V32 V33 V34
## -171.07 234.99 -181.58 67.45 258.65
## V35 V36 V37 V38 V39
## -417.93 376.45 -191.05 241.65 -456.91
## V40 V41 V42 V43 V44
## 383.27 -153.17 113.53 -267.66 85.57
## V45 V46 V47 V48 V49
## -40.81 231.24 -398.87 616.56 -1509.64
## V50 V51 V52 V53 V54
## 2067.74 -1443.52 -618.43 185.89 1874.93
## V55 V56 V57 V58 V59
## 2674.37 -1592.68 -514.08 1475.45 208.12
## V60
## -6.22
##
## Degrees of Freedom: 124 Total (i.e. Null); 64 Residual
## Null Deviance: 172.3
## Residual Deviance: 3.997e-09 AIC: 122
# Predict on test: p
p <- predict(model, test, type = "response")
confusionMatix
function from caret
gives you the contigency table as well as important statistics
# Calculate class probabilities: p_class
p_class <- ifelse(p > .50, "M", "R")
# Create confusion matrix
confusionMatrix(p_class, test$Class)
## Confusion Matrix and Statistics
##
## Reference
## Prediction M R
## M 17 29
## R 26 11
##
## Accuracy : 0.3373
## 95% CI : (0.2372, 0.4495)
## No Information Rate : 0.5181
## P-Value [Acc > NIR] : 0.9997
##
## Kappa : -0.3305
## Mcnemar's Test P-Value : 0.7874
##
## Sensitivity : 0.3953
## Specificity : 0.2750
## Pos Pred Value : 0.3696
## Neg Pred Value : 0.2973
## Prevalence : 0.5181
## Detection Rate : 0.2048
## Detection Prevalence : 0.5542
## Balanced Accuracy : 0.3352
##
## 'Positive' Class : M
##
# Apply threshold of 0.9: p_class
p_class <- ifelse(p > .9, "M", "R")
# Create confusion matrix
confusionMatrix(p_class, test[["Class"]])
## Confusion Matrix and Statistics
##
## Reference
## Prediction M R
## M 16 29
## R 27 11
##
## Accuracy : 0.3253
## 95% CI : (0.2265, 0.437)
## No Information Rate : 0.5181
## P-Value [Acc > NIR] : 0.9999
##
## Kappa : -0.3535
## Mcnemar's Test P-Value : 0.8937
##
## Sensitivity : 0.3721
## Specificity : 0.2750
## Pos Pred Value : 0.3556
## Neg Pred Value : 0.2895
## Prevalence : 0.5181
## Detection Rate : 0.1928
## Detection Prevalence : 0.5422
## Balanced Accuracy : 0.3235
##
## 'Positive' Class : M
##
# Apply threshold of 0.10: p_class
p_class <- ifelse(p > .10, "M", "R")
# Create confusion matrix
confusionMatrix(p_class, test[["Class"]])
## Confusion Matrix and Statistics
##
## Reference
## Prediction M R
## M 18 32
## R 25 8
##
## Accuracy : 0.3133
## 95% CI : (0.2159, 0.4244)
## No Information Rate : 0.5181
## P-Value [Acc > NIR] : 0.9999
##
## Kappa : -0.3837
## Mcnemar's Test P-Value : 0.4268
##
## Sensitivity : 0.4186
## Specificity : 0.2000
## Pos Pred Value : 0.3600
## Neg Pred Value : 0.2424
## Prevalence : 0.5181
## Detection Rate : 0.2169
## Detection Prevalence : 0.6024
## Balanced Accuracy : 0.3093
##
## 'Positive' Class : M
##
caTools
package and colAUC
function.
# the caTools library is loaded
# Predict on test: p
p <- predict(model, test, type = "response")
# Make ROC curve
colAUC(p, test$Class, plotROC = T)
## [,1]
## M vs. R 0.7148256
trainControl()
function in caret
to use AUC (instead of accuracy) to tune the parameters of your models.twoClassSummary()
convience function lets us do this easily
classprobs = TRUE
or the model will throw an errordefaultSummary
for the summaryFunction
argumentmyControl <- trainControl(
method = "cv",
number = 10,
summaryFunction = twoClassSummary,
classProbs = T, # IMPORTANT!
verboseIter = TRUE
)
# Train glm with custom trainControl: model
model <- train(Class ~., Sonar,
method = "glm",
trControl = myControl)
## + Fold01: parameter=none
## - Fold01: parameter=none
## + Fold02: parameter=none
## - Fold02: parameter=none
## + Fold03: parameter=none
## - Fold03: parameter=none
## + Fold04: parameter=none
## - Fold04: parameter=none
## + Fold05: parameter=none
## - Fold05: parameter=none
## + Fold06: parameter=none
## - Fold06: parameter=none
## + Fold07: parameter=none
## - Fold07: parameter=none
## + Fold08: parameter=none
## - Fold08: parameter=none
## + Fold09: parameter=none
## - Fold09: parameter=none
## + Fold10: parameter=none
## - Fold10: parameter=none
## Aggregating results
## Fitting final model on full training set
# Print model to console
model
## Generalized Linear Model
##
## 208 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 187, 187, 187, 187, 188, 187, ...
## Resampling results:
##
## ROC Sens Spec
## 0.7473737 0.7575758 0.6711111
Quick random forest example:
data(Sonar)
set.seed(42)
model <- train(Class~., data = Sonar, method = "ranger")
plot(model)
I’ll learn what this plot means later I think.
randomForest
method in R but much faster. It is strongly suggest by the instructors to use this.glimpse(wine)
## Observations: 100
## Variables: 13
## $ fixed.acidity <dbl> 6.7, 6.7, 5.8, 6.3, 6.6, 7.8, 5.5, 9.1, 6...
## $ volatile.acidity <dbl> 0.270, 0.480, 0.360, 0.320, 0.240, 0.390,...
## $ citric.acid <dbl> 0.69, 0.49, 0.38, 0.26, 0.28, 0.26, 0.33,...
## $ residual.sugar <dbl> 1.2, 2.9, 0.9, 12.0, 1.8, 9.9, 1.0, 1.6, ...
## $ chlorides <dbl> 0.176, 0.030, 0.037, 0.049, 0.028, 0.059,...
## $ free.sulfur.dioxide <dbl> 36.0, 28.0, 3.0, 63.0, 39.0, 33.0, 23.0, ...
## $ total.sulfur.dioxide <dbl> 106.0, 122.0, 75.0, 170.0, 132.0, 181.0, ...
## $ density <dbl> 0.99288, 0.98926, 0.99040, 0.99610, 0.991...
## $ pH <dbl> 2.96, 3.13, 3.28, 3.14, 3.34, 3.04, 3.25,...
## $ sulphates <dbl> 0.43, 0.40, 0.34, 0.55, 0.46, 0.42, 0.45,...
## $ alcohol <dbl> 9.20, 13.00, 11.40, 9.90, 11.40, 10.90, 9...
## $ quality <int> 6, 6, 4, 6, 5, 6, 5, 7, 7, 6, 6, 6, 6, 6,...
## $ color <fctr> white, white, white, white, white, white...
# Fit random forest: model
model <- train(
quality ~ .,
data = wine,
method = "ranger",
tuneLength = 1,
trControl = trainControl(
method = "cv",
number = 5,
verboseIter = TRUE
)
)
## + Fold1: mtry=3, splitrule=variance
## - Fold1: mtry=3, splitrule=variance
## + Fold1: mtry=3, splitrule=extratrees
## - Fold1: mtry=3, splitrule=extratrees
## + Fold2: mtry=3, splitrule=variance
## - Fold2: mtry=3, splitrule=variance
## + Fold2: mtry=3, splitrule=extratrees
## - Fold2: mtry=3, splitrule=extratrees
## + Fold3: mtry=3, splitrule=variance
## - Fold3: mtry=3, splitrule=variance
## + Fold3: mtry=3, splitrule=extratrees
## - Fold3: mtry=3, splitrule=extratrees
## + Fold4: mtry=3, splitrule=variance
## - Fold4: mtry=3, splitrule=variance
## + Fold4: mtry=3, splitrule=extratrees
## - Fold4: mtry=3, splitrule=extratrees
## + Fold5: mtry=3, splitrule=variance
## - Fold5: mtry=3, splitrule=variance
## + Fold5: mtry=3, splitrule=extratrees
## - Fold5: mtry=3, splitrule=extratrees
## Aggregating results
## Selecting tuning parameters
## Fitting mtry = 3, splitrule = variance on full training set
# Print model to console
model
## Random Forest
##
## 100 samples
## 12 predictor
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 80, 80, 80, 80, 80
## Resampling results across tuning parameters:
##
## splitrule RMSE Rsquared MAE
## variance 0.6494058 0.3317282 0.4912203
## extratrees 0.6916759 0.2631151 0.5162687
##
## Tuning parameter 'mtry' was held constant at a value of 3
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were mtry = 3 and splitrule = variance.
mtry
parameter is one of the key onestunelength
allows us to explore more potential models and can potentially find a better model
# Fit random forest: model
model <- train(
quality ~ .,
tuneLength = 3,
data = wine,
method = "ranger",
trControl = trainControl(
method = "cv",
number = 5,
verboseIter = TRUE)
)
## + Fold1: mtry= 2, splitrule=variance
## - Fold1: mtry= 2, splitrule=variance
## + Fold1: mtry= 7, splitrule=variance
## - Fold1: mtry= 7, splitrule=variance
## + Fold1: mtry=12, splitrule=variance
## - Fold1: mtry=12, splitrule=variance
## + Fold1: mtry= 2, splitrule=extratrees
## - Fold1: mtry= 2, splitrule=extratrees
## + Fold1: mtry= 7, splitrule=extratrees
## - Fold1: mtry= 7, splitrule=extratrees
## + Fold1: mtry=12, splitrule=extratrees
## - Fold1: mtry=12, splitrule=extratrees
## + Fold2: mtry= 2, splitrule=variance
## - Fold2: mtry= 2, splitrule=variance
## + Fold2: mtry= 7, splitrule=variance
## - Fold2: mtry= 7, splitrule=variance
## + Fold2: mtry=12, splitrule=variance
## - Fold2: mtry=12, splitrule=variance
## + Fold2: mtry= 2, splitrule=extratrees
## - Fold2: mtry= 2, splitrule=extratrees
## + Fold2: mtry= 7, splitrule=extratrees
## - Fold2: mtry= 7, splitrule=extratrees
## + Fold2: mtry=12, splitrule=extratrees
## - Fold2: mtry=12, splitrule=extratrees
## + Fold3: mtry= 2, splitrule=variance
## - Fold3: mtry= 2, splitrule=variance
## + Fold3: mtry= 7, splitrule=variance
## - Fold3: mtry= 7, splitrule=variance
## + Fold3: mtry=12, splitrule=variance
## - Fold3: mtry=12, splitrule=variance
## + Fold3: mtry= 2, splitrule=extratrees
## - Fold3: mtry= 2, splitrule=extratrees
## + Fold3: mtry= 7, splitrule=extratrees
## - Fold3: mtry= 7, splitrule=extratrees
## + Fold3: mtry=12, splitrule=extratrees
## - Fold3: mtry=12, splitrule=extratrees
## + Fold4: mtry= 2, splitrule=variance
## - Fold4: mtry= 2, splitrule=variance
## + Fold4: mtry= 7, splitrule=variance
## - Fold4: mtry= 7, splitrule=variance
## + Fold4: mtry=12, splitrule=variance
## - Fold4: mtry=12, splitrule=variance
## + Fold4: mtry= 2, splitrule=extratrees
## - Fold4: mtry= 2, splitrule=extratrees
## + Fold4: mtry= 7, splitrule=extratrees
## - Fold4: mtry= 7, splitrule=extratrees
## + Fold4: mtry=12, splitrule=extratrees
## - Fold4: mtry=12, splitrule=extratrees
## + Fold5: mtry= 2, splitrule=variance
## - Fold5: mtry= 2, splitrule=variance
## + Fold5: mtry= 7, splitrule=variance
## - Fold5: mtry= 7, splitrule=variance
## + Fold5: mtry=12, splitrule=variance
## - Fold5: mtry=12, splitrule=variance
## + Fold5: mtry= 2, splitrule=extratrees
## - Fold5: mtry= 2, splitrule=extratrees
## + Fold5: mtry= 7, splitrule=extratrees
## - Fold5: mtry= 7, splitrule=extratrees
## + Fold5: mtry=12, splitrule=extratrees
## - Fold5: mtry=12, splitrule=extratrees
## Aggregating results
## Selecting tuning parameters
## Fitting mtry = 12, splitrule = variance on full training set
# Print model to console
model
## Random Forest
##
## 100 samples
## 12 predictor
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 80, 80, 80, 80, 80
## Resampling results across tuning parameters:
##
## mtry splitrule RMSE Rsquared MAE
## 2 variance 0.6509389 0.3520433 0.4895340
## 2 extratrees 0.6894546 0.2800724 0.5150384
## 7 variance 0.6237783 0.3952847 0.4768267
## 7 extratrees 0.6841207 0.2475494 0.5174830
## 12 variance 0.6189958 0.3965301 0.4781017
## 12 extratrees 0.6742885 0.2692004 0.5121810
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were mtry = 12 and splitrule
## = variance.
# Plot model
plot(model)
# Define a custome tuning grid
myGrid <- data.frame(mtry = c(2,3,4,5,10,20,30,40),
splitrule = rep("extratrees",8))
# fit a model with a custom tuning grid
set.seed(42)
model <- train(Class ~ .,
data = Sonar,
method = "ranger",
tuneGrid = myGrid)
plot(model)
# Fit random forest: model
model <- train(
quality ~ .,
tuneGrid = data.frame(mtry = c(2,3,7,2,3,7),
splitrule = c("variance","variance","variance","extratrees","extratrees","extratrees")),
data = wine,
method = "ranger",
trControl = trainControl(
method = "cv",
number = 5,
verboseIter = TRUE)
)
## + Fold1: mtry=2, splitrule=variance
## - Fold1: mtry=2, splitrule=variance
## + Fold1: mtry=3, splitrule=variance
## - Fold1: mtry=3, splitrule=variance
## + Fold1: mtry=7, splitrule=variance
## - Fold1: mtry=7, splitrule=variance
## + Fold1: mtry=2, splitrule=extratrees
## - Fold1: mtry=2, splitrule=extratrees
## + Fold1: mtry=3, splitrule=extratrees
## - Fold1: mtry=3, splitrule=extratrees
## + Fold1: mtry=7, splitrule=extratrees
## - Fold1: mtry=7, splitrule=extratrees
## + Fold2: mtry=2, splitrule=variance
## - Fold2: mtry=2, splitrule=variance
## + Fold2: mtry=3, splitrule=variance
## - Fold2: mtry=3, splitrule=variance
## + Fold2: mtry=7, splitrule=variance
## - Fold2: mtry=7, splitrule=variance
## + Fold2: mtry=2, splitrule=extratrees
## - Fold2: mtry=2, splitrule=extratrees
## + Fold2: mtry=3, splitrule=extratrees
## - Fold2: mtry=3, splitrule=extratrees
## + Fold2: mtry=7, splitrule=extratrees
## - Fold2: mtry=7, splitrule=extratrees
## + Fold3: mtry=2, splitrule=variance
## - Fold3: mtry=2, splitrule=variance
## + Fold3: mtry=3, splitrule=variance
## - Fold3: mtry=3, splitrule=variance
## + Fold3: mtry=7, splitrule=variance
## - Fold3: mtry=7, splitrule=variance
## + Fold3: mtry=2, splitrule=extratrees
## - Fold3: mtry=2, splitrule=extratrees
## + Fold3: mtry=3, splitrule=extratrees
## - Fold3: mtry=3, splitrule=extratrees
## + Fold3: mtry=7, splitrule=extratrees
## - Fold3: mtry=7, splitrule=extratrees
## + Fold4: mtry=2, splitrule=variance
## - Fold4: mtry=2, splitrule=variance
## + Fold4: mtry=3, splitrule=variance
## - Fold4: mtry=3, splitrule=variance
## + Fold4: mtry=7, splitrule=variance
## - Fold4: mtry=7, splitrule=variance
## + Fold4: mtry=2, splitrule=extratrees
## - Fold4: mtry=2, splitrule=extratrees
## + Fold4: mtry=3, splitrule=extratrees
## - Fold4: mtry=3, splitrule=extratrees
## + Fold4: mtry=7, splitrule=extratrees
## - Fold4: mtry=7, splitrule=extratrees
## + Fold5: mtry=2, splitrule=variance
## - Fold5: mtry=2, splitrule=variance
## + Fold5: mtry=3, splitrule=variance
## - Fold5: mtry=3, splitrule=variance
## + Fold5: mtry=7, splitrule=variance
## - Fold5: mtry=7, splitrule=variance
## + Fold5: mtry=2, splitrule=extratrees
## - Fold5: mtry=2, splitrule=extratrees
## + Fold5: mtry=3, splitrule=extratrees
## - Fold5: mtry=3, splitrule=extratrees
## + Fold5: mtry=7, splitrule=extratrees
## - Fold5: mtry=7, splitrule=extratrees
## Aggregating results
## Selecting tuning parameters
## Fitting mtry = 7, splitrule = variance on full training set
# Print model to console
model
## Random Forest
##
## 100 samples
## 12 predictor
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 80, 80, 80, 80, 80
## Resampling results across tuning parameters:
##
## mtry splitrule RMSE Rsquared MAE
## 2 extratrees 0.6998244 0.2294546 0.5205483
## 2 variance 0.6491212 0.3552880 0.4919073
## 3 extratrees 0.6967726 0.2451629 0.5185667
## 3 variance 0.6529793 0.3223926 0.4939690
## 7 extratrees 0.6847887 0.2512857 0.5122317
## 7 variance 0.6461164 0.3229418 0.4852607
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were mtry = 7 and splitrule = variance.
# Plot model
plot(model)
Example: “don’t overfit” first kaggle competition the instructor competed in
# load data
overfit <- read.csv("http://s3.amazonaws.com/assets.datacamp.com/production/course_1048/datasets/overfit.csv")
glimpse(overfit)
## Observations: 250
## Variables: 201
## $ y <fctr> class2, class2, class2, class1, class2, class2, class2, ...
## $ X1 <dbl> 0.91480604, 0.93707541, 0.28613953, 0.83044763, 0.6417455...
## $ X2 <dbl> 0.33423133, 0.18843433, 0.26971618, 0.53074408, 0.0214502...
## $ X3 <dbl> 0.1365052, 0.1771364, 0.5195605, 0.8111208, 0.1153620, 0....
## $ X4 <dbl> 0.244920995, 0.087635909, 0.391108497, 0.182561425, 0.133...
## $ X5 <dbl> 0.84829322, 0.06274633, 0.81984509, 0.53936029, 0.4990201...
## $ X6 <dbl> 0.73592037, 0.75178575, 0.33261448, 0.05754862, 0.6744154...
## $ X7 <dbl> 0.053911000, 0.955095770, 0.025600940, 0.920763139, 0.366...
## $ X8 <dbl> 0.16517872, 0.72778108, 0.20615786, 0.58646553, 0.9135459...
## $ X9 <dbl> 0.98996559, 0.43849361, 0.69990322, 0.88907696, 0.8341594...
## $ X10 <dbl> 0.24640458, 0.02302811, 0.28421418, 0.81289268, 0.7189183...
## $ X11 <dbl> 0.06038098, 0.93300437, 0.34894162, 0.41179789, 0.9611464...
## $ X12 <dbl> 0.40882313, 0.31586194, 0.49472762, 0.64893000, 0.6498624...
## $ X13 <dbl> 0.27379245, 0.94419670, 0.44598332, 0.54178716, 0.1617544...
## $ X14 <dbl> 0.313369113, 0.688717818, 0.532391957, 0.759994458, 0.649...
## $ X15 <dbl> 0.19940178, 0.56649540, 0.16805282, 0.94363115, 0.5044080...
## $ X16 <dbl> 0.85550816, 0.21655583, 0.31746986, 0.54139804, 0.4303393...
## $ X17 <dbl> 0.59892983, 0.96894761, 0.39053533, 0.85281451, 0.0422875...
## $ X18 <dbl> 0.36410202, 0.43261605, 0.72886793, 0.26279787, 0.7689716...
## $ X19 <dbl> 0.29991068, 0.07423252, 0.92831706, 0.40114898, 0.6653751...
## $ X20 <dbl> 0.51241735, 0.49846335, 0.13677730, 0.45071464, 0.3133368...
## $ X21 <dbl> 0.73149367, 0.56095270, 0.49818835, 0.84105947, 0.4636444...
## $ X22 <dbl> 0.88671723, 0.78957374, 0.54639519, 0.06316993, 0.5761548...
## $ X23 <dbl> 0.84276937, 0.76905791, 0.91884069, 0.35572491, 0.9376524...
## $ X24 <dbl> 0.22653454, 0.29303699, 0.12607726, 0.71019872, 0.8479341...
## $ X25 <dbl> 0.24646324, 0.53030519, 0.21397210, 0.02584904, 0.3420024...
## $ X26 <dbl> 0.03078282, 0.91007049, 0.14263568, 0.43959793, 0.4498638...
## $ X27 <dbl> 0.24399196, 0.88613340, 0.36574050, 0.25022753, 0.7712135...
## $ X28 <dbl> 0.440314306, 0.445281863, 0.080888182, 0.152905060, 0.820...
## $ X29 <dbl> 0.61425763, 0.87616254, 0.09615188, 0.17540170, 0.4545790...
## $ X30 <dbl> 0.60700663, 0.03163189, 0.93982109, 0.74308481, 0.1437081...
## $ X31 <dbl> 0.776540211, 0.023826756, 0.431787197, 0.366538520, 0.395...
## $ X32 <dbl> 0.2101436, 0.7116446, 0.4351647, 0.9759542, 0.7004988, 0....
## $ X33 <dbl> 0.44361558, 0.32834604, 0.20785167, 0.19192139, 0.3723859...
## $ X34 <dbl> 0.23989117, 0.89145398, 0.14831578, 0.42380827, 0.7290696...
## $ X35 <dbl> 0.63630187, 0.68359615, 0.46272876, 0.59296401, 0.1989479...
## $ X36 <dbl> 0.57228237, 0.46182573, 0.13543040, 0.58246224, 0.1145796...
## $ X37 <dbl> 0.41975693, 0.82613983, 0.11230086, 0.20392210, 0.4922106...
## $ X38 <dbl> 0.95004438, 0.11276168, 0.85793189, 0.26989327, 0.2721555...
## $ X39 <dbl> 0.35872817, 0.49724268, 0.25065359, 0.77495504, 0.1394773...
## $ X40 <dbl> 0.49694824, 0.99363492, 0.68541077, 0.71740832, 0.5102301...
## $ X41 <dbl> 0.52838964, 0.64638788, 0.83404903, 0.34576255, 0.6217329...
## $ X42 <dbl> 0.53637297, 0.02224803, 0.97147909, 0.60075658, 0.4173548...
## $ X43 <dbl> 0.003498503, 0.470631273, 0.447381378, 0.418609717, 0.865...
## $ X44 <dbl> 0.031088332, 0.371195921, 0.289065880, 0.142796479, 0.645...
## $ X45 <dbl> 0.78957600, 0.60768116, 0.98599694, 0.65056791, 0.3890770...
## $ X46 <dbl> 0.90801477, 0.22839369, 0.03609499, 0.75519239, 0.1496413...
## $ X47 <dbl> 0.57435600, 0.34725679, 0.54284271, 0.27019571, 0.7032646...
## $ X48 <dbl> 0.04658951, 0.81377534, 0.06971775, 0.45726551, 0.9325349...
## $ X49 <dbl> 0.568608369, 0.217880581, 0.101515466, 0.698102254, 0.192...
## $ X50 <dbl> 0.70674255, 0.93513637, 0.32386434, 0.42993684, 0.2638991...
## $ X51 <dbl> 0.35659105, 0.20800169, 0.10062093, 0.13811304, 0.0237243...
## $ X52 <dbl> 0.6176513, 0.3523196, 0.6447219, 0.2108743, 0.9677561, 0....
## $ X53 <dbl> 0.89379587, 0.48063380, 0.16016976, 0.90606024, 0.7480467...
## $ X54 <dbl> 0.09195006, 0.04368308, 0.03980983, 0.67766094, 0.3246483...
## $ X55 <dbl> 0.40665568, 0.90273489, 0.18040390, 0.05992027, 0.3798599...
## $ X56 <dbl> 0.82231507, 0.46761749, 0.88325679, 0.23977046, 0.0860767...
## $ X57 <dbl> 0.92166222, 0.02544768, 0.71131622, 0.08098793, 0.8367631...
## $ X58 <dbl> 0.70143425, 0.20914840, 0.30949109, 0.75071442, 0.8386771...
## $ X59 <dbl> 0.57086475, 0.31707423, 0.00622546, 0.32235315, 0.7659728...
## $ X60 <dbl> 0.22403629, 0.71769261, 0.23348081, 0.11093189, 0.0150913...
## $ X61 <dbl> 0.61544117, 0.81500369, 0.34300935, 0.38827650, 0.0146181...
## $ X62 <dbl> 0.39420984, 0.94813472, 0.75282056, 0.86577343, 0.3746095...
## $ X63 <dbl> 0.88882277, 0.51720327, 0.34884470, 0.85602299, 0.1388358...
## $ X64 <dbl> 0.609959334, 0.801461577, 0.524867217, 0.672061241, 0.290...
## $ X65 <dbl> 0.47709234, 0.19922838, 0.40166670, 0.57517642, 0.0637341...
## $ X66 <dbl> 0.55782963, 0.58179578, 0.35338641, 0.91979352, 0.1432807...
## $ X67 <dbl> 0.69121938, 0.35959736, 0.51719022, 0.49845355, 0.1268600...
## $ X68 <dbl> 0.79023679, 0.29431606, 0.32632441, 0.02167086, 0.4726536...
## $ X69 <dbl> 0.53079266, 0.64474625, 0.13998155, 0.31667922, 0.8713631...
## $ X70 <dbl> 0.61211085, 0.22469525, 0.44505162, 0.44177333, 0.8194539...
## $ X71 <dbl> 0.992744554, 0.660086332, 0.990871294, 0.843593776, 0.931...
## $ X72 <dbl> 0.66891022, 0.76956286, 0.04286123, 0.09322869, 0.3702975...
## $ X73 <dbl> 0.1782290782, 0.5898728392, 0.3100116646, 0.9048300500, 0...
## $ X74 <dbl> 0.65168895, 0.09957865, 0.61739505, 0.37862555, 0.0233105...
## $ X75 <dbl> 0.13139016, 0.20797646, 0.65724972, 0.05183997, 0.8911270...
## $ X76 <dbl> 0.6016485, 0.5147675, 0.9524858, 0.6899449, 0.6909429, 0....
## $ X77 <dbl> 0.80038216, 0.37053159, 0.43891339, 0.08944712, 0.1671795...
## $ X78 <dbl> 0.03375497, 0.19464773, 0.64983446, 0.21965484, 0.8581793...
## $ X79 <dbl> 0.61489699, 0.07567500, 0.55251763, 0.46364387, 0.9300709...
## $ X80 <dbl> 0.550195466, 0.907458914, 0.782375696, 0.605479471, 0.914...
## $ X81 <dbl> 0.87760735, 0.45543547, 0.42456263, 0.93879964, 0.3861163...
## $ X82 <dbl> 0.87712898, 0.55860808, 0.68894556, 0.67370478, 0.1886578...
## $ X83 <dbl> 0.45917438, 0.96913990, 0.79622297, 0.11110564, 0.4308363...
## $ X84 <dbl> 2.265618e-02, 5.166253e-01, 1.737207e-01, 8.481156e-01, 4...
## $ X85 <dbl> 0.82100470, 0.09394557, 0.17385249, 0.08457514, 0.9957042...
## $ X86 <dbl> 0.9451794, 0.6691676, 0.3524983, 0.2725157, 0.7055095, 0....
## $ X87 <dbl> 0.40031711, 0.36259803, 0.93234951, 0.21494830, 0.1853166...
## $ X88 <dbl> 0.5063815, 0.4020096, 0.1412987, 0.6899701, 0.1049531, 0....
## $ X89 <dbl> 0.38419708, 0.38109557, 0.67869493, 0.75592458, 0.0621395...
## $ X90 <dbl> 0.5978431, 0.3437839, 0.3873761, 0.8271274, 0.7161128, 0....
## $ X91 <dbl> 0.31784285, 0.95808926, 0.26911697, 0.60201218, 0.5659496...
## $ X92 <dbl> 0.63577017, 0.18256200, 0.61096669, 0.09389158, 0.5087271...
## $ X93 <dbl> 0.16898034, 0.16977626, 0.05505954, 0.65476919, 0.8837555...
## $ X94 <dbl> 0.43631094, 0.50747459, 0.70454115, 0.66092330, 0.2501755...
## $ X95 <dbl> 0.74558734, 0.88742323, 0.05273777, 0.12751347, 0.4315957...
## $ X96 <dbl> 0.91218140, 0.74437113, 0.24672669, 0.74064849, 0.8910056...
## $ X97 <dbl> 0.521450796, 0.002551967, 0.774404447, 0.037558054, 0.598...
## $ X98 <dbl> 0.09177065, 0.72371277, 0.47358313, 0.06946549, 0.4187190...
## $ X99 <dbl> 0.73411696, 0.20450219, 0.75417832, 0.30300187, 0.1878361...
## $ X100 <dbl> 0.320770567, 0.593723377, 0.858523079, 0.757005788, 0.591...
## $ X101 <dbl> 0.34108814, 0.07692370, 0.17643467, 0.43990862, 0.9375751...
## $ X102 <dbl> 0.6124809, 0.1278179, 0.2325618, 0.2011980, 0.3795888, 0....
## $ X103 <dbl> 0.0006371774, 0.9994430819, 0.3747437985, 0.0339846939, 0...
## $ X104 <dbl> 0.54743368, 0.47927159, 0.17748406, 0.63595546, 0.5783285...
## $ X105 <dbl> 0.03589639, 0.96449950, 0.45762041, 0.68747009, 0.9666334...
## $ X106 <dbl> 0.64268733, 0.34132692, 0.03583915, 0.19993296, 0.6526820...
## $ X107 <dbl> 0.23045683, 0.25149597, 0.04275977, 0.98033429, 0.4848958...
## $ X108 <dbl> 0.593971942, 0.528231378, 0.809968454, 0.588263725, 0.220...
## $ X109 <dbl> 0.53344042, 0.03137963, 0.04959282, 0.23380904, 0.8963145...
## $ X110 <dbl> 0.95277784, 0.65035223, 0.26233431, 0.96049400, 0.7733770...
## $ X111 <dbl> 0.95001493, 0.80696333, 0.76912510, 0.74866022, 0.5731922...
## $ X112 <dbl> 0.44486670, 0.34772122, 0.80746171, 0.03664129, 0.8928324...
## $ X113 <dbl> 0.01083590, 0.12578299, 0.50185677, 0.18031296, 0.0530103...
## $ X114 <dbl> 0.01484451, 0.17502226, 0.25231490, 0.99640743, 0.8664863...
## $ X115 <dbl> 0.06289119, 0.89214340, 0.28707657, 0.64055932, 0.1317425...
## $ X116 <dbl> 0.581997880, 0.665076771, 0.875653535, 0.070306693, 0.004...
## $ X117 <dbl> 0.5924448, 0.8878863, 0.5235236, 0.3749164, 0.8752077, 0....
## $ X118 <dbl> 0.46235305, 0.86934930, 0.22476541, 0.54021836, 0.6109594...
## $ X119 <dbl> 0.450867994, 0.529698077, 0.158835076, 0.062714112, 0.449...
## $ X120 <dbl> 0.194360079, 0.976134765, 0.987192112, 0.511937499, 0.198...
## $ X121 <dbl> 0.15381438, 0.40933156, 0.22537445, 0.35218675, 0.1101618...
## $ X122 <dbl> 0.982242217, 0.520649012, 0.445695450, 0.462261930, 0.002...
## $ X123 <dbl> 0.29473281, 0.25111598, 0.80001735, 0.23422839, 0.1068260...
## $ X124 <dbl> 0.110216588, 0.080252397, 0.625394831, 0.722216522, 0.076...
## $ X125 <dbl> 0.40581025, 0.77477036, 0.36472709, 0.48756066, 0.7666030...
## $ X126 <dbl> 0.5527448577, 0.8601254835, 0.7557375506, 0.4862461151, 0...
## $ X127 <dbl> 0.287887782, 0.130535906, 0.678490788, 0.731766421, 0.852...
## $ X128 <dbl> 0.55946411, 0.21613476, 0.64567381, 0.05425676, 0.7748187...
## $ X129 <dbl> 0.68992925, 0.65888045, 0.69797577, 0.28211569, 0.3364384...
## $ X130 <dbl> 0.60577396, 0.12628377, 0.45778471, 0.31388965, 0.6482185...
## $ X131 <dbl> 0.63405048, 0.91769299, 0.91924084, 0.71750403, 0.5750649...
## $ X132 <dbl> 0.041380533, 0.404302723, 0.766801183, 0.005256724, 0.354...
## $ X133 <dbl> 0.11652459, 0.03312483, 0.12827443, 0.82357734, 0.3065927...
## $ X134 <dbl> 0.58257253, 0.56697654, 0.98460286, 0.52459081, 0.5602921...
## $ X135 <dbl> 0.70794842, 0.48408234, 0.77778677, 0.02017853, 0.0177778...
## $ X136 <dbl> 0.290923866, 0.461145974, 0.862764690, 0.664291186, 0.342...
## $ X137 <dbl> 0.54379718, 0.79342000, 0.22947185, 0.35309806, 0.3047963...
## $ X138 <dbl> 0.12864488, 0.23981618, 0.57799386, 0.61747643, 0.4213162...
## $ X139 <dbl> 0.80948772, 0.82146921, 0.18526767, 0.25488536, 0.9207230...
## $ X140 <dbl> 0.40163113, 0.79277151, 0.11738340, 0.36496598, 0.5366804...
## $ X141 <dbl> 0.93672809, 0.93622742, 0.89760746, 0.45901700, 0.5760530...
## $ X142 <dbl> 0.27464358, 0.29368103, 0.62729678, 0.44948535, 0.1779099...
## $ X143 <dbl> 0.59995153, 0.16334854, 0.17710410, 0.26653590, 0.0014934...
## $ X144 <dbl> 0.61420305, 0.70992172, 0.07593862, 0.77857178, 0.6301943...
## $ X145 <dbl> 0.84734796, 0.31810196, 0.07364713, 0.02329525, 0.9218279...
## $ X146 <dbl> 0.21923829, 0.15674629, 0.08922226, 0.74250158, 0.1047593...
## $ X147 <dbl> 0.82867974, 0.97047839, 0.47023973, 0.93823417, 0.9565947...
## $ X148 <dbl> 0.94290037, 0.64720576, 0.55771586, 0.39179048, 0.0940794...
## $ X149 <dbl> 0.50831853, 0.71756154, 0.45590210, 0.62706908, 0.8008656...
## $ X150 <dbl> 0.25527703, 0.03806863, 0.45961098, 0.29450119, 0.6976419...
## $ X151 <dbl> 0.75268296, 0.89675927, 0.76857671, 0.22820199, 0.7520310...
## $ X152 <dbl> 0.01497518, 0.04082960, 0.70849826, 0.07697517, 0.1076180...
## $ X153 <dbl> 0.96331279, 0.80074165, 0.39650955, 0.63353348, 0.0972440...
## $ X154 <dbl> 0.16493185, 0.83442190, 0.96948408, 0.69882495, 0.0847765...
## $ X155 <dbl> 0.5734490, 0.7298989, 0.5114832, 0.9638506, 0.8876985, 0....
## $ X156 <dbl> 0.61722732, 0.48946893, 0.98226532, 0.47115250, 0.2279777...
## $ X157 <dbl> 0.01984267, 0.71974924, 0.13222445, 0.15887531, 0.6819612...
## $ X158 <dbl> 0.99238742, 0.76420404, 0.12251792, 0.87889145, 0.9136540...
## $ X159 <dbl> 0.8431326, 0.9155692, 0.2006651, 0.6495318, 0.5322480, 0....
## $ X160 <dbl> 0.4516252, 0.3701119, 0.2851132, 0.7246809, 0.6712590, 0....
## $ X161 <dbl> 0.27610230, 0.30687340, 0.41455622, 0.91913345, 0.5334006...
## $ X162 <dbl> 0.74179679, 0.30933747, 0.66539232, 0.06219363, 0.4495071...
## $ X163 <dbl> 0.48524905, 0.36506517, 0.05123008, 0.49290181, 0.1527127...
## $ X164 <dbl> 0.003358962, 0.837234325, 0.249701695, 0.994318883, 0.331...
## $ X165 <dbl> 0.35415790, 0.13497580, 0.25971016, 0.60124062, 0.2942072...
## $ X166 <dbl> 0.89672245, 0.46684672, 0.53437967, 0.77532677, 0.6948135...
## $ X167 <dbl> 0.88342865, 0.52352974, 0.30868933, 0.79706734, 0.8076349...
## $ X168 <dbl> 0.80612878, 0.62438562, 0.32505099, 0.45196535, 0.8714401...
## $ X169 <dbl> 0.12920871, 0.65226440, 0.55483697, 0.41089107, 0.9276495...
## $ X170 <dbl> 0.191168652, 0.932518969, 0.666639159, 0.996674388, 0.080...
## $ X171 <dbl> 0.285561043, 0.198662249, 0.803086133, 0.394731248, 0.715...
## $ X172 <dbl> 0.88923767, 0.11641914, 0.75219803, 0.70289791, 0.0153314...
## $ X173 <dbl> 0.587049352, 0.133504347, 0.906179729, 0.392166968, 0.941...
## $ X174 <dbl> 0.88877075, 0.85607289, 0.46824726, 0.32963398, 0.0496130...
## $ X175 <dbl> 0.15143056, 0.42410270, 0.43306244, 0.83792064, 0.6613412...
## $ X176 <dbl> 0.212245659, 0.516595498, 0.044004837, 0.018832981, 0.279...
## $ X177 <dbl> 0.15721560, 0.88458393, 0.43112766, 0.94759465, 0.1502022...
## $ X178 <dbl> 0.41750018, 0.15750317, 0.94900001, 0.77882266, 0.4259149...
## $ X179 <dbl> 0.7225748, 0.0794626, 0.9359233, 0.7653506, 0.9541247, 0....
## $ X180 <dbl> 0.75372096, 0.61866392, 0.22093972, 0.49391558, 0.3108216...
## $ X181 <dbl> 0.39552157, 0.87280757, 0.16521581, 0.15224052, 0.9833864...
## $ X182 <dbl> 0.4887184, 0.9823968, 0.8082559, 0.7771462, 0.5740921, 0....
## $ X183 <dbl> 0.50930862, 0.39664574, 0.99125548, 0.34878537, 0.0761662...
## $ X184 <dbl> 0.40526502, 0.04680688, 0.85048518, 0.88635905, 0.2564270...
## $ X185 <dbl> 0.89972692, 0.41385540, 0.14096706, 0.76801954, 0.4703454...
## $ X186 <dbl> 0.20133410, 0.02252797, 0.88720811, 0.64712794, 0.0269461...
## $ X187 <dbl> 0.993279049, 0.551354672, 0.001778496, 0.927141098, 0.934...
## $ X188 <dbl> 0.27672405, 0.80417281, 0.03994549, 0.17404809, 0.2726361...
## $ X189 <dbl> 0.452414463, 0.758011176, 0.527843759, 0.890178879, 0.714...
## $ X190 <dbl> 0.69119399, 0.78047338, 0.25094101, 0.52802851, 0.4105000...
## $ X191 <dbl> 0.54065139, 0.14117820, 0.21067407, 0.82111615, 0.8930576...
## $ X192 <dbl> 0.72225972, 0.13109110, 0.58957105, 0.59130608, 0.9879968...
## $ X193 <dbl> 0.002576062, 0.411775569, 0.891389510, 0.767070756, 0.598...
## $ X194 <dbl> 0.25223405, 0.52042257, 0.77904746, 0.08815411, 0.6051281...
## $ X195 <dbl> 0.55681595, 0.49866879, 0.39123403, 0.82405905, 0.5665961...
## $ X196 <dbl> 0.08803946, 0.67626884, 0.09020876, 0.41487972, 0.0170128...
## $ X197 <dbl> 0.739964231, 0.972430318, 0.659945773, 0.333751146, 0.236...
## $ X198 <dbl> 0.37386198, 0.83937505, 0.83710146, 0.55367959, 0.7016139...
## $ X199 <dbl> 0.671563295, 0.746817349, 0.978584267, 0.704102835, 0.700...
## $ X200 <dbl> 0.0001012264, 0.8653565315, 0.3703812116, 0.6651317959, 0...
# Make a custom trainControl
myControl <- trainControl(
method = "cv",
number = 10,
summaryFunction = twoClassSummary,
classProbs = T,
verboseIter = T
)
# We will start with a simple model that uses default auc and tuning grid (3 alpha, 3 lambda)
# fit a model
set.seed(42)
model <- train(y ~ ., overfit,
method = "glmnet",
trControl = myControl)
## + Fold01: alpha=0.10, lambda=0.01013
## - Fold01: alpha=0.10, lambda=0.01013
## + Fold01: alpha=0.55, lambda=0.01013
## - Fold01: alpha=0.55, lambda=0.01013
## + Fold01: alpha=1.00, lambda=0.01013
## - Fold01: alpha=1.00, lambda=0.01013
## + Fold02: alpha=0.10, lambda=0.01013
## - Fold02: alpha=0.10, lambda=0.01013
## + Fold02: alpha=0.55, lambda=0.01013
## - Fold02: alpha=0.55, lambda=0.01013
## + Fold02: alpha=1.00, lambda=0.01013
## - Fold02: alpha=1.00, lambda=0.01013
## + Fold03: alpha=0.10, lambda=0.01013
## - Fold03: alpha=0.10, lambda=0.01013
## + Fold03: alpha=0.55, lambda=0.01013
## - Fold03: alpha=0.55, lambda=0.01013
## + Fold03: alpha=1.00, lambda=0.01013
## - Fold03: alpha=1.00, lambda=0.01013
## + Fold04: alpha=0.10, lambda=0.01013
## - Fold04: alpha=0.10, lambda=0.01013
## + Fold04: alpha=0.55, lambda=0.01013
## - Fold04: alpha=0.55, lambda=0.01013
## + Fold04: alpha=1.00, lambda=0.01013
## - Fold04: alpha=1.00, lambda=0.01013
## + Fold05: alpha=0.10, lambda=0.01013
## - Fold05: alpha=0.10, lambda=0.01013
## + Fold05: alpha=0.55, lambda=0.01013
## - Fold05: alpha=0.55, lambda=0.01013
## + Fold05: alpha=1.00, lambda=0.01013
## - Fold05: alpha=1.00, lambda=0.01013
## + Fold06: alpha=0.10, lambda=0.01013
## - Fold06: alpha=0.10, lambda=0.01013
## + Fold06: alpha=0.55, lambda=0.01013
## - Fold06: alpha=0.55, lambda=0.01013
## + Fold06: alpha=1.00, lambda=0.01013
## - Fold06: alpha=1.00, lambda=0.01013
## + Fold07: alpha=0.10, lambda=0.01013
## - Fold07: alpha=0.10, lambda=0.01013
## + Fold07: alpha=0.55, lambda=0.01013
## - Fold07: alpha=0.55, lambda=0.01013
## + Fold07: alpha=1.00, lambda=0.01013
## - Fold07: alpha=1.00, lambda=0.01013
## + Fold08: alpha=0.10, lambda=0.01013
## - Fold08: alpha=0.10, lambda=0.01013
## + Fold08: alpha=0.55, lambda=0.01013
## - Fold08: alpha=0.55, lambda=0.01013
## + Fold08: alpha=1.00, lambda=0.01013
## - Fold08: alpha=1.00, lambda=0.01013
## + Fold09: alpha=0.10, lambda=0.01013
## - Fold09: alpha=0.10, lambda=0.01013
## + Fold09: alpha=0.55, lambda=0.01013
## - Fold09: alpha=0.55, lambda=0.01013
## + Fold09: alpha=1.00, lambda=0.01013
## - Fold09: alpha=1.00, lambda=0.01013
## + Fold10: alpha=0.10, lambda=0.01013
## - Fold10: alpha=0.10, lambda=0.01013
## + Fold10: alpha=0.55, lambda=0.01013
## - Fold10: alpha=0.55, lambda=0.01013
## + Fold10: alpha=1.00, lambda=0.01013
## - Fold10: alpha=1.00, lambda=0.01013
## Aggregating results
## Selecting tuning parameters
## Fitting alpha = 0.1, lambda = 0.0101 on full training set
plot(model)
- My plot results always look a lot different than the class example. - They had the highest point as the middle dot on the the top line. - and it was a much lower value, around .40. - Clearly there is a lot of randomness built into these ML models
summaryFunction
to the train()
function to use the AUC metric to rank your models.classProbs = TRUE
, otherwise the twoClassSummary
for summaryFunction
will break.twoClassSummary
. I made that mistake at first.# Create custom trainControl: myControl
myControl <- trainControl(
method = "cv",
number = 10,
summaryFunction = twoClassSummary,
classProbs = T, # IMPORTANT!
verboseIter = TRUE
)
# Fit glmnet model: model
model <- train(
y ~ ., overfit,
method = "glmnet",
trControl = myControl
)
## + Fold01: alpha=0.10, lambda=0.01013
## - Fold01: alpha=0.10, lambda=0.01013
## + Fold01: alpha=0.55, lambda=0.01013
## - Fold01: alpha=0.55, lambda=0.01013
## + Fold01: alpha=1.00, lambda=0.01013
## - Fold01: alpha=1.00, lambda=0.01013
## + Fold02: alpha=0.10, lambda=0.01013
## - Fold02: alpha=0.10, lambda=0.01013
## + Fold02: alpha=0.55, lambda=0.01013
## - Fold02: alpha=0.55, lambda=0.01013
## + Fold02: alpha=1.00, lambda=0.01013
## - Fold02: alpha=1.00, lambda=0.01013
## + Fold03: alpha=0.10, lambda=0.01013
## - Fold03: alpha=0.10, lambda=0.01013
## + Fold03: alpha=0.55, lambda=0.01013
## - Fold03: alpha=0.55, lambda=0.01013
## + Fold03: alpha=1.00, lambda=0.01013
## - Fold03: alpha=1.00, lambda=0.01013
## + Fold04: alpha=0.10, lambda=0.01013
## - Fold04: alpha=0.10, lambda=0.01013
## + Fold04: alpha=0.55, lambda=0.01013
## - Fold04: alpha=0.55, lambda=0.01013
## + Fold04: alpha=1.00, lambda=0.01013
## - Fold04: alpha=1.00, lambda=0.01013
## + Fold05: alpha=0.10, lambda=0.01013
## - Fold05: alpha=0.10, lambda=0.01013
## + Fold05: alpha=0.55, lambda=0.01013
## - Fold05: alpha=0.55, lambda=0.01013
## + Fold05: alpha=1.00, lambda=0.01013
## - Fold05: alpha=1.00, lambda=0.01013
## + Fold06: alpha=0.10, lambda=0.01013
## - Fold06: alpha=0.10, lambda=0.01013
## + Fold06: alpha=0.55, lambda=0.01013
## - Fold06: alpha=0.55, lambda=0.01013
## + Fold06: alpha=1.00, lambda=0.01013
## - Fold06: alpha=1.00, lambda=0.01013
## + Fold07: alpha=0.10, lambda=0.01013
## - Fold07: alpha=0.10, lambda=0.01013
## + Fold07: alpha=0.55, lambda=0.01013
## - Fold07: alpha=0.55, lambda=0.01013
## + Fold07: alpha=1.00, lambda=0.01013
## - Fold07: alpha=1.00, lambda=0.01013
## + Fold08: alpha=0.10, lambda=0.01013
## - Fold08: alpha=0.10, lambda=0.01013
## + Fold08: alpha=0.55, lambda=0.01013
## - Fold08: alpha=0.55, lambda=0.01013
## + Fold08: alpha=1.00, lambda=0.01013
## - Fold08: alpha=1.00, lambda=0.01013
## + Fold09: alpha=0.10, lambda=0.01013
## - Fold09: alpha=0.10, lambda=0.01013
## + Fold09: alpha=0.55, lambda=0.01013
## - Fold09: alpha=0.55, lambda=0.01013
## + Fold09: alpha=1.00, lambda=0.01013
## - Fold09: alpha=1.00, lambda=0.01013
## + Fold10: alpha=0.10, lambda=0.01013
## - Fold10: alpha=0.10, lambda=0.01013
## + Fold10: alpha=0.55, lambda=0.01013
## - Fold10: alpha=0.55, lambda=0.01013
## + Fold10: alpha=1.00, lambda=0.01013
## - Fold10: alpha=1.00, lambda=0.01013
## Aggregating results
## Selecting tuning parameters
## Fitting alpha = 0.55, lambda = 0.0101 on full training set
# Print model to console
model
## glmnet
##
## 250 samples
## 200 predictors
## 2 classes: 'class1', 'class2'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 226, 225, 224, 225, 225, 225, ...
## Resampling results across tuning parameters:
##
## alpha lambda ROC Sens Spec
## 0.10 0.0001012745 0.4172101 0.00 0.9742754
## 0.10 0.0010127448 0.4299819 0.00 0.9786232
## 0.10 0.0101274483 0.4361413 0.00 0.9956522
## 0.55 0.0001012745 0.4148551 0.05 0.9445652
## 0.55 0.0010127448 0.4191123 0.05 0.9617754
## 0.55 0.0101274483 0.4596014 0.00 0.9873188
## 1.00 0.0001012745 0.3730072 0.05 0.9315217
## 1.00 0.0010127448 0.3663043 0.00 0.9487319
## 1.00 0.0101274483 0.4227355 0.00 0.9914855
##
## ROC was used to select the optimal model using the largest value.
## The final values used for the model were alpha = 0.55 and lambda
## = 0.01012745.
# Print maximum ROC statistic
max(model$results$ROC)
## [1] 0.4596014
# Make a custom tuning grid
myGrid <- expand.grid(
alpha = 0:1,
lambda = seq(0.0001, 0.1, length = 10)
)
# Fit a model
set.seed(42)
model <- train(
y ~ ., overfit,
method = "glmnet",
tuneGrid = myGrid,
trControl = myControl)
## + Fold01: alpha=0, lambda=0.1
## - Fold01: alpha=0, lambda=0.1
## + Fold01: alpha=1, lambda=0.1
## - Fold01: alpha=1, lambda=0.1
## + Fold02: alpha=0, lambda=0.1
## - Fold02: alpha=0, lambda=0.1
## + Fold02: alpha=1, lambda=0.1
## - Fold02: alpha=1, lambda=0.1
## + Fold03: alpha=0, lambda=0.1
## - Fold03: alpha=0, lambda=0.1
## + Fold03: alpha=1, lambda=0.1
## - Fold03: alpha=1, lambda=0.1
## + Fold04: alpha=0, lambda=0.1
## - Fold04: alpha=0, lambda=0.1
## + Fold04: alpha=1, lambda=0.1
## - Fold04: alpha=1, lambda=0.1
## + Fold05: alpha=0, lambda=0.1
## - Fold05: alpha=0, lambda=0.1
## + Fold05: alpha=1, lambda=0.1
## - Fold05: alpha=1, lambda=0.1
## + Fold06: alpha=0, lambda=0.1
## - Fold06: alpha=0, lambda=0.1
## + Fold06: alpha=1, lambda=0.1
## - Fold06: alpha=1, lambda=0.1
## + Fold07: alpha=0, lambda=0.1
## - Fold07: alpha=0, lambda=0.1
## + Fold07: alpha=1, lambda=0.1
## - Fold07: alpha=1, lambda=0.1
## + Fold08: alpha=0, lambda=0.1
## - Fold08: alpha=0, lambda=0.1
## + Fold08: alpha=1, lambda=0.1
## - Fold08: alpha=1, lambda=0.1
## + Fold09: alpha=0, lambda=0.1
## - Fold09: alpha=0, lambda=0.1
## + Fold09: alpha=1, lambda=0.1
## - Fold09: alpha=1, lambda=0.1
## + Fold10: alpha=0, lambda=0.1
## - Fold10: alpha=0, lambda=0.1
## + Fold10: alpha=1, lambda=0.1
## - Fold10: alpha=1, lambda=0.1
## Aggregating results
## Selecting tuning parameters
## Fitting alpha = 1, lambda = 0.0445 on full training set
plot(model)
The regularization path
# Full regularization path
plot(model$finalModel)
My favorite tuning grid for glmnet models is:
expand.grid(alpha = 0:1,
lambda = seq(0.0001, 1, length = 100))
## alpha lambda
## 1 0 0.0001
## 2 1 0.0001
## 3 0 0.0102
## 4 1 0.0102
## 5 0 0.0203
## 6 1 0.0203
## 7 0 0.0304
## 8 1 0.0304
## 9 0 0.0405
## 10 1 0.0405
## 11 0 0.0506
## 12 1 0.0506
## 13 0 0.0607
## 14 1 0.0607
## 15 0 0.0708
## 16 1 0.0708
## 17 0 0.0809
## 18 1 0.0809
## 19 0 0.0910
## 20 1 0.0910
## 21 0 0.1011
## 22 1 0.1011
## 23 0 0.1112
## 24 1 0.1112
## 25 0 0.1213
## 26 1 0.1213
## 27 0 0.1314
## 28 1 0.1314
## 29 0 0.1415
## 30 1 0.1415
## 31 0 0.1516
## 32 1 0.1516
## 33 0 0.1617
## 34 1 0.1617
## 35 0 0.1718
## 36 1 0.1718
## 37 0 0.1819
## 38 1 0.1819
## 39 0 0.1920
## 40 1 0.1920
## 41 0 0.2021
## 42 1 0.2021
## 43 0 0.2122
## 44 1 0.2122
## 45 0 0.2223
## 46 1 0.2223
## 47 0 0.2324
## 48 1 0.2324
## 49 0 0.2425
## 50 1 0.2425
## 51 0 0.2526
## 52 1 0.2526
## 53 0 0.2627
## 54 1 0.2627
## 55 0 0.2728
## 56 1 0.2728
## 57 0 0.2829
## 58 1 0.2829
## 59 0 0.2930
## 60 1 0.2930
## 61 0 0.3031
## 62 1 0.3031
## 63 0 0.3132
## 64 1 0.3132
## 65 0 0.3233
## 66 1 0.3233
## 67 0 0.3334
## 68 1 0.3334
## 69 0 0.3435
## 70 1 0.3435
## 71 0 0.3536
## 72 1 0.3536
## 73 0 0.3637
## 74 1 0.3637
## 75 0 0.3738
## 76 1 0.3738
## 77 0 0.3839
## 78 1 0.3839
## 79 0 0.3940
## 80 1 0.3940
## 81 0 0.4041
## 82 1 0.4041
## 83 0 0.4142
## 84 1 0.4142
## 85 0 0.4243
## 86 1 0.4243
## 87 0 0.4344
## 88 1 0.4344
## 89 0 0.4445
## 90 1 0.4445
## 91 0 0.4546
## 92 1 0.4546
## 93 0 0.4647
## 94 1 0.4647
## 95 0 0.4748
## 96 1 0.4748
## 97 0 0.4849
## 98 1 0.4849
## 99 0 0.4950
## 100 1 0.4950
## 101 0 0.5051
## 102 1 0.5051
## 103 0 0.5152
## 104 1 0.5152
## 105 0 0.5253
## 106 1 0.5253
## 107 0 0.5354
## 108 1 0.5354
## 109 0 0.5455
## 110 1 0.5455
## 111 0 0.5556
## 112 1 0.5556
## 113 0 0.5657
## 114 1 0.5657
## 115 0 0.5758
## 116 1 0.5758
## 117 0 0.5859
## 118 1 0.5859
## 119 0 0.5960
## 120 1 0.5960
## 121 0 0.6061
## 122 1 0.6061
## 123 0 0.6162
## 124 1 0.6162
## 125 0 0.6263
## 126 1 0.6263
## 127 0 0.6364
## 128 1 0.6364
## 129 0 0.6465
## 130 1 0.6465
## 131 0 0.6566
## 132 1 0.6566
## 133 0 0.6667
## 134 1 0.6667
## 135 0 0.6768
## 136 1 0.6768
## 137 0 0.6869
## 138 1 0.6869
## 139 0 0.6970
## 140 1 0.6970
## 141 0 0.7071
## 142 1 0.7071
## 143 0 0.7172
## 144 1 0.7172
## 145 0 0.7273
## 146 1 0.7273
## 147 0 0.7374
## 148 1 0.7374
## 149 0 0.7475
## 150 1 0.7475
## 151 0 0.7576
## 152 1 0.7576
## 153 0 0.7677
## 154 1 0.7677
## 155 0 0.7778
## 156 1 0.7778
## 157 0 0.7879
## 158 1 0.7879
## 159 0 0.7980
## 160 1 0.7980
## 161 0 0.8081
## 162 1 0.8081
## 163 0 0.8182
## 164 1 0.8182
## 165 0 0.8283
## 166 1 0.8283
## 167 0 0.8384
## 168 1 0.8384
## 169 0 0.8485
## 170 1 0.8485
## 171 0 0.8586
## 172 1 0.8586
## 173 0 0.8687
## 174 1 0.8687
## 175 0 0.8788
## 176 1 0.8788
## 177 0 0.8889
## 178 1 0.8889
## 179 0 0.8990
## 180 1 0.8990
## 181 0 0.9091
## 182 1 0.9091
## 183 0 0.9192
## 184 1 0.9192
## 185 0 0.9293
## 186 1 0.9293
## 187 0 0.9394
## 188 1 0.9394
## 189 0 0.9495
## 190 1 0.9495
## 191 0 0.9596
## 192 1 0.9596
## 193 0 0.9697
## 194 1 0.9697
## 195 0 0.9798
## 196 1 0.9798
## 197 0 0.9899
## 198 1 0.9899
## 199 0 1.0000
## 200 1 1.0000
# Train glmnet with custom trainControl and tuning: model
model <- train(
y ~ ., overfit,
tuneGrid = expand.grid(
alpha = c(0,.05,1),
lambda = seq(0.0001, 1, length = 100)),
method = "glmnet",
trControl = myControl
)
## + Fold01: alpha=0.00, lambda=1
## - Fold01: alpha=0.00, lambda=1
## + Fold01: alpha=0.05, lambda=1
## - Fold01: alpha=0.05, lambda=1
## + Fold01: alpha=1.00, lambda=1
## - Fold01: alpha=1.00, lambda=1
## + Fold02: alpha=0.00, lambda=1
## - Fold02: alpha=0.00, lambda=1
## + Fold02: alpha=0.05, lambda=1
## - Fold02: alpha=0.05, lambda=1
## + Fold02: alpha=1.00, lambda=1
## - Fold02: alpha=1.00, lambda=1
## + Fold03: alpha=0.00, lambda=1
## - Fold03: alpha=0.00, lambda=1
## + Fold03: alpha=0.05, lambda=1
## - Fold03: alpha=0.05, lambda=1
## + Fold03: alpha=1.00, lambda=1
## - Fold03: alpha=1.00, lambda=1
## + Fold04: alpha=0.00, lambda=1
## - Fold04: alpha=0.00, lambda=1
## + Fold04: alpha=0.05, lambda=1
## - Fold04: alpha=0.05, lambda=1
## + Fold04: alpha=1.00, lambda=1
## - Fold04: alpha=1.00, lambda=1
## + Fold05: alpha=0.00, lambda=1
## - Fold05: alpha=0.00, lambda=1
## + Fold05: alpha=0.05, lambda=1
## - Fold05: alpha=0.05, lambda=1
## + Fold05: alpha=1.00, lambda=1
## - Fold05: alpha=1.00, lambda=1
## + Fold06: alpha=0.00, lambda=1
## - Fold06: alpha=0.00, lambda=1
## + Fold06: alpha=0.05, lambda=1
## - Fold06: alpha=0.05, lambda=1
## + Fold06: alpha=1.00, lambda=1
## - Fold06: alpha=1.00, lambda=1
## + Fold07: alpha=0.00, lambda=1
## - Fold07: alpha=0.00, lambda=1
## + Fold07: alpha=0.05, lambda=1
## - Fold07: alpha=0.05, lambda=1
## + Fold07: alpha=1.00, lambda=1
## - Fold07: alpha=1.00, lambda=1
## + Fold08: alpha=0.00, lambda=1
## - Fold08: alpha=0.00, lambda=1
## + Fold08: alpha=0.05, lambda=1
## - Fold08: alpha=0.05, lambda=1
## + Fold08: alpha=1.00, lambda=1
## - Fold08: alpha=1.00, lambda=1
## + Fold09: alpha=0.00, lambda=1
## - Fold09: alpha=0.00, lambda=1
## + Fold09: alpha=0.05, lambda=1
## - Fold09: alpha=0.05, lambda=1
## + Fold09: alpha=1.00, lambda=1
## - Fold09: alpha=1.00, lambda=1
## + Fold10: alpha=0.00, lambda=1
## - Fold10: alpha=0.00, lambda=1
## + Fold10: alpha=0.05, lambda=1
## - Fold10: alpha=0.05, lambda=1
## + Fold10: alpha=1.00, lambda=1
## - Fold10: alpha=1.00, lambda=1
## Aggregating results
## Selecting tuning parameters
## Fitting alpha = 0.05, lambda = 0.869 on full training set
# Print model to console
model
## glmnet
##
## 250 samples
## 200 predictors
## 2 classes: 'class1', 'class2'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 225, 226, 225, 225, 226, 225, ...
## Resampling results across tuning parameters:
##
## alpha lambda ROC Sens Spec
## 0.00 0.0001 0.4199275 0 0.9871377
## 0.00 0.0102 0.4282609 0 0.9871377
## 0.00 0.0203 0.4303442 0 0.9914855
## 0.00 0.0304 0.4368659 0 0.9956522
## 0.00 0.0405 0.4433877 0 0.9956522
## 0.00 0.0506 0.4412138 0 0.9956522
## 0.00 0.0607 0.4389493 0 1.0000000
## 0.00 0.0708 0.4389493 0 1.0000000
## 0.00 0.0809 0.4454710 0 1.0000000
## 0.00 0.0910 0.4497283 0 1.0000000
## 0.00 0.1011 0.4560688 0 1.0000000
## 0.00 0.1112 0.4538949 0 1.0000000
## 0.00 0.1213 0.4559783 0 1.0000000
## 0.00 0.1314 0.4538043 0 1.0000000
## 0.00 0.1415 0.4581522 0 1.0000000
## 0.00 0.1516 0.4559783 0 1.0000000
## 0.00 0.1617 0.4580616 0 1.0000000
## 0.00 0.1718 0.4580616 0 1.0000000
## 0.00 0.1819 0.4601449 0 1.0000000
## 0.00 0.1920 0.4601449 0 1.0000000
## 0.00 0.2021 0.4600543 0 1.0000000
## 0.00 0.2122 0.4622283 0 1.0000000
## 0.00 0.2223 0.4622283 0 1.0000000
## 0.00 0.2324 0.4601449 0 1.0000000
## 0.00 0.2425 0.4601449 0 1.0000000
## 0.00 0.2526 0.4601449 0 1.0000000
## 0.00 0.2627 0.4623188 0 1.0000000
## 0.00 0.2728 0.4623188 0 1.0000000
## 0.00 0.2829 0.4644928 0 1.0000000
## 0.00 0.2930 0.4666667 0 1.0000000
## 0.00 0.3031 0.4666667 0 1.0000000
## 0.00 0.3132 0.4666667 0 1.0000000
## 0.00 0.3233 0.4666667 0 1.0000000
## 0.00 0.3334 0.4666667 0 1.0000000
## 0.00 0.3435 0.4666667 0 1.0000000
## 0.00 0.3536 0.4666667 0 1.0000000
## 0.00 0.3637 0.4666667 0 1.0000000
## 0.00 0.3738 0.4708333 0 1.0000000
## 0.00 0.3839 0.4708333 0 1.0000000
## 0.00 0.3940 0.4686594 0 1.0000000
## 0.00 0.4041 0.4686594 0 1.0000000
## 0.00 0.4142 0.4686594 0 1.0000000
## 0.00 0.4243 0.4751812 0 1.0000000
## 0.00 0.4344 0.4751812 0 1.0000000
## 0.00 0.4445 0.4751812 0 1.0000000
## 0.00 0.4546 0.4751812 0 1.0000000
## 0.00 0.4647 0.4751812 0 1.0000000
## 0.00 0.4748 0.4751812 0 1.0000000
## 0.00 0.4849 0.4751812 0 1.0000000
## 0.00 0.4950 0.4773551 0 1.0000000
## 0.00 0.5051 0.4773551 0 1.0000000
## 0.00 0.5152 0.4773551 0 1.0000000
## 0.00 0.5253 0.4773551 0 1.0000000
## 0.00 0.5354 0.4773551 0 1.0000000
## 0.00 0.5455 0.4773551 0 1.0000000
## 0.00 0.5556 0.4773551 0 1.0000000
## 0.00 0.5657 0.4773551 0 1.0000000
## 0.00 0.5758 0.4773551 0 1.0000000
## 0.00 0.5859 0.4773551 0 1.0000000
## 0.00 0.5960 0.4773551 0 1.0000000
## 0.00 0.6061 0.4794384 0 1.0000000
## 0.00 0.6162 0.4794384 0 1.0000000
## 0.00 0.6263 0.4794384 0 1.0000000
## 0.00 0.6364 0.4794384 0 1.0000000
## 0.00 0.6465 0.4794384 0 1.0000000
## 0.00 0.6566 0.4816123 0 1.0000000
## 0.00 0.6667 0.4837862 0 1.0000000
## 0.00 0.6768 0.4881341 0 1.0000000
## 0.00 0.6869 0.4903080 0 1.0000000
## 0.00 0.6970 0.4903080 0 1.0000000
## 0.00 0.7071 0.4903080 0 1.0000000
## 0.00 0.7172 0.4903080 0 1.0000000
## 0.00 0.7273 0.4903080 0 1.0000000
## 0.00 0.7374 0.4903080 0 1.0000000
## 0.00 0.7475 0.4924819 0 1.0000000
## 0.00 0.7576 0.4924819 0 1.0000000
## 0.00 0.7677 0.4924819 0 1.0000000
## 0.00 0.7778 0.4924819 0 1.0000000
## 0.00 0.7879 0.4924819 0 1.0000000
## 0.00 0.7980 0.4924819 0 1.0000000
## 0.00 0.8081 0.4924819 0 1.0000000
## 0.00 0.8182 0.4924819 0 1.0000000
## 0.00 0.8283 0.4924819 0 1.0000000
## 0.00 0.8384 0.4924819 0 1.0000000
## 0.00 0.8485 0.4924819 0 1.0000000
## 0.00 0.8586 0.4924819 0 1.0000000
## 0.00 0.8687 0.4924819 0 1.0000000
## 0.00 0.8788 0.4924819 0 1.0000000
## 0.00 0.8889 0.4924819 0 1.0000000
## 0.00 0.8990 0.4924819 0 1.0000000
## 0.00 0.9091 0.4924819 0 1.0000000
## 0.00 0.9192 0.4924819 0 1.0000000
## 0.00 0.9293 0.4924819 0 1.0000000
## 0.00 0.9394 0.4924819 0 1.0000000
## 0.00 0.9495 0.4924819 0 1.0000000
## 0.00 0.9596 0.4924819 0 1.0000000
## 0.00 0.9697 0.4924819 0 1.0000000
## 0.00 0.9798 0.4924819 0 1.0000000
## 0.00 0.9899 0.4924819 0 1.0000000
## 0.00 1.0000 0.4924819 0 1.0000000
## 0.05 0.0001 0.4137681 0 0.9612319
## 0.05 0.0102 0.4281703 0 0.9871377
## 0.05 0.0203 0.4520833 0 0.9956522
## 0.05 0.0304 0.4737319 0 0.9956522
## 0.05 0.0405 0.4672101 0 0.9956522
## 0.05 0.0506 0.4864130 0 0.9956522
## 0.05 0.0607 0.4994565 0 1.0000000
## 0.05 0.0708 0.5015399 0 1.0000000
## 0.05 0.0809 0.5015399 0 1.0000000
## 0.05 0.0910 0.5036232 0 1.0000000
## 0.05 0.1011 0.5165761 0 1.0000000
## 0.05 0.1112 0.5250000 0 1.0000000
## 0.05 0.1213 0.5270833 0 1.0000000
## 0.05 0.1314 0.5270833 0 1.0000000
## 0.05 0.1415 0.5270833 0 1.0000000
## 0.05 0.1516 0.5270833 0 1.0000000
## 0.05 0.1617 0.5335145 0 1.0000000
## 0.05 0.1718 0.5377717 0 1.0000000
## 0.05 0.1819 0.5399457 0 1.0000000
## 0.05 0.1920 0.5442029 0 1.0000000
## 0.05 0.2021 0.5527174 0 1.0000000
## 0.05 0.2122 0.5483696 0 1.0000000
## 0.05 0.2223 0.5505435 0 1.0000000
## 0.05 0.2324 0.5592391 0 1.0000000
## 0.05 0.2425 0.5570652 0 1.0000000
## 0.05 0.2526 0.5570652 0 1.0000000
## 0.05 0.2627 0.5549819 0 1.0000000
## 0.05 0.2728 0.5550725 0 1.0000000
## 0.05 0.2829 0.5528986 0 1.0000000
## 0.05 0.2930 0.5507246 0 1.0000000
## 0.05 0.3031 0.5464674 0 1.0000000
## 0.05 0.3132 0.5442935 0 1.0000000
## 0.05 0.3233 0.5400362 0 1.0000000
## 0.05 0.3334 0.5376812 0 1.0000000
## 0.05 0.3435 0.5334239 0 1.0000000
## 0.05 0.3536 0.5397645 0 1.0000000
## 0.05 0.3637 0.5355072 0 1.0000000
## 0.05 0.3738 0.5312500 0 1.0000000
## 0.05 0.3839 0.5269928 0 1.0000000
## 0.05 0.3940 0.5206522 0 1.0000000
## 0.05 0.4041 0.5206522 0 1.0000000
## 0.05 0.4142 0.5099638 0 1.0000000
## 0.05 0.4243 0.4951087 0 1.0000000
## 0.05 0.4344 0.4886775 0 1.0000000
## 0.05 0.4445 0.4909420 0 1.0000000
## 0.05 0.4546 0.4907609 0 1.0000000
## 0.05 0.4647 0.4951087 0 1.0000000
## 0.05 0.4748 0.4886775 0 1.0000000
## 0.05 0.4849 0.4843297 0 1.0000000
## 0.05 0.4950 0.4800725 0 1.0000000
## 0.05 0.5051 0.4843297 0 1.0000000
## 0.05 0.5152 0.4822464 0 1.0000000
## 0.05 0.5253 0.4759964 0 1.0000000
## 0.05 0.5354 0.4780797 0 1.0000000
## 0.05 0.5455 0.4780797 0 1.0000000
## 0.05 0.5556 0.4694746 0 1.0000000
## 0.05 0.5657 0.4694746 0 1.0000000
## 0.05 0.5758 0.4673007 0 1.0000000
## 0.05 0.5859 0.4802536 0 1.0000000
## 0.05 0.5960 0.4846920 0 1.0000000
## 0.05 0.6061 0.4913043 0 1.0000000
## 0.05 0.6162 0.5021739 0 1.0000000
## 0.05 0.6263 0.5020833 0 1.0000000
## 0.05 0.6364 0.5213768 0 1.0000000
## 0.05 0.6465 0.5321558 0 1.0000000
## 0.05 0.6566 0.5302536 0 1.0000000
## 0.05 0.6667 0.5324275 0 1.0000000
## 0.05 0.6768 0.5301630 0 1.0000000
## 0.05 0.6869 0.5364130 0 1.0000000
## 0.05 0.6970 0.5471920 0 1.0000000
## 0.05 0.7071 0.5598732 0 1.0000000
## 0.05 0.7172 0.5682971 0 1.0000000
## 0.05 0.7273 0.5853261 0 1.0000000
## 0.05 0.7374 0.5938406 0 1.0000000
## 0.05 0.7475 0.6153080 0 1.0000000
## 0.05 0.7576 0.6348732 0 1.0000000
## 0.05 0.7677 0.6478261 0 1.0000000
## 0.05 0.7778 0.6627717 0 1.0000000
## 0.05 0.7879 0.6734601 0 1.0000000
## 0.05 0.7980 0.6778080 0 1.0000000
## 0.05 0.8081 0.6842391 0 1.0000000
## 0.05 0.8182 0.6971920 0 1.0000000
## 0.05 0.8283 0.7015399 0 1.0000000
## 0.05 0.8384 0.7102355 0 1.0000000
## 0.05 0.8485 0.7186594 0 1.0000000
## 0.05 0.8586 0.7208333 0 1.0000000
## 0.05 0.8687 0.7272645 0 1.0000000
## 0.05 0.8788 0.6962862 0 1.0000000
## 0.05 0.8889 0.6941123 0 1.0000000
## 0.05 0.8990 0.6962862 0 1.0000000
## 0.05 0.9091 0.6984601 0 1.0000000
## 0.05 0.9192 0.7006341 0 1.0000000
## 0.05 0.9293 0.6745471 0 1.0000000
## 0.05 0.9394 0.6767210 0 1.0000000
## 0.05 0.9495 0.6767210 0 1.0000000
## 0.05 0.9596 0.6788949 0 1.0000000
## 0.05 0.9697 0.6788949 0 1.0000000
## 0.05 0.9798 0.6788949 0 1.0000000
## 0.05 0.9899 0.6788949 0 1.0000000
## 0.05 1.0000 0.6788949 0 1.0000000
## 1.00 0.0001 0.4460145 0 0.9442029
## 1.00 0.0102 0.4586957 0 0.9871377
## 1.00 0.0203 0.4821558 0 1.0000000
## 1.00 0.0304 0.5280797 0 1.0000000
## 1.00 0.0405 0.6972826 0 1.0000000
## 1.00 0.0506 0.6288949 0 1.0000000
## 1.00 0.0607 0.5000000 0 1.0000000
## 1.00 0.0708 0.5000000 0 1.0000000
## 1.00 0.0809 0.5000000 0 1.0000000
## 1.00 0.0910 0.5000000 0 1.0000000
## 1.00 0.1011 0.5000000 0 1.0000000
## 1.00 0.1112 0.5000000 0 1.0000000
## 1.00 0.1213 0.5000000 0 1.0000000
## 1.00 0.1314 0.5000000 0 1.0000000
## 1.00 0.1415 0.5000000 0 1.0000000
## 1.00 0.1516 0.5000000 0 1.0000000
## 1.00 0.1617 0.5000000 0 1.0000000
## 1.00 0.1718 0.5000000 0 1.0000000
## 1.00 0.1819 0.5000000 0 1.0000000
## 1.00 0.1920 0.5000000 0 1.0000000
## 1.00 0.2021 0.5000000 0 1.0000000
## 1.00 0.2122 0.5000000 0 1.0000000
## 1.00 0.2223 0.5000000 0 1.0000000
## 1.00 0.2324 0.5000000 0 1.0000000
## 1.00 0.2425 0.5000000 0 1.0000000
## 1.00 0.2526 0.5000000 0 1.0000000
## 1.00 0.2627 0.5000000 0 1.0000000
## 1.00 0.2728 0.5000000 0 1.0000000
## 1.00 0.2829 0.5000000 0 1.0000000
## 1.00 0.2930 0.5000000 0 1.0000000
## 1.00 0.3031 0.5000000 0 1.0000000
## 1.00 0.3132 0.5000000 0 1.0000000
## 1.00 0.3233 0.5000000 0 1.0000000
## 1.00 0.3334 0.5000000 0 1.0000000
## 1.00 0.3435 0.5000000 0 1.0000000
## 1.00 0.3536 0.5000000 0 1.0000000
## 1.00 0.3637 0.5000000 0 1.0000000
## 1.00 0.3738 0.5000000 0 1.0000000
## 1.00 0.3839 0.5000000 0 1.0000000
## 1.00 0.3940 0.5000000 0 1.0000000
## 1.00 0.4041 0.5000000 0 1.0000000
## 1.00 0.4142 0.5000000 0 1.0000000
## 1.00 0.4243 0.5000000 0 1.0000000
## 1.00 0.4344 0.5000000 0 1.0000000
## 1.00 0.4445 0.5000000 0 1.0000000
## 1.00 0.4546 0.5000000 0 1.0000000
## 1.00 0.4647 0.5000000 0 1.0000000
## 1.00 0.4748 0.5000000 0 1.0000000
## 1.00 0.4849 0.5000000 0 1.0000000
## 1.00 0.4950 0.5000000 0 1.0000000
## 1.00 0.5051 0.5000000 0 1.0000000
## 1.00 0.5152 0.5000000 0 1.0000000
## 1.00 0.5253 0.5000000 0 1.0000000
## 1.00 0.5354 0.5000000 0 1.0000000
## 1.00 0.5455 0.5000000 0 1.0000000
## 1.00 0.5556 0.5000000 0 1.0000000
## 1.00 0.5657 0.5000000 0 1.0000000
## 1.00 0.5758 0.5000000 0 1.0000000
## 1.00 0.5859 0.5000000 0 1.0000000
## 1.00 0.5960 0.5000000 0 1.0000000
## 1.00 0.6061 0.5000000 0 1.0000000
## 1.00 0.6162 0.5000000 0 1.0000000
## 1.00 0.6263 0.5000000 0 1.0000000
## 1.00 0.6364 0.5000000 0 1.0000000
## 1.00 0.6465 0.5000000 0 1.0000000
## 1.00 0.6566 0.5000000 0 1.0000000
## 1.00 0.6667 0.5000000 0 1.0000000
## 1.00 0.6768 0.5000000 0 1.0000000
## 1.00 0.6869 0.5000000 0 1.0000000
## 1.00 0.6970 0.5000000 0 1.0000000
## 1.00 0.7071 0.5000000 0 1.0000000
## 1.00 0.7172 0.5000000 0 1.0000000
## 1.00 0.7273 0.5000000 0 1.0000000
## 1.00 0.7374 0.5000000 0 1.0000000
## 1.00 0.7475 0.5000000 0 1.0000000
## 1.00 0.7576 0.5000000 0 1.0000000
## 1.00 0.7677 0.5000000 0 1.0000000
## 1.00 0.7778 0.5000000 0 1.0000000
## 1.00 0.7879 0.5000000 0 1.0000000
## 1.00 0.7980 0.5000000 0 1.0000000
## 1.00 0.8081 0.5000000 0 1.0000000
## 1.00 0.8182 0.5000000 0 1.0000000
## 1.00 0.8283 0.5000000 0 1.0000000
## 1.00 0.8384 0.5000000 0 1.0000000
## 1.00 0.8485 0.5000000 0 1.0000000
## 1.00 0.8586 0.5000000 0 1.0000000
## 1.00 0.8687 0.5000000 0 1.0000000
## 1.00 0.8788 0.5000000 0 1.0000000
## 1.00 0.8889 0.5000000 0 1.0000000
## 1.00 0.8990 0.5000000 0 1.0000000
## 1.00 0.9091 0.5000000 0 1.0000000
## 1.00 0.9192 0.5000000 0 1.0000000
## 1.00 0.9293 0.5000000 0 1.0000000
## 1.00 0.9394 0.5000000 0 1.0000000
## 1.00 0.9495 0.5000000 0 1.0000000
## 1.00 0.9596 0.5000000 0 1.0000000
## 1.00 0.9697 0.5000000 0 1.0000000
## 1.00 0.9798 0.5000000 0 1.0000000
## 1.00 0.9899 0.5000000 0 1.0000000
## 1.00 1.0000 0.5000000 0 1.0000000
##
## ROC was used to select the optimal model using the largest value.
## The final values used for the model were alpha = 0.05 and lambda = 0.8687.
# Print maximum ROC statistic
max(model$results$ROC)
## [1] 0.7272645
plot(model)
Example of missing data:
# generate some data with missing values
set.seed(42)
mtcars[sample(1:nrow(mtcars), 10), "hp"] <- NA
# split target for predictors
Y <- mtcars$mpg
X <- mtcars[, 2:4]
# Try to fit a caret model
# Notice carets non formula interace
model <- train(X,Y)
## note: only 2 unique complexity parameters in default grid. Truncating the grid to 2 .
##
## Something is wrong; all the RMSE metric values are missing:
## RMSE Rsquared MAE
## Min. : NA Min. : NA Min. : NA
## 1st Qu.: NA 1st Qu.: NA 1st Qu.: NA
## Median : NA Median : NA Median : NA
## Mean :NaN Mean :NaN Mean :NaN
## 3rd Qu.: NA 3rd Qu.: NA 3rd Qu.: NA
## Max. : NA Max. : NA Max. : NA
## NA's :2 NA's :2 NA's :2
## Error: Stopping
# Using median imputation fixes this issue
model <- train(X,Y, preProcess = "medianImpute")
## note: only 2 unique complexity parameters in default grid. Truncating the grid to 2 .
print(model)
## Random Forest
##
## 32 samples
## 3 predictor
##
## Pre-processing: median imputation (3)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 32, 32, 32, 32, 32, 32, ...
## Resampling results across tuning parameters:
##
## mtry RMSE Rsquared MAE
## 2 2.996936 0.7706454 2.421628
## 3 3.026853 0.7676774 2.427185
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was mtry = 2.
load('data/BreastCancer.RData')
glimpse(breast_cancer_x)
## Observations: 699
## Variables: 9
## $ Cl.thickness <int> 5, NA, NA, 6, 4, 8, 1, 2, NA, NA, 1, 2, 5, 1, ...
## $ Cell.size <int> NA, 4, NA, 8, 1, 10, 1, 1, 1, 2, 1, 1, NA, 1, ...
## $ Cell.shape <int> 1, 4, 1, 8, 1, 10, NA, 2, 1, 1, 1, 1, 3, NA, 5...
## $ Marg.adhesion <int> 1, NA, 1, NA, 3, 8, NA, 1, NA, 1, 1, 1, 3, 1, ...
## $ Epith.c.size <int> NA, 7, 2, NA, 2, 7, 2, 2, 2, 2, 1, 2, NA, 2, 7...
## $ Bare.nuclei <int> 1, 10, NA, 4, 1, 10, 10, 1, 1, 1, 1, 1, 3, 3, ...
## $ Bl.cromatin <int> 3, 3, 3, 3, 3, NA, 3, 3, NA, 2, 3, 2, 4, 3, 5,...
## $ Normal.nucleoli <int> 1, NA, 1, 7, NA, 7, 1, 1, 1, NA, 1, 1, NA, 1, ...
## $ Mitoses <int> 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 1, NA, 1, 1, 4, ...
glimpse(breast_cancer_y)
## Factor w/ 2 levels "benign","malignant": 1 1 1 1 1 2 1 1 1 1 ...
# Apply median imputation: model
model1 <- train(
x = breast_cancer_x, y = breast_cancer_y,
method = "glm",
trControl = myControl,
preProcess = 'medianImpute'
)
## + Fold01: parameter=none
## - Fold01: parameter=none
## + Fold02: parameter=none
## - Fold02: parameter=none
## + Fold03: parameter=none
## - Fold03: parameter=none
## + Fold04: parameter=none
## - Fold04: parameter=none
## + Fold05: parameter=none
## - Fold05: parameter=none
## + Fold06: parameter=none
## - Fold06: parameter=none
## + Fold07: parameter=none
## - Fold07: parameter=none
## + Fold08: parameter=none
## - Fold08: parameter=none
## + Fold09: parameter=none
## - Fold09: parameter=none
## + Fold10: parameter=none
## - Fold10: parameter=none
## Aggregating results
## Fitting final model on full training set
# Print model to console
model1
## Generalized Linear Model
##
## 699 samples
## 9 predictor
## 2 classes: 'benign', 'malignant'
##
## Pre-processing: median imputation (9)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 629, 630, 630, 628, 629, 629, ...
## Resampling results:
##
## ROC Sens Spec
## 0.9923543 0.969372 0.9376667
Example - Median vs KNN imputation:
data(mtcars)
# Generate data with missing values
mtcars[mtcars$disp < 140, "hp"] <- NA
Y <- mtcars$mpg
X <- mtcars[, 2:4]
# Use median imputation
set.seed(42)
model <- train(X,Y,
method = "glm",
preProcess = "medianImpute")
print(model)
## Generalized Linear Model
##
## 32 samples
## 3 predictor
##
## Pre-processing: median imputation (3)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 32, 32, 32, 32, 32, 32, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 3.252487 0.7672791 2.587883
# using KNN imputation
set.seed(42)
model <- train(X,Y,
method = "glm",
preProcess = "knnImpute")
print(model)
## Generalized Linear Model
##
## 32 samples
## 3 predictor
##
## Pre-processing: nearest neighbor imputation (3), centered (3), scaled (3)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 32, 32, 32, 32, 32, 32, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 3.22105 0.7682637 2.570241
# Apply KNN imputation: model2
model2 <- train(
x = breast_cancer_x, y = breast_cancer_y,
method = "glm",
trControl = myControl,
preProcess = "knnImpute"
)
## + Fold01: parameter=none
## - Fold01: parameter=none
## + Fold02: parameter=none
## - Fold02: parameter=none
## + Fold03: parameter=none
## - Fold03: parameter=none
## + Fold04: parameter=none
## - Fold04: parameter=none
## + Fold05: parameter=none
## - Fold05: parameter=none
## + Fold06: parameter=none
## - Fold06: parameter=none
## + Fold07: parameter=none
## - Fold07: parameter=none
## + Fold08: parameter=none
## - Fold08: parameter=none
## + Fold09: parameter=none
## - Fold09: parameter=none
## + Fold10: parameter=none
## - Fold10: parameter=none
## Aggregating results
## Fitting final model on full training set
# Print model to console
model2
## Generalized Linear Model
##
## 699 samples
## 9 predictor
## 2 classes: 'benign', 'malignant'
##
## Pre-processing: nearest neighbor imputation (9), centered (9), scaled (9)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 629, 629, 629, 629, 628, 629, ...
## Resampling results:
##
## ROC Sens Spec
## 0.991657 0.9716425 0.9296667
resamples <- resamples(x = list(median_model = model1, knn_model = model2))
dotplot(resamples, metric = "ROC")
median imputation -> center -> scale -> fit glm
is a commonn ‘recipe’ for linear models (order matters)Example - preprocessing mtcrs:
# Generate some data with missing values
data(mtcars)
set.seed(42)
mtcars[sample(1:nrow(mtcars), 10), "hp"] <- NA
Y <- mtcars$mpg
X <- mtcars[, 2:4]
# Use linear model "recipe"
set.seed(42)
model <- train(
X,Y, method = "glm",
preProcess = c("medianImpute", "center", "scale"))
print(min(model$results$RMSE))
## [1] 3.332758
# use pca also. just add it to the "recipe"
set.seed(42)
model <- train(
X,Y, method = "glm",
preProcess = c("medianImpute", "center", "scale", "pca"))
print(min(model$results$RMSE))
## [1] 3.25045
- using "spacialSign" project the data onto a sphere
- it is a good way to preprocess data with lots of outliers or high dimensionality
Preprocessing cheat sheet - always start with median imputation - also try knn imputation if data missing not at random - For linear models… - alwasy center and scale (you just get better results) - try pca and spatial sign. sometimes you will get better results - tree-based models and randome forest typically don’t need much preprocessing. - You can usually get away with just median imputation
preProcess
:
set.seed(42)
load('data/BreastCancer.RData')
# Fit glm with median imputation: model1
model1 <- train(
x = breast_cancer_x, y = breast_cancer_y,
method = "glm",
trControl = myControl,
preProcess = c('medianImpute')
)
## + Fold01: parameter=none
## - Fold01: parameter=none
## + Fold02: parameter=none
## - Fold02: parameter=none
## + Fold03: parameter=none
## - Fold03: parameter=none
## + Fold04: parameter=none
## - Fold04: parameter=none
## + Fold05: parameter=none
## - Fold05: parameter=none
## + Fold06: parameter=none
## - Fold06: parameter=none
## + Fold07: parameter=none
## - Fold07: parameter=none
## + Fold08: parameter=none
## - Fold08: parameter=none
## + Fold09: parameter=none
## - Fold09: parameter=none
## + Fold10: parameter=none
## - Fold10: parameter=none
## Aggregating results
## Fitting final model on full training set
# Print model1
model1
## Generalized Linear Model
##
## 699 samples
## 9 predictor
## 2 classes: 'benign', 'malignant'
##
## Pre-processing: median imputation (9)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 630, 629, 629, 629, 628, 629, ...
## Resampling results:
##
## ROC Sens Spec
## 0.9914915 0.9694203 0.9333333
# Fit glm with median imputation and standardization: model2
model2 <- train(
x = breast_cancer_x, y = breast_cancer_y,
method = "glm",
trControl = myControl,
preProcess = c('medianImpute', 'center', 'scale')
)
## + Fold01: parameter=none
## - Fold01: parameter=none
## + Fold02: parameter=none
## - Fold02: parameter=none
## + Fold03: parameter=none
## - Fold03: parameter=none
## + Fold04: parameter=none
## - Fold04: parameter=none
## + Fold05: parameter=none
## - Fold05: parameter=none
## + Fold06: parameter=none
## - Fold06: parameter=none
## + Fold07: parameter=none
## - Fold07: parameter=none
## + Fold08: parameter=none
## - Fold08: parameter=none
## + Fold09: parameter=none
## - Fold09: parameter=none
## + Fold10: parameter=none
## - Fold10: parameter=none
## Aggregating results
## Fitting final model on full training set
# Print model2
model2
## Generalized Linear Model
##
## 699 samples
## 9 predictor
## 2 classes: 'benign', 'malignant'
##
## Pre-processing: median imputation (9), centered (9), scaled (9)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 628, 630, 629, 629, 629, 630, ...
## Resampling results:
##
## ROC Sens Spec
## 0.9915487 0.9695652 0.9501667
Example: constan column in mtcars
# Reproduce dataset from last video
data(mtcars)
set.seed(42)
mtcars[sample(1:nrow(mtcars), 10), "hp"] <- NA
Y <- mtcars$mpg
X <- mtcars[, 2:4]
# Add a constant-values column to mtcars
X$bad <- 1
# Try to fit a model with pca + glm
model <- train(
X,Y, method = "glm",
preProcess = c("medianImpute", "center", "scale", "pca"))
## Something is wrong; all the RMSE metric values are missing:
## RMSE Rsquared MAE
## Min. : NA Min. : NA Min. : NA
## 1st Qu.: NA 1st Qu.: NA 1st Qu.: NA
## Median : NA Median : NA Median : NA
## Mean :NaN Mean :NaN Mean :NaN
## 3rd Qu.: NA 3rd Qu.: NA 3rd Qu.: NA
## Max. : NA Max. : NA Max. : NA
## NA's :1 NA's :1 NA's :1
## Error: Stopping
# Have caret remove those columns during modeling
set.seed(42)
model <- train(
X,Y, method = "glm",
preProcess = c("zv", "medianImpute", "center", "scale","pca"))
min(model$results$RMSE)
## [1] 3.25045
nearZeroVar()
takes in data x, then looks at the ratio of the most common value to the second most common value, freqCut,nearZeroVar()
.load('data/BloodBrain.RData')
glimpse(bloodbrain_x)
## Observations: 208
## Variables: 132
## $ tpsa <dbl> 12.03, 49.33, 50.53, 37.39, 37.39, 37.39,...
## $ nbasic <int> 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0,...
## $ vsa_hyd <dbl> 167.06700, 92.64243, 295.16700, 319.11220...
## $ a_aro <int> 0, 6, 15, 15, 12, 11, 6, 12, 12, 6, 9, 12...
## $ weight <dbl> 156.293, 151.165, 366.485, 382.552, 326.4...
## $ peoe_vsa.0 <dbl> 76.947490, 38.243390, 58.054730, 62.23933...
## $ peoe_vsa.1 <dbl> 43.44619, 25.52006, 124.74020, 124.74020,...
## $ peoe_vsa.2 <dbl> 0.000000, 0.000000, 21.650840, 13.192320,...
## $ peoe_vsa.3 <dbl> 0.000000, 8.619013, 8.619013, 21.785640, ...
## $ peoe_vsa.4 <dbl> 0.00000, 23.27370, 17.44054, 0.00000, 0.0...
## $ peoe_vsa.5 <dbl> 0.000000, 0.000000, 0.000000, 0.000000, 0...
## $ peoe_vsa.6 <dbl> 17.238030, 0.000000, 8.619013, 8.619013, ...
## $ peoe_vsa.0.1 <dbl> 18.74768, 49.01962, 83.82487, 83.82487, 8...
## $ peoe_vsa.1.1 <dbl> 43.50657, 0.00000, 49.01962, 68.78024, 36...
## $ peoe_vsa.2.1 <dbl> 0.00000, 0.00000, 0.00000, 0.00000, 0.000...
## $ peoe_vsa.3.1 <dbl> 0.000000, 0.000000, 0.000000, 0.000000, 0...
## $ peoe_vsa.4.1 <dbl> 0.000000, 0.000000, 5.682576, 5.682576, 5...
## $ peoe_vsa.5.1 <dbl> 0.000000, 13.566920, 2.503756, 0.000000, ...
## $ peoe_vsa.6.1 <dbl> 0.000000, 7.904431, 2.640647, 2.640647, 2...
## $ a_acc <int> 0, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 1, 2, 3,...
## $ a_acid <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ a_base <int> 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 0, 0,...
## $ vsa_acc <dbl> 0.000000, 13.566920, 8.186332, 8.186332, ...
## $ vsa_acid <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ vsa_base <dbl> 5.682576, 0.000000, 0.000000, 0.000000, 0...
## $ vsa_don <dbl> 5.682576, 5.682576, 5.682576, 5.682576, 5...
## $ vsa_other <dbl> 0.000000, 28.107600, 43.560890, 28.324700...
## $ vsa_pol <dbl> 0.00000, 13.56692, 0.00000, 0.00000, 0.00...
## $ slogp_vsa0 <dbl> 18.010750, 25.385230, 14.124200, 14.12420...
## $ slogp_vsa1 <dbl> 0.000000, 23.269540, 34.796280, 34.796280...
## $ slogp_vsa2 <dbl> 3.981969, 23.862220, 0.000000, 0.000000, ...
## $ slogp_vsa3 <dbl> 0.00000, 0.00000, 76.24500, 76.24500, 76....
## $ slogp_vsa4 <dbl> 4.410796, 0.000000, 3.185575, 3.185575, 3...
## $ slogp_vsa5 <dbl> 32.897190, 0.000000, 9.507346, 0.000000, ...
## $ slogp_vsa6 <dbl> 0.000000, 0.000000, 0.000000, 0.000000, 0...
## $ slogp_vsa7 <dbl> 0.00000, 70.57274, 148.12580, 144.03540, ...
## $ slogp_vsa8 <dbl> 113.210400, 0.000000, 75.473630, 75.47363...
## $ slogp_vsa9 <dbl> 33.326020, 41.326190, 28.274170, 55.46144...
## $ smr_vsa0 <dbl> 0.000000, 23.862220, 12.631660, 3.124314,...
## $ smr_vsa1 <dbl> 18.01075, 25.38523, 27.78542, 27.78542, 2...
## $ smr_vsa2 <dbl> 4.410796, 0.000000, 0.000000, 0.000000, 0...
## $ smr_vsa3 <dbl> 3.981969, 5.243428, 8.429003, 8.429003, 8...
## $ smr_vsa4 <dbl> 0.000000, 20.767500, 29.582260, 21.401420...
## $ smr_vsa5 <dbl> 113.21040, 70.57274, 235.05870, 235.05870...
## $ smr_vsa6 <dbl> 0.000000, 5.258784, 76.245000, 76.245000,...
## $ smr_vsa7 <dbl> 66.22321, 33.32602, 0.00000, 31.27769, 0....
## $ tpsa.1 <dbl> 16.61, 49.33, 51.73, 38.59, 38.59, 38.59,...
## $ logp.o.w. <dbl> 2.94800, 0.88900, 4.43900, 5.25400, 3.800...
## $ frac.anion7. <dbl> 0.000, 0.001, 0.000, 0.000, 0.000, 0.000,...
## $ frac.cation7. <dbl> 0.999, 0.000, 0.986, 0.986, 0.986, 0.986,...
## $ andrewbind <dbl> 3.4, -3.3, 12.8, 12.8, 10.3, 10.0, 10.4, ...
## $ rotatablebonds <int> 3, 2, 8, 8, 8, 8, 8, 7, 4, 5, 4, 7, 1, 3,...
## $ mlogp <dbl> 2.50245, 1.05973, 4.66091, 3.82458, 3.272...
## $ clogp <dbl> 2.970000, 0.494000, 5.136999, 5.877599, 4...
## $ mw <dbl> 155.2856, 151.1664, 365.4794, 381.5440, 3...
## $ nocount <int> 1, 3, 5, 4, 4, 4, 4, 3, 2, 4, 6, 4, 3, 6,...
## $ hbdnr <int> 1, 2, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 1, 3,...
## $ rule.of.5violations <int> 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,...
## $ prx <int> 0, 1, 6, 2, 2, 2, 1, 0, 0, 4, 5, 0, 1, 12...
## $ ub <dbl> 0.0, 3.0, 5.3, 5.3, 4.2, 3.6, 3.0, 4.7, 4...
## $ pol <int> 0, 2, 3, 3, 2, 2, 2, 3, 4, 1, 2, 4, 4, 2,...
## $ inthb <int> 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,...
## $ adistm <dbl> 0.0000, 395.3757, 1364.5514, 702.6387, 74...
## $ adistd <dbl> 0.0000, 10.8921, 25.6784, 10.0232, 10.575...
## $ polar_area <dbl> 21.1242, 117.4081, 82.0943, 65.0890, 66.1...
## $ nonpolar_area <dbl> 379.0702, 247.5371, 637.7242, 667.9713, 6...
## $ psa_npsa <dbl> 0.0557, 0.4743, 0.1287, 0.0974, 0.1100, 0...
## $ tcsa <dbl> 0.0097, 0.0134, 0.0111, 0.0108, 0.0118, 0...
## $ tcpa <dbl> 0.1842, 0.0417, 0.0972, 0.1218, 0.1186, 0...
## $ tcnp <dbl> 0.0103, 0.0198, 0.0125, 0.0119, 0.0130, 0...
## $ ovality <dbl> 1.0960, 1.1173, 1.3005, 1.3013, 1.2711, 1...
## $ surface_area <dbl> 400.1944, 364.9453, 719.8185, 733.0603, 6...
## $ volume <dbl> 656.0650, 555.0969, 1224.4553, 1257.2002,...
## $ most_negative_charge <dbl> -0.6174, -0.8397, -0.8012, -0.7608, -0.85...
## $ most_positive_charge <dbl> 0.3068, 0.4967, 0.5414, 0.4800, 0.4547, 0...
## $ sum_absolute_charge <dbl> 3.8918, 4.8925, 7.9796, 7.9308, 7.8516, 7...
## $ dipole_moment <dbl> 1.1898, 4.2109, 3.5234, 3.1463, 3.2676, 3...
## $ homo <dbl> -9.6672, -8.9618, -8.6271, -8.5592, -8.67...
## $ lumo <dbl> 3.4038, 0.1942, 0.0589, -0.2651, 0.3149, ...
## $ hardness <dbl> 6.5355, 4.5780, 4.3430, 4.1471, 4.4940, 4...
## $ ppsa1 <dbl> 349.1390, 223.1310, 517.8218, 507.6144, 5...
## $ ppsa2 <dbl> 679.3832, 545.8328, 2066.0186, 2012.9060,...
## $ ppsa3 <dbl> 30.9705, 42.3030, 63.9503, 61.6890, 61.56...
## $ pnsa1 <dbl> 51.0554, 141.8143, 201.9967, 225.4459, 15...
## $ pnsa2 <dbl> -99.3477, -346.9123, -805.9311, -893.9880...
## $ pnsa3 <dbl> -10.4876, -44.0368, -43.7587, -42.0328, -...
## $ fpsa1 <dbl> 0.8724, 0.6114, 0.7194, 0.6925, 0.7623, 0...
## $ fpsa2 <dbl> 1.6976, 1.4957, 2.8702, 2.7459, 2.9927, 2...
## $ fpsa3 <dbl> 0.0774, 0.1159, 0.0888, 0.0842, 0.0922, 0...
## $ fnsa1 <dbl> 0.1276, 0.3886, 0.2806, 0.3075, 0.2377, 0...
## $ fnsa2 <dbl> -0.2482, -0.9506, -1.1196, -1.2195, -0.93...
## $ fnsa3 <dbl> -0.0262, -0.1207, -0.0608, -0.0573, -0.05...
## $ wpsa1 <dbl> 139.7235, 81.4306, 372.7377, 372.1120, 34...
## $ wpsa2 <dbl> 271.8854, 199.1991, 1487.1583, 1475.5815,...
## $ wpsa3 <dbl> 12.3942, 15.4383, 46.0326, 45.2218, 41.12...
## $ wnsa1 <dbl> 20.4321, 51.7544, 145.4010, 165.2654, 106...
## $ wnsa2 <dbl> -39.7584, -126.6040, -580.1241, -655.3471...
## $ wnsa3 <dbl> -4.1971, -16.0710, -31.4983, -30.8126, -2...
## $ dpsa1 <dbl> 298.0836, 81.3167, 315.8251, 282.1685, 35...
## $ dpsa2 <dbl> 778.7310, 892.7451, 2871.9497, 2906.8940,...
## $ dpsa3 <dbl> 41.4580, 86.3398, 107.7089, 103.7218, 101...
## $ rpcg <dbl> 0.1577, 0.2030, 0.1357, 0.1210, 0.1158, 0...
## $ rncg <dbl> 0.3173, 0.3433, 0.2008, 0.1919, 0.2182, 0...
## $ wpcs <dbl> 2.3805, 1.3116, 1.1351, 0.7623, 0.7884, 2...
## $ wncs <dbl> 1.9117, 2.2546, 1.5725, 1.5302, 1.6795, 1...
## $ sadh1 <dbl> 15.0988, 45.2163, 16.7192, 17.2491, 16.02...
## $ sadh2 <dbl> 15.0988, 22.6082, 16.7192, 17.2491, 16.02...
## $ sadh3 <dbl> 0.0377, 0.1239, 0.0232, 0.0235, 0.0240, 0...
## $ chdh1 <dbl> 0.3068, 0.7960, 0.4550, 0.4354, 0.4366, 0...
## $ chdh2 <dbl> 0.3068, 0.3980, 0.4550, 0.4354, 0.4366, 0...
## $ chdh3 <dbl> 0.0008, 0.0022, 0.0006, 0.0006, 0.0007, 0...
## $ scdh1 <dbl> 4.6321, 17.6195, 7.6077, 7.5102, 6.9970, ...
## $ scdh2 <dbl> 4.6321, 8.8098, 7.6077, 7.5102, 6.9970, 7...
## $ scdh3 <dbl> 0.0116, 0.0483, 0.0106, 0.0102, 0.0105, 0...
## $ saaa1 <dbl> 6.0255, 65.6236, 57.5440, 39.8638, 42.454...
## $ saaa2 <dbl> 6.0255, 32.8118, 14.3860, 13.2879, 14.151...
## $ saaa3 <dbl> 0.0151, 0.1798, 0.0799, 0.0544, 0.0636, 0...
## $ chaa1 <dbl> -0.6174, -0.8371, -1.3671, -1.2332, -1.14...
## $ chaa2 <dbl> -0.6174, -0.4185, -0.3418, -0.4111, -0.38...
## $ chaa3 <dbl> -0.0015, -0.0023, -0.0019, -0.0017, -0.00...
## $ scaa1 <dbl> -3.7199, -27.5143, -21.7898, -17.5957, -1...
## $ scaa2 <dbl> -3.7199, -13.7571, -5.4475, -5.8652, -5.6...
## $ scaa3 <dbl> -0.0093, -0.0754, -0.0303, -0.0240, -0.02...
## $ ctdh <int> 1, 2, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 1, 3,...
## $ ctaa <int> 1, 2, 4, 3, 3, 3, 3, 3, 1, 3, 5, 3, 2, 3,...
## $ mchg <dbl> 0.9241, 1.2685, 1.2562, 1.1962, 1.2934, 1...
## $ achg <dbl> 0.9241, 1.0420, 1.2562, 1.1962, 1.2934, 1...
## $ rdta <dbl> 1.0000, 1.0000, 0.2500, 0.3333, 0.3333, 0...
## $ n_sp2 <dbl> 0.0000, 0.0000, 26.9733, 21.7065, 24.2061...
## $ n_sp3 <dbl> 6.0255, 6.5681, 10.8567, 11.0017, 10.8109...
## $ o_sp2 <dbl> 0.0000, 32.0102, 0.0000, 0.0000, 0.0000, ...
## $ o_sp3 <dbl> 0.0000, 33.6135, 27.5451, 15.1316, 15.133...
glimpse(bloodbrain_y)
## num [1:208] 1.08 -0.4 0.22 0.14 0.69 0.44 -0.43 1.38 0.75 0.88 ...
# Identify near zero variance predictors: remove_cols
remove_cols <- nearZeroVar(bloodbrain_x, names = TRUE,
freqCut = 2, uniqueCut = 20)
# Get all column names from bloodbrain_x: all_cols
all_cols <- names(bloodbrain_x)
# Remove from data: bloodbrain_x_small
bloodbrain_x_small <- bloodbrain_x[ , setdiff(all_cols, remove_cols)]
# Fit model on reduced data: model
model <- train(x = bloodbrain_x_small, y = bloodbrain_y, method = "glm")
# Print model to console
model
## Generalized Linear Model
##
## 208 samples
## 112 predictors
##
## No pre-processing
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 208, 208, 208, 208, 208, 208, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 1.737597 0.1226598 1.136949
Example: blood-brain data
# Basic model
set.seed(42)
data(BloodBrain)
model <- train(
bbbDescr, logBBB, method = "glm"
)
model
## Generalized Linear Model
##
## 208 samples
## 134 predictors
##
## No pre-processing
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 208, 208, 208, 208, 208, 208, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 6.523485 0.05478762 4.021009
# Adding preprocesing steps including "zv"
model2 <- train(
bbbDescr, logBBB, method = "glm",
trControl = trainControl(method = "cv", number = 10, verbose = F),
preProcess = c("zv", "center", "scale")
)
model2
## Generalized Linear Model
##
## 208 samples
## 134 predictors
##
## Pre-processing: centered (134), scaled (134)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 187, 187, 187, 188, 187, 186, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 1.069966 0.2257437 0.7691277
# change "zv" to "nzv"
model3 <- train(
bbbDescr, logBBB, method = "glm",
trControl = trainControl(method = "cv", number = 10, verbose = F),
preProcess = c("nzv", "center", "scale")
)
model3
## Generalized Linear Model
##
## 208 samples
## 134 predictors
##
## Pre-processing: centered (127), scaled (127), remove (7)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 188, 188, 186, 188, 188, 187, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 1.085857 0.2134409 0.748278
# now keep the low variance predictors in the model but use pca
model4 <- train(
bbbDescr, logBBB, method = "glm",
trControl = trainControl(method = "cv", number = 10, verbose = F),
preProcess = c("zv", "center", "scale", "pca")
)
model4
## Generalized Linear Model
##
## 208 samples
## 134 predictors
##
## Pre-processing: centered (134), scaled (134), principal component
## signal extraction (134)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 188, 187, 187, 188, 186, 187, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 0.5627461 0.5152928 0.4224397
# Fit glm model using PCA: model
model <- train(
x = bloodbrain_x, y = bloodbrain_y,
method = "glm", preProcess = c("pca")
)
# Print model to console
model
## Generalized Linear Model
##
## 208 samples
## 132 predictors
##
## Pre-processing: principal component signal extraction (132),
## centered (132), scaled (132)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 208, 208, 208, 208, 208, 208, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 0.6015217 0.4534377 0.4511828
nearZeroVar()
first to remove data.
trainControl
object which specifies which rows you use for model building and which are used as holdouts
Example: customer churn data
caret
’s createFolds
functionlibrary(C50)
data(churn)
table(churnTrain$churn) / nrow(churnTrain)
##
## yes no
## 0.1449145 0.8550855
# Create train/test indexes
set.seed(42)
myFolds <- createFolds(churnTrain$churn, k = 5)
# Compare class distribution
i <- myFolds$Fold1
table(churnTrain$churn[i]) / length(i)
##
## yes no
## 0.1441441 0.8558559
trainControl
object which we can then use to create multiple modelsmyControl <- trainControl(
summaryFunction = twoClassSummary,
classProbs = T,
verboseIter = T,
savePredictions = T,
index = myFolds
)
myControl
## $method
## [1] "boot"
##
## $number
## [1] 25
##
## $repeats
## [1] NA
##
## $search
## [1] "grid"
##
## $p
## [1] 0.75
##
## $initialWindow
## NULL
##
## $horizon
## [1] 1
##
## $fixedWindow
## [1] TRUE
##
## $skip
## [1] 0
##
## $verboseIter
## [1] TRUE
##
## $returnData
## [1] TRUE
##
## $returnResamp
## [1] "final"
##
## $savePredictions
## [1] TRUE
##
## $classProbs
## [1] TRUE
##
## $summaryFunction
## function (data, lev = NULL, model = NULL)
## {
## lvls <- levels(data$obs)
## if (length(lvls) > 2)
## stop(paste("Your outcome has", length(lvls), "levels. The twoClassSummary() function isn't appropriate."))
## requireNamespaceQuietStop("ModelMetrics")
## if (!all(levels(data[, "pred"]) == lvls))
## stop("levels of observed and predicted data do not match")
## data$y = as.numeric(data$obs == lvls[2])
## rocAUC <- ModelMetrics::auc(ifelse(data$obs == lev[2], 0,
## 1), data[, lvls[1]])
## out <- c(rocAUC, sensitivity(data[, "pred"], data[, "obs"],
## lev[1]), specificity(data[, "pred"], data[, "obs"], lev[2]))
## names(out) <- c("ROC", "Sens", "Spec")
## out
## }
## <environment: namespace:caret>
##
## $selectionFunction
## [1] "best"
##
## $preProcOptions
## $preProcOptions$thresh
## [1] 0.95
##
## $preProcOptions$ICAcomp
## [1] 3
##
## $preProcOptions$k
## [1] 5
##
## $preProcOptions$freqCut
## [1] 19
##
## $preProcOptions$uniqueCut
## [1] 10
##
## $preProcOptions$cutoff
## [1] 0.9
##
##
## $sampling
## NULL
##
## $index
## $index$Fold1
## [1] 21 34 35 38 41 48 49 56 60 68 70 76 83 85
## [15] 94 102 104 109 110 116 122 126 136 143 151 154 157 163
## [29] 169 170 179 183 184 186 192 195 200 203 209 213 215 217
## [43] 227 229 231 232 238 240 241 243 246 250 252 257 261 268
## [57] 274 281 282 288 291 301 314 317 323 328 333 335 336 365
## [71] 372 384 398 406 421 423 429 436 440 441 442 443 452 462
## [85] 463 466 469 471 473 478 480 482 483 485 487 491 493 494
## [99] 495 496 499 509 515 520 527 529 530 536 548 551 557 581
## [113] 583 588 589 590 599 601 606 610 611 612 614 620 621 624
## [127] 634 636 644 645 650 651 652 653 654 657 662 664 668 671
## [141] 673 682 685 697 701 703 710 716 736 738 741 745 747 750
## [155] 754 755 758 762 764 769 772 775 779 784 794 796 803 805
## [169] 806 807 808 811 821 828 831 834 850 851 854 860 864 866
## [183] 874 877 878 883 885 887 898 904 905 907 909 910 920 931
## [197] 945 954 958 987 992 995 999 1000 1004 1015 1018 1024 1025 1028
## [211] 1031 1038 1041 1042 1059 1062 1067 1087 1104 1112 1116 1123 1125 1127
## [225] 1129 1132 1157 1170 1178 1179 1184 1185 1187 1188 1198 1200 1204 1215
## [239] 1227 1228 1232 1235 1237 1240 1262 1265 1266 1269 1282 1283 1290 1303
## [253] 1307 1309 1312 1317 1319 1320 1322 1323 1324 1331 1341 1365 1368 1370
## [267] 1373 1377 1378 1383 1388 1392 1394 1396 1401 1416 1420 1428 1433 1441
## [281] 1444 1447 1451 1453 1456 1462 1466 1473 1483 1484 1489 1498 1500 1508
## [295] 1511 1515 1519 1523 1530 1539 1543 1545 1553 1569 1577 1578 1580 1583
## [309] 1585 1587 1596 1603 1606 1607 1610 1636 1638 1643 1644 1648 1650 1652
## [323] 1662 1665 1667 1669 1673 1689 1692 1705 1707 1723 1726 1729 1733 1735
## [337] 1739 1743 1750 1752 1756 1758 1761 1767 1773 1774 1775 1777 1778 1782
## [351] 1785 1792 1794 1808 1809 1822 1825 1829 1833 1836 1839 1842 1844 1862
## [365] 1867 1868 1870 1882 1883 1890 1894 1895 1898 1906 1920 1931 1935 1939
## [379] 1943 1944 1945 1947 1948 1950 1964 1970 1974 1978 1984 1988 1989 1991
## [393] 1996 2001 2006 2014 2018 2027 2031 2032 2034 2037 2043 2044 2048 2053
## [407] 2055 2056 2059 2060 2065 2079 2080 2095 2105 2110 2113 2126 2137 2139
## [421] 2146 2151 2158 2160 2173 2175 2177 2178 2182 2196 2204 2206 2207 2217
## [435] 2218 2224 2225 2245 2248 2253 2260 2261 2262 2265 2267 2271 2277 2285
## [449] 2291 2294 2300 2309 2317 2318 2319 2330 2341 2342 2348 2349 2352 2356
## [463] 2362 2369 2371 2372 2373 2379 2383 2384 2385 2399 2405 2407 2409 2410
## [477] 2427 2431 2435 2447 2451 2462 2470 2473 2475 2476 2481 2485 2487 2491
## [491] 2494 2495 2497 2504 2507 2509 2510 2527 2534 2535 2537 2540 2541 2543
## [505] 2546 2551 2553 2556 2569 2575 2576 2583 2598 2599 2611 2613 2621 2638
## [519] 2644 2645 2658 2660 2685 2690 2691 2697 2698 2699 2715 2718 2719 2725
## [533] 2726 2727 2728 2735 2737 2741 2742 2743 2744 2747 2748 2765 2772 2773
## [547] 2774 2788 2792 2803 2805 2811 2817 2819 2821 2828 2835 2840 2862 2865
## [561] 2866 2868 2870 2878 2888 2914 2916 2926 2927 2934 2935 2939 2944 2945
## [575] 2960 2963 2964 2967 2980 2994 2997 3001 3010 3012 3014 3015 3017 3018
## [589] 3020 3023 3026 3033 3034 3038 3040 3041 3045 3051 3053 3055 3057 3061
## [603] 3069 3084 3091 3098 3100 3112 3115 3119 3123 3128 3133 3137 3139 3142
## [617] 3151 3160 3166 3177 3179 3181 3182 3184 3193 3195 3197 3198 3203 3205
## [631] 3206 3207 3211 3218 3223 3224 3229 3232 3235 3237 3240 3257 3261 3266
## [645] 3269 3273 3278 3279 3281 3282 3283 3292 3294 3301 3302 3304 3307 3309
## [659] 3311 3314 3315 3319 3323 3329 3331 3333
##
## $index$Fold2
## [1] 5 6 13 17 23 26 27 30 31 32 36 39 42 46
## [15] 51 52 71 84 86 89 91 93 96 99 101 106 112 118
## [29] 121 129 131 133 147 148 150 152 155 156 166 176 180 182
## [43] 193 202 207 210 212 219 223 224 225 239 242 255 259 262
## [57] 263 266 267 269 271 275 279 285 293 298 306 311 313 319
## [71] 324 326 330 338 345 350 352 360 362 363 369 376 377 381
## [85] 391 393 397 399 405 407 410 411 412 415 418 431 438 439
## [99] 444 446 447 450 455 457 461 475 498 501 512 526 528 539
## [113] 552 555 559 574 575 577 600 602 603 609 616 619 631 637
## [127] 641 648 656 666 672 675 678 684 690 705 708 712 713 715
## [141] 717 727 730 733 739 748 752 760 763 770 773 782 793 797
## [155] 798 799 802 812 814 816 818 820 822 827 830 833 837 838
## [169] 839 841 842 845 858 859 861 873 886 890 892 893 902 906
## [183] 915 926 943 947 951 963 964 965 970 980 982 986 991 996
## [197] 1001 1013 1014 1020 1032 1035 1050 1051 1052 1071 1081 1089 1091 1093
## [211] 1097 1099 1100 1108 1113 1114 1121 1122 1130 1133 1137 1140 1144 1146
## [225] 1148 1149 1150 1152 1153 1158 1161 1162 1164 1165 1171 1173 1194 1195
## [239] 1203 1207 1208 1212 1214 1217 1221 1224 1225 1229 1231 1242 1245 1246
## [253] 1249 1250 1256 1274 1277 1278 1281 1288 1289 1293 1294 1299 1301 1305
## [267] 1316 1318 1326 1327 1328 1329 1336 1337 1339 1345 1351 1354 1356 1357
## [281] 1367 1369 1376 1398 1399 1403 1405 1407 1409 1413 1414 1423 1429 1431
## [295] 1438 1440 1457 1458 1468 1469 1479 1481 1486 1496 1504 1507 1510 1514
## [309] 1520 1528 1538 1542 1544 1559 1561 1564 1566 1572 1573 1576 1582 1586
## [323] 1592 1593 1597 1611 1613 1633 1646 1657 1658 1661 1674 1675 1676 1677
## [337] 1680 1681 1684 1697 1700 1706 1708 1709 1712 1714 1715 1716 1717 1718
## [351] 1720 1730 1747 1753 1754 1755 1762 1765 1776 1783 1787 1789 1791 1797
## [365] 1798 1812 1813 1815 1823 1834 1835 1846 1848 1849 1853 1856 1861 1863
## [379] 1864 1865 1878 1879 1884 1889 1904 1914 1917 1918 1923 1926 1928 1930
## [393] 1941 1942 1946 1955 1958 1960 1962 1968 1971 1975 1995 1997 1998 1999
## [407] 2003 2010 2022 2028 2035 2036 2045 2050 2061 2067 2071 2072 2073 2078
## [421] 2081 2083 2085 2086 2087 2088 2090 2099 2109 2114 2117 2125 2130 2134
## [435] 2135 2143 2145 2154 2156 2162 2166 2168 2179 2180 2190 2198 2200 2208
## [449] 2223 2231 2232 2233 2234 2239 2243 2244 2246 2249 2254 2256 2264 2266
## [463] 2273 2279 2281 2286 2288 2296 2316 2320 2322 2323 2326 2327 2328 2331
## [477] 2335 2337 2338 2340 2343 2344 2354 2361 2368 2381 2395 2400 2413 2418
## [491] 2422 2426 2440 2444 2445 2452 2453 2454 2461 2466 2469 2478 2482 2489
## [505] 2490 2493 2500 2501 2506 2508 2518 2519 2523 2536 2539 2549 2557 2559
## [519] 2564 2574 2578 2585 2595 2596 2604 2622 2623 2626 2627 2629 2633 2636
## [533] 2640 2647 2648 2649 2656 2663 2667 2671 2672 2676 2678 2681 2687 2692
## [547] 2694 2695 2700 2704 2716 2731 2734 2738 2750 2755 2759 2764 2766 2771
## [561] 2780 2782 2784 2786 2787 2804 2806 2808 2810 2829 2834 2843 2853 2863
## [575] 2871 2876 2896 2905 2910 2915 2925 2930 2941 2946 2948 2949 2951 2957
## [589] 2958 2971 2979 2981 2982 2989 2993 3005 3008 3009 3016 3024 3025 3029
## [603] 3030 3037 3039 3042 3044 3047 3048 3054 3062 3063 3066 3071 3074 3075
## [617] 3076 3077 3078 3080 3082 3087 3089 3096 3103 3113 3114 3125 3129 3130
## [631] 3135 3136 3143 3145 3147 3148 3152 3158 3163 3169 3173 3180 3183 3191
## [645] 3194 3196 3212 3226 3227 3228 3231 3234 3236 3239 3250 3255 3263 3264
## [659] 3276 3291 3295 3299 3300 3305 3321 3327 3330
##
## $index$Fold3
## [1] 1 8 9 14 19 20 22 25 28 29 50 64 65 72
## [15] 74 75 77 87 92 97 107 108 111 113 114 115 117 120
## [29] 124 125 127 134 139 140 153 164 165 174 187 189 190 191
## [43] 198 199 205 208 214 218 221 222 226 233 234 244 256 280
## [57] 292 294 302 303 318 322 327 331 337 346 354 361 364 367
## [71] 370 373 375 379 385 389 390 395 396 400 401 404 408 414
## [85] 416 422 424 426 435 464 472 474 481 484 488 490 502 503
## [99] 504 507 508 525 543 544 549 550 560 561 563 567 568 570
## [113] 571 572 576 578 580 582 594 595 596 604 618 623 629 630
## [127] 635 639 642 643 647 658 663 669 680 688 689 693 694 696
## [141] 698 699 700 702 709 711 714 721 723 725 744 753 756 761
## [155] 774 780 787 790 804 809 823 825 832 835 846 857 865 879
## [169] 880 888 891 896 899 901 912 914 925 928 932 937 939 946
## [183] 949 961 962 966 968 969 973 974 975 981 984 988 990 1003
## [197] 1005 1006 1012 1019 1022 1026 1029 1030 1033 1034 1040 1045 1046 1047
## [211] 1053 1054 1060 1061 1063 1065 1068 1069 1072 1073 1075 1076 1082 1090
## [225] 1095 1098 1107 1117 1120 1131 1143 1147 1160 1167 1168 1169 1176 1177
## [239] 1182 1189 1191 1193 1196 1206 1209 1213 1216 1218 1230 1238 1247 1248
## [253] 1251 1253 1258 1261 1263 1267 1271 1279 1285 1291 1296 1297 1298 1300
## [267] 1302 1308 1310 1315 1321 1330 1346 1355 1358 1364 1366 1371 1379 1381
## [281] 1382 1386 1391 1397 1408 1417 1418 1422 1425 1426 1434 1435 1442 1449
## [295] 1461 1472 1474 1475 1476 1477 1478 1487 1488 1494 1495 1502 1505 1506
## [309] 1518 1521 1524 1525 1546 1548 1549 1550 1554 1556 1557 1560 1568 1570
## [323] 1588 1589 1594 1595 1598 1599 1600 1602 1609 1621 1623 1624 1627 1628
## [337] 1639 1651 1653 1654 1659 1663 1670 1695 1696 1698 1701 1702 1703 1704
## [351] 1713 1736 1737 1740 1745 1746 1768 1771 1772 1779 1784 1786 1799 1802
## [365] 1806 1810 1811 1816 1818 1820 1821 1831 1838 1843 1845 1847 1851 1859
## [379] 1873 1875 1885 1886 1896 1900 1901 1902 1909 1910 1911 1912 1913 1916
## [393] 1919 1925 1932 1933 1934 1940 1949 1957 1961 1976 1977 1980 1994 2000
## [407] 2002 2008 2012 2015 2024 2029 2033 2042 2049 2058 2062 2070 2082 2084
## [421] 2091 2094 2104 2106 2116 2118 2131 2132 2138 2141 2153 2159 2161 2163
## [435] 2164 2170 2171 2174 2183 2191 2192 2193 2194 2215 2219 2222 2228 2235
## [449] 2236 2247 2251 2257 2258 2270 2272 2274 2276 2280 2282 2284 2292 2293
## [463] 2299 2304 2305 2307 2311 2332 2333 2346 2357 2358 2360 2364 2365 2375
## [477] 2376 2382 2389 2391 2392 2396 2398 2403 2411 2412 2415 2436 2437 2438
## [491] 2439 2441 2457 2458 2464 2465 2474 2477 2483 2488 2492 2496 2499 2512
## [505] 2513 2515 2520 2522 2529 2530 2533 2547 2548 2550 2554 2555 2561 2562
## [519] 2567 2571 2573 2581 2586 2587 2590 2593 2594 2600 2601 2602 2605 2608
## [533] 2609 2610 2612 2618 2620 2624 2634 2639 2641 2643 2652 2653 2657 2662
## [547] 2669 2673 2674 2686 2711 2713 2720 2723 2724 2733 2736 2739 2745 2746
## [561] 2752 2753 2757 2789 2790 2791 2798 2801 2816 2818 2830 2831 2833 2836
## [575] 2837 2856 2857 2869 2872 2874 2880 2881 2886 2893 2897 2909 2929 2932
## [589] 2933 2937 2943 2959 2962 2974 2978 2984 2988 2991 2995 2998 2999 3002
## [603] 3006 3013 3021 3027 3031 3032 3049 3064 3068 3070 3083 3092 3093 3097
## [617] 3101 3104 3106 3107 3109 3110 3118 3124 3126 3127 3134 3140 3149 3153
## [631] 3155 3159 3162 3164 3171 3176 3185 3189 3190 3192 3202 3214 3215 3216
## [645] 3220 3221 3225 3233 3238 3241 3242 3244 3245 3248 3251 3254 3259 3277
## [659] 3288 3289 3306 3308 3312 3313 3318 3326 3328
##
## $index$Fold4
## [1] 10 16 24 33 37 54 55 57 59 63 66 67 73 80
## [15] 81 88 98 103 105 119 123 132 137 138 141 142 145 146
## [29] 149 161 162 171 172 177 181 194 196 197 201 204 211 228
## [43] 245 249 253 254 258 260 264 270 272 273 289 296 297 304
## [57] 307 310 312 315 320 321 325 329 332 339 341 343 347 348
## [71] 349 353 356 357 359 371 374 382 383 388 392 394 427 430
## [85] 432 433 434 437 445 448 449 451 454 467 477 492 510 514
## [99] 516 517 519 523 535 537 542 545 546 554 556 558 566 573
## [113] 579 584 587 591 592 597 598 605 608 615 617 622 625 626
## [127] 628 632 646 649 655 667 674 677 681 686 692 695 704 707
## [141] 718 722 724 726 728 732 735 737 740 759 765 766 776 777
## [155] 785 801 810 813 815 817 824 840 844 849 853 856 863 868
## [169] 872 875 876 882 884 889 897 900 908 911 913 919 921 922
## [183] 927 929 933 934 938 941 942 950 952 955 957 959 971 976
## [197] 977 983 985 993 994 997 998 1008 1010 1011 1016 1017 1027 1043
## [211] 1044 1048 1055 1057 1058 1066 1074 1077 1079 1080 1083 1084 1086 1092
## [225] 1094 1102 1106 1110 1111 1119 1124 1126 1128 1134 1138 1142 1156 1159
## [239] 1163 1172 1183 1186 1190 1192 1199 1205 1210 1211 1219 1223 1226 1233
## [253] 1234 1244 1254 1255 1259 1268 1276 1280 1284 1286 1287 1292 1314 1325
## [267] 1332 1334 1335 1342 1343 1347 1350 1352 1353 1359 1362 1363 1372 1374
## [281] 1380 1390 1395 1402 1404 1411 1421 1427 1430 1432 1436 1437 1443 1448
## [295] 1450 1452 1455 1465 1467 1470 1471 1491 1492 1497 1503 1509 1513 1516
## [309] 1517 1527 1529 1535 1537 1540 1551 1552 1555 1558 1567 1574 1579 1584
## [323] 1590 1601 1604 1605 1614 1615 1616 1617 1620 1626 1629 1630 1631 1634
## [337] 1635 1640 1645 1660 1664 1668 1671 1672 1678 1679 1682 1690 1691 1693
## [351] 1710 1719 1721 1727 1728 1732 1734 1748 1749 1763 1770 1796 1803 1804
## [365] 1805 1817 1824 1827 1830 1832 1837 1857 1858 1860 1876 1877 1888 1897
## [379] 1922 1924 1936 1938 1951 1952 1953 1954 1959 1965 1966 1969 1972 1973
## [393] 1979 1982 1986 1992 2007 2009 2020 2021 2026 2039 2040 2051 2052 2063
## [407] 2064 2068 2069 2076 2077 2092 2096 2097 2102 2103 2107 2108 2111 2120
## [421] 2121 2122 2123 2124 2127 2128 2133 2140 2142 2144 2147 2148 2165 2167
## [435] 2172 2181 2185 2186 2197 2199 2202 2203 2205 2214 2238 2240 2241 2250
## [449] 2263 2269 2278 2283 2287 2289 2290 2297 2306 2310 2314 2315 2324 2325
## [463] 2336 2345 2351 2355 2363 2366 2367 2370 2374 2377 2380 2386 2390 2397
## [477] 2401 2406 2414 2419 2420 2421 2425 2428 2429 2434 2442 2449 2468 2472
## [491] 2484 2486 2498 2503 2505 2514 2516 2521 2524 2526 2528 2532 2538 2542
## [505] 2552 2558 2565 2566 2568 2577 2579 2580 2588 2589 2597 2603 2607 2615
## [519] 2616 2619 2625 2628 2632 2635 2650 2651 2654 2655 2659 2664 2665 2666
## [533] 2670 2677 2679 2680 2683 2688 2689 2693 2696 2701 2703 2706 2708 2709
## [547] 2729 2732 2740 2749 2754 2756 2758 2768 2770 2775 2778 2779 2783 2785
## [561] 2794 2795 2797 2799 2814 2822 2823 2824 2827 2841 2842 2844 2848 2854
## [575] 2858 2859 2861 2867 2875 2877 2884 2885 2889 2890 2892 2894 2895 2902
## [589] 2904 2908 2911 2912 2913 2917 2918 2919 2920 2921 2924 2931 2936 2940
## [603] 2942 2947 2954 2956 2965 2972 2975 2977 2983 2985 2986 2987 2990 3003
## [617] 3043 3052 3072 3073 3079 3081 3085 3094 3095 3099 3102 3105 3116 3122
## [631] 3131 3138 3144 3150 3157 3161 3167 3172 3174 3175 3178 3187 3201 3204
## [645] 3208 3210 3219 3222 3230 3243 3247 3249 3256 3260 3262 3267 3271 3272
## [659] 3274 3284 3287 3310 3317 3320 3324 3332
##
## $index$Fold5
## [1] 2 3 4 7 11 12 15 18 40 43 44 45 47 53
## [15] 58 61 62 69 78 79 82 90 95 100 128 130 135 144
## [29] 158 159 160 167 168 173 175 178 185 188 206 216 220 230
## [43] 235 236 237 247 248 251 265 276 277 278 283 284 286 287
## [57] 290 295 299 300 305 308 309 316 334 340 342 344 351 355
## [71] 358 366 368 378 380 386 387 402 403 409 413 417 419 420
## [85] 425 428 453 456 458 459 460 465 468 470 476 479 486 489
## [99] 497 500 505 506 511 513 518 521 522 524 531 532 533 534
## [113] 538 540 541 547 553 562 564 565 569 585 586 593 607 613
## [127] 627 633 638 640 659 660 661 665 670 676 679 683 687 691
## [141] 706 719 720 729 731 734 742 743 746 749 751 757 767 768
## [155] 771 778 781 783 786 788 789 791 792 795 800 819 826 829
## [169] 836 843 847 848 852 855 862 867 869 870 871 881 894 895
## [183] 903 916 917 918 923 924 930 935 936 940 944 948 953 956
## [197] 960 967 972 978 979 989 1002 1007 1009 1021 1023 1036 1037 1039
## [211] 1049 1056 1064 1070 1078 1085 1088 1096 1101 1103 1105 1109 1115 1118
## [225] 1135 1136 1139 1141 1145 1151 1154 1155 1166 1174 1175 1180 1181 1197
## [239] 1201 1202 1220 1222 1236 1239 1241 1243 1252 1257 1260 1264 1270 1272
## [253] 1273 1275 1295 1304 1306 1311 1313 1333 1338 1340 1344 1348 1349 1360
## [267] 1361 1375 1384 1385 1387 1389 1393 1400 1406 1410 1412 1415 1419 1424
## [281] 1439 1445 1446 1454 1459 1460 1463 1464 1480 1482 1485 1490 1493 1499
## [295] 1501 1512 1522 1526 1531 1532 1533 1534 1536 1541 1547 1562 1563 1565
## [309] 1571 1575 1581 1591 1608 1612 1618 1619 1622 1625 1632 1637 1641 1642
## [323] 1647 1649 1655 1656 1666 1683 1685 1686 1687 1688 1694 1699 1711 1722
## [337] 1724 1725 1731 1738 1741 1742 1744 1751 1757 1759 1760 1764 1766 1769
## [351] 1780 1781 1788 1790 1793 1795 1800 1801 1807 1814 1819 1826 1828 1840
## [365] 1841 1850 1852 1854 1855 1866 1869 1871 1872 1874 1880 1881 1887 1891
## [379] 1892 1893 1899 1903 1905 1907 1908 1915 1921 1927 1929 1937 1956 1963
## [393] 1967 1981 1983 1985 1987 1990 1993 2004 2005 2011 2013 2016 2017 2019
## [407] 2023 2025 2030 2038 2041 2046 2047 2054 2057 2066 2074 2075 2089 2093
## [421] 2098 2100 2101 2112 2115 2119 2129 2136 2149 2150 2152 2155 2157 2169
## [435] 2176 2184 2187 2188 2189 2195 2201 2209 2210 2211 2212 2213 2216 2220
## [449] 2221 2226 2227 2229 2230 2237 2242 2252 2255 2259 2268 2275 2295 2298
## [463] 2301 2302 2303 2308 2312 2313 2321 2329 2334 2339 2347 2350 2353 2359
## [477] 2378 2387 2388 2393 2394 2402 2404 2408 2416 2417 2423 2424 2430 2432
## [491] 2433 2443 2446 2448 2450 2455 2456 2459 2460 2463 2467 2471 2479 2480
## [505] 2502 2511 2517 2525 2531 2544 2545 2560 2563 2570 2572 2582 2584 2591
## [519] 2592 2606 2614 2617 2630 2631 2637 2642 2646 2661 2668 2675 2682 2684
## [533] 2702 2705 2707 2710 2712 2714 2717 2721 2722 2730 2751 2760 2761 2762
## [547] 2763 2767 2769 2776 2777 2781 2793 2796 2800 2802 2807 2809 2812 2813
## [561] 2815 2820 2825 2826 2832 2838 2839 2845 2846 2847 2849 2850 2851 2852
## [575] 2855 2860 2864 2873 2879 2882 2883 2887 2891 2898 2899 2900 2901 2903
## [589] 2906 2907 2922 2923 2928 2938 2950 2952 2953 2955 2961 2966 2968 2969
## [603] 2970 2973 2976 2992 2996 3000 3004 3007 3011 3019 3022 3028 3035 3036
## [617] 3046 3050 3056 3058 3059 3060 3065 3067 3086 3088 3090 3108 3111 3117
## [631] 3120 3121 3132 3141 3146 3154 3156 3165 3168 3170 3186 3188 3199 3200
## [645] 3209 3213 3217 3246 3252 3253 3258 3265 3268 3270 3275 3280 3285 3286
## [659] 3290 3293 3296 3297 3298 3303 3316 3322 3325
##
##
## $indexOut
## NULL
##
## $indexFinal
## NULL
##
## $timingSamps
## [1] 0
##
## $predictionBounds
## [1] FALSE FALSE
##
## $seeds
## [1] NA
##
## $adaptive
## $adaptive$min
## [1] 5
##
## $adaptive$alpha
## [1] 0.05
##
## $adaptive$method
## [1] "gls"
##
## $adaptive$complete
## [1] TRUE
##
##
## $trim
## [1] FALSE
##
## $allowParallel
## [1] TRUE
summaryFunction
and tuning parameters for multiple modelsglimpse(churn_x)
## Observations: 250
## Variables: 70
## $ stateAK <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateAL <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateAR <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateAZ <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateCA <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateCO <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateCT <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateDC <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,...
## $ stateDE <int> 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,...
## $ stateFL <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateGA <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateHI <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateIA <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateID <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateIL <int> 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,...
## $ stateIN <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateKS <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateKY <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateLA <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateMA <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateMD <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateME <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateMI <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateMN <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateMO <int> 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateMS <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateMT <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateNC <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateND <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateNE <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateNH <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateNJ <int> 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,...
## $ stateNM <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateNV <int> 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,...
## $ stateNY <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateOH <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateOK <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateOR <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ statePA <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,...
## $ stateRI <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateSC <int> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateSD <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateTN <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateTX <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateUT <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateVA <int> 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,...
## $ stateVT <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateWA <int> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,...
## $ stateWI <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateWV <int> 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ stateWY <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ account_length <int> 137, 83, 48, 67, 143, 163, 100, ...
## $ area_codearea_code_415 <int> 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1,...
## $ area_codearea_code_510 <int> 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,...
## $ international_planyes <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ voice_mail_planyes <int> 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0,...
## $ number_vmail_messages <int> 0, 0, 34, 0, 0, 0, 39, 0, 43, 30...
## $ total_day_minutes <dbl> 109.8, 196.7, 198.0, 164.5, 133....
## $ total_day_calls <int> 112, 117, 70, 79, 107, 100, 74, ...
## $ total_day_charge <dbl> 18.67, 33.44, 33.66, 27.97, 22.6...
## $ total_eve_minutes <dbl> 223.5, 272.0, 273.7, 110.3, 223....
## $ total_eve_calls <int> 88, 89, 121, 108, 117, 46, 80, 8...
## $ total_eve_charge <dbl> 19.00, 23.12, 23.26, 9.38, 19.03...
## $ total_night_minutes <dbl> 247.5, 199.9, 217.9, 203.9, 180....
## $ total_night_calls <int> 96, 62, 71, 102, 85, 116, 89, 88...
## $ total_night_charge <dbl> 11.14, 9.00, 9.81, 9.18, 8.12, 9...
## $ total_intl_minutes <dbl> 17.8, 10.1, 7.6, 9.8, 10.2, 12.8...
## $ total_intl_calls <int> 2, 11, 4, 2, 13, 3, 4, 5, 5, 2, ...
## $ total_intl_charge <dbl> 4.81, 2.73, 2.05, 2.65, 2.75, 3....
## $ number_customer_service_calls <int> 1, 3, 1, 1, 1, 5, 2, 0, 2, 3, 2,...
glimpse(churn_y)
## Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
# Create custom indices: myFolds
myFolds <- createFolds(churn_y, k = 5)
# Create reusable trainControl object: myControl
myControl <- trainControl(
summaryFunction = twoClassSummary,
classProbs = TRUE, # IMPORTANT!
verboseIter = TRUE,
savePredictions = TRUE,
index = myFolds
)
lm
or glm
functionsExample: glmnet on churn data
set.seed(42)
model_glmnet <- train(
churn ~ ., churnTrain,
metric = "ROC",
method = "glmnet",
tuneGrid = expand.grid(
alpha = 0:1,
lambda = 0:10/10
),
trControl = myControl
)
## + Fold1: alpha=0, lambda=1
## - Fold1: alpha=0, lambda=1
## + Fold1: alpha=1, lambda=1
## - Fold1: alpha=1, lambda=1
## + Fold2: alpha=0, lambda=1
## - Fold2: alpha=0, lambda=1
## + Fold2: alpha=1, lambda=1
## - Fold2: alpha=1, lambda=1
## + Fold3: alpha=0, lambda=1
## - Fold3: alpha=0, lambda=1
## + Fold3: alpha=1, lambda=1
## - Fold3: alpha=1, lambda=1
## + Fold4: alpha=0, lambda=1
## - Fold4: alpha=0, lambda=1
## + Fold4: alpha=1, lambda=1
## - Fold4: alpha=1, lambda=1
## + Fold5: alpha=0, lambda=1
## - Fold5: alpha=0, lambda=1
## + Fold5: alpha=1, lambda=1
## - Fold5: alpha=1, lambda=1
## Aggregating results
## Selecting tuning parameters
## Fitting alpha = 0, lambda = 1 on full training set
# Plot the results
plot(model_glmnet)
plot(model_glmnet$finalModel)
caret
automatically chooses the best values for lambda so we don’t need to do anything after looking at this plot# Fit glmnet model: model_glmnet
model_glmnet <- train(
x = churn_x, y = churn_y,
metric = "ROC",
method = "glmnet",
trControl = myControl
)
## + Fold1: alpha=0.10, lambda=0.01821
## - Fold1: alpha=0.10, lambda=0.01821
## + Fold1: alpha=0.55, lambda=0.01821
## - Fold1: alpha=0.55, lambda=0.01821
## + Fold1: alpha=1.00, lambda=0.01821
## - Fold1: alpha=1.00, lambda=0.01821
## + Fold2: alpha=0.10, lambda=0.01821
## - Fold2: alpha=0.10, lambda=0.01821
## + Fold2: alpha=0.55, lambda=0.01821
## - Fold2: alpha=0.55, lambda=0.01821
## + Fold2: alpha=1.00, lambda=0.01821
## - Fold2: alpha=1.00, lambda=0.01821
## + Fold3: alpha=0.10, lambda=0.01821
## - Fold3: alpha=0.10, lambda=0.01821
## + Fold3: alpha=0.55, lambda=0.01821
## - Fold3: alpha=0.55, lambda=0.01821
## + Fold3: alpha=1.00, lambda=0.01821
## - Fold3: alpha=1.00, lambda=0.01821
## + Fold4: alpha=0.10, lambda=0.01821
## - Fold4: alpha=0.10, lambda=0.01821
## + Fold4: alpha=0.55, lambda=0.01821
## - Fold4: alpha=0.55, lambda=0.01821
## + Fold4: alpha=1.00, lambda=0.01821
## - Fold4: alpha=1.00, lambda=0.01821
## + Fold5: alpha=0.10, lambda=0.01821
## - Fold5: alpha=0.10, lambda=0.01821
## + Fold5: alpha=0.55, lambda=0.01821
## - Fold5: alpha=0.55, lambda=0.01821
## + Fold5: alpha=1.00, lambda=0.01821
## - Fold5: alpha=1.00, lambda=0.01821
## Aggregating results
## Selecting tuning parameters
## Fitting alpha = 1, lambda = 0.0182 on full training set
Eample: random forest on churn data
set.seed(42)
churnTrain$churn <- factor(churnTrain$churn, levels = c("no", "yes"))
model_rf <- train(
churn ~ ., churnTrain,
metric = "ROC",
method = "ranger",
trControl = myControl
)
## + Fold1: mtry= 2, splitrule=gini
## - Fold1: mtry= 2, splitrule=gini
## + Fold1: mtry=35, splitrule=gini
## - Fold1: mtry=35, splitrule=gini
## + Fold1: mtry=69, splitrule=gini
## - Fold1: mtry=69, splitrule=gini
## + Fold1: mtry= 2, splitrule=extratrees
## - Fold1: mtry= 2, splitrule=extratrees
## + Fold1: mtry=35, splitrule=extratrees
## - Fold1: mtry=35, splitrule=extratrees
## + Fold1: mtry=69, splitrule=extratrees
## - Fold1: mtry=69, splitrule=extratrees
## + Fold2: mtry= 2, splitrule=gini
## - Fold2: mtry= 2, splitrule=gini
## + Fold2: mtry=35, splitrule=gini
## - Fold2: mtry=35, splitrule=gini
## + Fold2: mtry=69, splitrule=gini
## - Fold2: mtry=69, splitrule=gini
## + Fold2: mtry= 2, splitrule=extratrees
## - Fold2: mtry= 2, splitrule=extratrees
## + Fold2: mtry=35, splitrule=extratrees
## - Fold2: mtry=35, splitrule=extratrees
## + Fold2: mtry=69, splitrule=extratrees
## - Fold2: mtry=69, splitrule=extratrees
## + Fold3: mtry= 2, splitrule=gini
## - Fold3: mtry= 2, splitrule=gini
## + Fold3: mtry=35, splitrule=gini
## - Fold3: mtry=35, splitrule=gini
## + Fold3: mtry=69, splitrule=gini
## - Fold3: mtry=69, splitrule=gini
## + Fold3: mtry= 2, splitrule=extratrees
## - Fold3: mtry= 2, splitrule=extratrees
## + Fold3: mtry=35, splitrule=extratrees
## - Fold3: mtry=35, splitrule=extratrees
## + Fold3: mtry=69, splitrule=extratrees
## - Fold3: mtry=69, splitrule=extratrees
## + Fold4: mtry= 2, splitrule=gini
## - Fold4: mtry= 2, splitrule=gini
## + Fold4: mtry=35, splitrule=gini
## - Fold4: mtry=35, splitrule=gini
## + Fold4: mtry=69, splitrule=gini
## - Fold4: mtry=69, splitrule=gini
## + Fold4: mtry= 2, splitrule=extratrees
## - Fold4: mtry= 2, splitrule=extratrees
## + Fold4: mtry=35, splitrule=extratrees
## - Fold4: mtry=35, splitrule=extratrees
## + Fold4: mtry=69, splitrule=extratrees
## - Fold4: mtry=69, splitrule=extratrees
## + Fold5: mtry= 2, splitrule=gini
## - Fold5: mtry= 2, splitrule=gini
## + Fold5: mtry=35, splitrule=gini
## - Fold5: mtry=35, splitrule=gini
## + Fold5: mtry=69, splitrule=gini
## - Fold5: mtry=69, splitrule=gini
## + Fold5: mtry= 2, splitrule=extratrees
## - Fold5: mtry= 2, splitrule=extratrees
## + Fold5: mtry=35, splitrule=extratrees
## - Fold5: mtry=35, splitrule=extratrees
## + Fold5: mtry=69, splitrule=extratrees
## - Fold5: mtry=69, splitrule=extratrees
## Aggregating results
## Selecting tuning parameters
## Fitting mtry = 35, splitrule = extratrees on full training set
plot(model_rf)
# Fit random forest: model_rf
model_rf <- train(
x = churn_x, y = churn_y,
metric = "ROC",
method = "ranger",
trControl = myControl
)
## + Fold1: mtry= 2, splitrule=gini
## - Fold1: mtry= 2, splitrule=gini
## + Fold1: mtry=36, splitrule=gini
## - Fold1: mtry=36, splitrule=gini
## + Fold1: mtry=70, splitrule=gini
## - Fold1: mtry=70, splitrule=gini
## + Fold1: mtry= 2, splitrule=extratrees
## - Fold1: mtry= 2, splitrule=extratrees
## + Fold1: mtry=36, splitrule=extratrees
## - Fold1: mtry=36, splitrule=extratrees
## + Fold1: mtry=70, splitrule=extratrees
## - Fold1: mtry=70, splitrule=extratrees
## + Fold2: mtry= 2, splitrule=gini
## - Fold2: mtry= 2, splitrule=gini
## + Fold2: mtry=36, splitrule=gini
## - Fold2: mtry=36, splitrule=gini
## + Fold2: mtry=70, splitrule=gini
## - Fold2: mtry=70, splitrule=gini
## + Fold2: mtry= 2, splitrule=extratrees
## - Fold2: mtry= 2, splitrule=extratrees
## + Fold2: mtry=36, splitrule=extratrees
## - Fold2: mtry=36, splitrule=extratrees
## + Fold2: mtry=70, splitrule=extratrees
## - Fold2: mtry=70, splitrule=extratrees
## + Fold3: mtry= 2, splitrule=gini
## - Fold3: mtry= 2, splitrule=gini
## + Fold3: mtry=36, splitrule=gini
## - Fold3: mtry=36, splitrule=gini
## + Fold3: mtry=70, splitrule=gini
## - Fold3: mtry=70, splitrule=gini
## + Fold3: mtry= 2, splitrule=extratrees
## - Fold3: mtry= 2, splitrule=extratrees
## + Fold3: mtry=36, splitrule=extratrees
## - Fold3: mtry=36, splitrule=extratrees
## + Fold3: mtry=70, splitrule=extratrees
## - Fold3: mtry=70, splitrule=extratrees
## + Fold4: mtry= 2, splitrule=gini
## - Fold4: mtry= 2, splitrule=gini
## + Fold4: mtry=36, splitrule=gini
## - Fold4: mtry=36, splitrule=gini
## + Fold4: mtry=70, splitrule=gini
## - Fold4: mtry=70, splitrule=gini
## + Fold4: mtry= 2, splitrule=extratrees
## - Fold4: mtry= 2, splitrule=extratrees
## + Fold4: mtry=36, splitrule=extratrees
## - Fold4: mtry=36, splitrule=extratrees
## + Fold4: mtry=70, splitrule=extratrees
## - Fold4: mtry=70, splitrule=extratrees
## + Fold5: mtry= 2, splitrule=gini
## - Fold5: mtry= 2, splitrule=gini
## + Fold5: mtry=36, splitrule=gini
## - Fold5: mtry=36, splitrule=gini
## + Fold5: mtry=70, splitrule=gini
## - Fold5: mtry=70, splitrule=gini
## + Fold5: mtry= 2, splitrule=extratrees
## - Fold5: mtry= 2, splitrule=extratrees
## + Fold5: mtry=36, splitrule=extratrees
## - Fold5: mtry=36, splitrule=extratrees
## + Fold5: mtry=70, splitrule=extratrees
## - Fold5: mtry=70, splitrule=extratrees
## Aggregating results
## Selecting tuning parameters
## Fitting mtry = 36, splitrule = extratrees on full training set
caret
package provides the resamples()
function which is very useful for collecting the results from multiple models# make a model list
model_list <-list(
glmnet = model_glmnet,
rf = model_rf)
# collect resamples from the CV folds
resamps <- resamples(model_list)
# summarize the results
summary(resamps)
##
## Call:
## summary.resamples(object = resamps)
##
## Models: glmnet, rf
## Number of resamples: 5
##
## ROC
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## glmnet 0.3919540 0.5509890 0.6317241 0.5916097 0.6686561 0.7147253 0
## rf 0.5894253 0.6587091 0.6676923 0.6716094 0.7055172 0.7367033 0
##
## Sens
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## glmnet 0.9314286 0.9425287 0.9485714 0.9518621 0.9655172 0.9712644 0
## rf 0.9885057 0.9885057 0.9942529 0.9931100 0.9942857 1.0000000 0
##
## Spec
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## glmnet 0.04 0.07692308 0.08 0.15476923 0.1923077 0.38461538 0
## rf 0.00 0.00000000 0.00 0.02338462 0.0400000 0.07692308 0
resamples()
function, provided they have the same training data and use the same trainControl object with preset cross-validation folds.resamples()
takes as input a list of models and can be used to compare dozens of models at once (though in this case you are only comparing two models).# Create model_list
model_list <- list(item1 = model_glmnet,
item2 = model_rf)
# Pass model_list to resamples(): resamples
resamples <- resamples(model_list)
# Summarize the results
summary(resamples)
##
## Call:
## summary.resamples(object = resamples)
##
## Models: item1, item2
## Number of resamples: 5
##
## ROC
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## item1 0.3919540 0.5509890 0.6317241 0.5916097 0.6686561 0.7147253 0
## item2 0.5894253 0.6587091 0.6676923 0.6716094 0.7055172 0.7367033 0
##
## Sens
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## item1 0.9314286 0.9425287 0.9485714 0.9518621 0.9655172 0.9712644 0
## item2 0.9885057 0.9885057 0.9942529 0.9931100 0.9942857 1.0000000 0
##
## Spec
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## item1 0.04 0.07692308 0.08 0.15476923 0.1923077 0.38461538 0
## item2 0.00 0.00000000 0.00 0.02338462 0.0400000 0.07692308 0
dotplot(resamples, metric = "ROC")
caretEnsemble
packagebwplot
is a good way to find the best model visuallydotplot
is a visually simpler way to view the data and works well when there are many modelsdensityplot
shows the full distibution of AUC scores and is a good way to find outlier foldsxyplot
can directly compare the AUC on each foldbwplot(resamps, metric = "ROC")
dotplot(resamps, metric = "ROC")
densityplot(resamps, metric = "ROC")
xyplot(resamps, metric = "ROC")
- rf, gbm, svm, glmnet, rpart are all models we could use here. I’d like to try doing all of these and comparing with the
dotplot
chart -
bwplot()
function, which makes a box and whisker plot of the model’s out of sample scores.# Create bwplot
bwplot(resamples)
bwplot(resamples, metric = "ROC")
# Create xyplot
xyplot(resamples, metric = "ROC")
caretEnsemble
to get this workinglibrary(caretEnsemble)
model_list <- caretList(
churn ~ ., churnTrain,
trControl = myControl,
methodList = c("glm", "rpart", "rf", "gbm", "glmnet")
)
## + Fold1: parameter=none
## - Fold1: parameter=none
## + Fold2: parameter=none
## - Fold2: parameter=none
## + Fold3: parameter=none
## - Fold3: parameter=none
## + Fold4: parameter=none
## - Fold4: parameter=none
## + Fold5: parameter=none
## - Fold5: parameter=none
## Aggregating results
## Fitting final model on full training set
## + Fold1: cp=0.07867
## - Fold1: cp=0.07867
## + Fold2: cp=0.07867
## - Fold2: cp=0.07867
## + Fold3: cp=0.07867
## - Fold3: cp=0.07867
## + Fold4: cp=0.07867
## - Fold4: cp=0.07867
## + Fold5: cp=0.07867
## - Fold5: cp=0.07867
## Aggregating results
## Selecting tuning parameters
## Fitting cp = 0.089 on full training set
## + Fold1: mtry= 2
## - Fold1: mtry= 2
## + Fold1: mtry=35
## - Fold1: mtry=35
## + Fold1: mtry=69
## - Fold1: mtry=69
## + Fold2: mtry= 2
## - Fold2: mtry= 2
## + Fold2: mtry=35
## - Fold2: mtry=35
## + Fold2: mtry=69
## - Fold2: mtry=69
## + Fold3: mtry= 2
## - Fold3: mtry= 2
## + Fold3: mtry=35
## - Fold3: mtry=35
## + Fold3: mtry=69
## - Fold3: mtry=69
## + Fold4: mtry= 2
## - Fold4: mtry= 2
## + Fold4: mtry=35
## - Fold4: mtry=35
## + Fold4: mtry=69
## - Fold4: mtry=69
## + Fold5: mtry= 2
## - Fold5: mtry= 2
## + Fold5: mtry=35
## - Fold5: mtry=35
## + Fold5: mtry=69
## - Fold5: mtry=69
## Aggregating results
## Selecting tuning parameters
## Fitting mtry = 35 on full training set
## + Fold1: shrinkage=0.1, interaction.depth=1, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.8403 nan 0.1000 -0.0134
## 2 0.8370 nan 0.1000 -0.0166
## 3 0.8210 nan 0.1000 0.0058
## 4 0.8075 nan 0.1000 -0.0077
## 5 0.7931 nan 0.1000 0.0030
## 6 0.7826 nan 0.1000 -0.0022
## 7 0.7900 nan 0.1000 -0.0211
## 8 0.7750 nan 0.1000 -0.0098
## 9 0.7696 nan 0.1000 -0.0119
## 10 0.7550 nan 0.1000 -0.0072
## 20 0.6562 nan 0.1000 -0.0054
## 40 0.5646 nan 0.1000 -0.0046
## 60 0.4938 nan 0.1000 -0.0039
## 80 0.4435 nan 0.1000 -0.0072
## 100 0.3876 nan 0.1000 -0.0086
## 120 0.3637 nan 0.1000 -0.0062
## 140 0.3177 nan 0.1000 -0.0063
## 150 0.2954 nan 0.1000 -0.0045
##
## - Fold1: shrinkage=0.1, interaction.depth=1, n.minobsinnode=10, n.trees=150
## + Fold1: shrinkage=0.1, interaction.depth=2, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.8615 nan 0.1000 -0.0087
## 2 0.8489 nan 0.1000 -0.0054
## 3 0.8463 nan 0.1000 -0.0077
## 4 0.8512 nan 0.1000 -0.0197
## 5 0.8234 nan 0.1000 0.0111
## 6 0.7983 nan 0.1000 0.0064
## 7 0.7715 nan 0.1000 -0.0198
## 8 0.7670 nan 0.1000 -0.0063
## 9 0.7488 nan 0.1000 0.0005
## 10 0.7357 nan 0.1000 0.0008
## 20 0.6776 nan 0.1000 -0.0083
## 40 0.5274 nan 0.1000 -0.0053
## 60 0.4571 nan 0.1000 -0.0090
## 80 0.4178 nan 0.1000 -0.0051
## 100 0.3963 nan 0.1000 -0.0041
## 120 0.3522 nan 0.1000 -0.0065
## 140 0.3122 nan 0.1000 -0.0029
## 150 0.2918 nan 0.1000 -0.0015
##
## - Fold1: shrinkage=0.1, interaction.depth=2, n.minobsinnode=10, n.trees=150
## + Fold1: shrinkage=0.1, interaction.depth=3, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.8568 nan 0.1000 -0.0040
## 2 0.8529 nan 0.1000 -0.0071
## 3 0.8266 nan 0.1000 0.0007
## 4 0.8177 nan 0.1000 -0.0070
## 5 0.8202 nan 0.1000 -0.0161
## 6 0.7961 nan 0.1000 0.0097
## 7 0.7887 nan 0.1000 -0.0088
## 8 0.7695 nan 0.1000 0.0070
## 9 0.7507 nan 0.1000 -0.0046
## 10 0.7418 nan 0.1000 -0.0036
## 20 0.6477 nan 0.1000 -0.0003
## 40 0.5290 nan 0.1000 -0.0041
## 60 0.4959 nan 0.1000 -0.0030
## 80 0.4265 nan 0.1000 -0.0088
## 100 0.3639 nan 0.1000 -0.0044
## 120 0.3433 nan 0.1000 -0.0073
## 140 0.3011 nan 0.1000 -0.0082
## 150 0.2841 nan 0.1000 -0.0041
##
## - Fold1: shrinkage=0.1, interaction.depth=3, n.minobsinnode=10, n.trees=150
## + Fold2: shrinkage=0.1, interaction.depth=1, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.6156 nan 0.1000 0.0027
## 2 0.6138 nan 0.1000 -0.0046
## 3 0.6007 nan 0.1000 0.0018
## 4 0.5981 nan 0.1000 -0.0085
## 5 0.6002 nan 0.1000 -0.0118
## 6 0.5765 nan 0.1000 0.0011
## 7 0.5590 nan 0.1000 0.0014
## 8 0.5473 nan 0.1000 0.0028
## 9 0.5505 nan 0.1000 -0.0084
## 10 0.5315 nan 0.1000 -0.0007
## 20 0.4621 nan 0.1000 0.0044
## 40 0.3285 nan 0.1000 -0.0040
## 60 0.2439 nan 0.1000 -0.0011
## 80 0.2040 nan 0.1000 -0.0011
## 100 0.1716 nan 0.1000 -0.0052
## 120 0.1646 nan 0.1000 -0.0054
## 140 0.1454 nan 0.1000 -0.0048
## 150 0.1198 nan 0.1000 -0.0018
##
## - Fold2: shrinkage=0.1, interaction.depth=1, n.minobsinnode=10, n.trees=150
## + Fold2: shrinkage=0.1, interaction.depth=2, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.6073 nan 0.1000 0.0108
## 2 0.5748 nan 0.1000 -0.0061
## 3 0.5644 nan 0.1000 -0.0158
## 4 0.5641 nan 0.1000 -0.0073
## 5 0.5683 nan 0.1000 -0.0139
## 6 0.5473 nan 0.1000 0.0017
## 7 0.5467 nan 0.1000 -0.0088
## 8 0.5483 nan 0.1000 -0.0086
## 9 0.5487 nan 0.1000 -0.0065
## 10 0.5511 nan 0.1000 -0.0077
## 20 0.4399 nan 0.1000 0.0025
## 40 0.3290 nan 0.1000 -0.0076
## 60 0.2538 nan 0.1000 -0.0050
## 80 0.1917 nan 0.1000 -0.0039
## 100 0.1555 nan 0.1000 -0.0003
## 120 0.1394 nan 0.1000 -0.0009
## 140 0.0982 nan 0.1000 -0.0032
## 150 0.0948 nan 0.1000 -0.0009
##
## - Fold2: shrinkage=0.1, interaction.depth=2, n.minobsinnode=10, n.trees=150
## + Fold2: shrinkage=0.1, interaction.depth=3, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.6237 nan 0.1000 0.0072
## 2 0.6059 nan 0.1000 -0.0229
## 3 0.5988 nan 0.1000 -0.0016
## 4 0.5831 nan 0.1000 -0.0032
## 5 0.5814 nan 0.1000 -0.0094
## 6 0.5820 nan 0.1000 -0.0107
## 7 0.5617 nan 0.1000 0.0085
## 8 0.5458 nan 0.1000 0.0053
## 9 0.5331 nan 0.1000 0.0039
## 10 0.5123 nan 0.1000 -0.0078
## 20 0.4402 nan 0.1000 0.0028
## 40 0.2933 nan 0.1000 0.0001
## 60 0.2296 nan 0.1000 -0.0051
## 80 0.1823 nan 0.1000 -0.0022
## 100 0.1696 nan 0.1000 -0.0053
## 120 0.1243 nan 0.1000 -0.0007
## 140 0.1020 nan 0.1000 -0.0003
## 150 0.0927 nan 0.1000 -0.0022
##
## - Fold2: shrinkage=0.1, interaction.depth=3, n.minobsinnode=10, n.trees=150
## + Fold3: shrinkage=0.1, interaction.depth=1, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.7816 nan 0.1000 0.0085
## 2 0.7765 nan 0.1000 -0.0035
## 3 0.7715 nan 0.1000 -0.0108
## 4 0.7581 nan 0.1000 -0.0150
## 5 0.7390 nan 0.1000 0.0003
## 6 0.7356 nan 0.1000 -0.0061
## 7 0.7152 nan 0.1000 0.0046
## 8 0.7154 nan 0.1000 -0.0084
## 9 0.6916 nan 0.1000 -0.0019
## 10 0.6863 nan 0.1000 -0.0118
## 20 0.6230 nan 0.1000 0.0006
## 40 0.5171 nan 0.1000 -0.0071
## 60 0.4101 nan 0.1000 -0.0047
## 80 0.3340 nan 0.1000 -0.0031
## 100 0.2788 nan 0.1000 -0.0020
## 120 0.2446 nan 0.1000 -0.0018
## 140 0.2179 nan 0.1000 -0.0036
## 150 0.1881 nan 0.1000 -0.0024
##
## - Fold3: shrinkage=0.1, interaction.depth=1, n.minobsinnode=10, n.trees=150
## + Fold3: shrinkage=0.1, interaction.depth=2, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.8080 nan 0.1000 0.0045
## 2 0.7980 nan 0.1000 0.0000
## 3 0.7547 nan 0.1000 0.0005
## 4 0.7501 nan 0.1000 -0.0133
## 5 0.7419 nan 0.1000 -0.0075
## 6 0.7340 nan 0.1000 -0.0060
## 7 0.7208 nan 0.1000 0.0026
## 8 0.7172 nan 0.1000 -0.0076
## 9 0.7029 nan 0.1000 -0.0062
## 10 0.6964 nan 0.1000 -0.0072
## 20 0.6357 nan 0.1000 -0.0028
## 40 0.5346 nan 0.1000 0.0120
## 60 0.4005 nan 0.1000 -0.0042
## 80 0.3155 nan 0.1000 -0.0056
## 100 0.2720 nan 0.1000 -0.0061
## 120 0.2180 nan 0.1000 -0.0063
## 140 0.1779 nan 0.1000 -0.0021
## 150 0.1716 nan 0.1000 -0.0042
##
## - Fold3: shrinkage=0.1, interaction.depth=2, n.minobsinnode=10, n.trees=150
## + Fold3: shrinkage=0.1, interaction.depth=3, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.8149 nan 0.1000 -0.0113
## 2 0.7841 nan 0.1000 0.0123
## 3 0.7777 nan 0.1000 0.0026
## 4 0.7441 nan 0.1000 0.0021
## 5 0.7418 nan 0.1000 -0.0096
## 6 0.7263 nan 0.1000 0.0018
## 7 0.7120 nan 0.1000 0.0011
## 8 0.7086 nan 0.1000 -0.0009
## 9 0.6852 nan 0.1000 0.0033
## 10 0.6891 nan 0.1000 -0.0136
## 20 0.6271 nan 0.1000 -0.0102
## 40 0.5138 nan 0.1000 -0.0041
## 60 0.4358 nan 0.1000 -0.0038
## 80 0.3644 nan 0.1000 -0.0078
## 100 0.3223 nan 0.1000 -0.0024
## 120 0.2947 nan 0.1000 -0.0059
## 140 0.2486 nan 0.1000 -0.0088
## 150 0.2276 nan 0.1000 -0.0018
##
## - Fold3: shrinkage=0.1, interaction.depth=3, n.minobsinnode=10, n.trees=150
## + Fold4: shrinkage=0.1, interaction.depth=1, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.7213 nan 0.1000 0.0123
## 2 0.7020 nan 0.1000 0.0006
## 3 0.7051 nan 0.1000 -0.0104
## 4 0.6583 nan 0.1000 0.0154
## 5 0.6413 nan 0.1000 0.0066
## 6 0.6173 nan 0.1000 -0.0025
## 7 0.6101 nan 0.1000 0.0006
## 8 0.6100 nan 0.1000 -0.0105
## 9 0.6109 nan 0.1000 -0.0078
## 10 0.6109 nan 0.1000 -0.0057
## 20 0.5028 nan 0.1000 -0.0138
## 40 0.3675 nan 0.1000 0.0003
## 60 0.3089 nan 0.1000 0.0005
## 80 0.2607 nan 0.1000 0.0003
## 100 0.2106 nan 0.1000 0.0024
## 120 0.1770 nan 0.1000 -0.0041
## 140 0.1431 nan 0.1000 -0.0070
## 150 0.1249 nan 0.1000 -0.0009
##
## - Fold4: shrinkage=0.1, interaction.depth=1, n.minobsinnode=10, n.trees=150
## + Fold4: shrinkage=0.1, interaction.depth=2, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.7094 nan 0.1000 0.0149
## 2 0.6741 nan 0.1000 0.0148
## 3 0.6713 nan 0.1000 -0.0136
## 4 0.6558 nan 0.1000 0.0060
## 5 0.6561 nan 0.1000 -0.0138
## 6 0.6565 nan 0.1000 -0.0088
## 7 0.6383 nan 0.1000 0.0049
## 8 0.6099 nan 0.1000 0.0102
## 9 0.5894 nan 0.1000 0.0013
## 10 0.5810 nan 0.1000 -0.0001
## 20 0.4673 nan 0.1000 -0.0070
## 40 0.3712 nan 0.1000 -0.0009
## 60 0.2936 nan 0.1000 -0.0057
## 80 0.2237 nan 0.1000 -0.0021
## 100 0.1845 nan 0.1000 -0.0024
## 120 0.1709 nan 0.1000 0.0007
## 140 0.1179 nan 0.1000 -0.0028
## 150 0.1037 nan 0.1000 -0.0017
##
## - Fold4: shrinkage=0.1, interaction.depth=2, n.minobsinnode=10, n.trees=150
## + Fold4: shrinkage=0.1, interaction.depth=3, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.7037 nan 0.1000 0.0205
## 2 0.6815 nan 0.1000 0.0049
## 3 0.6575 nan 0.1000 0.0089
## 4 0.6560 nan 0.1000 -0.0129
## 5 0.6554 nan 0.1000 -0.0093
## 6 0.6261 nan 0.1000 0.0020
## 7 0.6256 nan 0.1000 -0.0075
## 8 0.6072 nan 0.1000 0.0040
## 9 0.6024 nan 0.1000 -0.0034
## 10 0.5812 nan 0.1000 -0.0121
## 20 0.5227 nan 0.1000 0.0064
## 40 0.3991 nan 0.1000 -0.0083
## 60 0.3173 nan 0.1000 -0.0068
## 80 0.2659 nan 0.1000 -0.0031
## 100 0.2367 nan 0.1000 -0.0073
## 120 0.1710 nan 0.1000 -0.0026
## 140 0.1351 nan 0.1000 -0.0026
## 150 0.1188 nan 0.1000 -0.0007
##
## - Fold4: shrinkage=0.1, interaction.depth=3, n.minobsinnode=10, n.trees=150
## + Fold5: shrinkage=0.1, interaction.depth=1, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.7175 nan 0.1000 -0.0001
## 2 0.6991 nan 0.1000 0.0083
## 3 0.6792 nan 0.1000 0.0015
## 4 0.6812 nan 0.1000 -0.0097
## 5 0.6689 nan 0.1000 -0.0046
## 6 0.6724 nan 0.1000 -0.0216
## 7 0.6697 nan 0.1000 -0.0105
## 8 0.6524 nan 0.1000 -0.0110
## 9 0.6340 nan 0.1000 -0.0004
## 10 0.6204 nan 0.1000 0.0016
## 20 0.5511 nan 0.1000 -0.0138
## 40 0.4604 nan 0.1000 -0.0073
## 60 0.3763 nan 0.1000 -0.0054
## 80 0.3186 nan 0.1000 -0.0051
## 100 0.2823 nan 0.1000 -0.0020
## 120 0.2383 nan 0.1000 -0.0053
## 140 0.2150 nan 0.1000 -0.0056
## 150 0.2003 nan 0.1000 -0.0078
##
## - Fold5: shrinkage=0.1, interaction.depth=1, n.minobsinnode=10, n.trees=150
## + Fold5: shrinkage=0.1, interaction.depth=2, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.6945 nan 0.1000 -0.0120
## 2 0.6767 nan 0.1000 -0.0152
## 3 0.6635 nan 0.1000 -0.0067
## 4 0.6589 nan 0.1000 -0.0060
## 5 0.6448 nan 0.1000 -0.0047
## 6 0.6453 nan 0.1000 -0.0099
## 7 0.6229 nan 0.1000 -0.0015
## 8 0.6116 nan 0.1000 0.0000
## 9 0.5964 nan 0.1000 -0.0039
## 10 0.5984 nan 0.1000 -0.0098
## 20 0.5789 nan 0.1000 -0.0030
## 40 0.4388 nan 0.1000 -0.0097
## 60 0.3718 nan 0.1000 -0.0080
## 80 0.3201 nan 0.1000 -0.0079
## 100 0.2790 nan 0.1000 -0.0082
## 120 0.2428 nan 0.1000 -0.0054
## 140 0.1996 nan 0.1000 -0.0032
## 150 0.1976 nan 0.1000 -0.0060
##
## - Fold5: shrinkage=0.1, interaction.depth=2, n.minobsinnode=10, n.trees=150
## + Fold5: shrinkage=0.1, interaction.depth=3, n.minobsinnode=10, n.trees=150
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.7307 nan 0.1000 -0.0048
## 2 0.7179 nan 0.1000 0.0029
## 3 0.7001 nan 0.1000 -0.0047
## 4 0.6828 nan 0.1000 0.0076
## 5 0.6681 nan 0.1000 -0.0008
## 6 0.6579 nan 0.1000 -0.0099
## 7 0.6347 nan 0.1000 0.0009
## 8 0.6375 nan 0.1000 -0.0120
## 9 0.6425 nan 0.1000 -0.0121
## 10 0.6457 nan 0.1000 -0.0123
## 20 0.5627 nan 0.1000 -0.0090
## 40 0.4499 nan 0.1000 -0.0051
## 60 0.3633 nan 0.1000 -0.0073
## 80 0.3097 nan 0.1000 -0.0018
## 100 0.2779 nan 0.1000 -0.0021
## 120 0.2452 nan 0.1000 -0.0022
## 140 0.2358 nan 0.1000 0.0009
## 150 0.2072 nan 0.1000 -0.0060
##
## - Fold5: shrinkage=0.1, interaction.depth=3, n.minobsinnode=10, n.trees=150
## Aggregating results
## Selecting tuning parameters
## Fitting n.trees = 150, interaction.depth = 3, shrinkage = 0.1, n.minobsinnode = 10 on full training set
## Iter TrainDeviance ValidDeviance StepSize Improve
## 1 0.7763 nan 0.1000 0.0256
## 2 0.7409 nan 0.1000 0.0187
## 3 0.7140 nan 0.1000 0.0126
## 4 0.6918 nan 0.1000 0.0103
## 5 0.6727 nan 0.1000 0.0080
## 6 0.6457 nan 0.1000 0.0133
## 7 0.6244 nan 0.1000 0.0089
## 8 0.6082 nan 0.1000 0.0074
## 9 0.5961 nan 0.1000 0.0056
## 10 0.5780 nan 0.1000 0.0085
## 20 0.4833 nan 0.1000 0.0034
## 40 0.4047 nan 0.1000 0.0008
## 60 0.3611 nan 0.1000 0.0010
## 80 0.3324 nan 0.1000 0.0001
## 100 0.3092 nan 0.1000 0.0006
## 120 0.2934 nan 0.1000 -0.0002
## 140 0.2789 nan 0.1000 -0.0002
## 150 0.2727 nan 0.1000 0.0001
##
## + Fold1: alpha=0.10, lambda=0.01829
## - Fold1: alpha=0.10, lambda=0.01829
## + Fold1: alpha=0.55, lambda=0.01829
## - Fold1: alpha=0.55, lambda=0.01829
## + Fold1: alpha=1.00, lambda=0.01829
## - Fold1: alpha=1.00, lambda=0.01829
## + Fold2: alpha=0.10, lambda=0.01829
## - Fold2: alpha=0.10, lambda=0.01829
## + Fold2: alpha=0.55, lambda=0.01829
## - Fold2: alpha=0.55, lambda=0.01829
## + Fold2: alpha=1.00, lambda=0.01829
## - Fold2: alpha=1.00, lambda=0.01829
## + Fold3: alpha=0.10, lambda=0.01829
## - Fold3: alpha=0.10, lambda=0.01829
## + Fold3: alpha=0.55, lambda=0.01829
## - Fold3: alpha=0.55, lambda=0.01829
## + Fold3: alpha=1.00, lambda=0.01829
## - Fold3: alpha=1.00, lambda=0.01829
## + Fold4: alpha=0.10, lambda=0.01829
## - Fold4: alpha=0.10, lambda=0.01829
## + Fold4: alpha=0.55, lambda=0.01829
## - Fold4: alpha=0.55, lambda=0.01829
## + Fold4: alpha=1.00, lambda=0.01829
## - Fold4: alpha=1.00, lambda=0.01829
## + Fold5: alpha=0.10, lambda=0.01829
## - Fold5: alpha=0.10, lambda=0.01829
## + Fold5: alpha=0.55, lambda=0.01829
## - Fold5: alpha=0.55, lambda=0.01829
## + Fold5: alpha=1.00, lambda=0.01829
## - Fold5: alpha=1.00, lambda=0.01829
## Aggregating results
## Selecting tuning parameters
## Fitting alpha = 1, lambda = 0.0183 on full training set
# Create ensemble model: stack
stack <- caretStack(
model_list,
method = "glm",
metric = "ROC",
trControl=trainControl(
method="boot",
number=10,
savePredictions="final",
classProbs=TRUE,
summaryFunction=twoClassSummary
))
# Look at summary
summary(stack)
##
## Call:
## NULL
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.8956 -0.5425 -0.4428 -0.4033 2.3576
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -2.63237 0.04982 -52.839 < 2e-16 ***
## glm 0.08619 0.04733 1.821 0.068567 .
## rpart 1.02053 0.28036 3.640 0.000273 ***
## rf 5.37613 0.19904 27.011 < 2e-16 ***
## gbm 0.15405 0.10395 1.482 0.138357
## glmnet -0.62098 0.13222 -4.696 2.65e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 13600 on 16414 degrees of freedom
## Residual deviance: 12510 on 16409 degrees of freedom
## AIC: 12522
##
## Number of Fisher Scoring iterations: 4
dotplot(resamples(model_list), metric = "ROC")