6

I would like to use the tabular()-function from the tables-package to do a cross-tabulation of two variables (e.g. v1 and v2), and present the p-value of the chisq-test in the table. It is easy to get the crosstabulation, but I cant get the p-value inside the table. This is what I've been trying, without any luck:

enter image description here

\documentclass{article}

\begin{document}

<<echo=TRUE,message=FALSE>>=
library(Hmisc)
library(tables)
v1 <- sample(letters[1:2],200,replace=TRUE)
v2 <- sample(month.name[1:3],200,replace=TRUE)
df <- data.frame(v1,v2)
@

It is straight forward to get the crosstabulation:
<<results='asis'>>=
latex(   tabular(    Factor(v1)   ~   Factor(v2)      , data=df)  )
@

But I cant get the p-value inside the table:

<<results='asis'>>=
latex(   tabular(    Factor(v1)*chisq.test(v1,v2)$p.value   ~   Factor(v2)      , data=df)  )
@

\end{document}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rasmus Larsen
  • 5,721
  • 8
  • 47
  • 79
  • Maybe you could use the [reporttools](http://cran.r-project.org/web/packages/reporttools/index.html) package. – sgibb May 12 '13 at 19:13

2 Answers2

8

I don't know how to do it with tables::tabular but this will do it with Hmisc::summary.formula.reverse assuming you have your system configured to produce pdf files through latex(). I had to do searching of the Rhelp archives to figure out that the 'exclude1' argument needed to go in the latex argument list. Once you go back through the documentation exclude1 does appear in the usage example of latex.summary.formula.reverse although I thought I was reading the help page for summary.rms:

library(Hmisc)
latex(summary( v2 ~ v1, data=df,  method="reverse" ,test=TRUE), exclude1=FALSE)

enter image description here

You can intercept the latex output "along the way" if you want to embed it in a longer document by assigning the output to a named file.

latex(summary( v2 ~ v1, data=df,  method="reverse" ,test=TRUE), exclude1=FALSE, file="")
#--------
% latex.default(cstats, title = title, caption = caption, rowlabel = rowlabel,      col.just = col.just, numeric.dollar = FALSE, insert.bottom = legend,      rowname = lab, dcolumn = dcolumn, extracolheads = extracolheads,      extracolsize = Nsize, ...) 
%
\begin{table}[!tbp]
\caption{Descriptive Statistics by v2\label{summary}} 
\begin{center}
\begin{tabular}{lcccc}
\hline\hline
\multicolumn{1}{l}{}&\multicolumn{1}{c}{February}&\multicolumn{1}{c}{January}&\multicolumn{1}{c}{March}&\multicolumn{1}{c}{Test Statistic}\tabularnewline
&\multicolumn{1}{c}{{\scriptsize $N=56$}}&\multicolumn{1}{c}{{\scriptsize $N=73$}}&\multicolumn{1}{c}{{\scriptsize $N=71$}}&\tabularnewline
\hline
v1~:~a&43\%~{\scriptsize~(24)}&47\%~{\scriptsize~(34)}&44\%~{\scriptsize~(31)}&$ \chi^{2}_{2}=0.21 ,~ P=0.901  $\tabularnewline
~~~~b&57\%~{\scriptsize~(32)}&53\%~{\scriptsize~(39)}&56\%~{\scriptsize~(40)}&\tabularnewline
\hline
\end{tabular}
\end{center}
Numbers after percents are frequencies.\\\noindent Test used:\\Pearson test\end{table}
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • How do i get it to work with Knitr in Rstudio. When I put this: latex(summary( v2 ~ v1, data=df, method="reverse" ,test=TRUE), exclude1=FALSE) in an results="asis"-chunk, Rstudio just hangs( by "hangs" i mean that the "stop" sign appears in the lower right corner of the "compile-pdf"-tab, and I need to click stop before I can run other chunks. – Rasmus Larsen May 13 '13 at 07:32
  • 1
    I'm neither an RStudio nor a knitr user, so I am not familiar with the use of "asis"-chunks. You can get "raw" latex output to the console by specifying file="" which might keep the output within the RStudio environment. I'll post the output. – IRTFM May 13 '13 at 15:43
0

You can also paste text from a chi-squared statistic into a caption via xtable(). For example:

#sample data
var1<-sample(c('A', 'B'), 10, replace=T)
var2<-sample(c('Red', 'Blue'), 10, replace=T)
#join in frequency table
tab<-table(var1, var2)
#conduct chisq.test
test<-chisq.test(tab)
#show values of chisq.test()
name(test)
#Use xtable, use print.xtable for further manipulations
out<-xtable(tab, caption=paste('Important table, chi-squared =', test$statistic, ', p=', test$p.value,',' ,test$parameter, 'df', sep=' '))
#print
out 
spindoctor
  • 1,719
  • 1
  • 18
  • 42