Search code examples
rmachine-learningclassificationr-caretmulticollinearity

How to use VIF in r?


I am new in R and learning ml using caret. I was working on UCI bank marketing response data but used iris data here for reproducibility.

Issue is that I am getting error on running vif from car package on classification models.

library(tidyverse)
library(caret)
library(car)

iris

# to make it binary classification
iris_train <- iris %>% filter(Species %in% c("setosa","versicolor"))
iris_train$Species <- factor(iris_train$Species)

Creating Model


model_iris3 <- train(Species ~ ., 
                      data = iris_train, 
                      method = "gbm",
                     verbose = FALSE
                      # tuneLength = 5,
                      # metric = "Spec", 
                      # trControl = fitCtrl
                      )

Error in vif

# vif
car::vif(model_iris3)

Error in UseMethod("vcov") : no applicable method for 'vcov' applied to an object of class "c('train', 'train.formula')"

I got to know about using finalModel for vif from this SO post: Variance inflation VIF for glm caret model in R

But still getting an error

car::vif(model_iris3$finalModel)

Error in UseMethod("vcov") : no applicable method for 'vcov' applied to an object of class "gbm"

same error I get with adaboost, earth etc.

Appreciate any help or suggestions to solve this issue.

(UPDATE)

Finally this worked (see the complete solution in Answers if you still get an error):

vif doesn't work on classification models so convert dependent variable to numeric and run linear regression on it and then vif


model_iris4 <- train(as.numeric(Species) ~ ., 
                      data = iris_train, 
                      method = "lm",
                     verbose = FALSE
                      # tuneLength = 5,
                      # metric = "Spec", 
                      # trControl = fitCtrl
                      )

car::vif(model_iris4$finalModel)

######## output ##########

Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
    4.803414     2.594389    36.246326    25.421395 

Solution

  • car::vif is a function that needs to be adapted for each type of model. It works in the linked question because car::vif has been implemented to cope with glm models. car::vif does not support your chosen model type: gbm.