I’m fitting different models and then I would like to compute the VIF from package car. The VIF requires more than one regressor in the formula so returns an error. The idea is that check whether there is more than one regressor before running the function. Alternatively, I can try to sort the models by number of regressors so the VIF function is called only from the ith position on.
My code is:
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
ot=rnorm(length(trt))
ot2=rnorm(length(trt))
DF=as.data.frame(cbind(ot,ot2,ctl,trt))
y=colnames(DF[1])
x = colnames(DF)
x = x[! x %in% y]
n = length(x)
id = unlist(
lapply(1:n,
function(i)combn(1:n,i,simplify=F)
)
,recursive=F)
models = sapply(id,function(i)
paste(y,paste(x[i],collapse="+"), sep="~")
)
allModelsResults=lapply(models, function(i)
lm(as.formula(i), data=DF))
library(car)
vif.r=sapply(allModelsResults,vif)
This works:
Note that
length(labels(terms(mod))) < 2is whatvifis using as its criterion, seegetAnywhere(vif.lm).