2

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!

logjammin
  • 1,121
  • 6
  • 21

2 Answers2

3

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}
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    I'm gonna try this out in about 15 minutes and I'll mark the answer if it works or give you another comment if it doesn't. Thanks as always Akrun. – logjammin Oct 11 '21 at 19:06
  • 1
    Works very well, cheers. Though check out @TarJae answer above with GTSummary, also very clean! – logjammin Oct 11 '21 at 19:13
2

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

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Wow, didn't know about `gtsummary`. Looks brilliant. I'll check out the documentation when I have some time later, but it looks like the output is images, right -- PDF or PNG or so? – logjammin Oct 11 '21 at 19:12
  • 1
    It is really worth it to take some minutes to go through ´gtsummary` IT produces really publication ready tables! – TarJae Oct 11 '21 at 19:14