Print glm-output to HTML table #rstats

[This article was first published on Strenge Jacke! » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

We often use logistic regression models in our analyses and we also often need to publish the results in table format. And, we always use MS Word since this is our standard office 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 a little script called sjTabOdds.R, which can be downloaded here. This script requires one or more glm-objects, the destination file path and the labels of predictor and dependent variables as parameters. Here are some examples of different table styles…

First, load the script, compute two fitted models and create labels:

source("sjTabOdds.R")
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 word and the shown table can be copied’n'pasted into your own document.


Tagged: R, rstats, Statistik

To leave a comment for the author, please follow the link and comment on their blog: Strenge Jacke! » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)