Reputation: 129
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.
Upvotes: 0
Views: 301
Reputation: 1495
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.
Upvotes: 2