'Why can I not use vif when my model is made from a matrix
I'm using an example based on cars for my inquiry here.
I can use vif from the car package when the model is listed out.
vif(lm(mpg ~ wheels + tires + weight))
But let's say I combined wheels, tires, and weight to a matrix called "car_specs". Now I can no longer use vif on the model.
vif(lm(mpg ~ car_specs))
I want to use vif in the second case for a function I wrote
Model_and_VIF <- function(...){
#Getting list of names of the variables
argnames = toupper(unlist(lapply(as.list(sys.call())[-1], deparse)))
#making a list of vectors
args = list(...)
#Making a blank matrix
data <- matrix(unlist(args[1]), ncol = 1,nrow = 4019)
colnames(data) <- c(argnames[1])
#Adding columns of other variabls
for (i in 2:length(args)){
data <- cbind(data, unlist(args[i]))
colnames(data)[i] <- c(argnames[i])
}
dep <- data[,1]
indep <- data[,2:ncol(data)]
print(c(argnames[1], "vs.", argnames[2:length(argnames)]))
print(summary(lm(dep ~ indep)))
vif(lm(dep ~ indep))
}
Why will vif no longer work?
Here is some sample code
mdat <- matrix(c(1:150, 151:300), nrow = 100, ncol = 3, byrow = TRUE)
colnames(mdat) <- c("boat", "coal", "grain")```
boat <- mdat[,1]
coal <- mdat[,2]
grain <- mdat[,3]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
