简体   繁体   中英

Include vif information in stargazer output

I have a an output from the lm function to which I applied the vif function from the car package. I would like to include the vif in a regression table produced by the stargazer package, but I do not manage to add an additional column.

Is this possible at all? I would also be open for suggestions regarding using other packages.

Thx and please let me know incase you need further information.

As suggested by others , the possibilities of customising output with stargazer are limited. One approach is to leverage summary=FALSE option, which just prints the data frame you insert.

library(stargazer)
library(car)
library(broom)
library(tidyverse)

#generate dummy data with correlated regressors
set.seed(123)
x <- runif(1000)
z <- x^0.5
y <-  x + z + rnorm(1000, sd=.05)
model <- lm(y ~ x + z)

# create a new tibble with vifs
vif(model) %>% tibble(.) %>% mutate(term = names(vif(model)) %>% rename('vif'='.') -> vifs

#merge summary stats of model with vifs
tidy_sum <- tidy(summary(model))
left_join(tidy_sum, vifs) -> summary_with_vif

stargazer(summary_with_vif, type='html', summary=FALSE) 

The output looks like this:

 <table style="text-align:center"><tr><td colspan="7" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"></td><td>term</td><td>estimate</td><td>std.error</td><td>statistic</td><td>p.value</td><td>vif</td></tr> <tr><td colspan="7" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">1</td><td>(Intercept)</td><td>0.0004</td><td>0.009</td><td>0.041</td><td>0.968</td><td></td></tr> <tr><td style="text-align:left">2</td><td>x</td><td>0.992</td><td>0.027</td><td>36.180</td><td>0</td><td>24.716</td></tr> <tr><td style="text-align:left">3</td><td>z</td><td>1.006</td><td>0.034</td><td>30.003</td><td>0</td><td>24.716</td></tr> <tr><td colspan="7" style="border-bottom: 1px solid black"></td></tr></table>

Unfortunately, you need to rename your columns, and choose the number of digits yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM