对于模拟数据,我想找到VIF。我还想知道,在不同的迭代中,变量看起来有多少次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)
}
}
提前致谢
答案 0 :(得分:1)
如果我理解正确,你需要这个。
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