In R, make a "pretty" result table in LaTeX, PDF, or HTML from "IRR" package output

777 views Asked by At

Background

I'm using the irr package in R to generate some inter-rater reliability statistics for a project I'm doing. Here's an example of Fleiss's Kappa using the package's built-in data:

install.packages("irr")
library(irr)
data("diagnoses", package = "irr")
diagnoses2 <- diagnoses[, 1:3]
kappam.fleiss(diagnoses2)

This gives a simple little output table that looks like this in the R console:

enter image description here

The Problem & Desired Result

I'd like to "prettify" this output so that I can import it into a LaTeX document that I'm using to write up the results.

I'll take basically any format as long as its more or less automatic: LaTeX, a PDF or PNG image, even an HTML version of the table. The issue is that I have several of these output tables to make, and I'd rather not have to awkwardly convert them into LaTeX format manually. I'll do it if I have to, but I'd love to save the time.

What I've Tried

I've tried using knitr on this like so:

knitr::kable(diagnoses2, "latex")

But it doesn't work too well: cannot coerce class ‘"irrlist"’ to a data.frame.

I've looked at another 2 packages that've helped me in the past, texreg and stargazer, but their documentation doesn't say that they support irr outputs so I haven't actually tried them yet. (Though I will once I post this.) In the meantime, I'm also going through this post to see if anything works. Thanks for your time.

EDIT / ADDENDUM

There are some neat answers below from @akrun and @TarJae, using novel (to me) methods and packages. These approaches may be of great help for statistical outputs from packages that aren't irr but that also aren't "officially supported" in packages like texreg or stargazer. I think they may generalize to other cases, in other words. I hope readers check them out!

2

There are 2 answers

2
akrun On BEST ANSWER

If we directly apply the data.frame on the output, it wouldn't work because of the class

as.data.frame(kappam.fleiss(diagnoses2))

Error in as.data.frame.default(kappam.fleiss(diagnoses2)) : cannot coerce class ‘"irrlist"’ to a data.frame

We may convert to data.frame within do.call

out <- do.call(data.frame, kappam.fleiss(diagnoses2))

-check the structure

> str(out)
'data.frame':   1 obs. of  8 variables:
 $ method   : chr "Fleiss' Kappa for m Raters"
 $ subjects : int 30
 $ raters   : int 3
 $ irr.name : chr "Kappa"
 $ value    : num 0.534
 $ stat.name: chr "z"
 $ statistic: num 9.89
 $ p.value  : num 0

Once we convert to data.frame, the kable will work

> knitr::kable(out, "latex")

\begin{tabular}{l|r|r|l|r|l|r|r}
\hline
method & subjects & raters & irr.name & value & stat.name & statistic & p.value\\
\hline
Fleiss' Kappa for m Raters & 30 & 3 & Kappa & 0.5343368 & z & 9.893792 & 0\\
\hline
\end{tabular}
2
TarJae On

We could use gtsummary. See here for detailed adaptions: http://www.danieldsjoberg.com/gtsummary/

out <- do.call(data.frame, kappam.fleiss(diagnoses2))

library(gtsummary)

out %>% 
  tbl_summary

enter image description here