Reputation: 380
For simulated data I want to find VIF. I also want to know that at different iterations how many times a variable appeared to have VIF >10.
for (i in 1:10){
z1<-rnorm(1000,0,1)
z2<-rnorm(1000,0,1)
z3<-rnorm(1000,0,1)
x1<-z1
x2<-z1*2+z2
x3<-z2+z3
X<-cbind(x1,x2,x3)
sx<-scale(X)/sqrt(999)
for(v in 1:ncol(X)){
R2<-summary(lm(X[,v]~X[,-v]))$r.squared
vif<-1/(1-R2)
if(vif>10)
cname<-as.data.frame(colnames(X)[v])
table(cname)
}
}
Thanks in advance
Upvotes: 4
Views: 432
Reputation: 23898
If I understood correctly, you need this one.
set.seed(12345)
SimNo <- 10
mat <- matrix(data=NA, nrow=SimNo, ncol=3, byrow=TRUE)
for (i in 1:SimNo){
z1<-rnorm(1000,0,1)
z2<-rnorm(1000,0,1)
z3<-rnorm(1000,0,1)
x1<-z1
x2<-z1*2+z2
x3<-z2+z3
X<-cbind(x1,x2,x3)
sx<-scale(X)/sqrt(999)
for(v in 1:ncol(X)){
R2<-summary(lm(X[,v]~X[,-v]))$r.squared
vif<-1/(1-R2)
if(vif>10) mat[i, v] <- 1 else mat[i, v] <- 0
}
}
mat
[,1] [,2] [,3]
[1,] 0 1 0
[2,] 0 0 0
[3,] 0 1 0
[4,] 0 1 0
[5,] 0 0 0
[6,] 0 0 0
[7,] 0 0 0
[8,] 0 1 0
[9,] 0 1 0
[10,] 0 1 0
colSums(mat)
[1] 0 6 0
Upvotes: 1