Handling low-information predictors

Machine Learning with caret in R

Zach Mayer

Data Scientist at DataRobot and co-author of caret

No (or low) variance variables

  • Some variables don't contain much information
    • Constant (i.e. no variance)
    • Nearly constant (i.e. low variance)
  • Easy for one fold of CV to end up with constant column
    • Can cause problems for your models
  • Usually remove extremely low variance variables
Machine Learning with caret in R

Example: constant 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 constant-valued column to mtcars
X$bad <- 1
Machine Learning with caret in R

Example: constant column in mtcars

# 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   
Machine Learning with caret in R

caret to the rescue (again)

  • "zv" removes constant columns
  • "nzv" removes nearly constant columns
# 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

Let’s practice!

Machine Learning with caret in R

Preparing Video For Download...