How can I read a variable in R Language

For data modeling, I want to find VIF. I also want to know that at different iterations, how many times the variable had 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) } } 

Thank you in advance

+4
source share
1 answer

If I understand 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 
+1
source

Source: https://habr.com/ru/post/1482701/


All Articles