Include vif information in stargazer output

社会主义新天地 提交于 2019-12-11 06:00:21

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/49004490/include-vif-information-in-stargazer-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!