Variance inflation VIF for glm caret model in R

Viewed 1779

I want to calculate variance inflation factor (VIF) for a caret glm model in R. This is my code and the dataset is from UCI:

library(caret)
library(tidyverse)

url <- paste0("https://archive.ics.uci.edu/ml/machine-learning-databases/",
              "00267/data_banknote_authentication.txt")

dataset <- read_csv(url, col_names = c("varWav","skeWav","curtWav","entropy","class"))
dataset$class <- as.factor(ifelse(dataset$class == 0,"Authentic","Forgery"))

idx <- createDataPartition(dataset$class, p = 0.8, list = FALSE)
train_set <- dataset[idx,]
test_set <- dataset[-idx,]

notes_model <- train(class ~.,
                   data = train_set,
                   method = "glm")

But when I try this code it returns me an error:

car::vif(notes_model)

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

Perhaps my code is wrong? Please, any help will be greatly appreciated.

1 Answers

You can extract the final trained model and then calculate vif with that:

car::vif(notes_model$finalModel)
    varWav     skeWav    curtWav    entropy 
 63.978111 184.323806 356.526156   1.935005 
Related