Search code examples
rlatexxtablestargazersignificance

How do I add significance stars to a custom LaTeX table from R?


I have a data frame of the format:

PartitionA PartitionB HA HB IAB pval
All        Kingdom    5  3  2.3 0
All        Phylum     5  4  3.1 .09

The p-values in the last column are associated with the column IAB. I am trying to convert this into a LaTeX table that looks like this:

\begin{tabular}{ccccc}
\hline
Partition $A$ & Partition $B$ & $H(A)$ & $H(B)$ & $MI_{AB}$ \\ 
\hline
All & Kingdom & 5 & 3 & 2.3^{***} \\
All & Phylum & 5 & 4 & 3.1 \\
\hline
\end{tabular}

I have used xtable before to generate basic LaTeX tables from data frames like this. The thing I can't figure out how to do through R is to add significance stars to the last LaTeX column based on the p-values in my data frame. Is there a way to automatically add these significance stars using an R package like stargazer or xtable? I don't use a linear model to generate them, which makes it difficult to use the packages that require a model object. I would like to have three significance levels: * for p < .05, ** for p < .01, and *** for p < .001.


Solution

  • Look at the symnum() function. (This is what print.summary.lm() and others use). There is an example in the ?symnum help page doing pretty much exactly this

    pval <- rev(sort(c(outer(1:6, 10^-(1:3)))))
    symp <- symnum(pval, corr = FALSE,
                   cutpoints = c(0,  .001,.01,.05, .1, 1),
                   symbols = c("***","**","*","."," "))
    noquote(cbind(P.val = format(pval), Signif = symp))
    

    It should be easy to customize to whatever you like.