How can I make a table for vif output for multiple models?

409 views Asked by At

I created 5 polr models and then using the Anova anf vif functions to my models.

Here is an example of my data set:

gender Work less happy lifestatisfied country Work much
2 0 7 8 GB 1
1 1 8 8 SE 0
1 0 7 9 DK 1
1 0 6 9 DE 1
1 NA 7 5 NO NA

continued:

health education income age marital status
3 3 Na 61 NA
4 2 2 30 NA
1 3 4 39 6
5 7 5 52 4
4 1 5 17 5
  • country is character (i.e. name of countries) I have 5 countries
  • gender is dummy 1 or 2
  • age is respondents age like 35, 47 etc.
  • income is scaled and is 1 to 10
  • educ (education) is 1 to 7
  • health is scaled 1 to 5
  • work less is dummy i.e. 1 or 0
  • work much is dummy, i.e. 1 or 0
  • marital status is scaled 1 to 6

Here is an example of how I convert my output of vif into table:

vif.model1 = vif(model1)
vif.model1.tolerance = 1/vif.model1

vif.model2 = vif(model2)
vif.model2.tolerance = 1/vif.model2

vif.model3 = vif(model3)
vif.model3.tolerance = 1/vif.model3

vif.model4 = vif(model4)
vif.model4.tolerance = 1/vif.model4

vif.model5 = vif(model5)
vif.model5.tolerance = 1/vif.model5

write.table(vif.model1, "vif.m1.txt", sep = ";", dec = ",") 
write.table(vif.model2, "vif.m2.txt", sep = ";", dec = ",")
write.table(vif.model3, "vif.m3.txt", sep = ";", dec = ",")
write.table(vif.model4, "vif.m4.txt", sep = ";", dec = ",")
write.table(vif.model5, "vif.m5.txt", sep = ";", dec = ",")

When I then want to read them in excel I need to do it for all 5, five times.. is there a more easy way to load all the table into one table (with the variable names and model names appearing in the table .. it's not need to be excel. It is also fine if it's something similar to the method of regression output using stargazer.

I tried:

vif.table = table(vif.model1, vif.model2, row.names=TRUE, colnames=TRUE)

But this doesn't work.. the variable names are not included and the table it self just look wrong/weird/strange.

2

There are 2 answers

1
rr19 On BEST ANSWER

I figured out.. this post helped me: Anova table

v1 <- data.frame(vif.model1)  
v2 <- data.frame(vif.model2)  
v3 <- data.frame(vif.model3)  
v4 <- data.frame(vif.model4)  
v5 <- data.frame(vif.model5)  

v.tabel = data.frame(cbind(v1,v2,v3,v4,v5)) 

write.table(v.table, "vtabel.txt", sep = ";", dec = ",") # i want to have decimals with comma instead of dot, that's why dec =","

and then I just open excel and click on Data -> From Text/CSV and then load my data. It is actually not needed to create tolerance since it easliy can be calculated in excel but I just did it.

0
12666727b9 On

Just as an example I have created on my own chuck of the dataset (small version)

require(tidyverse)
dat =structure(list(gender = structure(c(2L, 1L, 1L, 1L, 1L), .Label = c("1",
"2"), class = "factor"), Work_less = c(0, 1, 0, 0, NA), happy = c(7, 8, 7, 6,
7), lifestatisfied = c(8, 8, 9, 9, 7), country = c(8, 8, 9, 9, 7)), row.names =
c(NA, -5L), class = "data.frame")

I have implemented a nested for loop to fit the model and save the documents in one shot

xvar <- names(dat)[-1]

model <- vector("list", length(xvar))
results <- list()

for (i in 1:length(xvar)) {
  form <- reformulate(xvar[i], "lifestatisfied")
  model[[i]] <- summary(do.call("lm", list(formula = form, data = quote(dat))))
  results[[i]] <- model[[i]]$coefficients
  for(j in seq_along(results)) {
    write.table(results[[j]], paste(xvar[j], ".txt"))
  }
} 

Just give a try with your extended version and let me know.