Print glm-output to HTML table #rstats

We often use logistic regression models in our analyses and we also often need to publish the results as tables in our papers. And, we always use MS Word since this is the standard office application in our department. So I thought about an easy way of how to transfer the results of fitted (generalized) linear models from R to Word. An appropriate way – for me – is to create HTML tables, simply open them in Word and copy’n’paste them into my document. This works much better than all things I have tried with SPSS tables (if someone has an easier solution, let me know!).

I wrote two small functions called sjt.lm resp. sjt.glm, which are included in my sjPlot-R-package. These functions require at least one or more fitted (g)lm-objects. It’s recommended to supply labels of predictor and dependent variables as further parameters. Here are some examples of different table styles…

First, compute two fitted models and create labels:

require("sjPlot")
y1 <- ifelse(swiss$Fertility<median(swiss$Fertility), 0, 1)
y2 <- ifelse(swiss$Agriculture<median(swiss$Agriculture), 0, 1)

fitOR1 <- glm(y1 ~ swiss$Education +
              swiss$Examination + 
              swiss$Infant.Mortality + 
              swiss$Catholic, 
              family=binomial(link="logit"))

fitOR2 <- glm(y2 ~ swiss$Education +
              swiss$Examination + 
              swiss$Infant.Mortality + 
              swiss$Catholic, 
              family=binomial(link="logit"))

lab <- c("Education", "Examination", "Infant Mortality", "Catholic")
labdep <- c("Fertility", "Agriculture")

Now, generate the tables:

sjt.glm(fitOR1, fitOR2,
        labelDependentVariables=labdep,
        labelPredictors=lab,
        file="or_table1.html")

Default table style

sjt.glm(fitOR1, fitOR2,
        labelDependentVariables=labdep,
        labelPredictors=lab,
        file="or_table2.html",
        pvaluesAsNumbers=T)

Table with p-values as numbers

sjt.glm(fitOR1, fitOR2,
        labelDependentVariables=labdep,
        labelPredictors=lab,
        file="or_table3.html",
        separateConfColumn=T)

Table with separated column for CI

sjt.glm(fitOR1, fitOR2,
        labelDependentVariables=labdep,
        labelPredictors=lab,
        file="or_table4.html",
        pvaluesAsNumbers=T,
        separateConfColumn=T)

Table with p-values as numbers and separated column for CI

These html-files can be opened with many word processors and the table can be copied’n’pasted into your own document. If you don’t specify the file parameter, the table will be shown in your default browser or in the viewer pane of your R-IDE (for instance, the RStudio viewer pane).

About these ads

8 Gedanken zu “Print glm-output to HTML table #rstats

  1. […] Print glm-output to HTML table #rstats | Strenge Jacke! […]

  2. I used the R2HTML package to copy results via a html output into a word file. It can redirect all output to a html file. It may not look as nice as your function but it should work with most R standard output.

  3. Vielen Dank für die schönen Skripts!

    SPSS ist zwar mindestens das Fegefeuer auf Erden, aber vielleicht hilft das ja für die Fälle, wo Du zwecks Zusammenarbeit mit anderen nicht auf SPSS verzichten “darfst”…:

    */. OMS SPSS Syntax-Beispiel.
    oms select tables
    /desination format = html outfile “c:\Logistic_results.html”
    /if commands ['logistic regression'] subtypes = ['Coefficients']
    logistic regression variables = Geschlecht with BDI QoL
    oms end.

    Da müsstest Du nur mal gucken, wie genau der “Ergebnis”-Table heißt, denn das oms abgreifen soll, aber analog zu diesem Beispiel kann man damit eigentlich jeden Output von SPSS abgreifen und in andere Formate abspeichern. Geht auch gut bei ANOVAs, wenn man das wieder als neue *.sav abspeichert, um dann daraus eine Ergebnis-Graphik zu basteln…

    Gruß, Ulf

    • Vielen Dank! Oftmals muss ich “doppele Buchführung” machen und meine R-Analysen auch durch Syntaxen auf SPSS nachbilden, da die Kollegen damit arbeiten. Die müssen natürlich auch die Analyseschritte nachvollziehen und darauf aufbauen können. Grade für die Kollegen scheint mir deine Syntax praktisch, ich probier’s nachher mal aus.

  4. You may want to try the htmlreg command in the texreg package to export regression output to MS Office and HTML. Version 1.28.3 also supports confidence intervals.

  5. […] linear models in one graph (see comment). While it is already possible to compare multiple models as table output, I now managed to build a function that plots several (g)lm-objects in a single […]

  6. […] which I already published last year, can be used to display (generalized) linear models and have been described here. Yet I want to give another short example for quickly viewing at linear […]

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ photo

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s