SamR
SamR

Reputation: 55

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

I have used the solution presented by @StupidWold here to develop a glm and the results are stored in models. The outputs seem to be correct. However I am wondering how to present all of the outputs at once instead of calling each one separately. I tried using the stargazer package but the result is not as neat as I want it to be, mainly the orientation of the html is horizontal rather than vertical (have to scroll to the right). Here is the code I used for stargazer asking for an html file output: stargazer(models, type="html", out = "table1.html", flip = T)

Any suggestions? Thank you.

Maybe my steps will help clarify things, here is the structure of data

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))

I assume here that the structure of the data is correct, i.e. categorical data are read as factors and so on.

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") 
}

the output of models is correct because I can call, for example, summary(abide) and it works perfectly fine. So my question is how can I look at all the results at once. I have 9000+ DVs and 3 IVs.

Upvotes: 2

Views: 613

Answers (1)

stomper
stomper

Reputation: 1387

I think you could make it work like this.


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

Upvotes: 2

Related Questions