0

Dear Stackoverflow community

I’m working on a study in R where I’m doing several binomial logistic regressions with different dependents. These analyses are done repeatedly with minor changes, and I’m sharing the results with my co-workers, optimally in nice-looking tables and not messy R-results. If I was going to do this only a few times, I could just do all the analyses as single regressions and then use sjt.glm to make the nice tables. Though as I am doing these similar analyses over and over again, I’m using lapply loops to speed up and simplify the process. Unfortunately I am not able to make lappply and sjt.glm cooperate. Optimally I would just take the results from the lapply loop and make one nice horizontally aligned table with sjt.glm.

See the example (and sorry for the ugly coding)

    library(sjPlot)
    swiss$y1 <- ifelse(swiss$Fertility < median(swiss$Fertility), 0, 1)
    swiss$y2 <- ifelse(swiss$Infant.Mortality < median(swiss$Infant.Mortality), 0, 1)
    swiss$y3 <- ifelse(swiss$Agriculture < median(swiss$Agriculture), 0, 1)

    #Normal slow way would be
    fitOR1 <- glm(y1 ~ Education + Examination + Catholic, data = swiss,
         family = binomial(link = "logit"))
    fitOR2 <- glm(y2 ~ Education + Examination + Catholic, data = swiss,
          family = binomial(link = "logit"))
    fitOR3 <- glm(y3 ~ Education + Examination + Catholic, data = swiss,
          family = binomial(link = "logit"))

    #and then simply use summary and other formulas to look at the results
    summary(fitOR1);exp(cbind(OR = coef(fitOR1), confint(fitOR1)))

    #but with 20+ dependents, this would become tedious

    #Doing the same analysis as a laply loop, is relatively easy (and non-tedious)
    varlist <- names(swiss[c(7:9)])

    results <- lapply(varlist, function(x){
      glm(substitute(i ~ Education + Examination + Catholic, list(i=as.name(x))), 
  family =binomial, data = swiss)})

    for (i in 1:3) print(summary(results[[i]]))
    for (i in 1:3) print(exp(cbind(OR = coef(results[[i]]), confint(results[[i]]))))

    #Though here is the catch. To get the output/results into a nice table
    #I can easily use sjt.glm for the "standard" single logistic regressions.
    sjt.glm(fitOR1,fitOR2,fitOR3, file = "SwissFits.html")

    #Though I can't think of how I could do this for the loop-results.
    #The closest I have come is perhaps something like
    for(i in 1:3)(sjt.glm(results[[i]],file="LoopSwissFits.html"))

    #but then I only get the results from the last regression. 

    #One alternative is to do
    lapply(varlist,function(x){ sjt.glm(
      glm(substitute(i ~ Education + Examination + Catholic, list(i=as.name(x))), 
  family =binomial, data = swiss), file = paste0("SwissFits_",(i=as.name(x)),".html"))})

    #but then I get three separate files, when it would be preferable to
     #have the results in one horizontally oriented file

Do any of you have neat and elegant solution to my problem?

Thank you very much in advance!

TAH
  • 1
  • 1
  • `do.call(what = sjt.glm, args = c(results, file = "LoopSwissFits.html"))`? – Gregor Thomas Nov 23 '16 at 20:17
  • Does it work for you? When I run it I only get Error in eval(x$expr, data, x$env) : variable names are limited to 10000 bytes – TAH Nov 24 '16 at 16:03

2 Answers2

1

The stargazer package is well suited for this. Use write function to store the results in a text file. For displaying in console, simply use-

install.packages("stargazer")  
library(stargazer)

stargazer(results, align = TRUE, type = "text")

# To write to a word file
write(stargazer(results, align = TRUE, type = "text"), "results.txt")
code_is_entropy
  • 611
  • 3
  • 11
1

Simply put the list as first argument: sjt.glm(results).

Daniel
  • 7,252
  • 6
  • 26
  • 38
  • That worked! And right now I'm blushing of embarrassment in my office! Thank you very much! I have no idea of why i just didn't try that myself.... – TAH Nov 24 '16 at 12:59