以漂亮的 table 形式呈现 glm 的所有输出以供比较

present all the outputs of glm in a nice table for comparison

我使用@StupidWold here提供的解决方案开发了一个glm,结果存储在models中。输出似乎是正确的。但是我想知道如何一次呈现所有输出而不是分别调用每个输出。我尝试使用 stargazer 包,但结果并不像我想要的那样整洁,主要是 html 的方向是水平的而不是垂直的(必须向右滚动)。 这是我用于 stargazer 请求 html 文件输出的代码: stargazer(models, type="html", out = "table1.html", flip = T)

有什么建议吗?谢谢。

也许我的步骤会有助于澄清事情,这是数据的结构

FIPSCode<- c(4030,3820,33654,65985,62587,62548)
PRC_W<- c(86.7,56.4,64,52,22,13.6)
MHHI <- c(32564,365265,100000,35467365,353212,3514635132)
abide <- c(0,1,1,0,0,0)
stuff<- c(0,0,0,1,1,0)
takers <- c(1,1,0,1,1,0)
passers <- c(0,1,1,1,0,1)

df <- as.data.frame(cbind(FIPScode, PRC_W, MHHI, abide, stuff, takers, passers))

我在这里假设数据的结构是正确的,即分类数据被读取为因子等等。

DV = colnames(df)[4:7]
IV = colnames(df)[2:3]

models = vector("list",length(DV))
names(models) = DV

for (y in DV){
  form <- reformulate(response=y,IV)
  models[[y]] <- glm(form, data = df, family="binomial") 
}

models 的输出是正确的,因为我可以调用,例如,summary(abide) 并且它工作得很好。 所以我的问题是如何一次查看所有结果。我有 9000+ 个 DV 和 3 个 IV。

我想你可以让它像这样工作。


model_capture <- data.frame()

for (y in DV){
        data = (data.frame(df[y], df[IV]))
        form <- reformulate(response=y,IV)
        dd <- data.frame(vars = y, data) %>%
            group_by(vars) %>%
            nest()
            
        dm <- dd %>% mutate(model = map(data, function(df) glm(form, data = df, family="binomial")))
        
        model_capture <- rbind(model_capture, dm)
    }



model_results <- model_capture %>% 
    mutate(results = map(model, broom::tidy)) %>% 
    unnest(results, .drop = TRUE)

model_results
   vars    data             model  term        estimate std.error statistic p.value
   <chr>   <list>           <list> <chr>          <dbl>     <dbl>     <dbl>   <dbl>
 1 abide   <tibble [6 × 3]> <glm>  (Intercept) -5.43e-1   2.73e+0 -0.199      0.843
 2 abide   <tibble [6 × 3]> <glm>  PRC_W        1.01e-2   4.39e-2  0.230      0.818
 3 abide   <tibble [6 × 3]> <glm>  MHHI        -1.74e-7   6.30e-7 -0.276      0.782
 4 stuff   <tibble [6 × 3]> <glm>  (Intercept)  6.12e+2   8.21e+5  0.000745   0.999
 5 stuff   <tibble [6 × 3]> <glm>  PRC_W       -1.12e+1   1.50e+4 -0.000749   0.999
 6 stuff   <tibble [6 × 3]> <glm>  MHHI        -1.38e-7   1.97e-4 -0.000701   0.999
 7 takers  <tibble [6 × 3]> <glm>  (Intercept)  2.84e+0   3.86e+0  0.735      0.463
 8 takers  <tibble [6 × 3]> <glm>  PRC_W       -2.42e-2   5.93e-2 -0.409      0.683
 9 takers  <tibble [6 × 3]> <glm>  MHHI        -2.50e-9   6.66e-9 -0.376      0.707
10 passers <tibble [6 × 3]> <glm>  (Intercept) -6.02e+0   7.73e+0 -0.779      0.436
11 passers <tibble [6 × 3]> <glm>  PRC_W        6.64e-2   8.77e-2  0.757      0.449
12 passers <tibble [6 × 3]> <glm>  MHHI         1.07e-5   1.45e-5  0.734      0.463