Print glm-output to HTML table #rstats

August 20, 2013
By

(This article was first published on Strenge Jacke! » R, and kindly contributed to R-bloggers)

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 his blog: Strenge Jacke! » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.