Machine Learning with caret in R
Zach Mayer
Data Scientist at DataRobot and co-author of caret
# 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 constant-valued column to mtcars
X$bad <- 1
# Try to fit a model with PCA + glm
model <- train(
X, Y, method = "glm",
preProcess = c("center", "scale", "medianImpute", "pca"))
Warning in preProcess.default(thresh = 0.95, k = 5, method = c("medianImpute", :
These variables have zero variances: bad
Something is wrong; all the RMSE metric values are missing:
RMSE Rsquared
Min. : NA Min. : NA
1st Qu.: NA 1st Qu.: NA
Median : NA Median : NA
Mean :NaN Mean :NaN
3rd Qu.: NA 3rd Qu.: NA
Max. : NA Max. : NA
NA's :1 NA's :1
# Have caret remove those columns during modeling
set.seed(42)
model <- train(
X, Y, method = "glm",
preProcess = c("zv", "center", "scale", "medianImpute", "pca")
)
min(model$results$RMSE)
3.402557
Machine Learning with caret in R