0

I am trying to run the vif function (lowercase letters) in rstudio using the data frame below but I keep getting an error stating

Error in y[, i] : incorrect number of dimensions

What am I doing wrong? Why is the vif function giving me the error?

What I have already tried is the code below.

library(usdm) # needed for vif function
x1 <- c(1,2,3,4,5)
x2 <- c(6,7,8,9,10)
x3 <- c(11,12,13,14,15)
y <- c(44,55,66,77,88)
dataFrame = data.frame(x1,x2,x3,y)
vif(dataFrame)

The expected results should be a table such as,

  Variables            VIF
1        x1       9.294002
2        x2       3.324287
3        x3       5.665959
4         Y      12.011500

Note: in this case all the numbers in the VIF column are just picked at random by me.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Boognish
  • 129
  • 12

1 Answers1

2

So I tracked down this error to the following line of code within the usdm:::.vif function (more on how I did this later, if you're interested):

lm(y[, i] ~ ., data = y[-i])

Here, y is your dataFrame object, and i is a column index from your dataFrame.... so if you did something like the following, you will encounter the same error:

y = dataFrame
i = 1
lm(y[, i] ~ ., data = y[-i])

I believe the issue here is the .vif function is naming your object (dataFrame) as y and your data also has a variable named y.

To get past this, you can just re-name the variable y in your data frame to something else, say z:

library(usdm) # needed for vif function
x1 <- c(1,2,3,4,5)
x2 <- c(6,7,8,9,10)
x3 <- c(11,12,13,14,15)
z <- c(44,55,66,77,88)
dataFrame = data.frame(x1,x2,x3,z)
vif(dataFrame)

In case you're interested:

To track down this error, I initially just typed the vif function in the console to look at the code. However, it is a generic function. I followed How can I view the source code for a function? and by doing:

showMethods("vif")
getMethod("vif", "data.frame")

I was able to see the code for said function. Within the code that is revealed, you will see a point where the following line is executed:

v <- .vif(x)

I then ran debugonce(usdm:::.vif) in the console, and ran your code. This allowed me to step into the function to find the issue.

jav
  • 1,485
  • 8
  • 11
  • Thank you so much! I got it to work using your explanation by doing the following: library(usdm) # needed for vif function x1 <- c(1,2,3,4,5) x2 <- c(6,7,8,9,10) x3 <- c(11,12,13,14,15) z <- c(44,55,66,77,88) y1 <- z # put all the variables above into a data frame so we can run vif() dataset <- CH06PR12 dataset <- dataset[c("x1","x2","x3")] dataset$cases1 <- y1 # append y1 to the data set with column name "cases1" vif(dataset) – Boognish Nov 07 '19 at 12:28
  • 1
    Also, thank you for providing information about the source code so I know how the vif function works 'under the hood' in R. – Boognish Nov 07 '19 at 12:31